sigs.k8s.io/cluster-api/bootstrap/kubeadm@v0.0.0-20191016155141-23a891785b60/controllers/token.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package controllers 18 19 import ( 20 "time" 21 22 "github.com/pkg/errors" 23 v1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 corev1 "k8s.io/client-go/kubernetes/typed/core/v1" 26 bootstrapapi "k8s.io/cluster-bootstrap/token/api" 27 bootstraputil "k8s.io/cluster-bootstrap/token/util" 28 clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha2" 29 capiremote "sigs.k8s.io/cluster-api/controllers/remote" 30 "sigs.k8s.io/controller-runtime/pkg/client" 31 ) 32 33 var ( 34 // DefaultTokenTTL is the amount of time a bootstrap token (and therefore a KubeadmConfig) will be valid 35 DefaultTokenTTL = 15 * time.Minute 36 ) 37 38 // ClusterSecretsClientFactory support creation of secrets client for clusters 39 type ClusterSecretsClientFactory struct{} 40 41 // NewSecretsClient returns a new client supporting SecretInterface for the cluster 42 func (f ClusterSecretsClientFactory) NewSecretsClient(client client.Client, cluster *clusterv1.Cluster) (corev1.SecretInterface, error) { 43 remoteClient, err := capiremote.NewClusterClient(client, cluster) 44 if err != nil { 45 return nil, err 46 } 47 48 corev1Client, err := remoteClient.CoreV1() 49 if err != nil { 50 return nil, err 51 } 52 53 return corev1Client.Secrets(metav1.NamespaceSystem), nil 54 } 55 56 // createToken attempts to create a token with the given ID. 57 func createToken(client corev1.SecretInterface) (string, error) { 58 token, err := bootstraputil.GenerateBootstrapToken() 59 if err != nil { 60 return "", errors.Wrap(err, "unable to generate bootstrap token") 61 } 62 63 substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(token) 64 if len(substrs) != 3 { 65 return "", errors.Errorf("the bootstrap token %q was not of the form %q", token, bootstrapapi.BootstrapTokenPattern) 66 } 67 tokenID := substrs[1] 68 tokenSecret := substrs[2] 69 70 secretName := bootstraputil.BootstrapTokenSecretName(tokenID) 71 secretToken := &v1.Secret{ 72 ObjectMeta: metav1.ObjectMeta{ 73 Name: secretName, 74 Namespace: metav1.NamespaceSystem, 75 }, 76 Type: bootstrapapi.SecretTypeBootstrapToken, 77 Data: map[string][]byte{ 78 bootstrapapi.BootstrapTokenIDKey: []byte(tokenID), 79 bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret), 80 bootstrapapi.BootstrapTokenExpirationKey: []byte(time.Now().UTC().Add(DefaultTokenTTL).Format(time.RFC3339)), 81 bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"), 82 bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"), 83 bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:kubeadm:default-node-token"), 84 bootstrapapi.BootstrapTokenDescriptionKey: []byte("token generated by cluster-api-bootstrap-provider-kubeadm"), 85 }, 86 } 87 88 if _, err = client.Create(secretToken); err != nil { 89 return "", err 90 } 91 return token, nil 92 } 93 94 // refreshToken extends the TTL for an existing token 95 func refreshToken(client corev1.SecretInterface, token string) error { 96 substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(token) 97 if len(substrs) != 3 { 98 return errors.Errorf("the bootstrap token %q was not of the form %q", token, bootstrapapi.BootstrapTokenPattern) 99 } 100 tokenID := substrs[1] 101 102 secretName := bootstraputil.BootstrapTokenSecretName(tokenID) 103 secret, err := client.Get(secretName, metav1.GetOptions{}) 104 if err != nil { 105 return err 106 } 107 108 if secret.Data == nil { 109 return errors.Errorf("Invalid bootstrap secret %q, remove the token from the kubadm config to re-create", secretName) 110 } 111 secret.Data[bootstrapapi.BootstrapTokenExpirationKey] = []byte(time.Now().UTC().Add(DefaultTokenTTL).Format(time.RFC3339)) 112 113 _, err = client.Update(secret) 114 return err 115 }