Skip to content

Add login_type to coder_workspace_owner data source #235 #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Sep 11, 2024
1 change: 1 addition & 0 deletions docs/data-sources/workspace_owner.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
1 change: 1 addition & 0 deletions integration/workspace-owner/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}

Expand Down
18 changes: 18 additions & 0 deletions provider/workspace_owner.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package provider
import (
"context"
"encoding/json"
"errors"
"os"
"slices"
"strings"

"github.com/google/uuid"
Expand Down Expand Up @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mtojek should we instead set login_type to an empty string if Coder does not set the required env?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps at the very least, drop a diag.Warn?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we instead set login_type to an empty string

This seems reasonable and consistent with other ENV vars. I would start with empty string, we can always revisit it in the future and replace it with none.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems reasonable and consistent with other ENV vars. I would start with empty string, we can always revisit it in the future and replace it with none.

Great, what about the warn?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a diag.Warn if the CODER_WORKSPACE_OWNER_LOGIN_TYPE environment variable is not present or empty would be a good indication to the user that they have a potential mismatch of the Coder version and provider version.

Copy link
Contributor Author

@pmareke pmareke Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, but taking a look to the diag package there is not Warn method.

https://pkg.go.dev/github.com/hashicorp/terraform-plugin-sdk/v2/diag

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry! I should have been more specific -- you can append a diag.Diagnostic with severity diag.Warning to the second return argument of type diag.Diagnostics.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, this is the first time I have to deal with the diags, so please let me know if this solution is correct 🙏🏼 .

}

return nil
},
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -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.",
},
},
}
}
5 changes: 5 additions & 0 deletions provider/workspace_owner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
},
}},
Expand All @@ -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)
Expand Down Expand Up @@ -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
},
}},
Expand Down
Loading