Skip to content

Commit 846e179

Browse files
committed
configobservation/auth: remove auth metadata config when auth type is None or OIDC
1 parent ecdc657 commit 846e179

File tree

2 files changed

+80
-41
lines changed

2 files changed

+80
-41
lines changed

pkg/operator/configobservation/auth/auth_metadata.go

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
configv1 "github.com/openshift/api/config/v1"
1010
"github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/configobservation"
11+
"github.com/openshift/cluster-kube-apiserver-operator/pkg/operator/operatorclient"
1112
"github.com/openshift/library-go/pkg/operator/configobserver"
1213
"github.com/openshift/library-go/pkg/operator/events"
1314
"github.com/openshift/library-go/pkg/operator/resourcesynccontroller"
@@ -46,8 +47,9 @@ func ObserveAuthMetadata(genericListers configobserver.Listers, recorder events.
4647
}
4748

4849
observedConfig := map[string]interface{}{}
49-
authConfigNoDefaults, err := listers.AuthConfigLister.Get("cluster")
50+
authConfig, err := listers.AuthConfigLister.Get("cluster")
5051
if errors.IsNotFound(err) {
52+
recorder.Eventf("ObserveAuthMetadataConfigMap", "authentications.config.openshift.io/cluster: not found")
5153
klog.Warningf("authentications.config.openshift.io/cluster: not found")
5254
return observedConfig, errs
5355
}
@@ -56,34 +58,45 @@ func ObserveAuthMetadata(genericListers configobserver.Listers, recorder events.
5658
return prevObservedConfig, errs
5759
}
5860

59-
authConfig := defaultAuthConfig(authConfigNoDefaults)
60-
6161
var (
6262
sourceNamespace string
6363
sourceConfigMap string
64-
statusConfigMap string
6564
)
6665

67-
specConfigMap := authConfig.Spec.OAuthMetadata.Name
66+
switch authConfig.Spec.Type {
67+
case configv1.AuthenticationTypeIntegratedOAuth, "":
68+
specConfigMap := authConfig.Spec.OAuthMetadata.Name
69+
statusConfigMap := authConfig.Status.IntegratedOAuthMetadata.Name
70+
if len(statusConfigMap) == 0 {
71+
klog.V(5).Infof("no integrated oauth metadata configmap observed from status")
72+
}
6873

69-
// TODO: Add a case here for the KeyCloak type.
70-
switch {
71-
case len(authConfig.Status.IntegratedOAuthMetadata.Name) > 0 && authConfig.Spec.Type == configv1.AuthenticationTypeIntegratedOAuth:
72-
statusConfigMap = authConfig.Status.IntegratedOAuthMetadata.Name
73-
default:
74-
klog.V(5).Infof("no integrated oauth metadata configmap observed from status")
75-
}
74+
// Spec configMap takes precedence over Status.
75+
switch {
76+
case len(specConfigMap) > 0:
77+
sourceConfigMap = specConfigMap
78+
sourceNamespace = configNamespace
79+
case len(statusConfigMap) > 0:
80+
sourceConfigMap = statusConfigMap
81+
sourceNamespace = managedNamespace
82+
default:
83+
klog.V(5).Infof("no authentication config metadata specified")
84+
}
85+
86+
case configv1.AuthenticationTypeNone:
87+
// no oauth metadata is served; do not set anything as source
88+
// in order to delete the configmap and unset oauthMetadataFile
89+
90+
case configv1.AuthenticationTypeOIDC:
91+
if _, err := listers.ConfigmapLister_.ConfigMaps(operatorclient.TargetNamespace).Get(AuthConfigCMName); errors.IsNotFound(err) {
92+
// auth-config does not exist in target namespace yet; do not remove oauth metadata until it's there
93+
return prevObservedConfig, errs
94+
} else if err != nil {
95+
return prevObservedConfig, append(errs, err)
96+
}
7697

77-
// Spec configMap takes precedence over Status.
78-
switch {
79-
case len(specConfigMap) > 0:
80-
sourceConfigMap = specConfigMap
81-
sourceNamespace = configNamespace
82-
case len(statusConfigMap) > 0:
83-
sourceConfigMap = statusConfigMap
84-
sourceNamespace = managedNamespace
85-
default:
86-
klog.V(5).Infof("no authentication config metadata specified")
98+
// no oauth metadata is served; do not set anything as source
99+
// in order to delete the configmap and unset oauthMetadataFile
87100
}
88101

89102
// Sync the user or status-specified configMap to the well-known resting place that corresponds to the oauthMetadataFile path.
@@ -116,13 +129,3 @@ func ObserveAuthMetadata(genericListers configobserver.Listers, recorder events.
116129

117130
return observedConfig, errs
118131
}
119-
120-
func defaultAuthConfig(authConfig *configv1.Authentication) *configv1.Authentication {
121-
out := authConfig.DeepCopy() // do not mutate informer cache
122-
123-
if len(out.Spec.Type) == 0 {
124-
out.Spec.Type = configv1.AuthenticationTypeIntegratedOAuth
125-
}
126-
127-
return out
128-
}

pkg/operator/configobservation/auth/auth_metadata_test.go

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestObserveAuthMetadata(t *testing.T) {
4444
cmIndexer cache.Indexer
4545

4646
existingConfig map[string]interface{}
47-
existingConfigMap *corev1.ConfigMap
47+
authConfigMap *corev1.ConfigMap
4848
authSpec *configv1.AuthenticationSpec
4949
statusMetadataName string
5050
syncerError error
@@ -155,13 +155,25 @@ func TestObserveAuthMetadata(t *testing.T) {
155155
},
156156
},
157157
statusMetadataName: "metadata-from-status",
158-
expectedConfig: baseAuthMetadataConfig,
158+
expectedConfig: nil,
159159
expectedSynced: map[string]string{
160-
// FIXME should be delete
161-
"configmap/oauth-metadata.openshift-kube-apiserver": "configmap/metadata-from-spec.openshift-config",
160+
"configmap/oauth-metadata.openshift-kube-apiserver": "DELETE",
162161
},
163162
expectErrors: false,
164163
},
164+
{
165+
name: "metadata from spec with auth type OIDC but auth-config missing",
166+
authSpec: &configv1.AuthenticationSpec{
167+
Type: configv1.AuthenticationTypeOIDC,
168+
OAuthMetadata: configv1.ConfigMapNameReference{
169+
Name: "metadata-from-spec",
170+
},
171+
},
172+
statusMetadataName: "metadata-from-status",
173+
expectedConfig: nil,
174+
expectedSynced: map[string]string{},
175+
expectErrors: false,
176+
},
165177
{
166178
name: "metadata from spec with auth type OIDC",
167179
authSpec: &configv1.AuthenticationSpec{
@@ -170,11 +182,16 @@ func TestObserveAuthMetadata(t *testing.T) {
170182
Name: "metadata-from-spec",
171183
},
172184
},
185+
authConfigMap: &corev1.ConfigMap{
186+
ObjectMeta: metav1.ObjectMeta{
187+
Name: "auth-config",
188+
Namespace: "openshift-kube-apiserver",
189+
},
190+
},
173191
statusMetadataName: "metadata-from-status",
174-
expectedConfig: baseAuthMetadataConfig,
192+
expectedConfig: nil,
175193
expectedSynced: map[string]string{
176-
// FIXME should be delete
177-
"configmap/oauth-metadata.openshift-kube-apiserver": "configmap/metadata-from-spec.openshift-config",
194+
"configmap/oauth-metadata.openshift-kube-apiserver": "DELETE",
178195
},
179196
expectErrors: false,
180197
},
@@ -223,6 +240,19 @@ func TestObserveAuthMetadata(t *testing.T) {
223240
},
224241
expectErrors: false,
225242
},
243+
{
244+
name: "metadata from status with auth type OIDC but auth-config missing",
245+
authSpec: &configv1.AuthenticationSpec{
246+
Type: configv1.AuthenticationTypeOIDC,
247+
OAuthMetadata: configv1.ConfigMapNameReference{
248+
Name: "",
249+
},
250+
},
251+
statusMetadataName: "metadata-from-status",
252+
expectedConfig: nil,
253+
expectedSynced: map[string]string{},
254+
expectErrors: false,
255+
},
226256
{
227257
name: "metadata from status with auth type OIDC",
228258
authSpec: &configv1.AuthenticationSpec{
@@ -231,6 +261,12 @@ func TestObserveAuthMetadata(t *testing.T) {
231261
Name: "",
232262
},
233263
},
264+
authConfigMap: &corev1.ConfigMap{
265+
ObjectMeta: metav1.ObjectMeta{
266+
Name: "auth-config",
267+
Namespace: "openshift-kube-apiserver",
268+
},
269+
},
234270
statusMetadataName: "metadata-from-status",
235271
expectedConfig: nil,
236272
expectedSynced: map[string]string{
@@ -267,8 +303,8 @@ func TestObserveAuthMetadata(t *testing.T) {
267303
tt.cmIndexer = cache.NewIndexer(cache.MetaNamespaceKeyFunc, cache.Indexers{})
268304
}
269305

270-
if tt.existingConfigMap != nil {
271-
tt.cmIndexer.Add(tt.existingConfigMap)
306+
if tt.authConfigMap != nil {
307+
tt.cmIndexer.Add(tt.authConfigMap)
272308
}
273309

274310
listers := configobservation.Listers{

0 commit comments

Comments
 (0)