Skip to content

Commit 8b4897c

Browse files
committed
udpate: code review
- fix bug for the wrong type of authentication check - add unit test on the OIDC part - fix typo Signed-off-by: Wen Zhou <[email protected]>
1 parent 6f151cf commit 8b4897c

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

internal/controller/dscinitialization/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func BuildDefaultAuth(ctx context.Context, cli client.Client, platform common.Pl
7575
isOAuth, err := cluster.IsIntegratedOAuth(ctx, cli)
7676

7777
if err == nil && !isOAuth {
78-
// OIDC mode: set an non-exist group that cluster admin shoulud replace with actual OIDC group.
78+
// OIDC mode: set an non-exist group that cluster admin should replace with actual OIDC group.
7979
adminGroup = "REPLACE-WITH-OIDC-ADMIN-GROUP"
8080
} else {
8181
// OAuth mode: Use platform-specific admin group

internal/controller/dscinitialization/auth_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ func TestBuildDefaultAuth(t *testing.T) {
9090
g.Expect(auth.Spec.AllowedGroups[0]).Should(Equal("system:authenticated"))
9191
})
9292
}
93+
t.Run("OIDC mode uses placeholder group", func(t *testing.T) {
94+
g := NewWithT(t)
95+
ctx := t.Context()
96+
cli, err := fakeclient.New(fakeclient.WithClusterAuthType(cluster.AuthModeOIDC))
97+
g.Expect(err).ShouldNot(HaveOccurred())
98+
obj := dscinitialization.BuildDefaultAuth(ctx, cli, cluster.OpenDataHub)
99+
auth, ok := obj.(*serviceApi.Auth)
100+
g.Expect(ok).To(BeTrue())
101+
g.Expect(auth.Spec.AdminGroups).To(HaveLen(1))
102+
g.Expect(auth.Spec.AdminGroups[0]).To(Equal("REPLACE-WITH-OIDC-ADMIN-GROUP"))
103+
// AllowedGroups should remain unchanged
104+
g.Expect(auth.Spec.AllowedGroups).To(Equal([]string{"system:authenticated"}))
105+
})
93106
}
94107

95108
func TestCreateAuth(t *testing.T) {

internal/controller/services/auth/auth_controller_actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ func createDefaultGroup(ctx context.Context, rr *odhtypes.ReconciliationRequest)
184184
return err
185185
}
186186
if !ok {
187-
logf.Log.Info("default auth method is not enabled")
187+
logf.Log.Info("Integrated OAuth not detected; skipping default group creation")
188188
return nil
189189
}
190190

pkg/cluster/cluster_config.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,11 @@ func setManagedMonitoringNamespace(ctx context.Context, cli client.Client) error
379379
func GetClusterAuthenticationMode(ctx context.Context, cli client.Client) (AuthenticationMode, error) {
380380
auth := &configv1.Authentication{}
381381
if err := cli.Get(ctx, client.ObjectKey{Name: ClusterAuthenticationObj}, auth); err != nil {
382-
if errors.Is(err, &meta.NoKindMatchError{}) { // when CRD is missing, convert error type
383-
return "", k8serr.NewNotFound(schema.GroupResource{Group: gvk.Auth.Group}, ClusterAuthenticationObj)
382+
if meta.IsNoMatchError(err) { // CRD missing
383+
return "", k8serr.NewNotFound(schema.GroupResource{
384+
Group: configv1.GroupName,
385+
Resource: "authentications",
386+
}, ClusterAuthenticationObj)
384387
}
385388
return "", fmt.Errorf("failed to get cluster authentication config: %w", err)
386389
}

pkg/utils/test/fakeclient/fakeclient.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package fakeclient
33
import (
44
"fmt"
55

6+
configv1 "github.com/openshift/api/config/v1"
67
"k8s.io/apimachinery/pkg/api/meta"
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
79
"k8s.io/apimachinery/pkg/runtime"
810
"sigs.k8s.io/controller-runtime/pkg/client"
911
clientFake "sigs.k8s.io/controller-runtime/pkg/client/fake"
1012
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
1113

14+
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
1215
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster/gvk"
1316
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/resources"
1417
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/utils/test/scheme"
@@ -39,6 +42,23 @@ func WithScheme(value *runtime.Scheme) ClientOpts {
3942
}
4043
}
4144

45+
// WithClusterAuthType creates a fake OpenShift Authentication object with the specified type.
46+
// This is useful for testing authentication mode detection logic.
47+
// authType should be one of cluster.AuthModeOIDC, cluster.AuthModeIntegratedOAuth, or cluster.AuthModeNone constants.
48+
func WithClusterAuthType(authType cluster.AuthenticationMode) ClientOpts {
49+
return func(o *clientOptions) {
50+
auth := &configv1.Authentication{
51+
ObjectMeta: metav1.ObjectMeta{
52+
Name: cluster.ClusterAuthenticationObj,
53+
},
54+
Spec: configv1.AuthenticationSpec{
55+
Type: configv1.AuthenticationType(authType),
56+
},
57+
}
58+
o.objects = append(o.objects, auth)
59+
}
60+
}
61+
4262
func New(opts ...ClientOpts) (client.Client, error) {
4363
co := clientOptions{}
4464
for _, o := range opts {

0 commit comments

Comments
 (0)