Skip to content
Merged
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
38 changes: 32 additions & 6 deletions pkg/operator2/auth.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package operator2

import (
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

configv1 "github.com/openshift/api/config/v1"
Expand All @@ -18,8 +19,15 @@ import (
// used to fill the data in the /.well-known/oauth-authorization-server
// endpoint, but since that endpoint belongs to the apiserver, its syncing is
// handled in cluster-kube-apiserver-operator
func (c *authOperator) handleAuthConfig() (*configv1.Authentication, error) {
auth, err := c.authentication.Get(globalConfigName, metav1.GetOptions{})
func (c *authOperator) handleAuthConfigInner() (*configv1.Authentication, error) {
// always make sure this function does not rely on defaulting from defaultAuthConfig

authConfigNoDefaults, err := c.authentication.Get(globalConfigName, metav1.GetOptions{})
if errors.IsNotFound(err) {
authConfigNoDefaults, err = c.authentication.Create(&configv1.Authentication{
ObjectMeta: defaultGlobalConfigMeta(),
})
}
if err != nil {
return nil, err
}
Expand All @@ -28,10 +36,28 @@ func (c *authOperator) handleAuthConfig() (*configv1.Authentication, error) {
Name: targetName,
}

if auth.Status.IntegratedOAuthMetadata == expectedReference {
return auth, nil
if authConfigNoDefaults.Status.IntegratedOAuthMetadata == expectedReference {
return authConfigNoDefaults, nil
}

authConfigNoDefaults.Status.IntegratedOAuthMetadata = expectedReference
return c.authentication.UpdateStatus(authConfigNoDefaults)
}

func (c *authOperator) handleAuthConfig() (*configv1.Authentication, error) {
auth, err := c.handleAuthConfigInner()
if err != nil {
return nil, err
}
return defaultAuthConfig(auth), nil
}

func defaultAuthConfig(authConfig *configv1.Authentication) *configv1.Authentication {
out := authConfig.DeepCopy() // do not mutate informer cache

if len(out.Spec.Type) == 0 {
out.Spec.Type = configv1.AuthenticationTypeIntegratedOAuth
}

auth.Status.IntegratedOAuthMetadata = expectedReference
return c.authentication.UpdateStatus(auth)
return out
}
19 changes: 18 additions & 1 deletion pkg/operator2/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"

Expand All @@ -30,10 +31,16 @@ func (c *authOperator) handleOAuthConfig(
*configSyncData,
error,
) {
oauthConfig, err := c.oauth.Get(globalConfigName, metav1.GetOptions{})
oauthConfigNoDefaults, err := c.oauth.Get(globalConfigName, metav1.GetOptions{})
if errors.IsNotFound(err) {
oauthConfigNoDefaults, err = c.oauth.Create(&configv1.OAuth{
ObjectMeta: defaultGlobalConfigMeta(),
})
}
if err != nil {
return nil, nil, nil, err
}
oauthConfig := defaultOAuthConfig(oauthConfigNoDefaults)

var accessTokenInactivityTimeoutSeconds *int32
timeout := oauthConfig.Spec.TokenConfig.AccessTokenInactivityTimeoutSeconds
Expand Down Expand Up @@ -163,3 +170,13 @@ func getMasterCA() *string {
ca := serviceCAPath // need local var to be able to take address of it
return &ca
}

func defaultOAuthConfig(oauthConfig *configv1.OAuth) *configv1.OAuth {
out := oauthConfig.DeepCopy() // do not mutate informer cache

if out.Spec.TokenConfig.AccessTokenMaxAgeSeconds == 0 {
out.Spec.TokenConfig.AccessTokenMaxAgeSeconds = 24 * 60 * 60 // 1 day
}

return out
}
10 changes: 10 additions & 0 deletions pkg/operator2/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,16 @@ func defaultMeta() metav1.ObjectMeta {
}
}

func defaultGlobalConfigMeta() metav1.ObjectMeta {
return metav1.ObjectMeta{
Name: globalConfigName,
Labels: map[string]string{},
Annotations: map[string]string{
"release.openshift.io/create-only": "true",
},
}
}

func getPrefixFilter() controller.Filter {
names := operator.FilterByNames(targetName)
prefix := func(obj metav1.Object) bool { // TODO add helper to combine filters
Expand Down
22 changes: 1 addition & 21 deletions pkg/operator2/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
const (
resync = 20 * time.Minute

// TODO handle defaulting for this one once lib-go tolerated empty managementState
defaultOperatorConfig = `
apiVersion: operator.openshift.io/v1
kind: Authentication
Expand All @@ -37,31 +38,10 @@ metadata:
spec:
managementState: Managed
`

// TODO figure out the permanent home for top level CRDs and default CRs
defaultAuthentication = `
apiVersion: config.openshift.io/v1
kind: Authentication
metadata:
name: ` + globalConfigName + `
spec:
type: IntegratedOAuth
`
defaultOAuth = `
apiVersion: config.openshift.io/v1
kind: OAuth
metadata:
name: ` + globalConfigName + `
spec:
tokenConfig:
accessTokenMaxAgeSeconds: 86400
`
)

var customResources = map[schema.GroupVersionResource]string{
operatorv1.GroupVersion.WithResource("authentications"): defaultOperatorConfig,
configv1.GroupVersion.WithResource("authentications"): defaultAuthentication,
configv1.GroupVersion.WithResource("oauths"): defaultOAuth,
}

func RunOperator(ctx *controllercmd.ControllerContext) error {
Expand Down