-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add ClusterKeycloakRealm browserFlow setting (#66)
- Loading branch information
Showing
16 changed files
with
246 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package chain | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
keycloakApi "github.com/epam/edp-keycloak-operator/api/v1alpha1" | ||
"github.com/epam/edp-keycloak-operator/pkg/client/keycloak" | ||
) | ||
|
||
type AuthFlow struct { | ||
} | ||
|
||
func NewAuthFlow() *AuthFlow { | ||
return &AuthFlow{} | ||
} | ||
|
||
func (a AuthFlow) ServeRequest(ctx context.Context, realm *keycloakApi.ClusterKeycloakRealm, kClient keycloak.Client) error { | ||
log := ctrl.LoggerFrom(ctx) | ||
log.Info("Start configuring authentication flow") | ||
|
||
if realm.Spec.AuthenticationFlow == nil || realm.Spec.AuthenticationFlow.BrowserFlow == "" { | ||
log.Info("Authentication flow is not provided, skip configuring") | ||
return nil | ||
} | ||
|
||
if err := kClient.SetRealmBrowserFlow(ctx, realm.Spec.RealmName, realm.Spec.AuthenticationFlow.BrowserFlow); err != nil { | ||
return fmt.Errorf("setting realm browser flow: %w", err) | ||
} | ||
|
||
log.Info("Authentication flow has been configured") | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package chain | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/go-logr/logr" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
|
||
keycloakApi "github.com/epam/edp-keycloak-operator/api/v1alpha1" | ||
"github.com/epam/edp-keycloak-operator/pkg/client/keycloak" | ||
"github.com/epam/edp-keycloak-operator/pkg/client/keycloak/mocks" | ||
) | ||
|
||
func TestAuthFlow_ServeRequest(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
realm *keycloakApi.ClusterKeycloakRealm | ||
kClient func(t *testing.T) keycloak.Client | ||
wantErr require.ErrorAssertionFunc | ||
}{ | ||
{ | ||
name: "realm browser flow is not provided", | ||
realm: &keycloakApi.ClusterKeycloakRealm{}, | ||
kClient: func(t *testing.T) keycloak.Client { | ||
return mocks.NewMockClient(t) | ||
}, | ||
wantErr: require.NoError, | ||
}, | ||
{ | ||
name: "realm browser flow updated successfully", | ||
realm: &keycloakApi.ClusterKeycloakRealm{ | ||
Spec: keycloakApi.ClusterKeycloakRealmSpec{ | ||
RealmName: "realm1", | ||
AuthenticationFlow: &keycloakApi.AuthenticationFlow{ | ||
BrowserFlow: "flow-alias-1", | ||
}, | ||
}, | ||
}, | ||
kClient: func(t *testing.T) keycloak.Client { | ||
kc := mocks.NewMockClient(t) | ||
kc.On("SetRealmBrowserFlow", mock.Anything, "realm1", "flow-alias-1"). | ||
Return(nil) | ||
|
||
return kc | ||
}, | ||
wantErr: require.NoError, | ||
}, | ||
{ | ||
name: "error on setting realm browser flow", | ||
realm: &keycloakApi.ClusterKeycloakRealm{ | ||
Spec: keycloakApi.ClusterKeycloakRealmSpec{ | ||
RealmName: "realm1", | ||
AuthenticationFlow: &keycloakApi.AuthenticationFlow{ | ||
BrowserFlow: "flow-alias-1", | ||
}, | ||
}, | ||
}, | ||
kClient: func(t *testing.T) keycloak.Client { | ||
kc := mocks.NewMockClient(t) | ||
kc.On("SetRealmBrowserFlow", mock.Anything, "realm1", "flow-alias-1"). | ||
Return(errors.New("failed to set realm browser flow")) | ||
return kc | ||
}, | ||
|
||
wantErr: func(t require.TestingT, err error, i ...interface{}) { | ||
require.Error(t, err) | ||
require.Contains(t, err.Error(), "setting realm browser flow") | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
tt := tt | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
a := NewAuthFlow() | ||
err := a.ServeRequest( | ||
ctrl.LoggerInto(context.Background(), logr.Discard()), | ||
tt.realm, | ||
tt.kClient(t), | ||
) | ||
|
||
tt.wantErr(t, err) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.