-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
130 additions
and
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package bootstraptoken | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
restclient "k8s.io/client-go/rest" | ||
clientcmdapi "k8s.io/client-go/tools/clientcmd/api" | ||
bootstrapapi "k8s.io/cluster-bootstrap/token/api" | ||
bootstraputil "k8s.io/cluster-bootstrap/token/util" | ||
) | ||
|
||
func GetTokenIDSecretFromBootstrapTokenStr(tokenStr string) (string, string, error) { | ||
substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(tokenStr) | ||
if len(substrs) != 3 { // nolint: gomnd | ||
return "", "", fmt.Errorf("the bootstrap token %q was not of the form %q", tokenStr, bootstrapapi.BootstrapTokenPattern) | ||
} | ||
tokenID := substrs[1] | ||
tokenSecret := substrs[2] | ||
|
||
return tokenID, tokenSecret, nil | ||
} | ||
|
||
func GenerateSecretFromBootstrapTokenStr(tokenStr string, ttl time.Duration) (*v1.Secret, error) { | ||
tokenID, tokenSecret, err := GetTokenIDSecretFromBootstrapTokenStr(tokenStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
secretData := map[string][]byte{ | ||
bootstrapapi.BootstrapTokenIDKey: []byte(tokenID), | ||
bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret), | ||
bootstrapapi.BootstrapTokenExpirationKey: []byte(time.Now().UTC().Add(ttl).Format(time.RFC3339)), | ||
bootstrapapi.BootstrapTokenDescriptionKey: []byte("jsdj"), | ||
bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:byoh"), | ||
bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"), | ||
bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"), | ||
} | ||
|
||
bootstrapSecret := &v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: bootstraputil.BootstrapTokenSecretName(tokenID), | ||
Namespace: metav1.NamespaceSystem, | ||
}, | ||
Type: bootstrapapi.SecretTypeBootstrapToken, | ||
Data: secretData, | ||
} | ||
return bootstrapSecret, nil | ||
} | ||
|
||
func GenerateBootstrapKubeconfigFromBootstrapToken(clientConfig *restclient.Config, tokenStr string) (*clientcmdapi.Config, error) { | ||
tokenID, tokenSecret, err := GetTokenIDSecretFromBootstrapTokenStr(tokenStr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Build resulting kubeconfig. | ||
kubeconfigData := clientcmdapi.Config{ | ||
// Define a cluster stanza based on the bootstrap kubeconfig. | ||
Clusters: map[string]*clientcmdapi.Cluster{"default-cluster": { | ||
Server: clientConfig.Host, | ||
InsecureSkipTLSVerify: clientConfig.Insecure, | ||
CertificateAuthorityData: clientConfig.CAData, | ||
}}, | ||
// Define auth based on the obtained client cert. | ||
AuthInfos: map[string]*clientcmdapi.AuthInfo{"default-auth": { | ||
Token: fmt.Sprintf(tokenID + "." + tokenSecret), | ||
}}, | ||
// Define a context that connects the auth info and cluster, and set it as the default | ||
Contexts: map[string]*clientcmdapi.Context{"default-context": { | ||
Cluster: "default-cluster", | ||
AuthInfo: "default-auth", | ||
Namespace: "default", | ||
}}, | ||
CurrentContext: "default-context", | ||
} | ||
|
||
return &kubeconfigData, nil | ||
} |
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,14 @@ | ||
// Copyright 2022 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package encoder | ||
|
||
import ( | ||
"encoding/base64" | ||
"encoding/json" | ||
"strings" | ||
) | ||
|
||
func DecodeFromBase64(v interface{}, enc string) error { | ||
return json.NewDecoder(base64.NewDecoder(base64.StdEncoding, strings.NewReader(enc))).Decode(v) | ||
} |
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,21 @@ | ||
// Copyright 2022 VMware, Inc. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package encoder | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"encoding/json" | ||
) | ||
|
||
func EncodeToBase64(v interface{}) (string, error) { | ||
var buf bytes.Buffer | ||
encoder := base64.NewEncoder(base64.StdEncoding, &buf) | ||
err := json.NewEncoder(encoder).Encode(v) | ||
if err != nil { | ||
return "", err | ||
} | ||
encoder.Close() | ||
return buf.String(), nil | ||
} |
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