Skip to content
This repository has been archived by the owner on Dec 26, 2023. It is now read-only.

Commit

Permalink
fix: use base58 alphabet for resource IDs (#680)
Browse files Browse the repository at this point in the history
Looks like TFC resource IDs, e.g. `ws-3i96qoAvc9BevrhS`, use only
characters from the base58 alphabet, whereas OTF uses any alphanumeric
characters, including non-base58 characters such as `0` and `I`.

This PR brings OTF into line with TFC. This is necessary because I've
noticed that the `tfe_workspace_settings` resource in the `tfe` provider
has started validating workspace IDs, checking that they are base58.
  • Loading branch information
leg100 committed Dec 24, 2023
1 parent a2d4583 commit 1e7d7a2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -694,8 +694,6 @@ github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack/v4 v4.3.12/go.mod h1:gborTTJjAo/GWTqqRjrLCn9pgNN+NXzzngzBKDPIqw4=
github.com/vmihailenco/tagparser v0.1.1/go.mod h1:OeAg3pn3UbLjkWt+rN9oFYB6u/cQgqMEUPoW2WPyhdI=
github.com/xanzy/go-gitlab v0.73.1 h1:UMagqUZLJdjss1SovIC+kJCH4k2AZWXl58gJd38Y/hI=
github.com/xanzy/go-gitlab v0.73.1/go.mod h1:d/a0vswScO7Agg1CZNz15Ic6SSvBG9vfw8egL99t4kA=
github.com/xanzy/go-gitlab v0.95.0 h1:lnYFPDsZuoSWXSC9xPLMcAWlGgndMn+erexGa+jJsS0=
github.com/xanzy/go-gitlab v0.95.0/go.mod h1:ETg8tcj4OhrB84UEgeE8dSuV/0h4BBL1uOV/qK0vlyI=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
Expand Down
11 changes: 8 additions & 3 deletions internal/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ func GetID(s any) (string, bool) {
return f.String(), true
}

// NewID constructs resource IDs, which are composed of the resource type and a
// random 16 character string, separated by a hyphen.
// base58 alphabet
var base58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"

// NewID constructs resource IDs, composed of:
// (1) a symbol representing a resource type, e.g. "ws" for workspaces
// (2) a hyphen
// (3) a 16 character string composed of random characters from the base58 alphabet
func NewID(rtype string) string {
return rtype + "-" + GenerateRandomString(16)
return rtype + "-" + GenerateRandomStringFromAlphabet(16, base58)
}

// ValidStringID checks if the given string pointer is non-nil and
Expand Down
8 changes: 7 additions & 1 deletion internal/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,15 @@ const alphanumeric = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKMNOPQRSTUVWXYZ01234567
// GenerateRandomString generates a random string composed of alphanumeric
// characters of length size.
func GenerateRandomString(size int) string {
return GenerateRandomStringFromAlphabet(size, alphanumeric)
}

// GenerateRandomStringFromAlphabet generates a random string of a given size
// using characters from the given alphabet.
func GenerateRandomStringFromAlphabet(size int, alphabet string) string {
buf := make([]byte, size)
for i := 0; i < size; i++ {
buf[i] = alphanumeric[rand.Intn(len(alphanumeric))]
buf[i] = alphabet[rand.Intn(len(alphabet))]
}
return string(buf)
}
Expand Down

0 comments on commit 1e7d7a2

Please sign in to comment.