Skip to content

Commit eab8698

Browse files
authored
feat: Add rbac_roles to coder_workspace_owner data source (#330)
1 parent 552eb5e commit eab8698

File tree

6 files changed

+37
-1
lines changed

6 files changed

+37
-1
lines changed

docs/data-sources/workspace_owner.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ resource "coder_env" "git_author_email" {
5353
- `login_type` (String) The type of login the user has.
5454
- `name` (String) The username of the user.
5555
- `oidc_access_token` (String) A valid OpenID Connect access token of the workspace owner. This is only available if the workspace owner authenticated with OpenID Connect. If a valid token cannot be obtained, this value will be an empty string.
56+
- `rbac_roles` (List of Map) The RBAC roles and associated org ids of which the user is assigned.
5657
- `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started.
5758
- `ssh_private_key` (String, Sensitive) The user's generated SSH private key.
5859
- `ssh_public_key` (String) The user's generated SSH public key.

integration/integration_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ func TestIntegration(t *testing.T) {
122122
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
123123
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
124124
"workspace_owner.login_type": ``,
125+
"workspace_owner.rbac_roles": `\[\]`,
125126
},
126127
},
127128
{
@@ -150,6 +151,7 @@ func TestIntegration(t *testing.T) {
150151
"workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`,
151152
"workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`,
152153
"workspace_owner.login_type": `password`,
154+
"workspace_owner.rbac_roles": `\[\]`,
153155
},
154156
},
155157
{

integration/workspace-owner-filled/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ locals {
4040
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4141
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
4242
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
43+
"workspace_owner.rbac_roles" : jsonencode(data.coder_workspace_owner.me.rbac_roles),
4344
}
4445
}
4546

integration/workspace-owner/main.tf

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ locals {
4040
"workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key,
4141
"workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key,
4242
"workspace_owner.login_type" : data.coder_workspace_owner.me.login_type,
43+
"workspace_owner.rbac_roles" : jsonencode(data.coder_workspace_owner.me.rbac_roles),
4344
}
4445
}
4546

provider/workspace_owner.go

+27
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ func workspaceOwnerDataSource() *schema.Resource {
5959
_ = rd.Set("login_type", loginType)
6060
}
6161

62+
var rbacRoles []map[string]string
63+
if rolesRaw, ok := os.LookupEnv("CODER_WORKSPACE_OWNER_RBAC_ROLES"); ok {
64+
if err := json.NewDecoder(strings.NewReader(rolesRaw)).Decode(&rbacRoles); err != nil {
65+
return diag.Errorf("invalid user rbac roles: %s", err.Error())
66+
}
67+
}
68+
_ = rd.Set("rbac_roles", rbacRoles)
69+
6270
return diags
6371
},
6472
Schema: map[string]*schema.Schema{
@@ -118,6 +126,25 @@ func workspaceOwnerDataSource() *schema.Resource {
118126
Computed: true,
119127
Description: "The type of login the user has.",
120128
},
129+
"rbac_roles": {
130+
Type: schema.TypeList,
131+
Elem: &schema.Resource{
132+
Schema: map[string]*schema.Schema{
133+
"name": {
134+
Type: schema.TypeString,
135+
Computed: true,
136+
Description: "The name of the RBAC role.",
137+
},
138+
"org_id": {
139+
Type: schema.TypeString,
140+
Computed: true,
141+
Description: "The organization ID associated with the RBAC role.",
142+
},
143+
},
144+
},
145+
Computed: true,
146+
Description: "The RBAC roles of which the user is assigned.",
147+
},
121148
},
122149
}
123150
}

provider/workspace_owner_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
3434
t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`)
3535
t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", `alsosupersecret`)
3636
t.Setenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE", `github`)
37+
t.Setenv("CODER_WORKSPACE_OWNER_RBAC_ROLES", `[{"name":"member","org_id":"00000000-0000-0000-0000-000000000000"}]`)
3738

3839
resource.Test(t, resource.TestCase{
3940
ProviderFactories: coderFactory(),
@@ -61,7 +62,8 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
6162
assert.Equal(t, `supersecret`, attrs["session_token"])
6263
assert.Equal(t, `alsosupersecret`, attrs["oidc_access_token"])
6364
assert.Equal(t, `github`, attrs["login_type"])
64-
65+
assert.Equal(t, `member`, attrs["rbac_roles.0.name"])
66+
assert.Equal(t, `00000000-0000-0000-0000-000000000000`, attrs["rbac_roles.0.org_id"])
6567
return nil
6668
},
6769
}},
@@ -80,6 +82,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
8082
"CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY",
8183
"CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY",
8284
"CODER_WORKSPACE_OWNER_LOGIN_TYPE",
85+
"CODER_WORKSPACE_OWNER_RBAC_ROLES",
8386
} { // https://github.com/golang/go/issues/52817
8487
t.Setenv(v, "")
8588
os.Unsetenv(v)
@@ -110,6 +113,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) {
110113
assert.Empty(t, attrs["session_token"])
111114
assert.Empty(t, attrs["oidc_access_token"])
112115
assert.Empty(t, attrs["login_type"])
116+
assert.Empty(t, attrs["rbac_roles.0"])
113117
return nil
114118
},
115119
}},

0 commit comments

Comments
 (0)