Skip to content

Commit dcf529e

Browse files
authored
Merge pull request integrations#1473 from F21/oidc-subject-claim-customization-template
feat: Add support for GitHub Actions OpenID Connect subject claim customization templates
2 parents 8adfbd2 + 4b79c14 commit dcf529e

20 files changed

+1590
-89
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package github
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
5+
)
6+
7+
func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplate() *schema.Resource {
8+
return &schema.Resource{
9+
Read: dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRead,
10+
11+
Schema: map[string]*schema.Schema{
12+
"include_claim_keys": {
13+
Type: schema.TypeList,
14+
Computed: true,
15+
Elem: &schema.Schema{
16+
Type: schema.TypeString,
17+
},
18+
},
19+
},
20+
}
21+
}
22+
23+
func dataSourceGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateRead(d *schema.ResourceData, meta interface{}) error {
24+
25+
client := meta.(*Owner).v3client
26+
orgName := meta.(*Owner).name
27+
ctx := meta.(*Owner).StopContext
28+
29+
err := checkOrganization(meta)
30+
if err != nil {
31+
return err
32+
}
33+
34+
template, _, err := client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, orgName)
35+
36+
if err != nil {
37+
return err
38+
}
39+
40+
d.SetId(orgName)
41+
d.Set("include_claim_keys", template.IncludeClaimKeys)
42+
43+
return nil
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package github
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
7+
)
8+
9+
func TestAccGithubActionsOrganizationOIDCSubjectClaimCustomizationTemplateDataSource(t *testing.T) {
10+
11+
t.Run("get an organization oidc subject claim customization template without error", func(t *testing.T) {
12+
13+
config := `
14+
resource "github_actions_organization_oidc_subject_claim_customization_template" "test" {
15+
include_claim_keys = ["actor", "actor_id", "head_ref", "repository"]
16+
}
17+
`
18+
19+
config2 := config + `
20+
data "github_actions_organization_oidc_subject_claim_customization_template" "test" {}
21+
`
22+
23+
check := resource.ComposeTestCheckFunc(
24+
resource.TestCheckResourceAttr("data.github_actions_organization_oidc_subject_claim_customization_template.test", "include_claim_keys.#", "4"),
25+
resource.TestCheckResourceAttr("data.github_actions_organization_oidc_subject_claim_customization_template.test", "include_claim_keys.0", "actor"),
26+
resource.TestCheckResourceAttr("data.github_actions_organization_oidc_subject_claim_customization_template.test", "include_claim_keys.1", "actor_id"),
27+
resource.TestCheckResourceAttr("data.github_actions_organization_oidc_subject_claim_customization_template.test", "include_claim_keys.2", "head_ref"),
28+
resource.TestCheckResourceAttr("data.github_actions_organization_oidc_subject_claim_customization_template.test", "include_claim_keys.3", "repository"),
29+
)
30+
31+
testCase := func(t *testing.T, mode string) {
32+
resource.Test(t, resource.TestCase{
33+
PreCheck: func() { skipUnlessMode(t, mode) },
34+
Providers: testAccProviders,
35+
Steps: []resource.TestStep{
36+
{
37+
Config: config,
38+
Check: resource.ComposeTestCheckFunc(),
39+
},
40+
{
41+
Config: config2,
42+
Check: check,
43+
},
44+
},
45+
})
46+
}
47+
48+
t.Run("with an anonymous account", func(t *testing.T) {
49+
t.Skip("anonymous account not supported for this operation")
50+
})
51+
52+
t.Run("with an individual account", func(t *testing.T) {
53+
t.Skip("individual account not supported for this operation")
54+
})
55+
56+
t.Run("with an organization account", func(t *testing.T) {
57+
testCase(t, organization)
58+
})
59+
})
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package github
2+
3+
import "github.com/hashicorp/terraform-plugin-sdk/helper/schema"
4+
5+
func dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplate() *schema.Resource {
6+
return &schema.Resource{
7+
Read: dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateRead,
8+
9+
Schema: map[string]*schema.Schema{
10+
"name": {
11+
Type: schema.TypeString,
12+
Required: true,
13+
},
14+
"use_default": {
15+
Type: schema.TypeBool,
16+
Computed: true,
17+
},
18+
"include_claim_keys": {
19+
Type: schema.TypeList,
20+
Computed: true,
21+
Elem: &schema.Schema{
22+
Type: schema.TypeString,
23+
},
24+
},
25+
},
26+
}
27+
}
28+
29+
func dataSourceGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateRead(d *schema.ResourceData, meta interface{}) error {
30+
client := meta.(*Owner).v3client
31+
32+
repository := d.Get("name").(string)
33+
owner := meta.(*Owner).name
34+
ctx := meta.(*Owner).StopContext
35+
36+
template, _, err := client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, owner, repository)
37+
38+
if err != nil {
39+
return err
40+
}
41+
42+
d.SetId(repository)
43+
d.Set("use_default", template.UseDefault)
44+
d.Set("include_claim_keys", template.IncludeClaimKeys)
45+
46+
return nil
47+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package github
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
)
10+
11+
func TestAccGithubActionsRepositoryOIDCSubjectClaimCustomizationTemplateDataSource(t *testing.T) {
12+
13+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
14+
15+
t.Run("get an repository oidc subject claim customization template without error", func(t *testing.T) {
16+
17+
config := fmt.Sprintf(`
18+
resource "github_repository" "test" {
19+
name = "tf-acc-test-%s"
20+
private = true
21+
}
22+
23+
resource "github_actions_repository_oidc_subject_claim_customization_template" "test" {
24+
repository = github_repository.test.name
25+
use_default = false
26+
include_claim_keys = ["repo", "context", "job_workflow_ref"]
27+
}
28+
`, randomID)
29+
30+
config2 := config + `
31+
data "github_actions_repository_oidc_subject_claim_customization_template" "test" {
32+
name = github_repository.test.name
33+
}
34+
`
35+
36+
config3 := fmt.Sprintf(`
37+
resource "github_repository" "test" {
38+
name = "tf-acc-test-%s"
39+
private = true
40+
}
41+
42+
resource "github_actions_repository_oidc_subject_claim_customization_template" "test" {
43+
repository = github_repository.test.name
44+
use_default = true
45+
}
46+
`, randomID)
47+
48+
config4 := config3 + `
49+
data "github_actions_repository_oidc_subject_claim_customization_template" "test" {
50+
name = github_repository.test.name
51+
}
52+
`
53+
54+
check1 := resource.ComposeTestCheckFunc(
55+
resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "use_default", "false"),
56+
resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "include_claim_keys.#", "3"),
57+
resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "include_claim_keys.0", "repo"),
58+
resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "include_claim_keys.1", "context"),
59+
resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "include_claim_keys.2", "job_workflow_ref"),
60+
)
61+
62+
check2 := resource.ComposeTestCheckFunc(
63+
resource.TestCheckResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "use_default", "true"),
64+
resource.TestCheckNoResourceAttr("data.github_actions_repository_oidc_subject_claim_customization_template.test", "include_claim_keys"),
65+
)
66+
67+
testCase := func(t *testing.T, mode string) {
68+
resource.Test(t, resource.TestCase{
69+
PreCheck: func() { skipUnlessMode(t, mode) },
70+
Providers: testAccProviders,
71+
Steps: []resource.TestStep{
72+
{
73+
Config: config,
74+
Check: resource.ComposeTestCheckFunc(),
75+
},
76+
{
77+
Config: config2,
78+
Check: check1,
79+
},
80+
{
81+
Config: config3,
82+
Check: resource.ComposeTestCheckFunc(),
83+
},
84+
{
85+
Config: config4,
86+
Check: check2,
87+
},
88+
},
89+
})
90+
}
91+
92+
t.Run("with an anonymous account", func(t *testing.T) {
93+
t.Skip("anonymous account not supported for this operation")
94+
})
95+
96+
t.Run("with an individual account", func(t *testing.T) {
97+
testCase(t, individual)
98+
})
99+
100+
t.Run("with an organization account", func(t *testing.T) {
101+
testCase(t, organization)
102+
})
103+
})
104+
}

0 commit comments

Comments
 (0)