Skip to content

Commit 14fedda

Browse files
committed
fix: require file-backed linode user data
1 parent 51f9365 commit 14fedda

3 files changed

Lines changed: 13 additions & 36 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ docker-machine create -d linode --linode-token=<linode-token> linode
6262
| `linode-swap-size` | `LINODE_SWAP_SIZE` | `512` | The amount of swap space provisioned on the Linode Instance
6363
| `linode-stackscript` | `LINODE_STACKSCRIPT` | None | Specifies the Linode StackScript to use to create the instance, either by numeric ID, or using the form *username*/*label*.
6464
| `linode-stackscript-data` | `LINODE_STACKSCRIPT_DATA` | None | A JSON string specifying data that is passed (via UDF) to the selected StackScript.
65-
| `linode-user-data` | `LINODE_USER_DATA` | None | Cloud-init user data passed to the Linode Metadata service; use inline content or prefix with `@` to read from a file. Content is base64-encoded automatically.
65+
| `linode-user-data` | `LINODE_USER_DATA` | None | Path to a cloud-init user-data file passed to the Linode Metadata service. File contents are base64-encoded automatically.
6666
| `linode-create-private-ip` | `LINODE_CREATE_PRIVATE_IP` | None | A flag specifying to create private IP for the Linode instance.
6767
| `linode-tags` | `LINODE_TAGS` | None | A comma separated list of tags to apply to the Linode resource
6868
| `linode-ua-prefix` | `LINODE_UA_PREFIX` | None | Prefix the User-Agent in Linode API calls with some 'product/version'

pkg/drivers/linode/linode.go

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
226226
mcnflag.StringFlag{
227227
EnvVar: "LINODE_USER_DATA",
228228
Name: "linode-user-data",
229-
Usage: "Cloud-init user data for the Linode Metadata service (inline or @path to file)",
229+
Usage: "Path to a cloud-init user-data file for the Linode Metadata service",
230230
},
231231
mcnflag.BoolFlag{
232232
EnvVar: "LINODE_CREATE_PRIVATE_IP",
@@ -345,21 +345,17 @@ func encodeUserData(userData string) (string, error) {
345345
return "", nil
346346
}
347347

348-
if strings.HasPrefix(userData, "@") {
349-
path := strings.TrimSpace(strings.TrimPrefix(userData, "@"))
350-
if path == "" {
351-
return "", fmt.Errorf("--linode-user-data requires a file path after '@'")
352-
}
353-
354-
content, err := os.ReadFile(path)
355-
if err != nil {
356-
return "", fmt.Errorf("failed to read user data from --linode-user-data file %q: %w", path, err)
357-
}
348+
path := strings.TrimSpace(userData)
349+
if path == "" {
350+
return "", fmt.Errorf("--linode-user-data requires a file path")
351+
}
358352

359-
userData = string(content)
353+
content, err := os.ReadFile(path)
354+
if err != nil {
355+
return "", fmt.Errorf("failed to read user data from --linode-user-data file %q: %w", path, err)
360356
}
361357

362-
return base64.StdEncoding.EncodeToString([]byte(userData)), nil
358+
return base64.StdEncoding.EncodeToString(content), nil
363359
}
364360

365361
// PreCreateCheck allows for pre-create operations to make sure a driver is ready for creation

pkg/drivers/linode/linode_test.go

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,6 @@ func TestSetConfigFromFlags(t *testing.T) {
3030
assert.Empty(t, checkFlags.InvalidFlags)
3131
}
3232

33-
func TestSetConfigFromFlagsUserDataInline(t *testing.T) {
34-
driver := NewDriver("", "")
35-
36-
userData := "#cloud-config\npackages:\n - htop\n"
37-
checkFlags := &drivers.CheckDriverOptions{
38-
FlagsValues: map[string]interface{}{
39-
"linode-token": "PROJECT",
40-
"linode-root-pass": "ROOTPASS",
41-
"linode-user-data": userData,
42-
},
43-
CreateFlags: driver.GetCreateFlags(),
44-
}
45-
46-
err := driver.SetConfigFromFlags(checkFlags)
47-
48-
assert.NoError(t, err)
49-
assert.Equal(t, base64.StdEncoding.EncodeToString([]byte(userData)), driver.UserData)
50-
}
51-
5233
func TestSetConfigFromFlagsUserDataFile(t *testing.T) {
5334
driver := NewDriver("", "")
5435

@@ -63,7 +44,7 @@ func TestSetConfigFromFlagsUserDataFile(t *testing.T) {
6344
FlagsValues: map[string]interface{}{
6445
"linode-token": "PROJECT",
6546
"linode-root-pass": "ROOTPASS",
66-
"linode-user-data": "@" + userDataPath,
47+
"linode-user-data": userDataPath,
6748
},
6849
CreateFlags: driver.GetCreateFlags(),
6950
}
@@ -81,7 +62,7 @@ func TestSetConfigFromFlagsUserDataMissingFile(t *testing.T) {
8162
FlagsValues: map[string]interface{}{
8263
"linode-token": "PROJECT",
8364
"linode-root-pass": "ROOTPASS",
84-
"linode-user-data": "@/does/not/exist",
65+
"linode-user-data": "/does/not/exist",
8566
},
8667
CreateFlags: driver.GetCreateFlags(),
8768
}
@@ -99,7 +80,7 @@ func TestSetConfigFromFlagsUserDataEmptyPath(t *testing.T) {
9980
FlagsValues: map[string]interface{}{
10081
"linode-token": "PROJECT",
10182
"linode-root-pass": "ROOTPASS",
102-
"linode-user-data": "@",
83+
"linode-user-data": " ",
10384
},
10485
CreateFlags: driver.GetCreateFlags(),
10586
}

0 commit comments

Comments
 (0)