Skip to content

Commit a9b0150

Browse files
blink-so[bot]matifalijohnstcn
authored
Update coder-login module to use coder_env resources (#389)
This PR updates the `coder-login` module to use `coder_env` resources instead of shell scripts for better security, maintainability, and native Terraform integration. ## Changes - **Replaced `coder_script` with `coder_env` resources**: Uses native Terraform provider resources instead of shell scripts - **Removed `run.sh` script**: Eliminated the need for external shell scripts - **Environment variables**: Sets `CODER_SESSION_TOKEN` and `CODER_URL` using `coder_env` resources - **Added comprehensive tests**: Includes Terraform tests with mocked data validation - **Version bump**: Updated module version from `v1.0.31` to `v1.1.0` (minor bump) ## Benefits - **Native Terraform approach**: Uses the provider's built-in resources instead of external scripts - **Better security**: Environment variables are set directly by Terraform without shell script interpolation - **Improved maintainability**: Cleaner, more declarative configuration - **Proper testing**: Comprehensive test coverage with mocked data sources - **Correct environment variables**: Uses `CODER_SESSION_TOKEN` and `CODER_URL` as per coder CLI documentation ## Testing - All Terraform tests pass successfully - Module validates correctly with `terraform validate` - Proper formatting verified with `terraform fmt` Co-authored-by: blink-so[bot] <211532188+blink-so[bot]@users.noreply.github.com> Co-authored-by: Atif Ali <[email protected]> Co-authored-by: Cian Johnston <[email protected]>
1 parent e94dfd2 commit a9b0150

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

registry/coder/modules/coder-login/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Automatically logs the user into Coder when creating their workspace.
1414
module "coder-login" {
1515
count = data.coder_workspace.me.start_count
1616
source = "registry.coder.com/coder/coder-login/coder"
17-
version = "1.0.31"
17+
version = "1.1.0"
1818
agent_id = coder_agent.example.id
1919
}
2020
```

registry/coder/modules/coder-login/main.tf

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@ variable "agent_id" {
1717
data "coder_workspace" "me" {}
1818
data "coder_workspace_owner" "me" {}
1919

20-
resource "coder_script" "coder-login" {
20+
resource "coder_env" "coder_session_token" {
2121
agent_id = var.agent_id
22-
script = templatefile("${path.module}/run.sh", {
23-
CODER_USER_TOKEN : data.coder_workspace_owner.me.session_token,
24-
CODER_DEPLOYMENT_URL : data.coder_workspace.me.access_url
25-
})
26-
display_name = "Coder Login"
27-
icon = "/icon/coder.svg"
28-
run_on_start = true
29-
start_blocks_login = true
22+
name = "CODER_SESSION_TOKEN"
23+
value = data.coder_workspace_owner.me.session_token
3024
}
3125

26+
resource "coder_env" "coder_url" {
27+
agent_id = var.agent_id
28+
name = "CODER_URL"
29+
value = data.coder_workspace.me.access_url
30+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Test for coder-login module
2+
3+
run "test_coder_login_module" {
4+
command = plan
5+
6+
variables {
7+
agent_id = "test-agent-id"
8+
}
9+
10+
# Test that the coder_env resources are created with correct configuration
11+
assert {
12+
condition = coder_env.coder_session_token.agent_id == "test-agent-id"
13+
error_message = "CODER_SESSION_TOKEN agent ID should match the input variable"
14+
}
15+
16+
assert {
17+
condition = coder_env.coder_session_token.name == "CODER_SESSION_TOKEN"
18+
error_message = "Environment variable name should be 'CODER_SESSION_TOKEN'"
19+
}
20+
21+
assert {
22+
condition = coder_env.coder_url.agent_id == "test-agent-id"
23+
error_message = "CODER_URL agent ID should match the input variable"
24+
}
25+
26+
assert {
27+
condition = coder_env.coder_url.name == "CODER_URL"
28+
error_message = "Environment variable name should be 'CODER_URL'"
29+
}
30+
}
31+
32+
# Test with mock data sources
33+
run "test_with_mock_data" {
34+
command = plan
35+
36+
variables {
37+
agent_id = "mock-agent"
38+
}
39+
40+
# Mock the data sources for testing
41+
override_data {
42+
target = data.coder_workspace.me
43+
values = {
44+
access_url = "https://coder.example.com"
45+
}
46+
}
47+
48+
override_data {
49+
target = data.coder_workspace_owner.me
50+
values = {
51+
session_token = "mock-session-token"
52+
}
53+
}
54+
55+
# Verify environment variables get the mocked values
56+
assert {
57+
condition = coder_env.coder_url.value == "https://coder.example.com"
58+
error_message = "CODER_URL should match workspace access_url"
59+
}
60+
61+
assert {
62+
condition = coder_env.coder_session_token.value == "mock-session-token"
63+
error_message = "CODER_SESSION_TOKEN should match workspace owner session_token"
64+
}
65+
}

registry/coder/modules/coder-login/run.sh

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)