Skip to content

Commit 1998a47

Browse files
authored
fix: capture all fields of WorkspaceOwner when converting to cty.Value (#82)
1 parent b379cc5 commit 1998a47

File tree

3 files changed

+109
-11
lines changed

3 files changed

+109
-11
lines changed

owner.go

+2-11
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ 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-
"github.com/zclconf/go-cty/cty/gocty"
10-
"golang.org/x/xerrors"
119
)
1210

1311
func workspaceOwnerHook(dfs fs.FS, input Input) (func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value), error) {
14-
if input.Owner.Groups == nil {
15-
input.Owner.Groups = []string{}
16-
}
17-
ownerGroups, err := gocty.ToCtyValue(input.Owner.Groups, cty.List(cty.String))
12+
ownerValue, err := input.Owner.ToCtyValue()
1813
if err != nil {
19-
return nil, xerrors.Errorf("converting owner groups: %w", err)
14+
return nil, err
2015
}
2116

22-
ownerValue := cty.ObjectVal(map[string]cty.Value{
23-
"groups": ownerGroups,
24-
})
25-
2617
return func(ctx *tfcontext.Context, blocks terraform.Blocks, inputVars map[string]cty.Value) {
2718
for _, block := range blocks.OfType("data") {
2819
// TODO: Does it have to be me?

types/owner.go

+50
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package types
22

33
import (
44
"github.com/google/uuid"
5+
"github.com/zclconf/go-cty/cty"
6+
"github.com/zclconf/go-cty/cty/gocty"
57
)
68

79
// Based on https://github.com/coder/terraform-provider-coder/blob/9a745586b23a9cb5de2f65a2dcac12e48b134ffa/provider/workspace_owner.go#L72
@@ -25,7 +27,55 @@ type WorkspaceOwner struct {
2527
RBACRoles []WorkspaceOwnerRBACRole `json:"rbac_roles"`
2628
}
2729

30+
func (o *WorkspaceOwner) ToCtyValue() (cty.Value, error) {
31+
if o.Groups == nil {
32+
o.Groups = []string{}
33+
}
34+
convertedGroups, err := gocty.ToCtyValue(o.Groups, cty.List(cty.String))
35+
if err != nil {
36+
return cty.Value{}, err
37+
}
38+
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)
50+
}
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+
2864
type WorkspaceOwnerRBACRole struct {
2965
Name string `json:"name"`
3066
OrgID uuid.UUID `json:"org_id"`
3167
}
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
81+
}

types/owner_test.go

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package types
2+
3+
import (
4+
"testing"
5+
6+
"github.com/google/uuid"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestToCtyValue(t *testing.T) {
11+
owner := WorkspaceOwner{
12+
ID: uuid.MustParse("f6457744-3e16-45b2-b3b0-80c2df491c99"),
13+
Name: "Nissa",
14+
FullName: "Nissa, Worldwaker",
15+
Email: "nissa@coder.com",
16+
SSHPublicKey: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBSHXs/HCgZlpEBOXLvLw4KaOrhy1DM1Vw6M/HPVE/UA\n",
17+
Groups: []string{"Everyone", "Planeswalkers", "Green"},
18+
LoginType: "password",
19+
RBACRoles: []WorkspaceOwnerRBACRole{
20+
{Name: "User Admin"},
21+
{Name: "Organization User Admin", OrgID: uuid.MustParse("5af9253a-ecde-4a71-b8f5-c8d15be9e52b")},
22+
},
23+
}
24+
25+
ownerValue, err := owner.ToCtyValue()
26+
require.NoError(t, err)
27+
28+
require.Equal(t, owner.ID.String(), ownerValue.AsValueMap()["id"].AsString())
29+
require.Equal(t, owner.Name, ownerValue.AsValueMap()["name"].AsString())
30+
require.Equal(t, owner.SSHPublicKey, ownerValue.AsValueMap()["ssh_public_key"].AsString())
31+
for i, it := range owner.Groups {
32+
require.Equal(t, it, ownerValue.AsValueMap()["groups"].AsValueSlice()[i].AsString())
33+
}
34+
for i, it := range owner.RBACRoles {
35+
roleValueMap := ownerValue.AsValueMap()["rbac_roles"].AsValueSlice()[i].AsValueMap()
36+
require.Equal(t, it.Name, roleValueMap["name"].AsString())
37+
require.Equal(t, it.OrgID.String(), roleValueMap["org_id"].AsString())
38+
}
39+
}
40+
41+
func TestToCtyValueWithNilLists(t *testing.T) {
42+
owner := WorkspaceOwner{
43+
ID: uuid.MustParse("f6457744-3e16-45b2-b3b0-80c2df491c99"),
44+
Name: "Nissa",
45+
FullName: "Nissa, Worldwaker",
46+
Email: "nissa@coder.com",
47+
SSHPublicKey: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBSHXs/HCgZlpEBOXLvLw4KaOrhy1DM1Vw6M/HPVE/UA\n",
48+
Groups: nil,
49+
LoginType: "password",
50+
RBACRoles: nil,
51+
}
52+
53+
ownerValue, err := owner.ToCtyValue()
54+
require.NoError(t, err)
55+
require.Empty(t, ownerValue.AsValueMap()["groups"].AsValueSlice())
56+
require.Empty(t, ownerValue.AsValueMap()["rbac_roles"].AsValueSlice())
57+
}

0 commit comments

Comments
 (0)