Skip to content

Commit

Permalink
Update the UserData Module to support base64 encoded userdata
Browse files Browse the repository at this point in the history
The UserData Module previously didn't support base64 encoding. With the
new changes to the UserData Module, decoding of base64 userdata is now
supported. This change maintains consistency with Amazon Linux 2's
cloud-init.

Signed-off-by: Matthew Carter <[email protected]>
Signed-off-by: Jon Oikawa <[email protected]>
  • Loading branch information
mattcataws committed Mar 15, 2021
1 parent cfb5d9e commit e83f1a6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/ec2macosinit/userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func (c *UserDataModule) Do(ctx *ModuleContext) (message string, err error) {
return "", fmt.Errorf("ec2macosinit: received an unexpected response code from IMDS: %d - %s\n", respCode, err)
}

// Attempt to base64 decode userdata.
// This maintains consistency alongside Amazon Linux 2's cloud-init, which states:
// "Some tools and users will base64 encode their data before handing it to
// an API like boto, which will base64 encode it again, so we try to decode."
decoded, err := decodeBase64(ud)
if err == nil {
ud = decoded
}

// Write user data to file
userDataFile := path.Join(baseDir, ctx.IMDS.InstanceID, fileName)
f, err := os.OpenFile(userDataFile, os.O_CREATE|os.O_WRONLY, 0755)
Expand Down
11 changes: 11 additions & 0 deletions lib/ec2macosinit/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ec2macosinit

import (
"bytes"
"encoding/base64"
"fmt"
"io"
"os"
Expand Down Expand Up @@ -194,3 +195,13 @@ func getOSProductVersion() (version string, err error) {

return version, nil
}

// decodeBase64 attempts to decode base64 data and returns the decoded string if successful
func decodeBase64(base64Data string) (decodedString string, err error) {
decodedBytes, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return "", fmt.Errorf("ec2macosinit: failed to decode base64 string: %s\n", err)
}

return string(decodedBytes), nil
}

0 comments on commit e83f1a6

Please sign in to comment.