|
| 1 | +package oauthclientsecret |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + apierrors "k8s.io/apimachinery/pkg/api/errors" |
| 8 | + corev1informers "k8s.io/client-go/informers/core/v1" |
| 9 | + corev1clients "k8s.io/client-go/kubernetes/typed/core/v1" |
| 10 | + corev1listers "k8s.io/client-go/listers/core/v1" |
| 11 | + "k8s.io/klog/v2" |
| 12 | + |
| 13 | + configv1 "github.com/openshift/api/config/v1" |
| 14 | + operatorv1 "github.com/openshift/api/operator/v1" |
| 15 | + configv1informers "github.com/openshift/client-go/config/informers/externalversions/config/v1" |
| 16 | + configv1listers "github.com/openshift/client-go/config/listers/config/v1" |
| 17 | + operatorv1informers "github.com/openshift/client-go/operator/informers/externalversions/operator/v1" |
| 18 | + operatorv1listers "github.com/openshift/client-go/operator/listers/operator/v1" |
| 19 | + "github.com/openshift/console-operator/pkg/api" |
| 20 | + "github.com/openshift/console-operator/pkg/console/status" |
| 21 | + "github.com/openshift/console-operator/pkg/crypto" |
| 22 | + "github.com/openshift/library-go/pkg/controller/factory" |
| 23 | + "github.com/openshift/library-go/pkg/operator/events" |
| 24 | + "github.com/openshift/library-go/pkg/operator/resource/resourceapply" |
| 25 | + v1helpers "github.com/openshift/library-go/pkg/operator/v1helpers" |
| 26 | + |
| 27 | + secretsub "github.com/openshift/console-operator/pkg/console/subresource/secret" |
| 28 | + utilsub "github.com/openshift/console-operator/pkg/console/subresource/util" |
| 29 | +) |
| 30 | + |
| 31 | +// oauthClientSecretController behaves differently based on authentication/cluster .spec.type: |
| 32 | +// |
| 33 | +// - IntegratedOAuth - self-manage the client secret string |
| 34 | +// - OIDC - lookup our client in the authentication/cluster .spec.oidcProviders[x].oidcClients |
| 35 | +// slice and use the 'clientSecret' from the secret referred to by .clientSecret.name |
| 36 | +// - None - do nothing |
| 37 | +// |
| 38 | +// The secret written is 'openshift-console/console-oauth-config' in .Data['clientSecret'] |
| 39 | +type oauthClientSecretController struct { |
| 40 | + operatorClient v1helpers.OperatorClient |
| 41 | + secretsClient corev1clients.SecretsGetter |
| 42 | + |
| 43 | + authConfigLister configv1listers.AuthenticationLister |
| 44 | + consoleOperatorLister operatorv1listers.ConsoleLister |
| 45 | + configSecretsLister corev1listers.SecretLister |
| 46 | + targetNSSecretsLister corev1listers.SecretLister |
| 47 | +} |
| 48 | + |
| 49 | +func NewOAuthClientSecretController( |
| 50 | + operatorClient v1helpers.OperatorClient, |
| 51 | + secretsClient corev1clients.SecretsGetter, |
| 52 | + authnInformer configv1informers.AuthenticationInformer, |
| 53 | + consoleOperatorInformer operatorv1informers.ConsoleInformer, |
| 54 | + configSecretsInformer corev1informers.SecretInformer, |
| 55 | + targetNSsecretsInformer corev1informers.SecretInformer, |
| 56 | + recorder events.Recorder, |
| 57 | +) factory.Controller { |
| 58 | + c := &oauthClientSecretController{ |
| 59 | + operatorClient: operatorClient, |
| 60 | + secretsClient: secretsClient, |
| 61 | + |
| 62 | + authConfigLister: authnInformer.Lister(), |
| 63 | + consoleOperatorLister: consoleOperatorInformer.Lister(), |
| 64 | + configSecretsLister: configSecretsInformer.Lister(), |
| 65 | + targetNSSecretsLister: targetNSsecretsInformer.Lister(), |
| 66 | + } |
| 67 | + |
| 68 | + return factory.New(). |
| 69 | + WithSync(c.sync). |
| 70 | + WithInformers( |
| 71 | + authnInformer.Informer(), |
| 72 | + consoleOperatorInformer.Informer(), |
| 73 | + configSecretsInformer.Informer(), |
| 74 | + ). |
| 75 | + WithFilteredEventsInformers( |
| 76 | + factory.NamesFilter("console-oauth-config"), targetNSsecretsInformer.Informer(), |
| 77 | + ). |
| 78 | + ToController("OAuthClientSecretController", recorder.WithComponentSuffix("oauthclient-secret-controller")) |
| 79 | +} |
| 80 | + |
| 81 | +func (c *oauthClientSecretController) sync(ctx context.Context, syncCtx factory.SyncContext) error { |
| 82 | + if shouldSync, err := c.handleManaged(ctx); err != nil { |
| 83 | + return err |
| 84 | + } else if !shouldSync { |
| 85 | + return nil |
| 86 | + } |
| 87 | + |
| 88 | + statusHandler := status.NewStatusHandler(c.operatorClient) |
| 89 | + |
| 90 | + clientSecret, err := c.targetNSSecretsLister.Secrets(api.TargetNamespace).Get("console-oauth-config") |
| 91 | + if err != nil && !apierrors.IsNotFound(err) { |
| 92 | + return err |
| 93 | + } |
| 94 | + |
| 95 | + authConfig, err := c.authConfigLister.Get("cluster") |
| 96 | + if err != nil { |
| 97 | + return fmt.Errorf("failed to retrieve authentication config: %w", err) |
| 98 | + } |
| 99 | + |
| 100 | + var secretString string |
| 101 | + switch authConfig.Spec.Type { |
| 102 | + case "", configv1.AuthenticationTypeIntegratedOAuth: |
| 103 | + // in OpenShift controlled world, we generate the client secret ourselves |
| 104 | + if clientSecret != nil { |
| 105 | + secretString = secretsub.GetSecretString(clientSecret) |
| 106 | + } |
| 107 | + if len(secretString) == 0 { |
| 108 | + secretString = crypto.Random256BitsString() |
| 109 | + } |
| 110 | + case configv1.AuthenticationTypeOIDC: |
| 111 | + clientConfig := utilsub.GetOIDCClientConfig(authConfig) |
| 112 | + if clientConfig == nil { |
| 113 | + // no config, flush the condition and return |
| 114 | + statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "", nil)) |
| 115 | + return statusHandler.FlushAndReturn(nil) |
| 116 | + } |
| 117 | + |
| 118 | + if len(clientConfig.ClientSecret.Name) == 0 { |
| 119 | + statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "MissingClientSecretConfig", fmt.Errorf("missing client secret name reference in config"))) |
| 120 | + return statusHandler.FlushAndReturn(nil) |
| 121 | + } |
| 122 | + |
| 123 | + conficClientSecret, err := c.configSecretsLister.Secrets(api.OpenShiftConfigNamespace).Get(clientConfig.ClientSecret.Name) |
| 124 | + if err != nil { |
| 125 | + statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "FailedClientSecretGet", err)) |
| 126 | + return statusHandler.FlushAndReturn(err) |
| 127 | + } |
| 128 | + |
| 129 | + secretString = secretsub.GetSecretString(conficClientSecret) |
| 130 | + if len(secretString) == 0 { |
| 131 | + statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "ClientSecretKeyMissing", fmt.Errorf("missing the 'clientSecret' key in the client secret secret %q", clientConfig.ClientSecret.Name))) |
| 132 | + return statusHandler.FlushAndReturn(nil) |
| 133 | + } |
| 134 | + default: |
| 135 | + klog.V(2).Infof("unknown authentication type: %s", authConfig.Spec.Type) |
| 136 | + return nil |
| 137 | + } |
| 138 | + |
| 139 | + err = c.syncSecret(ctx, secretString, syncCtx.Recorder()) |
| 140 | + statusHandler.AddConditions(status.HandleProgressingOrDegraded("OAuthClientSecretSync", "FailedApply", err)) |
| 141 | + return statusHandler.FlushAndReturn(err) |
| 142 | +} |
| 143 | + |
| 144 | +func (c *oauthClientSecretController) syncSecret(ctx context.Context, clientSecret string, recorder events.Recorder) error { |
| 145 | + operatorConfig, err := c.consoleOperatorLister.Get(api.ConfigResourceName) |
| 146 | + if err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + secret, err := c.targetNSSecretsLister.Secrets(api.TargetNamespace).Get("console-oauth-config") |
| 151 | + if apierrors.IsNotFound(err) || secretsub.GetSecretString(secret) == clientSecret { |
| 152 | + _, _, err = resourceapply.ApplySecret(ctx, c.secretsClient, recorder, secretsub.DefaultSecret(operatorConfig, clientSecret)) |
| 153 | + } |
| 154 | + return err |
| 155 | +} |
| 156 | + |
| 157 | +// handleStatus returns whether sync should happen and any error encountering |
| 158 | +// determining the operator's management state |
| 159 | +// TODO: extract this logic to where it can be used for all controllers |
| 160 | +func (c *oauthClientSecretController) handleManaged(ctx context.Context) (bool, error) { |
| 161 | + operatorSpec, _, _, err := c.operatorClient.GetOperatorState() |
| 162 | + if err != nil { |
| 163 | + return false, fmt.Errorf("failed to retrieve operator config: %w", err) |
| 164 | + } |
| 165 | + |
| 166 | + switch managementState := operatorSpec.ManagementState; managementState { |
| 167 | + case operatorv1.Managed: |
| 168 | + klog.V(4).Infoln("console is in a managed state.") |
| 169 | + return true, nil |
| 170 | + case operatorv1.Unmanaged: |
| 171 | + klog.V(4).Infoln("console is in an unmanaged state.") |
| 172 | + return false, nil |
| 173 | + case operatorv1.Removed: |
| 174 | + klog.V(4).Infoln("console has been removed.") |
| 175 | + return false, nil |
| 176 | + default: |
| 177 | + return false, fmt.Errorf("console is in an unknown state: %v", managementState) |
| 178 | + } |
| 179 | +} |
0 commit comments