Skip to content

Commit ca6b30b

Browse files
authored
feat: Add auth property to coder_agent_script (#5)
This enables explicit definition of auth type, so the agent doesn't misinterpret the running environment. This also adds "username" and "name" properties to "coder_workspace" to allow for pretty resource naming inside a cloud.
1 parent ff9ae88 commit ca6b30b

File tree

5 files changed

+33
-36
lines changed

5 files changed

+33
-36
lines changed

Diff for: docs/data-sources/agent_script.md

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ resource "kubernetes_pod" "dev" {
3737

3838
### Optional
3939

40+
- **auth** (String) The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity".
4041
- **id** (String) The ID of this resource.
4142

4243
### Read-Only

Diff for: docs/data-sources/workspace.md

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ resource "kubernetes_pod" "dev" {
3030

3131
### Read-Only
3232

33+
- **name** (String) Name of the workspace.
3334
- **transition** (String) Either "start" or "stop". Use this to start/stop resources with "count".
35+
- **username** (String) Username of the workspace owner.
3436

3537

Diff for: docs/resources/agent.md

+1-9
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,13 @@ resource "google_compute_instance" "dev" {
4040

4141
### Optional
4242

43-
- **auth** (Block List, Max: 1) Authenticate an instance with zero-trust by using cloud metadata APIs. (see [below for nested schema](#nestedblock--auth))
4443
- **env** (Map of String) A mapping of environment variables to set inside the workspace.
4544
- **id** (String) The ID of this resource.
45+
- **instance_id** (String) An instance ID from a provisioned instance to enable zero-trust agent authentication.
4646
- **startup_script** (String) A script to run after the agent starts.
4747

4848
### Read-Only
4949

5050
- **token** (String) Set the environment variable "CODER_TOKEN" with this token to authenticate an agent.
5151

52-
<a id="nestedblock--auth"></a>
53-
### Nested Schema for `auth`
54-
55-
Optional:
56-
57-
- **instance_id** (String) A unique ID from the created compute resource to identify with cloud metadata APIs.
58-
- **type** (String) The authentication type to use. Must be one of: "google-instance-identity".
59-
6052

Diff for: internal/provider/provider.go

+27-21
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ func New() *schema.Provider {
6565
transition = "start"
6666
}
6767
rd.Set("transition", transition)
68+
rd.Set("username", os.Getenv("CODER_WORKSPACE_USERNAME"))
69+
rd.Set("name", os.Getenv("CODER_WORKSPACE_NAME"))
6870
return nil
6971
},
7072
Schema: map[string]*schema.Schema{
@@ -73,6 +75,16 @@ func New() *schema.Provider {
7375
Computed: true,
7476
Description: `Either "start" or "stop". Use this to start/stop resources with "count".`,
7577
},
78+
"username": {
79+
Type: schema.TypeString,
80+
Computed: true,
81+
Description: "Username of the workspace owner.",
82+
},
83+
"name": {
84+
Type: schema.TypeString,
85+
Computed: true,
86+
Description: "Name of the workspace.",
87+
},
7688
},
7789
},
7890
"coder_agent_script": {
@@ -82,6 +94,10 @@ func New() *schema.Provider {
8294
if !valid {
8395
return diag.Errorf("config was unexpected type %q", reflect.TypeOf(i).String())
8496
}
97+
auth, valid := resourceData.Get("auth").(string)
98+
if !valid {
99+
return diag.Errorf("auth was unexpected type %q", reflect.TypeOf(resourceData.Get("auth")))
100+
}
85101
operatingSystem, valid := resourceData.Get("os").(string)
86102
if !valid {
87103
return diag.Errorf("os was unexpected type %q", reflect.TypeOf(resourceData.Get("os")))
@@ -97,6 +113,7 @@ func New() *schema.Provider {
97113
script := os.Getenv(fmt.Sprintf("CODER_AGENT_SCRIPT_%s_%s", operatingSystem, arch))
98114
if script != "" {
99115
script = strings.ReplaceAll(script, "${ACCESS_URL}", accessURL.String())
116+
script = strings.ReplaceAll(script, "${AUTH_TYPE}", auth)
100117
}
101118
err = resourceData.Set("value", script)
102119
if err != nil {
@@ -106,6 +123,13 @@ func New() *schema.Provider {
106123
return nil
107124
},
108125
Schema: map[string]*schema.Schema{
126+
"auth": {
127+
Type: schema.TypeString,
128+
Default: "token",
129+
Optional: true,
130+
Description: `The authentication type the agent will use. Must be one of: "token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity".`,
131+
ValidateFunc: validation.StringInSlice([]string{"token", "google-instance-identity", "aws-instance-identity", "azure-instance-identity"}, false),
132+
},
109133
"os": {
110134
Type: schema.TypeString,
111135
Required: true,
@@ -144,29 +168,11 @@ func New() *schema.Provider {
144168
return nil
145169
},
146170
Schema: map[string]*schema.Schema{
147-
"auth": {
171+
"instance_id": {
148172
ForceNew: true,
149-
Description: "Authenticate an instance with zero-trust by using cloud metadata APIs.",
150-
Type: schema.TypeList,
173+
Description: "An instance ID from a provisioned instance to enable zero-trust agent authentication.",
151174
Optional: true,
152-
MaxItems: 1,
153-
Elem: &schema.Resource{
154-
Schema: map[string]*schema.Schema{
155-
"type": {
156-
ForceNew: true,
157-
Description: `The authentication type to use. Must be one of: "google-instance-identity".`,
158-
Optional: true,
159-
Type: schema.TypeString,
160-
ValidateFunc: validation.StringInSlice([]string{"google-instance-identity"}, false),
161-
},
162-
"instance_id": {
163-
ForceNew: true,
164-
Description: "A unique ID from the created compute resource to identify with cloud metadata APIs.",
165-
Optional: true,
166-
Type: schema.TypeString,
167-
},
168-
},
169-
},
175+
Type: schema.TypeString,
170176
},
171177
"env": {
172178
ForceNew: true,

Diff for: internal/provider/provider_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,7 @@ func TestAgent(t *testing.T) {
117117
url = "https://example.com"
118118
}
119119
resource "coder_agent" "new" {
120-
auth {
121-
type = "google-instance-identity"
122-
instance_id = "instance"
123-
}
120+
instance_id = "instance"
124121
env = {
125122
hi = "test"
126123
}
@@ -133,8 +130,7 @@ func TestAgent(t *testing.T) {
133130
require.NotNil(t, resource)
134131
for _, key := range []string{
135132
"token",
136-
"auth.0.type",
137-
"auth.0.instance_id",
133+
"instance_id",
138134
"env.hi",
139135
"startup_script",
140136
} {

0 commit comments

Comments
 (0)