|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + |
| 6 | + rbacv1 "k8s.io/api/rbac/v1" |
| 7 | +) |
| 8 | + |
| 9 | +type OIDCProviderConfig struct { |
| 10 | + // Name is the name of the OIDC provider. |
| 11 | + // May be used in k8s resources, therefore has to be a valid k8s name. |
| 12 | + // +kubebuilder:validation:MinLength=1 |
| 13 | + // +kubebuilder:validation:MaxLength=253 |
| 14 | + // +kubebuilder:validation:Pattern=`[a-z0-9]([-a-z0-9]*[a-z0-9])?(\.[a-z0-9]([-a-z0-9]*[a-z0-9])?)*` |
| 15 | + Name string `json:"name"` |
| 16 | + |
| 17 | + // Issuer is the issuer URL of the OIDC provider. |
| 18 | + Issuer string `json:"issuer"` |
| 19 | + |
| 20 | + // ClientID is the client ID to use for the OIDC provider. |
| 21 | + ClientID string `json:"clientID"` |
| 22 | + |
| 23 | + // GroupsClaim is the claim in the OIDC token that contains the groups. |
| 24 | + // If empty, the default claim "groups" will be used. |
| 25 | + // +kubebuilder:default="groups" |
| 26 | + // +optional |
| 27 | + GroupsClaim string `json:"groupsClaim"` |
| 28 | + |
| 29 | + // GroupsPrefix is a prefix that will be added to all group names when referenced in RBAC rules. |
| 30 | + // This is required to avoid conflicts with Kubernetes built-in groups. |
| 31 | + // If the prefix does not end with a colon (:), it will be added automatically. |
| 32 | + // +kubebuilder:validation:MinLength=1 |
| 33 | + GroupsPrefix string `json:"groupsPrefix"` |
| 34 | + |
| 35 | + // UsernameClaim is the claim in the OIDC token that contains the username. |
| 36 | + // If empty, the default claim "sub" will be used. |
| 37 | + // +kubebuilder:default="sub" |
| 38 | + // +optional |
| 39 | + UsernameClaim string `json:"usernameClaim"` |
| 40 | + |
| 41 | + // UsernamePrefix is a prefix that will be added to all usernames when referenced in RBAC rules. |
| 42 | + // This is required to avoid conflicts with Kubernetes built-in users. |
| 43 | + // If the prefix does not end with a colon (:), it will be added automatically. |
| 44 | + // +kubebuilder:validation:MinLength=1 |
| 45 | + UsernamePrefix string `json:"usernamePrefix"` |
| 46 | + |
| 47 | + // RoleBindings is a list of subjects with (cluster) role bindings that should be created for them. |
| 48 | + // Note that the username prefix is added automatically to the subjects' names, it must not be explicitly specified here. |
| 49 | + RoleBindings []RoleBindings `json:"roleBindings"` |
| 50 | +} |
| 51 | + |
| 52 | +type RoleBindings struct { |
| 53 | + // Subjects is a list of subjects that should be bound to the specified roles. |
| 54 | + // The subjects' names will be prefixed with the username prefix of the OIDC provider. |
| 55 | + Subjects []rbacv1.Subject `json:"subjects"` |
| 56 | + |
| 57 | + // RoleRefs is a list of (cluster) role references that the subjects should be bound to. |
| 58 | + // Note that existence of the roles is not checked and missing (cluster) roles will result in ineffective (cluster) role bindings. |
| 59 | + RoleRefs []RoleRef `json:"roleRefs"` |
| 60 | +} |
| 61 | + |
| 62 | +// +kubebuilder:validation:XValidation:rule="self.kind == 'Role' && has(self.namespace) && self.namespace != ”", message="namespace must be set if kind is 'Role'" |
| 63 | +// +kubebuilder:validation:XValidation:rule="self.kind == 'ClusterRole' && (!has(self.namespace) || self.namespace == ”)", message="namespace must not be set if kind is 'ClusterRole'" |
| 64 | +type RoleRef struct { |
| 65 | + // Name is the name of the role or cluster role to bind to the subjects. |
| 66 | + // +kubebuilder:validation:MinLength=1 |
| 67 | + Name string `json:"name"` |
| 68 | + |
| 69 | + // Namespace is the namespace of the role to bind to the subjects. |
| 70 | + // It must be set if the kind is 'Role' and may not be set if the kind is 'ClusterRole'. |
| 71 | + // +optional |
| 72 | + Namespace string `json:"namespace,omitempty"` |
| 73 | + |
| 74 | + // Kind is the kind of the role to bind to the subjects. |
| 75 | + // It must be 'Role' or 'ClusterRole'. |
| 76 | + // +kubebuilder:validation:Enum=Role;ClusterRole |
| 77 | + Kind string `json:"kind"` |
| 78 | +} |
| 79 | + |
| 80 | +// Default sets default values for the OIDCProviderConfig. |
| 81 | +// Modifies in-place and returns the receiver for chaining. |
| 82 | +func (o *OIDCProviderConfig) Default() *OIDCProviderConfig { |
| 83 | + if o == nil { |
| 84 | + return nil |
| 85 | + } |
| 86 | + if o.GroupsClaim == "" { |
| 87 | + o.GroupsClaim = "groups" |
| 88 | + } |
| 89 | + if !strings.HasSuffix(o.GroupsPrefix, ":") { |
| 90 | + o.GroupsPrefix += ":" |
| 91 | + } |
| 92 | + if o.UsernameClaim == "" { |
| 93 | + o.UsernameClaim = "sub" |
| 94 | + } |
| 95 | + if !strings.HasSuffix(o.UsernamePrefix, ":") { |
| 96 | + o.UsernamePrefix += ":" |
| 97 | + } |
| 98 | + return o |
| 99 | +} |
0 commit comments