Skip to content

Commit 2ad9361

Browse files
committed
fixes #965 by loading the authorization settings
1 parent 3f6b75b commit 2ad9361

File tree

2 files changed

+74
-0
lines changed

2 files changed

+74
-0
lines changed

provider/resource_keycloak_openid_client.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ func setOpenidClientData(ctx context.Context, keycloakClient *keycloak.KeycloakC
502502
data.Set("access_type", "CONFIDENTIAL")
503503
}
504504

505+
if client.AuthorizationSettings != nil {
506+
authorizationSettings := make(map[string]any)
507+
authorizationSettings["policy_enforcement_mode"] = client.AuthorizationSettings.PolicyEnforcementMode
508+
authorizationSettings["decision_strategy"] = client.AuthorizationSettings.DecisionStrategy
509+
authorizationSettings["allow_remote_resource_management"] = client.AuthorizationSettings.AllowRemoteResourceManagement
510+
authorizationSettings["keep_defaults"] = client.AuthorizationSettings.KeepDefaults
511+
data.Set("authorization", []interface{}{authorizationSettings})
512+
}
513+
505514
if (keycloak.OpenidAuthenticationFlowBindingOverrides{}) == client.AuthenticationFlowBindingOverrides {
506515
data.Set("authentication_flow_binding_overrides", nil)
507516
} else {

provider/resource_keycloak_openid_client_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,23 @@ func TestAccKeycloakOpenidClient_basic_with_consent(t *testing.T) {
6161
})
6262
}
6363

64+
func TestAccKeycloakOpenidClient_basic_with_authorization(t *testing.T) {
65+
t.Parallel()
66+
clientId := acctest.RandomWithPrefix("tf-acc")
67+
68+
resource.Test(t, resource.TestCase{
69+
ProviderFactories: testAccProviderFactories,
70+
PreCheck: func() { testAccPreCheck(t) },
71+
CheckDestroy: testAccCheckKeycloakOpenidClientDestroy(),
72+
Steps: []resource.TestStep{
73+
{
74+
Config: testKeycloakOpenidClient_basic_with_authorization(clientId),
75+
Check: testAccCheckKeycloakOpenidClientExistsWithCorrectAuthorizationSettings("keycloak_openid_client.client"),
76+
},
77+
},
78+
})
79+
}
80+
6481
func TestAccKeycloakOpenidClient_createAfterManualDestroy(t *testing.T) {
6582
t.Parallel()
6683
var client = &keycloak.OpenidClient{}
@@ -814,6 +831,29 @@ func testAccCheckKeycloakOpenidClientExistsWithCorrectConsentSettings(resourceNa
814831
}
815832
}
816833

834+
func testAccCheckKeycloakOpenidClientExistsWithCorrectAuthorizationSettings(resourceName string) resource.TestCheckFunc {
835+
return func(s *terraform.State) error {
836+
client, err := getOpenidClientFromState(s, resourceName)
837+
if err != nil {
838+
return err
839+
}
840+
841+
if client.AuthorizationSettings == nil {
842+
return fmt.Errorf("expected openid client to have authorization settings")
843+
}
844+
845+
if client.AuthorizationSettings.DecisionStrategy != "AFFIRMATIVE" {
846+
return fmt.Errorf("expected openid client to have decision_strategy %v, but got %v", "AFFIRMATIVE", client.AuthorizationSettings.DecisionStrategy)
847+
}
848+
849+
if client.AuthorizationSettings.PolicyEnforcementMode != "ENFORCING" {
850+
return fmt.Errorf("expected openid client to have policy_enforcement_mode %v, but got %v", "ENFORCING", client.AuthorizationSettings.PolicyEnforcementMode)
851+
}
852+
853+
return nil
854+
}
855+
}
856+
817857
func testAccCheckKeycloakOpenidClientHasBackchannelSettings(resourceName, backchannelLogoutUrl string, backchannelLogoutSessionRequired, backchannelLogoutRevokeOfflineSessions bool) resource.TestCheckFunc {
818858
return func(s *terraform.State) error {
819859
client, err := getOpenidClientFromState(s, resourceName)
@@ -1284,6 +1324,31 @@ resource "keycloak_openid_client" "client" {
12841324
`, testAccRealm.Realm, clientId)
12851325
}
12861326

1327+
func testKeycloakOpenidClient_basic_with_authorization(clientId string) string {
1328+
return fmt.Sprintf(`
1329+
data "keycloak_realm" "realm" {
1330+
realm = "%s"
1331+
}
1332+
1333+
resource "keycloak_openid_client" "client" {
1334+
client_id = "%s"
1335+
realm_id = data.keycloak_realm.realm.id
1336+
access_type = "CONFIDENTIAL"
1337+
client_authenticator_type = "client-secret"
1338+
standard_flow_enabled = false
1339+
implicit_flow_enabled = false
1340+
direct_access_grants_enabled = false
1341+
service_accounts_enabled = true
1342+
1343+
authorization {
1344+
policy_enforcement_mode = "ENFORCING"
1345+
decision_strategy = "AFFIRMATIVE"
1346+
allow_remote_resource_management = "true"
1347+
}
1348+
}
1349+
`, testAccRealm.Realm, clientId)
1350+
}
1351+
12871352
func testKeycloakOpenidClient_AccessToken_basic(clientId, accessTokenLifespan string) string {
12881353
return fmt.Sprintf(`
12891354
data "keycloak_realm" "realm" {

0 commit comments

Comments
 (0)