Skip to content

Commit 377c360

Browse files
committed
feat: add coder_git_auth data source
This data source enables template authors to require git authentication for specific providers on workspace build.
1 parent 47888bd commit 377c360

File tree

5 files changed

+165
-0
lines changed

5 files changed

+165
-0
lines changed

docs/data-sources/git_auth.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "coder_git_auth Data Source - terraform-provider-coder"
4+
subcategory: ""
5+
description: |-
6+
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.
7+
---
8+
9+
# coder_git_auth (Data Source)
10+
11+
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.
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider "coder" {
17+
18+
}
19+
20+
data "coder_git_auth" "github" {
21+
# Matches the ID of the git auth provider in Coder.
22+
id = "github"
23+
}
24+
25+
resource "coder_agent" "dev" {
26+
os = "linux"
27+
arch = "amd64"
28+
dir = "~/coder"
29+
env = {
30+
GITHUB_TOKEN : data.coder_git_auth.github.access_token
31+
}
32+
startup_script = <<EOF
33+
if [ ! -d ~/coder ]; then
34+
git clone https://github.com/coder/coder
35+
fi
36+
EOF
37+
}
38+
```
39+
40+
<!-- schema generated by tfplugindocs -->
41+
## Schema
42+
43+
### Required
44+
45+
- `id` (String) The identifier of a configured git auth provider set up in your Coder deployment.
46+
47+
### Read-Only
48+
49+
- `access_token` (String) The access token returned by the git authentication provider. This can be used to pre-authenticate command-line tools.
50+
51+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
provider "coder" {
2+
3+
}
4+
5+
data "coder_git_auth" "github" {
6+
# Matches the ID of the git auth provider in Coder.
7+
id = "github"
8+
}
9+
10+
resource "coder_agent" "dev" {
11+
os = "linux"
12+
arch = "amd64"
13+
dir = "~/coder"
14+
env = {
15+
GITHUB_TOKEN : data.coder_git_auth.github.access_token
16+
}
17+
startup_script = <<EOF
18+
if [ ! -d ~/coder ]; then
19+
git clone https://github.com/coder/coder
20+
fi
21+
EOF
22+
}

provider/gitauth.go

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
// gitAuthDataSource returns a schema for a Git authentication data source.
13+
func gitAuthDataSource() *schema.Resource {
14+
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.",
16+
ReadContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
17+
rawID, ok := rd.GetOk("id")
18+
if !ok {
19+
return diag.Errorf("id is required")
20+
}
21+
id, ok := rawID.(string)
22+
if !ok {
23+
return diag.Errorf("unexpected type %q for id", rawID)
24+
}
25+
rd.SetId(id)
26+
27+
accessToken := os.Getenv(GitAuthAccessTokenEnvironmentVariable(id))
28+
rd.Set("access_token", accessToken)
29+
30+
return nil
31+
},
32+
Schema: map[string]*schema.Schema{
33+
"id": {
34+
Type: schema.TypeString,
35+
Required: true,
36+
Description: "The identifier of a configured git auth provider set up in your Coder deployment.",
37+
},
38+
"access_token": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
Description: "The access token returned by the git authentication provider. This can be used to pre-authenticate command-line tools.",
42+
},
43+
},
44+
}
45+
}
46+
47+
func GitAuthAccessTokenEnvironmentVariable(id string) string {
48+
return fmt.Sprintf("CODER_GIT_AUTH_ACCESS_TOKEN_%s", id)
49+
}

provider/gitauth_test.go

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

provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func New() *schema.Provider {
6969
"coder_workspace": workspaceDataSource(),
7070
"coder_provisioner": provisionerDataSource(),
7171
"coder_parameter": parameterDataSource(),
72+
"coder_git_auth": gitAuthDataSource(),
7273
},
7374
ResourcesMap: map[string]*schema.Resource{
7475
"coder_agent": agentResource(),

0 commit comments

Comments
 (0)