-
Notifications
You must be signed in to change notification settings - Fork 65
🐛 Refresh token for provisioning machines #250
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,9 @@ import ( | |
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
const ( | ||
defaultTokenTTL = 10 * time.Minute | ||
var ( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this still be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, that was a half-completed thought to make it configurable via the CLI. I've finished it, but I have no objection to tearing it back out and making it a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. until we do make it configurable let's leave it as a const There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh can you make this change @sethp-nr ? |
||
// DefaultTokenTTL is the amount of time a bootstrap token (and therefore a KubeadmConfig) will be valid | ||
DefaultTokenTTL = 15 * time.Minute | ||
) | ||
|
||
// ClusterSecretsClientFactory support creation of secrets client for clusters | ||
|
@@ -76,7 +77,7 @@ func createToken(client corev1.SecretInterface) (string, error) { | |
Data: map[string][]byte{ | ||
bootstrapapi.BootstrapTokenIDKey: []byte(tokenID), | ||
bootstrapapi.BootstrapTokenSecretKey: []byte(tokenSecret), | ||
bootstrapapi.BootstrapTokenExpirationKey: []byte(time.Now().UTC().Add(defaultTokenTTL).Format(time.RFC3339)), | ||
bootstrapapi.BootstrapTokenExpirationKey: []byte(time.Now().UTC().Add(DefaultTokenTTL).Format(time.RFC3339)), | ||
bootstrapapi.BootstrapTokenUsageSigningKey: []byte("true"), | ||
bootstrapapi.BootstrapTokenUsageAuthentication: []byte("true"), | ||
bootstrapapi.BootstrapTokenExtraGroupsKey: []byte("system:bootstrappers:kubeadm:default-node-token"), | ||
|
@@ -89,3 +90,26 @@ func createToken(client corev1.SecretInterface) (string, error) { | |
} | ||
return token, nil | ||
} | ||
|
||
// refreshToken extends the TTL for an existing token | ||
func refreshToken(client corev1.SecretInterface, token string) error { | ||
substrs := bootstraputil.BootstrapTokenRegexp.FindStringSubmatch(token) | ||
if len(substrs) != 3 { | ||
return errors.Errorf("the bootstrap token %q was not of the form %q", token, bootstrapapi.BootstrapTokenPattern) | ||
} | ||
tokenID := substrs[1] | ||
|
||
secretName := bootstraputil.BootstrapTokenSecretName(tokenID) | ||
secret, err := client.Get(secretName, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if secret.Data == nil { | ||
return errors.Errorf("Invalid bootstrap secret %q, remove the token from the kubadm config to re-create", secretName) | ||
} | ||
secret.Data[bootstrapapi.BootstrapTokenExpirationKey] = []byte(time.Now().UTC().Add(DefaultTokenTTL).Format(time.RFC3339)) | ||
|
||
_, err = client.Update(secret) | ||
return err | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was looking more for a switch to organize the quick-to-return cases before generating the cloud-init
What do you think about restructuring this a little bit to include just a few cases that return early like:
this switch wouldn't need a default case since it's encapsulating the idea "should we return before doing the work we want to be doing?". The default is no, we should not return (which is the same as doing nothing)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oooh, yeah, I like that much more. I'll give it a shot.