-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Anish Ramasekar <[email protected]>
- Loading branch information
Showing
8 changed files
with
301 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
Copyright 2021 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package k8s | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"time" | ||
|
||
authenticationv1 "k8s.io/api/authentication/v1" | ||
storagev1 "k8s.io/api/storage/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// TokenClient is a client for Kubernetes Token API | ||
type TokenClient struct { | ||
client kubernetes.Interface | ||
tokenRequests []storagev1.TokenRequest | ||
} | ||
|
||
// NewTokenClient creates a new TokenClient | ||
// The client will be used to request a token for the preconfigured audiences (--audiences) and expiration time. | ||
func NewTokenClient(config *rest.Config, audiences []string, expirationSeconds int64) (*TokenClient, error) { | ||
kubeClient := kubernetes.NewForConfigOrDie(config) | ||
|
||
tc := &TokenClient{ | ||
client: kubeClient, | ||
tokenRequests: []storagev1.TokenRequest{}, | ||
} | ||
|
||
for _, audience := range audiences { | ||
tokenRequest := storagev1.TokenRequest{ | ||
Audience: audience, | ||
ExpirationSeconds: &expirationSeconds, | ||
} | ||
tc.tokenRequests = append(tc.tokenRequests, tokenRequest) | ||
} | ||
|
||
errs := validateTokenRequests(tc.tokenRequests) | ||
if len(errs) > 0 { | ||
return nil, fmt.Errorf("failed to validate token requests: %v", errs) | ||
} | ||
return tc, nil | ||
} | ||
|
||
func (tc *TokenClient) PodServiceAccountTokenAttrs(ctx context.Context, podName, podNamespace, serviceAccountName string, podUID types.UID) (map[string]string, error) { | ||
if len(tc.tokenRequests) == 0 { | ||
return nil, nil | ||
} | ||
|
||
outputs := map[string]authenticationv1.TokenRequestStatus{} | ||
for _, tokenRequest := range tc.tokenRequests { | ||
audience := tokenRequest.Audience | ||
|
||
tr, err := tc.client.CoreV1(). | ||
ServiceAccounts(podNamespace). | ||
CreateToken(ctx, serviceAccountName, | ||
&authenticationv1.TokenRequest{ | ||
Spec: authenticationv1.TokenRequestSpec{ | ||
ExpirationSeconds: tokenRequest.ExpirationSeconds, | ||
Audiences: []string{audience}, | ||
BoundObjectRef: &authenticationv1.BoundObjectReference{ | ||
Kind: "Pod", | ||
APIVersion: "v1", | ||
Name: podName, | ||
UID: podUID, | ||
}, | ||
}, | ||
}, | ||
metav1.CreateOptions{}, | ||
) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("failed to create token request for %s: %w", audience, err) | ||
} | ||
outputs[audience] = tr.Status | ||
} | ||
|
||
tokens, err := json.Marshal(outputs) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to marshal token request status: %w", err) | ||
} | ||
|
||
return map[string]string{ | ||
"csi.storage.k8s.io/serviceAccount.tokens": string(tokens), | ||
}, nil | ||
} | ||
|
||
// Vendored from kubernetes/pkg/apis/storage/validation/validation.go | ||
// * tag: v1.23.0-alpha.1, | ||
// * commit: dfaeacb51f9e68f7730d9e400c7f19ddb08c0087 | ||
// * link: https://github.com/kubernetes/kubernetes/blob/dfaeacb51f9e68f7730d9e400c7f19ddb08c0087/pkg/apis/storage/validation/validation.go | ||
|
||
// validateTokenRequests tests if the Audience in each TokenRequest are different. | ||
// Besides, at most one TokenRequest can ignore Audience. | ||
func validateTokenRequests(tokenRequests []storagev1.TokenRequest) []error { | ||
const min = 10 * time.Minute | ||
var allErrs []error | ||
audiences := make(map[string]bool) | ||
for _, tokenRequest := range tokenRequests { | ||
audience := tokenRequest.Audience | ||
if _, ok := audiences[audience]; ok { | ||
allErrs = append(allErrs, fmt.Errorf("duplicate audience %s", audience)) | ||
continue | ||
} | ||
audiences[audience] = true | ||
|
||
if tokenRequest.ExpirationSeconds == nil { | ||
continue | ||
} | ||
if *tokenRequest.ExpirationSeconds < int64(min.Seconds()) { | ||
allErrs = append(allErrs, fmt.Errorf("expirationSeconds %d must be greater than %f", *tokenRequest.ExpirationSeconds, min.Seconds())) | ||
} | ||
if *tokenRequest.ExpirationSeconds > 1<<32 { | ||
allErrs = append(allErrs, fmt.Errorf("expirationSeconds %d must be less than %d", *tokenRequest.ExpirationSeconds, 1<<32)) | ||
} | ||
} | ||
|
||
return allErrs | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package k8s | ||
|
||
import ( | ||
"testing" | ||
|
||
storagev1 "k8s.io/api/storage/v1" | ||
"k8s.io/utils/pointer" | ||
) | ||
|
||
func TestValidateTokenRequests(t *testing.T) { | ||
test := []struct { | ||
name string | ||
tokenRequests []storagev1.TokenRequest | ||
wantErrorsLen int | ||
}{ | ||
{ | ||
name: "duplicate audience", | ||
tokenRequests: []storagev1.TokenRequest{ | ||
{ | ||
Audience: "aud1", | ||
ExpirationSeconds: pointer.Int64Ptr(3600), | ||
}, | ||
{ | ||
Audience: "aud1", | ||
ExpirationSeconds: pointer.Int64Ptr(1 << 33), | ||
}, | ||
}, | ||
wantErrorsLen: 1, | ||
}, | ||
{ | ||
name: "expiration seconds < 10m", | ||
tokenRequests: []storagev1.TokenRequest{ | ||
{ | ||
Audience: "aud1", | ||
ExpirationSeconds: pointer.Int64Ptr(599), | ||
}, | ||
}, | ||
wantErrorsLen: 1, | ||
}, | ||
{ | ||
name: "expiration seconds > 1<<32", | ||
tokenRequests: []storagev1.TokenRequest{ | ||
{ | ||
Audience: "aud1", | ||
ExpirationSeconds: pointer.Int64Ptr(1<<32 + 1), | ||
}, | ||
}, | ||
wantErrorsLen: 1, | ||
}, | ||
{ | ||
name: "token request has at most one token with empty string audience", | ||
tokenRequests: []storagev1.TokenRequest{ | ||
{ | ||
Audience: "", | ||
ExpirationSeconds: pointer.Int64Ptr(3600), | ||
}, | ||
}, | ||
wantErrorsLen: 0, | ||
}, | ||
{ | ||
name: "token request with different audiences", | ||
tokenRequests: []storagev1.TokenRequest{ | ||
{ | ||
Audience: "aud1", | ||
ExpirationSeconds: pointer.Int64Ptr(3600), | ||
}, | ||
{ | ||
Audience: "aud2", | ||
ExpirationSeconds: pointer.Int64Ptr(3600), | ||
}, | ||
}, | ||
wantErrorsLen: 0, | ||
}, | ||
} | ||
|
||
for _, test := range test { | ||
t.Run(test.name, func(t *testing.T) { | ||
errs := validateTokenRequests(test.tokenRequests) | ||
t.Log(errs) | ||
if len(errs) != test.wantErrorsLen { | ||
t.Errorf("validateTokenRequests() expected %v errors, got %v", test.wantErrorsLen, len(errs)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.