Skip to content

Commit

Permalink
fix: register required action in keycloak_required_action resource (k…
Browse files Browse the repository at this point in the history
  • Loading branch information
klausenbusk authored and hcl31415 committed Oct 12, 2020
1 parent 8fff9dd commit 5ce9b6b
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
7 changes: 7 additions & 0 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,13 @@ resource "keycloak_required_action" "custom-configured_totp" {
priority = keycloak_required_action.custom-terms-and-conditions.priority + 15
}

resource "keycloak_required_action" "required_action" {
realm_id = keycloak_realm.test.realm
alias = "webauthn-register"
enabled = true
name = "Webauthn Register"
}

resource "keycloak_group" "foo" {
realm_id = keycloak_realm.test.id
name = "foo"
Expand Down
21 changes: 21 additions & 0 deletions keycloak/required_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type RequiredAction struct {
RealmId string `json:"-"`
Alias string `json:"alias"`
Name string `json:"name"`
ProviderId string `json:"providerId"`
Enabled bool `json:"enabled"`
DefaultAction bool `json:"defaultAction"`
Priority int `json:"priority"`
Expand Down Expand Up @@ -42,6 +43,21 @@ func (keycloakClient *KeycloakClient) GetRequiredActions(realmId string) ([]*Req
return requiredActions, nil
}

func (keycloakClient *KeycloakClient) GetUnregisteredRequiredActions(realmId string) ([]*RequiredAction, error) {
var unregisteredRequiredActions []*RequiredAction

err := keycloakClient.get(fmt.Sprintf("/realms/%s/authentication/unregistered-required-actions", realmId), &unregisteredRequiredActions, nil)
if err != nil {
return nil, err
}

for _, unregisteredRequiredAction := range unregisteredRequiredActions {
unregisteredRequiredAction.RealmId = realmId
}

return unregisteredRequiredActions, nil
}

func (keycloakClient *KeycloakClient) GetRequiredAction(realmId string, alias string) (*RequiredAction, error) {
var requiredAction RequiredAction

Expand All @@ -53,6 +69,11 @@ func (keycloakClient *KeycloakClient) GetRequiredAction(realmId string, alias st
return &requiredAction, nil
}

func (keycloakClient *KeycloakClient) RegisterRequiredAction(requiredAction *RequiredAction) error {
_, _, err := keycloakClient.post(fmt.Sprintf("/realms/%s/authentication/register-required-action", requiredAction.RealmId), requiredAction)
return err
}

func (keycloakClient *KeycloakClient) CreateRequiredAction(requiredAction *RequiredAction) error {
requiredAction.Id = fmt.Sprintf("%s/%s", requiredAction.RealmId, requiredAction.Alias)
return keycloakClient.UpdateRequiredAction(requiredAction)
Expand Down
13 changes: 13 additions & 0 deletions provider/resource_keycloak_required_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ func resourceKeycloakRequiredActionsCreate(data *schema.ResourceData, meta inter
return err
}

unregisteredRequiredActions, err := keycloakClient.GetUnregisteredRequiredActions(action.RealmId)
if err != nil {
return err
}
for _, unregisteredRequiredAction := range unregisteredRequiredActions {
if unregisteredRequiredAction.ProviderId == action.Alias {
if err := keycloakClient.RegisterRequiredAction(unregisteredRequiredAction); err != nil {
return err
}
break
}
}

err = keycloakClient.CreateRequiredAction(action)
if err != nil {
return err
Expand Down
16 changes: 16 additions & 0 deletions provider/resource_keycloak_required_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ func TestAccKeycloakRequiredAction_basic(t *testing.T) {
})
}

func TestAccKeycloakRequiredAction_unregisteredAction(t *testing.T) {
realmName := "terraform-" + acctest.RandString(10)
requiredActionAlias := "webauthn-register"

resource.Test(t, resource.TestCase{
ProviderFactories: testAccProviderFactories,
PreCheck: func() { testAccPreCheck(t) },
Steps: []resource.TestStep{
{
Config: testKeycloakRequiredAction_basic(realmName, requiredActionAlias, 37),
Check: testAccCheckKeycloakRequiresActionExistsWithCorrectPriority(realmName, requiredActionAlias, 37),
},
},
})
}

func TestAccKeycloakRequiredAction_invalidAlias(t *testing.T) {
realmName := "terraform-" + acctest.RandString(10)
randomReqActionAlias := "randomRequiredAction-" + acctest.RandString(10)
Expand Down

0 comments on commit 5ce9b6b

Please sign in to comment.