Skip to content

Commit

Permalink
Fix refresh for org tokens with slashes (#412)
Browse files Browse the repository at this point in the history
Fixes #388
  • Loading branch information
seanyeh authored Sep 24, 2024
1 parent 94db6e3 commit 2236a45
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@

### Bug Fixes

- Fixes failing pulumi refresh on org tokens with a slash in its name [#388](https://github.com/pulumi/pulumi-pulumiservice/issues/388)

### Miscellaneous
10 changes: 8 additions & 2 deletions provider/pkg/provider/org_access_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,14 @@ func (ot *PulumiServiceOrgAccessTokenResource) deleteOrgAccessToken(ctx context.
func splitOrgAccessTokenId(id string) (string, string, string, error) {
// format: organization/name/tokenId
s := strings.Split(id, "/")
if len(s) != 3 {
if len(s) < 3 {
return "", "", "", fmt.Errorf("%q is invalid, must contain a single slash ('/')", id)
}
return s[0], s[1], s[2], nil

org := s[0]
tokenId := s[len(s)-1]
// Name can contain slashes so this joins the split parts except for first and last
name := strings.Join(s[1:len(s)-1], "/")

return org, name, tokenId, nil
}
39 changes: 39 additions & 0 deletions provider/pkg/provider/org_access_token_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package provider

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
)

func TestSplitOrgAccessTokenId(t *testing.T) {
t.Run("Splits org access token id", func(t *testing.T) {
tokenId := "org/name/id"

org, name, id, err := splitOrgAccessTokenId(tokenId)
assert.NoError(t, err)

assert.Equal(t, "org", org)
assert.Equal(t, "name", name)
assert.Equal(t, "id", id)
})

t.Run("Splits org access token id with name with slashes", func(t *testing.T) {
tokenId := "org/name/with/slashes/id"

org, name, id, err := splitOrgAccessTokenId(tokenId)
assert.NoError(t, err)

assert.Equal(t, "org", org)
assert.Equal(t, "name/with/slashes", name)
assert.Equal(t, "id", id)
})

t.Run("Splits org access token id with invalid id", func(t *testing.T) {
tokenId := "org/badname"

_, _, _, err := splitOrgAccessTokenId(tokenId)
assert.ErrorContains(t, err, fmt.Sprintf("%q is invalid, must contain a single slash ('/')", tokenId))
})
}

0 comments on commit 2236a45

Please sign in to comment.