Skip to content

Commit 6743c63

Browse files
authored
SSO: Add org_mapping and org_attribute_path to sso_settings resource (#1683)
* First changes * Add tests for validation * Regenerate docs
1 parent ec586e8 commit 6743c63

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

docs/resources/sso_settings.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Optional:
114114
- `login_attribute_path` (String) JMESPath expression to use for user login lookup from the user ID token. Only applicable to Generic OAuth.
115115
- `name` (String) Helpful if you use more than one identity providers or SSO protocols.
116116
- `name_attribute_path` (String) JMESPath expression to use for user name lookup from the user ID token. This name will be used as the user’s display name. Only applicable to Generic OAuth.
117+
- `org_attribute_path` (String) JMESPath expression to use for the organization mapping lookup from the user ID token. The extracted list will be used for the organization mapping (to match "Organization" in the "org_mapping"). Only applicable to Generic OAuth and Okta.
118+
- `org_mapping` (String) List of comma- or space-separated Organization:OrgIdOrOrgName:Role mappings. Organization can be * meaning “All users”. Role is optional and can have the following values: None, Viewer, Editor or Admin.
117119
- `role_attribute_path` (String) JMESPath expression to use for Grafana role lookup.
118120
- `role_attribute_strict` (Boolean) If enabled, denies user login if the Grafana role cannot be extracted using Role attribute path.
119121
- `scopes` (String) List of comma- or space-separated OAuth2 scopes.

internal/resources/grafana/resource_sso_settings.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,16 @@ var oauth2SettingsSchema = &schema.Resource{
204204
Optional: true,
205205
Description: "Prevent synchronizing users’ organization roles from your IdP.",
206206
},
207+
"org_mapping": {
208+
Type: schema.TypeString,
209+
Optional: true,
210+
Description: "List of comma- or space-separated Organization:OrgIdOrOrgName:Role mappings. Organization can be * meaning “All users”. Role is optional and can have the following values: None, Viewer, Editor or Admin.",
211+
},
212+
"org_attribute_path": {
213+
Type: schema.TypeString,
214+
Optional: true,
215+
Description: `JMESPath expression to use for the organization mapping lookup from the user ID token. The extracted list will be used for the organization mapping (to match "Organization" in the "org_mapping"). Only applicable to Generic OAuth and Okta.`,
216+
},
207217
"define_allowed_groups": {
208218
Type: schema.TypeBool,
209219
Optional: true,
@@ -685,6 +695,7 @@ var validationsByProvider = map[string][]validateFunc{
685695
ssoValidateNotEmpty("auth_url"),
686696
ssoValidateNotEmpty("token_url"),
687697
ssoValidateEmpty("api_url"),
698+
ssoValidateEmpty("org_attribute_path"),
688699
ssoValidateURL("auth_url"),
689700
ssoValidateURL("token_url"),
690701
},
@@ -695,6 +706,7 @@ var validationsByProvider = map[string][]validateFunc{
695706
ssoValidateURL("auth_url"),
696707
ssoValidateURL("token_url"),
697708
ssoValidateURL("api_url"),
709+
ssoValidateInterdependencyXOR("org_attribute_path", "org_mapping"),
698710
},
699711
"okta": {
700712
ssoValidateNotEmpty("auth_url"),
@@ -703,21 +715,25 @@ var validationsByProvider = map[string][]validateFunc{
703715
ssoValidateURL("auth_url"),
704716
ssoValidateURL("token_url"),
705717
ssoValidateURL("api_url"),
718+
ssoValidateInterdependencyXOR("org_attribute_path", "org_mapping"),
706719
},
707720
"github": {
708721
ssoValidateEmpty("auth_url"),
709722
ssoValidateEmpty("token_url"),
710723
ssoValidateEmpty("api_url"),
724+
ssoValidateEmpty("org_attribute_path"),
711725
},
712726
"gitlab": {
713727
ssoValidateEmpty("auth_url"),
714728
ssoValidateEmpty("token_url"),
715729
ssoValidateEmpty("api_url"),
730+
ssoValidateEmpty("org_attribute_path"),
716731
},
717732
"google": {
718733
ssoValidateEmpty("auth_url"),
719734
ssoValidateEmpty("token_url"),
720735
ssoValidateEmpty("api_url"),
736+
ssoValidateEmpty("org_attribute_path"),
721737
},
722738
"saml": {
723739
ssoValidateInterdependencyXOR("certificate", "private_key"),

internal/resources/grafana/resource_sso_settings_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,4 +595,36 @@ var testConfigsWithValidationErrors = []string{
595595
token_url = "https://myidp.com/oauth/token"
596596
}
597597
}`,
598+
// org_attribute_path is not empty for AzureAD
599+
`resource "grafana_sso_settings" "azure_sso_settings" {
600+
provider_name = "azuread"
601+
oauth2_settings {
602+
client_id = "client_id"
603+
auth_url = "https://login.microsoftonline.com/12345/oauth2/v2.0/authorize"
604+
token_url = "https://login.microsoftonline.com/12345/oauth2/v2.0/token"
605+
org_attribute_path = "org"
606+
}
607+
}`,
608+
// org_mapping is configured but org_attribute_path is missing for Okta
609+
`resource "grafana_sso_settings" "okta_sso_settings" {
610+
provider_name = "okta"
611+
oauth2_settings {
612+
client_id = "client_id"
613+
auth_url = "https://tenantid123.okta.com/oauth2/v1/auth"
614+
token_url = "https://tenantid123.okta.com/oauth2/v1/token"
615+
api_url = "https://tenantid123.okta.com/oauth2/v1/userinfo"
616+
org_mapping = "[\"Group A:1:Editor\",\"Group A:2:Admin\"]"
617+
}
618+
}`,
619+
// org_attribute_path is configured but org_mapping is missing for Generic OAuth
620+
`resource "grafana_sso_settings" "generic_oauth_sso_settings" {
621+
provider_name = "generic_oauth"
622+
oauth2_settings {
623+
client_id = "client_id"
624+
auth_url = "https://tenantid123.okta.com/oauth2/v1/auth"
625+
token_url = "https://tenantid123.okta.com/oauth2/v1/token"
626+
api_url = "https://tenantid123.okta.com/oauth2/v1/userinfo"
627+
org_attribute_path = "groups"
628+
}
629+
}`,
598630
}

0 commit comments

Comments
 (0)