Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple service-account-issuers in apiserver #16497

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions k8s/crds/kops.k8s.io_clusters.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1620,6 +1620,12 @@ spec:
description: KubeAPIServerConfig defines the configuration for the
kube api
properties:
additionalServiceAccountIssuers:
description: AdditionalServiceAccountIssuers can contain additional
service account token issuers.
items:
type: string
type: array
address:
description: 'Address is the binding address for the kube api:
Deprecated - use insecure-bind-address and bind-address'
Expand Down
24 changes: 24 additions & 0 deletions nodeup/pkg/model/kube_apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ func (b *KubeAPIServerBuilder) buildPod(ctx context.Context, kubeAPIServer *kops
"/usr/local/bin/kube-apiserver",
}
container.Args = append(container.Args, sortedStrings(flags)...)
container.Args = sortServiceAccountIssuers(container.Args, fi.ValueOf(kubeAPIServer.ServiceAccountIssuer), kubeAPIServer.AdditionalServiceAccountIssuers)
}

for _, path := range b.SSLHostPaths() {
Expand Down Expand Up @@ -755,3 +756,26 @@ func (b *KubeAPIServerBuilder) buildAnnotations() map[string]string {

return annotations
}

func sortServiceAccountIssuers(in []string, currentIssuer string, oldIssuers []string) []string {
if len(oldIssuers) == 0 {
return in
}

positionMap := make(map[int]string)
positionMap[0] = fmt.Sprintf("--service-account-issuer=%s", currentIssuer)
for k, v := range oldIssuers {
positionMap[k+1] = fmt.Sprintf("--service-account-issuer=%s", v)
}

out := make([]string, 0, len(in))
issuerPosition := 0
for _, flag := range in {
if strings.HasPrefix(flag, "--service-account-issuer=") {
flag = positionMap[issuerPosition]
issuerPosition++
}
out = append(out, flag)
}
return out
}
35 changes: 35 additions & 0 deletions nodeup/pkg/model/kube_apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package model
import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/flagbuilder"
"k8s.io/kops/upup/pkg/fi"
Expand Down Expand Up @@ -197,3 +198,37 @@ func TestKubeAPIServerBuilderARM64(t *testing.T) {
return builder.Build(target)
})
}

func TestSortServiceAccountIssuers(t *testing.T) {
type testCase struct {
name string
flags []string
currentIssuer string
oldIssuers []string
expected []string
}

testCases := []testCase{
{
name: "flags without old issuers",
flags: []string{"--bar=foo", "--service-account-issuer=https://foo"},
currentIssuer: "https://foo",
oldIssuers: nil,
expected: []string{"--bar=foo", "--service-account-issuer=https://foo"},
},
{
name: "flags without with old issuers",
flags: []string{"--bar=foo", "--service-account-issuer=https://aa", "--service-account-issuer=https://bar", "--service-account-issuer=https://foo", "--zz=zz"},
currentIssuer: "https://foo",
oldIssuers: []string{"https://aa", "https://bar"},
expected: []string{"--bar=foo", "--service-account-issuer=https://foo", "--service-account-issuer=https://aa", "--service-account-issuer=https://bar", "--zz=zz"},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
actual := sortServiceAccountIssuers(tc.flags, tc.currentIssuer, tc.oldIssuers)
assert.Equal(t, tc.expected, actual)
})
}
}
6 changes: 6 additions & 0 deletions nodeup/pkg/model/tests/golden/oidc/cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ spec:
- claim2=value2
oidcUsernameClaim: user
oidcUsernamePrefix: 'oidc:'
serviceAccountIssuer: https://huh.com/multisaissuer.example.com
additionalServiceAccountIssuers:
- https://foobar.com
- https://aaa.com
- https://hello.com
- https://dorld.com
kubelet:
anonymousAuth: false
kubernetesVersion: v1.28.0
Expand Down
8 changes: 6 additions & 2 deletions nodeup/pkg/model/tests/golden/oidc/tasks-kube-apiserver.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,12 @@ contents: |
- --requestheader-group-headers=X-Remote-Group
- --requestheader-username-headers=X-Remote-User
- --secure-port=443
- --service-account-issuer=https://api.internal.minimal.example.com
- --service-account-jwks-uri=https://api.internal.minimal.example.com/openid/v1/jwks
- --service-account-issuer=https://huh.com/multisaissuer.example.com
- --service-account-issuer=https://foobar.com
- --service-account-issuer=https://aaa.com
- --service-account-issuer=https://hello.com
- --service-account-issuer=https://dorld.com
- --service-account-jwks-uri=https://huh.com/multisaissuer.example.com/openid/v1/jwks
- --service-account-key-file=/srv/kubernetes/kube-apiserver/service-account.pub
- --service-account-signing-key-file=/srv/kubernetes/kube-apiserver/service-account.key
- --service-cluster-ip-range=100.64.0.0/13
Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/kops/componentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,9 @@ type KubeAPIServerConfig struct {
// in "iss" claim of issued tokens. This value is a string or URI.
ServiceAccountIssuer *string `json:"serviceAccountIssuer,omitempty" flag:"service-account-issuer"`

// AdditionalServiceAccountIssuers can contain additional service account token issuers.
AdditionalServiceAccountIssuers []string `json:"additionalServiceAccountIssuers,omitempty" flag:"service-account-issuer,repeat"`
zetaab marked this conversation as resolved.
Show resolved Hide resolved

// ServiceAccountJWKSURI overrides the path for the jwks document; this is useful when we are republishing the service account discovery information elsewhere.
ServiceAccountJWKSURI *string `json:"serviceAccountJWKSURI,omitempty" flag:"service-account-jwks-uri"`

Expand Down
3 changes: 3 additions & 0 deletions pkg/apis/kops/v1alpha2/componentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,9 @@ type KubeAPIServerConfig struct {
// in "iss" claim of issued tokens. This value is a string or URI.
ServiceAccountIssuer *string `json:"serviceAccountIssuer,omitempty" flag:"service-account-issuer"`

// AdditionalServiceAccountIssuers can contain additional service account token issuers.
AdditionalServiceAccountIssuers []string `json:"additionalServiceAccountIssuers,omitempty" flag:"service-account-issuer,repeat"`

// ServiceAccountJWKSURI overrides the path for the jwks document; this is useful when we are republishing the service account discovery information elsewhere.
ServiceAccountJWKSURI *string `json:"serviceAccountJWKSURI,omitempty" flag:"service-account-jwks-uri"`

Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kops/v1alpha2/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/kops/v1alpha2/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/apis/kops/v1alpha3/componentconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,9 @@ type KubeAPIServerConfig struct {
// in "iss" claim of issued tokens. This value is a string or URI.
ServiceAccountIssuer *string `json:"serviceAccountIssuer,omitempty" flag:"service-account-issuer"`

// AdditionalServiceAccountIssuers can contain additional service account token issuers.
AdditionalServiceAccountIssuers []string `json:"additionalServiceAccountIssuers,omitempty" flag:"service-account-issuer,repeat"`

// ServiceAccountJWKSURI overrides the path for the jwks document; this is useful when we are republishing the service account discovery information elsewhere.
ServiceAccountJWKSURI *string `json:"serviceAccountJWKSURI,omitempty" flag:"service-account-jwks-uri"`

Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/kops/v1alpha3/zz_generated.conversion.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/apis/kops/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/apis/kops/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -829,6 +829,9 @@ func validateKubeAPIServer(v *kops.KubeAPIServerConfig, c *kops.Cluster, fldPath
}
}

if c.IsKubernetesLT("1.22") && len(v.AdditionalServiceAccountIssuers) > 0 {
allErrs = append(allErrs, field.Forbidden(fldPath.Child("additionalServiceAccountIssuers"), "additionalServiceAccountIssuers requires Kubernetes 1.22+"))
}
zetaab marked this conversation as resolved.
Show resolved Hide resolved
return allErrs
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/apis/kops/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,23 @@ func TestValidateKubeAPIServer(t *testing.T) {
},
ExpectedErrors: []string{"Unsupported value::KubeAPIServer.logFormat"},
},
{
Input: kops.KubeAPIServerConfig{
AdditionalServiceAccountIssuers: []string{"https://foo.bar"},
},
Cluster: &kops.Cluster{
Spec: kops.ClusterSpec{
Authorization: &kops.AuthorizationSpec{
RBAC: &kops.RBACAuthorizationSpec{},
},
KubernetesVersion: "1.21.0",
CloudProvider: kops.CloudProviderSpec{
AWS: &kops.AWSSpec{},
},
},
},
ExpectedErrors: []string{"Forbidden::KubeAPIServer.additionalServiceAccountIssuers"},
},
}
for _, g := range grid {
if g.Cluster == nil {
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/kops/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading