From ab53274ec09908401f5b84f5ffe686bda14ff9b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Tue, 10 Sep 2024 17:28:41 +0200 Subject: [PATCH 01/11] feat: add login_type to coder_workspace_owner data source --- docs/data-sources/workspace_owner.md | 1 + integration/workspace-owner/main.tf | 1 + provider/workspace_owner.go | 18 ++++++++++++++++++ provider/workspace_owner_test.go | 5 +++++ 4 files changed, 25 insertions(+) diff --git a/docs/data-sources/workspace_owner.md b/docs/data-sources/workspace_owner.md index 1c64ea50..0f2425b3 100644 --- a/docs/data-sources/workspace_owner.md +++ b/docs/data-sources/workspace_owner.md @@ -55,3 +55,4 @@ resource "coder_env" "git_author_email" { - `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started. - `ssh_private_key` (String, Sensitive) The user's generated SSH private key. - `ssh_public_key` (String) The user's generated SSH public key. +- `login_type` (String) The user's login type. The valid options are `password`, `github`, `oidc` or `none`. diff --git a/integration/workspace-owner/main.tf b/integration/workspace-owner/main.tf index 2be11d8e..fd923a3d 100644 --- a/integration/workspace-owner/main.tf +++ b/integration/workspace-owner/main.tf @@ -39,6 +39,7 @@ locals { "workspace_owner.session_token" : data.coder_workspace_owner.me.session_token, "workspace_owner.ssh_private_key" : data.coder_workspace_owner.me.ssh_private_key, "workspace_owner.ssh_public_key" : data.coder_workspace_owner.me.ssh_public_key, + "workspace_owner.login_type" : data.coder_workspace_owner.me.login_type, } } diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index 13e36187..040784b1 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -3,7 +3,9 @@ package provider import ( "context" "encoding/json" + "errors" "os" + "slices" "strings" "github.com/google/uuid" @@ -53,6 +55,17 @@ func workspaceOwnerDataSource() *schema.Resource { _ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN")) _ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")) + types := []string{"password", "github", "oidc", "none"} + if login_type := os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"); login_type != "" { + if !slices.Contains(types, login_type) { + errorMessage := "invalid login type: %s, the valid types are: 'password, github, oidc, or none'" + return diag.Errorf(errorMessage, errors.New(errorMessage)) + } + _ = rd.Set("login_type", login_type) + } else { + _ = rd.Set("login_type", "none") + } + return nil }, Schema: map[string]*schema.Schema{ @@ -107,6 +120,11 @@ func workspaceOwnerDataSource() *schema.Resource { "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.", }, + "login_type": { + Type: schema.TypeString, + Computed: true, + Description: "The type of login the user has.", + }, }, } } diff --git a/provider/workspace_owner_test.go b/provider/workspace_owner_test.go index 90839cfc..146d958d 100644 --- a/provider/workspace_owner_test.go +++ b/provider/workspace_owner_test.go @@ -35,6 +35,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) { t.Setenv("CODER_WORKSPACE_OWNER_GROUPS", `["group1", "group2"]`) t.Setenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN", `supersecret`) t.Setenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", `alsosupersecret`) + t.Setenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE", `github`) resource.Test(t, resource.TestCase{ Providers: map[string]*schema.Provider{ @@ -63,6 +64,8 @@ func TestWorkspaceOwnerDatasource(t *testing.T) { assert.Equal(t, `group2`, attrs["groups.1"]) assert.Equal(t, `supersecret`, attrs["session_token"]) assert.Equal(t, `alsosupersecret`, attrs["oidc_access_token"]) + assert.Equal(t, `github`, attrs["login_type"]) + return nil }, }}, @@ -80,6 +83,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) { "CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN", "CODER_WORKSPACE_OWNER_SSH_PUBLIC_KEY", "CODER_WORKSPACE_OWNER_SSH_PRIVATE_KEY", + "CODER_WORKSPACE_OWNER_LOGIN_TYPE", } { // https://github.com/golang/go/issues/52817 t.Setenv(v, "") os.Unsetenv(v) @@ -111,6 +115,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) { assert.Empty(t, attrs["groups.0"]) assert.Empty(t, attrs["session_token"]) assert.Empty(t, attrs["oidc_access_token"]) + assert.Equal(t, "none", attrs["login_type"]) return nil }, }}, From bf8e5d0a9d49f79adabe891e3376fc4fa21ef47e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Tue, 10 Sep 2024 17:49:50 +0200 Subject: [PATCH 02/11] feat: remove types check --- provider/workspace_owner.go | 7 ------- 1 file changed, 7 deletions(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index 040784b1..d5914761 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -3,9 +3,7 @@ package provider import ( "context" "encoding/json" - "errors" "os" - "slices" "strings" "github.com/google/uuid" @@ -55,12 +53,7 @@ func workspaceOwnerDataSource() *schema.Resource { _ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN")) _ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")) - types := []string{"password", "github", "oidc", "none"} if login_type := os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"); login_type != "" { - if !slices.Contains(types, login_type) { - errorMessage := "invalid login type: %s, the valid types are: 'password, github, oidc, or none'" - return diag.Errorf(errorMessage, errors.New(errorMessage)) - } _ = rd.Set("login_type", login_type) } else { _ = rd.Set("login_type", "none") From a32460b75320261e67dc7cdfd69552b2532159ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Tue, 10 Sep 2024 17:52:47 +0200 Subject: [PATCH 03/11] feat: update integration test --- integration/integration_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/integration/integration_test.go b/integration/integration_test.go index 26b9544a..5362b34d 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -112,6 +112,7 @@ func TestIntegration(t *testing.T) { "workspace_owner.session_token": `.+`, "workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`, "workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`, + "workspace_owner.login_type": `none`, }, }, { From 6f969825bd8478478d09647c4761172c126c5a76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 13:36:47 +0200 Subject: [PATCH 04/11] feat: run linter --- docs/data-sources/workspace_owner.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/data-sources/workspace_owner.md b/docs/data-sources/workspace_owner.md index 0f2425b3..fbe4f205 100644 --- a/docs/data-sources/workspace_owner.md +++ b/docs/data-sources/workspace_owner.md @@ -50,9 +50,9 @@ resource "coder_env" "git_author_email" { - `full_name` (String) The full name of the user. - `groups` (List of String) The groups of which the user is a member. - `id` (String) The UUID of the workspace owner. +- `login_type` (String) The type of login the user has. - `name` (String) The username of the user. - `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. - `session_token` (String) Session token for authenticating with a Coder deployment. It is regenerated every time a workspace is started. - `ssh_private_key` (String, Sensitive) The user's generated SSH private key. - `ssh_public_key` (String) The user's generated SSH public key. -- `login_type` (String) The user's login type. The valid options are `password`, `github`, `oidc` or `none`. From fb691966c4cc73458fa14a041da9a7f6e24b04b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:00:32 +0200 Subject: [PATCH 05/11] feat: set empty string as the default value for the login type --- provider/workspace_owner.go | 6 +----- provider/workspace_owner_test.go | 2 +- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index d5914761..070bf8f5 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -53,11 +53,7 @@ func workspaceOwnerDataSource() *schema.Resource { _ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN")) _ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")) - if login_type := os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"); login_type != "" { - _ = rd.Set("login_type", login_type) - } else { - _ = rd.Set("login_type", "none") - } + _ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE")) return nil }, diff --git a/provider/workspace_owner_test.go b/provider/workspace_owner_test.go index 146d958d..91f47ea8 100644 --- a/provider/workspace_owner_test.go +++ b/provider/workspace_owner_test.go @@ -115,7 +115,7 @@ func TestWorkspaceOwnerDatasource(t *testing.T) { assert.Empty(t, attrs["groups.0"]) assert.Empty(t, attrs["session_token"]) assert.Empty(t, attrs["oidc_access_token"]) - assert.Equal(t, "none", attrs["login_type"]) + assert.Empty(t, attrs["login_type"]) return nil }, }}, From 1e4b8fbb20f5ed687edda886d6eeb87c153e75cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:16:29 +0200 Subject: [PATCH 06/11] feat: update integration test --- integration/integration_test.go | 2 +- provider/workspace_owner.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/integration/integration_test.go b/integration/integration_test.go index 5362b34d..dc3d5c98 100644 --- a/integration/integration_test.go +++ b/integration/integration_test.go @@ -112,7 +112,7 @@ func TestIntegration(t *testing.T) { "workspace_owner.session_token": `.+`, "workspace_owner.ssh_private_key": `(?s)^.+?BEGIN OPENSSH PRIVATE KEY.+?END OPENSSH PRIVATE KEY.+?$`, "workspace_owner.ssh_public_key": `(?s)^ssh-ed25519.+$`, - "workspace_owner.login_type": `none`, + "workspace_owner.login_type": ``, }, }, { diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index 070bf8f5..96554823 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -53,6 +53,9 @@ func workspaceOwnerDataSource() *schema.Resource { _ = rd.Set("session_token", os.Getenv("CODER_WORKSPACE_OWNER_SESSION_TOKEN")) _ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")) + if os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE") == "" { + diag.Warn("The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set") + } _ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE")) return nil From 210847d3c7e012a57d95e88d3e79ea39dcda09f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:32:35 +0200 Subject: [PATCH 07/11] feat: add a warning when the CODER_WORKSPACE_OWNER_LOGIN_TYPE is not set --- provider/workspace_owner.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index 96554823..fabc8d1b 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -54,11 +54,15 @@ func workspaceOwnerDataSource() *schema.Resource { _ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")) if os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE") == "" { - diag.Warn("The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set") + diags := req.Config.Get(ctx, &rd) + diags = append(diags, diag.Diagnostic{ + Severity: diag.Warning, + Summmary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set" + }, } _ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE")) - return nil + return diags }, Schema: map[string]*schema.Schema{ "id": { From 2f3f7345d5c657c58f75175317e17baa1f29060d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:48:44 +0200 Subject: [PATCH 08/11] feat: create a new diags variable --- provider/workspace_owner.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index fabc8d1b..d4e299c4 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -15,6 +15,8 @@ func workspaceOwnerDataSource() *schema.Resource { return &schema.Resource{ Description: "Use this data source to fetch information about the workspace owner.", ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { + diags := diag.Diagnostics{} + if idStr := os.Getenv("CODER_WORKSPACE_OWNER_ID"); idStr != "" { rd.SetId(idStr) } else { @@ -54,7 +56,6 @@ func workspaceOwnerDataSource() *schema.Resource { _ = rd.Set("oidc_access_token", os.Getenv("CODER_WORKSPACE_OWNER_OIDC_ACCESS_TOKEN")) if os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE") == "" { - diags := req.Config.Get(ctx, &rd) diags = append(diags, diag.Diagnostic{ Severity: diag.Warning, Summmary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set" From 63607a3cd777eb11441df8df65363e71835e2fa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:49:59 +0200 Subject: [PATCH 09/11] feat: add missing comma --- provider/workspace_owner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index d4e299c4..498969c9 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -58,7 +58,7 @@ func workspaceOwnerDataSource() *schema.Resource { if os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE") == "" { diags = append(diags, diag.Diagnostic{ Severity: diag.Warning, - Summmary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set" + Summmary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set", }, } _ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE")) From 3eb23d247451f6699deeca04a0c2141ea3a4e3e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:52:01 +0200 Subject: [PATCH 10/11] feat: add missing parenthesis --- provider/workspace_owner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index 498969c9..1f33d16c 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -59,7 +59,7 @@ func workspaceOwnerDataSource() *schema.Resource { diags = append(diags, diag.Diagnostic{ Severity: diag.Warning, Summmary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set", - }, + }) } _ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE")) From 241f90aab68136b5140923ed442d622f546cf90b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20L=C3=B3pez=20Mareque?= <Pedro.lopez.mareque@gmail.com> Date: Wed, 11 Sep 2024 14:57:13 +0200 Subject: [PATCH 11/11] feat: fix typo in summary field Co-authored-by: Cian Johnston <public@cianjohnston.ie> --- provider/workspace_owner.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/provider/workspace_owner.go b/provider/workspace_owner.go index 1f33d16c..c51751b0 100644 --- a/provider/workspace_owner.go +++ b/provider/workspace_owner.go @@ -58,7 +58,7 @@ func workspaceOwnerDataSource() *schema.Resource { if os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE") == "" { diags = append(diags, diag.Diagnostic{ Severity: diag.Warning, - Summmary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set", + Summary: "WARNING: The CODER_WORKSPACE_OWNER_LOGIN_TYPE env variable is not set", }) } _ = rd.Set("login_type", os.Getenv("CODER_WORKSPACE_OWNER_LOGIN_TYPE"))