Skip to content

Commit 7edcb9e

Browse files
authored
fix: don't use uuid.UUID for id fields (#90)
1 parent 2f4052d commit 7edcb9e

File tree

3 files changed

+46
-65
lines changed

3 files changed

+46
-65
lines changed

owner.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@ import (
66
"github.com/aquasecurity/trivy/pkg/iac/terraform"
77
tfcontext "github.com/aquasecurity/trivy/pkg/iac/terraform/context"
88
"github.com/zclconf/go-cty/cty"
9+
"golang.org/x/xerrors"
910
)
1011

11-
func workspaceOwnerHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
12+
func workspaceOwnerHook(_ fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
1213
ownerValue, err := input.Owner.ToCtyValue()
1314
if err != nil {
14-
return nil, err
15+
return nil, xerrors.Errorf("failed to convert owner value", err)
1516
}
1617

1718
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {

types/owner.go

Lines changed: 38 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,62 @@
11
package types
22

33
import (
4-
"github.com/google/uuid"
54
"github.com/zclconf/go-cty/cty"
65
"github.com/zclconf/go-cty/cty/gocty"
6+
"golang.org/x/xerrors"
77
)
88

99
// Based on https://github.com/coder/terraform-provider-coder/blob/9a745586b23a9cb5de2f65a2dcac12e48b134ffa/provider/workspace_owner.go#L72
1010
type WorkspaceOwner struct {
11-
ID uuid.UUID `json:"id"`
12-
Name string `json:"name"`
13-
FullName string `json:"full_name"`
14-
Email string `json:"email"`
15-
SSHPublicKey string `json:"ssh_public_key"`
11+
ID string `json:"id" cty:"id"`
12+
Name string `json:"name" cty:"name"`
13+
FullName string `json:"full_name" cty:"full_name"`
14+
Email string `json:"email" cty:"email"`
15+
SSHPublicKey string `json:"ssh_public_key" cty:"ssh_public_key"`
1616
// SSHPrivateKey is intentionally omitted for now, due to the security risk
1717
// that exposing it poses.
18-
// SSHPrivateKey string `json:"ssh_private_key"`
19-
Groups []string `json:"groups"`
18+
// SSHPrivateKey string `json:"ssh_private_key" cty:"ssh_private_key"`
19+
Groups []string `json:"groups" cty:"groups"`
2020
// SessionToken is intentionally omitted for now, due to the security risk
2121
// that exposing it poses.
22-
// SessionToken string `json:"session_token"`
22+
// SessionToken string `json:"session_token" cty:"session_token"`
2323
// OIDCAccessToken is intentionally omitted for now, due to the security risk
2424
// that exposing it poses.
25-
// OIDCAccessToken string `json:"oidc_access_token"`
26-
LoginType string `json:"login_type"`
27-
RBACRoles []WorkspaceOwnerRBACRole `json:"rbac_roles"`
25+
// OIDCAccessToken string `json:"oidc_access_token" cty:"oidc_access_token"`
26+
LoginType string `json:"login_type" cty:"login_type"`
27+
RBACRoles []WorkspaceOwnerRBACRole `json:"rbac_roles" cty:"rbac_roles"`
28+
}
29+
30+
type WorkspaceOwnerRBACRole struct {
31+
Name string `json:"name" cty:"name"`
32+
OrgID string `json:"org_id" cty:"org_id"`
2833
}
2934

3035
func (o *WorkspaceOwner) ToCtyValue() (cty.Value, error) {
3136
if o.Groups == nil {
32-
o.Groups = []string{}
37+
o.Groups = make([]string, 0)
3338
}
34-
convertedGroups, err := gocty.ToCtyValue(o.Groups, cty.List(cty.String))
35-
if err != nil {
36-
return cty.Value{}, err
39+
if o.RBACRoles == nil {
40+
o.RBACRoles = make([]WorkspaceOwnerRBACRole, 0)
3741
}
3842

39-
roleValues := make([]cty.Value, 0, len(o.RBACRoles))
40-
for _, role := range o.RBACRoles {
41-
roleValue, err := role.ToCtyValue()
42-
if err != nil {
43-
return cty.Value{}, err
44-
}
45-
roleValues = append(roleValues, roleValue)
46-
}
47-
var convertedRoles cty.Value = cty.ListValEmpty(WorkspaceOwnerRBACRole{}.CtyType())
48-
if len(roleValues) > 0 {
49-
convertedRoles = cty.ListVal(roleValues)
43+
ownerValue, err := gocty.ToCtyValue(o, cty.Object(map[string]cty.Type{
44+
"id": cty.String,
45+
"name": cty.String,
46+
"full_name": cty.String,
47+
"email": cty.String,
48+
"ssh_public_key": cty.String,
49+
"groups": cty.List(cty.String),
50+
"login_type": cty.String,
51+
"rbac_roles": cty.List(cty.Object(
52+
map[string]cty.Type{
53+
"name": cty.String,
54+
"org_id": cty.String,
55+
},
56+
)),
57+
}))
58+
if err != nil {
59+
return cty.Value{}, xerrors.Errorf("failed to convert owner value", err)
5060
}
51-
52-
return cty.ObjectVal(map[string]cty.Value{
53-
"id": cty.StringVal(o.ID.String()),
54-
"name": cty.StringVal(o.Name),
55-
"full_name": cty.StringVal(o.FullName),
56-
"email": cty.StringVal(o.Email),
57-
"ssh_public_key": cty.StringVal(o.SSHPublicKey),
58-
"groups": convertedGroups,
59-
"login_type": cty.StringVal(o.LoginType),
60-
"rbac_roles": convertedRoles,
61-
}), nil
62-
}
63-
64-
type WorkspaceOwnerRBACRole struct {
65-
Name string `json:"name"`
66-
OrgID uuid.UUID `json:"org_id"`
67-
}
68-
69-
func (_ WorkspaceOwnerRBACRole) CtyType() cty.Type {
70-
return cty.Object(map[string]cty.Type{
71-
"name": cty.String,
72-
"org_id": cty.String,
73-
})
74-
}
75-
76-
func (r *WorkspaceOwnerRBACRole) ToCtyValue() (cty.Value, error) {
77-
return cty.ObjectVal(map[string]cty.Value{
78-
"name": cty.StringVal(r.Name),
79-
"org_id": cty.StringVal(r.OrgID.String()),
80-
}), nil
61+
return ownerValue, nil
8162
}

types/owner_test.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package types
33
import (
44
"testing"
55

6-
"github.com/google/uuid"
76
"github.com/stretchr/testify/require"
87
)
98

109
func TestToCtyValue(t *testing.T) {
1110
owner := WorkspaceOwner{
12-
ID: uuid.MustParse("f6457744-3e16-45b2-b3b0-80c2df491c99"),
11+
ID: "f6457744-3e16-45b2-b3b0-80c2df491c99",
1312
Name: "Nissa",
1413
FullName: "Nissa, Worldwaker",
1514
@@ -18,14 +17,14 @@ func TestToCtyValue(t *testing.T) {
1817
LoginType: "password",
1918
RBACRoles: []WorkspaceOwnerRBACRole{
2019
{Name: "User Admin"},
21-
{Name: "Organization User Admin", OrgID: uuid.MustParse("5af9253a-ecde-4a71-b8f5-c8d15be9e52b")},
20+
{Name: "Organization User Admin", OrgID: "5af9253a-ecde-4a71-b8f5-c8d15be9e52b"},
2221
},
2322
}
2423

2524
ownerValue, err := owner.ToCtyValue()
2625
require.NoError(t, err)
2726

28-
require.Equal(t, owner.ID.String(), ownerValue.AsValueMap()["id"].AsString())
27+
require.Equal(t, owner.ID, ownerValue.AsValueMap()["id"].AsString())
2928
require.Equal(t, owner.Name, ownerValue.AsValueMap()["name"].AsString())
3029
require.Equal(t, owner.SSHPublicKey, ownerValue.AsValueMap()["ssh_public_key"].AsString())
3130
for i, it := range owner.Groups {
@@ -34,13 +33,13 @@ func TestToCtyValue(t *testing.T) {
3433
for i, it := range owner.RBACRoles {
3534
roleValueMap := ownerValue.AsValueMap()["rbac_roles"].AsValueSlice()[i].AsValueMap()
3635
require.Equal(t, it.Name, roleValueMap["name"].AsString())
37-
require.Equal(t, it.OrgID.String(), roleValueMap["org_id"].AsString())
36+
require.Equal(t, it.OrgID, roleValueMap["org_id"].AsString())
3837
}
3938
}
4039

4140
func TestToCtyValueWithNilLists(t *testing.T) {
4241
owner := WorkspaceOwner{
43-
ID: uuid.MustParse("f6457744-3e16-45b2-b3b0-80c2df491c99"),
42+
ID: "f6457744-3e16-45b2-b3b0-80c2df491c99",
4443
Name: "Nissa",
4544
FullName: "Nissa, Worldwaker",
4645

0 commit comments

Comments
 (0)