Skip to content

Commit c31c582

Browse files
authoredOct 3, 2023
feat: add coder_external_auth and deprecate coder_git_auth (#163)
* feat: add `coder_external_auth` and deprecate `coder_git_auth` * Improve docs
1 parent a9ebf4b commit c31c582

File tree

5 files changed

+119
-5
lines changed

5 files changed

+119
-5
lines changed
 

‎docs/data-sources/external_auth.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_external_auth Data Source - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
Use this data source to require users to authenticate with an external service prior to workspace creation. This can be used to pre-authenticate external services in a workspace. (e.g. gcloud, gh, docker, etc)
7+
---
8+
9+
# coder_external_auth (Data Source)
10+
11+
Use this data source to require users to authenticate with an external service prior to workspace creation. This can be used to pre-authenticate external services in a workspace. (e.g. gcloud, gh, docker, etc)
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `id` (String) The ID of a configured external auth provider set up in your Coder deployment.
21+
22+
### Read-Only
23+
24+
- `access_token` (String) The access token returned by the external auth provider. This can be used to pre-authenticate command-line tools.

‎provider/externalauth.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"os"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
// externalAuthDataSource returns a schema for an external authentication data source.
13+
func externalAuthDataSource() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "Use this data source to require users to authenticate with an external service prior to workspace creation. This can be used to pre-authenticate external services in a workspace. (e.g. gcloud, gh, docker, etc)",
16+
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
17+
id, ok := rd.Get("id").(string)
18+
if !ok || id == "" {
19+
return diag.Errorf("id is required")
20+
}
21+
rd.SetId(id)
22+
23+
accessToken := os.Getenv(ExternalAuthAccessTokenEnvironmentVariable(id))
24+
rd.Set("access_token", accessToken)
25+
return nil
26+
},
27+
Schema: map[string]*schema.Schema{
28+
"id": {
29+
Type: schema.TypeString,
30+
Description: "The ID of a configured external auth provider set up in your Coder deployment.",
31+
Required: true,
32+
},
33+
"access_token": {
34+
Type: schema.TypeString,
35+
Computed: true,
36+
Description: "The access token returned by the external auth provider. This can be used to pre-authenticate command-line tools.",
37+
},
38+
},
39+
}
40+
}
41+
42+
func ExternalAuthAccessTokenEnvironmentVariable(id string) string {
43+
return fmt.Sprintf("CODER_EXTERNAL_AUTH_ACCESS_TOKEN_%s", id)
44+
}

‎provider/externalauth_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package provider_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/terraform-provider-coder/provider"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
11+
12+
"github.com/stretchr/testify/require"
13+
)
14+
15+
func TestExternalAuth(t *testing.T) {
16+
t.Parallel()
17+
18+
resource.Test(t, resource.TestCase{
19+
Providers: map[string]*schema.Provider{
20+
"coder": provider.New(),
21+
},
22+
IsUnitTest: true,
23+
Steps: []resource.TestStep{{
24+
Config: `
25+
provider "coder" {
26+
}
27+
data "coder_external_auth" "github" {
28+
id = "github"
29+
}
30+
`,
31+
Check: func(state *terraform.State) error {
32+
require.Len(t, state.Modules, 1)
33+
require.Len(t, state.Modules[0].Resources, 1)
34+
resource := state.Modules[0].Resources["data.coder_external_auth.github"]
35+
require.NotNil(t, resource)
36+
37+
attribs := resource.Primary.Attributes
38+
require.Equal(t, "github", attribs["id"])
39+
40+
return nil
41+
},
42+
}},
43+
})
44+
}

‎provider/gitauth.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import (
1212
// gitAuthDataSource returns a schema for a Git authentication data source.
1313
func gitAuthDataSource() *schema.Resource {
1414
return &schema.Resource{
15-
Description: "Use this data source to require users to authenticate with a Git provider prior to workspace creation. This can be used to perform an authenticated `git clone` in startup scripts.",
15+
DeprecationMessage: "Use the `coder_external_auth` data source instead.",
16+
Description: "Use this data source to require users to authenticate with a Git provider prior to workspace creation. This can be used to perform an authenticated `git clone` in startup scripts.",
1617
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
1718
rawID, ok := rd.GetOk("id")
1819
if !ok {

‎provider/provider.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,11 @@ func New() *schema.Provider {
6868
}, nil
6969
},
7070
DataSourcesMap: map[string]*schema.Resource{
71-
"coder_workspace": workspaceDataSource(),
72-
"coder_provisioner": provisionerDataSource(),
73-
"coder_parameter": parameterDataSource(),
74-
"coder_git_auth": gitAuthDataSource(),
71+
"coder_workspace": workspaceDataSource(),
72+
"coder_provisioner": provisionerDataSource(),
73+
"coder_parameter": parameterDataSource(),
74+
"coder_git_auth": gitAuthDataSource(),
75+
"coder_external_auth": externalAuthDataSource(),
7576
},
7677
ResourcesMap: map[string]*schema.Resource{
7778
"coder_agent": agentResource(),

0 commit comments

Comments
 (0)
Please sign in to comment.