Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add priority field support in authentication flow executions #970

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/resources/authentication_execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Allows for creating and managing an authentication execution within Keycloak.
An authentication execution is an action that the user or service may or may not take when authenticating through an authentication
flow.

~> Due to limitations in the Keycloak API, the ordering of authentication executions within a flow must be specified using `depends_on`. Authentication executions that are created first will appear first within the flow.
~> Due to limitations in the Keycloak API, the ordering of authentication executions within a flow must be specified using `depends_on` in versions prior to Keycloak 25. Authentication executions that are created first will appear first within the flow.

## Example Usage

Expand All @@ -30,6 +30,7 @@ resource "keycloak_authentication_execution" "execution_one" {
parent_flow_alias = "${keycloak_authentication_flow.flow.alias}"
authenticator = "auth-cookie"
requirement = "ALTERNATIVE"
priority = 10 # Starting from Keycloak 25
}
# second execution
Expand All @@ -38,7 +39,9 @@ resource "keycloak_authentication_execution" "execution_two" {
parent_flow_alias = "${keycloak_authentication_flow.flow.alias}"
authenticator = "identity-provider-redirector"
requirement = "ALTERNATIVE"
priority = 20 # Starting from Keycloak 25
# Workaround for older Keycloak versions (Keycloak 24 and older)
depends_on = [
keycloak_authentication_execution.execution_one
]
Expand All @@ -51,6 +54,7 @@ resource "keycloak_authentication_execution" "execution_two" {
- `parent_flow_alias` - (Required) The alias of the flow this execution is attached to.
- `authenticator` - (Required) The name of the authenticator. This can be found by experimenting with the GUI and looking at HTTP requests within the network tab of your browser's development tools.
- `requirement`- (Optional) The requirement setting, which can be one of `REQUIRED`, `ALTERNATIVE`, `OPTIONAL`, `CONDITIONAL`, or `DISABLED`. Defaults to `DISABLED`.
- `priority`- (Optional) The authenticator priority, the lower the value the higher it will be placed in the parent flow. This option is supported only by Keycloak 25 and onwards.

## Import

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/authentication_subflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ resource "keycloak_authentication_subflow" "subflow" {
parent_flow_alias = keycloak_authentication_flow.flow.alias
provider_id = "basic-flow"
requirement = "ALTERNATIVE"
priority = 10 # Starting from Keycloak 25
}
```

Expand All @@ -43,6 +44,7 @@ and `client-flow`. Defaults to `basic-flow`.
authenticators. In general this will remain empty.
- `requirement`- (Optional) The requirement setting, which can be one of `REQUIRED`, `ALTERNATIVE`, `OPTIONAL`, `CONDITIONAL`,
or `DISABLED`. Defaults to `DISABLED`.
- `priority`- (Optional) The subflow priority, the lower the value the higher it will be placed in the parent flow. This option is supported only by Keycloak 25 and onwards.

## Import

Expand Down
18 changes: 6 additions & 12 deletions example/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -1020,53 +1020,47 @@ resource "keycloak_authentication_execution" "browser-copy-cookie" {
parent_flow_alias = keycloak_authentication_flow.browser-copy-flow.alias
authenticator = "auth-cookie"
requirement = "ALTERNATIVE"
depends_on = [
keycloak_authentication_execution.browser-copy-kerberos
]
priority = 20
}

resource "keycloak_authentication_execution" "browser-copy-kerberos" {
realm_id = keycloak_realm.test.id
parent_flow_alias = keycloak_authentication_flow.browser-copy-flow.alias
authenticator = "auth-spnego"
requirement = "DISABLED"
priority = 10
}

resource "keycloak_authentication_execution" "browser-copy-idp-redirect" {
realm_id = keycloak_realm.test.id
parent_flow_alias = keycloak_authentication_flow.browser-copy-flow.alias
authenticator = "identity-provider-redirector"
requirement = "ALTERNATIVE"
depends_on = [
keycloak_authentication_execution.browser-copy-cookie
]
priority = 30
}

resource "keycloak_authentication_subflow" "browser-copy-flow-forms" {
realm_id = keycloak_realm.test.id
parent_flow_alias = keycloak_authentication_flow.browser-copy-flow.alias
alias = "browser-copy-flow-forms"
requirement = "ALTERNATIVE"
depends_on = [
keycloak_authentication_execution.browser-copy-idp-redirect
]
priority = 40
}

resource "keycloak_authentication_execution" "browser-copy-auth-username-password-form" {
realm_id = keycloak_realm.test.id
parent_flow_alias = keycloak_authentication_subflow.browser-copy-flow-forms.alias
authenticator = "auth-username-password-form"
requirement = "REQUIRED"
priority = 10
}

resource "keycloak_authentication_execution" "browser-copy-otp" {
realm_id = keycloak_realm.test.id
parent_flow_alias = keycloak_authentication_subflow.browser-copy-flow-forms.alias
authenticator = "auth-otp-form"
requirement = "REQUIRED"
depends_on = [
keycloak_authentication_execution.browser-copy-auth-username-password-form
]
priority = 20
}

resource "keycloak_authentication_execution_config" "config" {
Expand Down
33 changes: 15 additions & 18 deletions keycloak/authentication_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
// POST /realms/${realmId}/authentication/flows/${flowAlias}/executions/execution
type authenticationExecutionCreate struct {
Provider string `json:"provider"` //authenticator of the execution
Priority int `json:"priority,omitempty"`
}

type authenticationExecutionRequirementUpdate struct {
RealmId string `json:"-"`
ParentFlowAlias string `json:"-"`
Id string `json:"id"`
Requirement string `json:"requirement"`
Priority int `json:"priority,omitempty"`
}

// this type is returned by GET /realms/${realmId}/authentication/flows/${flowAlias}/executions
Expand All @@ -30,7 +32,7 @@ type AuthenticationExecution struct {
AuthenticationFlow bool `json:"authenticationFlow"`
FlowId string `json:"flowId"`
ParentFlowId string `json:"parentFlow"`
Priority int `json:"priority"`
Priority int `json:"priority,omitempty"`
Requirement string `json:"requirement"`
}

Expand All @@ -47,6 +49,7 @@ type AuthenticationExecutionInfo struct {
Index int `json:"index"`
Level int `json:"level"`
ProviderId string `json:"providerId"`
Priority int `json:"priority,omitempty"`
Requirement string `json:"requirement"`
}

Expand Down Expand Up @@ -119,7 +122,14 @@ func (keycloakClient *KeycloakClient) GetAuthenticationExecutionInfoFromProvider
}

func (keycloakClient *KeycloakClient) NewAuthenticationExecution(ctx context.Context, execution *AuthenticationExecution) error {
_, location, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/authentication/flows/%s/executions/execution", execution.RealmId, execution.ParentFlowAlias), &authenticationExecutionCreate{Provider: execution.Authenticator})
executionCreate := &authenticationExecutionCreate{
Provider: execution.Authenticator,
}
if prioritySupported, _ := keycloakClient.VersionIsGreaterThanOrEqualTo(ctx, Version_25); prioritySupported {
executionCreate.Priority = execution.Priority
}
_, location, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/authentication/flows/%s/executions/execution", execution.RealmId, execution.ParentFlowAlias), executionCreate)

if err != nil {
return err
}
Expand Down Expand Up @@ -155,6 +165,9 @@ func (keycloakClient *KeycloakClient) UpdateAuthenticationExecution(ctx context.
Id: execution.Id,
Requirement: execution.Requirement,
}
if prioritySupported, _ := keycloakClient.VersionIsGreaterThanOrEqualTo(ctx, Version_25); prioritySupported {
authenticationExecutionUpdateRequirement.Priority = execution.Priority
}
return keycloakClient.UpdateAuthenticationExecutionRequirement(ctx, authenticationExecutionUpdateRequirement)
}

Expand All @@ -171,19 +184,3 @@ func (keycloakClient *KeycloakClient) DeleteAuthenticationExecution(ctx context.

return nil
}

func (keycloakClient *KeycloakClient) RaiseAuthenticationExecutionPriority(ctx context.Context, realmId, id string) error {
_, _, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/authentication/executions/%s/raise-priority", realmId, id), nil)
if err != nil {
return err
}
return nil
}

func (keycloakClient *KeycloakClient) LowerAuthenticationExecutionPriority(ctx context.Context, realmId, id string) error {
_, _, err := keycloakClient.post(ctx, fmt.Sprintf("/realms/%s/authentication/executions/%s/lower-priority", realmId, id), nil)
if err != nil {
return err
}
return nil
}
34 changes: 6 additions & 28 deletions keycloak/authentication_subflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ func (keycloakClient *KeycloakClient) GetAuthenticationSubFlow(ctx context.Conte
}
authenticationSubFlow.Authenticator = subFlowExecution.Authenticator
authenticationSubFlow.Requirement = subFlowExecution.Requirement
if prioritySupported, _ := keycloakClient.VersionIsGreaterThanOrEqualTo(ctx, Version_25); prioritySupported {
authenticationSubFlow.Priority = subFlowExecution.Priority
}

return &authenticationSubFlow, nil
}
Expand Down Expand Up @@ -111,6 +114,9 @@ func (keycloakClient *KeycloakClient) UpdateAuthenticationSubFlow(ctx context.Co
Id: executionId,
Requirement: authenticationSubFlow.Requirement,
}
if prioritySupported, _ := keycloakClient.VersionIsGreaterThanOrEqualTo(ctx, Version_25); prioritySupported {
authenticationExecutionUpdateRequirement.Priority = authenticationSubFlow.Priority
}
return keycloakClient.UpdateAuthenticationExecutionRequirement(ctx, authenticationExecutionUpdateRequirement)

}
Expand All @@ -128,31 +134,3 @@ func (keycloakClient *KeycloakClient) DeleteAuthenticationSubFlow(ctx context.Co

return keycloakClient.DeleteAuthenticationExecution(ctx, authenticationSubFlow.RealmId, executionId)
}

func (keycloakClient *KeycloakClient) RaiseAuthenticationSubFlowPriority(ctx context.Context, realmId, parentFlowAlias, id string) error {
authenticationSubFlow := AuthenticationSubFlow{
Id: id,
ParentFlowAlias: parentFlowAlias,
RealmId: realmId,
}
executionId, err := keycloakClient.getExecutionId(ctx, &authenticationSubFlow)
if err != nil {
return err
}

return keycloakClient.RaiseAuthenticationExecutionPriority(ctx, authenticationSubFlow.RealmId, executionId)
}

func (keycloakClient *KeycloakClient) LowerAuthenticationSubFlowPriority(ctx context.Context, realmId, parentFlowAlias, id string) error {
authenticationSubFlow := AuthenticationSubFlow{
Id: id,
ParentFlowAlias: parentFlowAlias,
RealmId: realmId,
}
executionId, err := keycloakClient.getExecutionId(ctx, &authenticationSubFlow)
if err != nil {
return err
}

return keycloakClient.LowerAuthenticationExecutionPriority(ctx, authenticationSubFlow.RealmId, executionId)
}
16 changes: 16 additions & 0 deletions keycloak/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,19 @@ func (keycloakClient *KeycloakClient) VersionIsLessThanOrEqualTo(ctx context.Con

return keycloakClient.version.LessThanOrEqual(v), nil
}

func (keycloakClient *KeycloakClient) VersionIsLessThan(ctx context.Context, versionString Version) (bool, error) {
if keycloakClient.version == nil {
err := keycloakClient.login(ctx)
if err != nil {
return false, err
}
}

v, err := version.NewVersion(string(versionString))
if err != nil {
return false, nil
}

return keycloakClient.version.LessThan(v), nil
}
6 changes: 5 additions & 1 deletion provider/data_source_keycloak_authentication_execution.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ func dataSourceKeycloakAuthenticationExecution() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"priority": {
Type: schema.TypeInt,
Optional: true,
},
},
}
}
Expand All @@ -39,7 +43,7 @@ func dataSourceKeycloakAuthenticationExecutionRead(ctx context.Context, data *sc
return diag.FromErr(err)
}

mapFromAuthenticationExecutionInfoToData(data, authenticationExecutionInfo)
mapFromAuthenticationExecutionInfoToData(keycloakClient, ctx, data, authenticationExecutionInfo)

return nil
}
Loading
Loading