Skip to content

Commit

Permalink
Support URL encoded bootstrap token replacement (#743)
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit91 authored Sep 6, 2022
1 parent 07c8043 commit d5e2aaf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions pkg/controller/machine_bootstrap_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"crypto/rand"
"encoding/hex"
"math/big"
"net/url"
"strings"
"time"

Expand All @@ -38,6 +39,9 @@ import (

const placeholder = "<<BOOTSTRAP_TOKEN>>"

// urlEncodedPlaceholder is a placeholder that can for instance occur in ignition userdata format
var urlEncodedPlaceholder = url.QueryEscape(placeholder)

func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineName string, driver driver.Driver) error {
userData := driver.GetUserData()
klog.V(4).Infof("Creating bootstrap token!")
Expand All @@ -50,8 +54,16 @@ func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineNam
string(bootstrapTokenSecret.Data[bootstraptokenapi.BootstrapTokenIDKey]),
string(bootstrapTokenSecret.Data[bootstraptokenapi.BootstrapTokenSecretKey]),
)
klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userData = strings.ReplaceAll(userData, placeholder, token)

if strings.Contains(userData, placeholder) {
klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userData = strings.ReplaceAll(userData, placeholder, token)
} else if strings.Contains(userData, urlEncodedPlaceholder) {
klog.V(4).Infof("replacing url encoded placeholder %s with %s in user-data!", urlEncodedPlaceholder, url.QueryEscape(token))
userData = strings.ReplaceAll(userData, urlEncodedPlaceholder, url.QueryEscape(token))
} else {
klog.V(4).Info("no bootstrap token placeholder found in user-data, nothing to replace!")
}

driver.SetUserData(userData)
return nil
Expand Down
16 changes: 14 additions & 2 deletions pkg/util/provider/machinecontroller/machine_bootstrap_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"encoding/hex"
"fmt"
"math/big"
"net/url"
"strings"
"time"

Expand All @@ -38,6 +39,9 @@ import (

const placeholder = "<<BOOTSTRAP_TOKEN>>"

// urlEncodedPlaceholder is a placeholder that can for instance occur in ignition userdata format
var urlEncodedPlaceholder = url.QueryEscape(placeholder)

func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineName string, secret *corev1.Secret) error {
var (
userDataB []byte
Expand All @@ -61,8 +65,16 @@ func (c *controller) addBootstrapTokenToUserData(ctx context.Context, machineNam
string(bootstrapTokenSecret.Data[bootstraptokenapi.BootstrapTokenSecretKey]),
)

klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userDataS = strings.ReplaceAll(userDataS, placeholder, token)
if strings.Contains(userDataS, placeholder) {
klog.V(4).Infof("replacing placeholder %s with %s in user-data!", placeholder, token)
userDataS = strings.ReplaceAll(userDataS, placeholder, token)
} else if strings.Contains(userDataS, urlEncodedPlaceholder) {
klog.V(4).Infof("replacing url encoded placeholder %s with %s in user-data!", urlEncodedPlaceholder, url.QueryEscape(token))
userDataS = strings.ReplaceAll(userDataS, urlEncodedPlaceholder, url.QueryEscape(token))
} else {
klog.V(4).Info("no bootstrap token placeholder found in user-data, nothing to replace!")
}

secret.Data["userData"] = []byte(userDataS)

return nil
Expand Down

0 comments on commit d5e2aaf

Please sign in to comment.