Skip to content
This repository was archived by the owner on Jan 21, 2020. It is now read-only.

Commit c3ed98a

Browse files
JacobFrericksDavid Chung
authored andcommitted
Updating tags to only contain valid characters (#461)
Signed-off-by: Jacob Frericks <[email protected]>
1 parent 05d22a5 commit c3ed98a

File tree

7 files changed

+67
-12
lines changed

7 files changed

+67
-12
lines changed

examples/flavor/swarm/flavor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (s *baseFlavor) prepare(role string, flavorProperties *types.Any, instanceS
265265
swarmID = swarmStatus.ID
266266
}
267267

268-
link = types.NewLink().WithContext("swarm/" + swarmID + "/" + role)
268+
link = types.NewLink().WithContext("swarm::" + swarmID + "::" + role)
269269
context := &templateContext{
270270
flavorSpec: spec,
271271
instanceSpec: instanceSpec,

examples/instance/terraform/plugin.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ JSON looks like below, where the value of `value` is the instance body of the TF
103103
"vpc_security_group_ids" : ["${aws_security_group.default.id}"],
104104
"subnet_id": "${aws_subnet.default.id}",
105105
"tags" : {
106-
"Name" : "web4",
106+
"name" : "web4",
107107
"InstancePlugin" : "terraform"
108108
},
109109
"connection" : {
@@ -383,8 +383,17 @@ func (p *plugin) Provision(spec instance.Spec) (*instance.ID, error) {
383383
// set the tags.
384384
// add a name
385385
if spec.Tags != nil {
386-
if _, has := spec.Tags["Name"]; !has {
387-
spec.Tags["Name"] = string(id)
386+
switch properties.Type {
387+
case "softlayer_virtual_guest":
388+
// Set the "name" tag to be lowercase to meet platform requirements
389+
if _, has := spec.Tags["name"]; !has {
390+
spec.Tags["name"] = string(id)
391+
}
392+
default:
393+
// Set the first character of the "Name" tag to be uppercase to meet platform requirements
394+
if _, has := spec.Tags["Name"]; !has {
395+
spec.Tags["Name"] = string(id)
396+
}
388397
}
389398
}
390399

@@ -616,7 +625,9 @@ func terraformTags(v interface{}, key string) map[string]string {
616625
value := fmt.Sprintf("%v", v)
617626
if strings.Contains(value, ":") {
618627
log.Debugln("terraformTags system tags detected v=", v)
619-
vv := strings.Split(value, ":")
628+
// This assumes that the first colon is separating the key and the value of the tag.
629+
// This is done so that colons are valid characters in the value.
630+
vv := strings.SplitN(value, ":", 2)
620631
if len(vv) == 2 {
621632
tags[vv[0]] = vv[1]
622633
} else {

examples/instance/terraform/plugin_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ func run(t *testing.T, resourceType, properties string) {
185185
"terraform_demo_swarm_mgr_sl",
186186
"label1:value1",
187187
"label2:value2",
188-
"Name:" + string(*id),
188+
"name:" + string(*id),
189189
}), conv(props["tags"].([]interface{})))
190190
require.Equal(t, instanceSpec.Init, props["user_metadata"])
191191

@@ -233,7 +233,7 @@ func run(t *testing.T, resourceType, properties string) {
233233
"label1:changed1",
234234
"label2:value2",
235235
"label3:value3",
236-
"Name:" + string(*id),
236+
"name:" + string(*id),
237237
}), conv(props["tags"].([]interface{})))
238238
case "aws_instance":
239239
require.Equal(t, map[string]interface{}{
@@ -258,7 +258,7 @@ func run(t *testing.T, resourceType, properties string) {
258258
"label1": "changed1",
259259
"label2": "value2",
260260
"label3": "value3",
261-
"Name": string(*id),
261+
"name": string(*id),
262262
},
263263
},
264264
}, list)

pkg/plugin/group/types/types.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ package types
22

33
import (
44
"crypto/sha1"
5-
"encoding/base64"
5+
"encoding/base32"
66
"encoding/json"
77
"fmt"
8+
"strings"
89

910
"github.com/docker/infrakit/pkg/plugin"
1011
"github.com/docker/infrakit/pkg/spi/group"
@@ -109,5 +110,10 @@ func (c Spec) InstanceHash() string {
109110
hasher := sha1.New()
110111
hasher.Write(stableFormat(c.Instance))
111112
hasher.Write(stableFormat(c.Flavor))
112-
return base64.URLEncoding.EncodeToString(hasher.Sum(nil))
113+
encoded := base32.StdEncoding.EncodeToString(hasher.Sum(nil))
114+
// Only valid characters are [a-z][0-9] for support on specific platforms
115+
encoded = strings.ToLower(encoded)
116+
// Remove extra padding
117+
encoded = strings.TrimRight(encoded, "=")
118+
return encoded
113119
}

pkg/plugin/group/types/types_test.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@ package types
22

33
import (
44
"encoding/json"
5-
"github.com/stretchr/testify/require"
5+
"fmt"
6+
"regexp"
67
"testing"
8+
9+
"github.com/stretchr/testify/require"
710
)
811

912
const (
@@ -77,4 +80,13 @@ func TestInstanceHash(t *testing.T) {
7780
require.Equal(t, hash(specA), hash(specA))
7881
require.Equal(t, hash(specA), hash(reordered))
7982
require.NotEqual(t, hash(specA), hash(different))
83+
VerifyValidCharsInHash(t, hash(specA))
84+
VerifyValidCharsInHash(t, hash(reordered))
85+
VerifyValidCharsInHash(t, hash(different))
86+
}
87+
88+
func VerifyValidCharsInHash(t *testing.T, hash string) {
89+
regex := "[a-z0-9]"
90+
validString := regexp.MustCompile(regex)
91+
require.True(t, validString.MatchString(hash), fmt.Sprintf("Invalid characters found in string: %v. Valid characters are %v", hash, regex))
8092
}

pkg/types/link.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
const (
10-
letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
10+
letters = "abcdefghijklmnopqrstuvwxyz0123456789"
1111
)
1212

1313
func init() {

pkg/types/link_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package types
2+
3+
import (
4+
"fmt"
5+
"regexp"
6+
"testing"
7+
8+
"github.com/stretchr/testify/require"
9+
)
10+
11+
func TestRandomAlphaNumericString(t *testing.T) {
12+
VerifyRandomAlphaNumericString(t, 15)
13+
VerifyRandomAlphaNumericString(t, 150)
14+
}
15+
16+
// VerifyRandomAlphaNumericString Verifies the length and characters of the string created from randomAlphaNumericString with the given length
17+
func VerifyRandomAlphaNumericString(t *testing.T, length int) {
18+
actual := randomAlphaNumericString(length)
19+
// Verify the length is as expected
20+
require.Equal(t, length, len(actual), fmt.Sprintf("Unexpected length of string %v: Expected %v but was %v\n", actual, length, len(actual)))
21+
22+
// Verify the characters are as expected
23+
regex := "[a-z0-9]"
24+
validString := regexp.MustCompile(regex)
25+
require.True(t, validString.MatchString(actual), fmt.Sprintf("Invalid characters found in string: %v. Valid characters are %v", actual, regex))
26+
}

0 commit comments

Comments
 (0)