Skip to content

Commit 62c19f7

Browse files
liathkfcampbell
andauthored
Add Dependabot secrets support (Fixes #1006) (#1036)
* Add dependabot secrets support * Forgot to tie the new resources into the provider * Fix error in tests for secrets with encrypted_value that isnt base64 * Make unit tests actually use new resources * Remove vestiges of google/go-github v42 Co-authored-by: Keegan Campbell <[email protected]>
1 parent ff222c3 commit 62c19f7

12 files changed

+1174
-48
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"log"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
)
9+
10+
func dataSourceGithubDependabotPublicKey() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubDependabotPublicKeyRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"repository": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"key_id": {
20+
Type: schema.TypeString,
21+
Computed: true,
22+
},
23+
"key": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
},
28+
}
29+
}
30+
31+
func dataSourceGithubDependabotPublicKeyRead(d *schema.ResourceData, meta interface{}) error {
32+
repository := d.Get("repository").(string)
33+
owner := meta.(*Owner).name
34+
log.Printf("[INFO] Refreshing GitHub Dependabot Public Key from: %s/%s", owner, repository)
35+
36+
client := meta.(*Owner).v3client
37+
ctx := context.Background()
38+
39+
publicKey, _, err := client.Dependabot.GetRepoPublicKey(ctx, owner, repository)
40+
if err != nil {
41+
return err
42+
}
43+
44+
d.SetId(publicKey.GetKeyID())
45+
d.Set("key_id", publicKey.GetKeyID())
46+
d.Set("key", publicKey.GetKey())
47+
48+
return nil
49+
}
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+
"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 TestAccGithubDependabotPublicKeyDataSource(t *testing.T) {
12+
13+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
14+
15+
t.Run("queries a repository public key without error", func(t *testing.T) {
16+
17+
config := fmt.Sprintf(`
18+
resource "github_repository" "test" {
19+
name = "tf-acc-test-%[1]s"
20+
auto_init = true
21+
}
22+
23+
data "github_actions_public_key" "test" {
24+
repository = github_repository.test.id
25+
}
26+
`, randomID)
27+
28+
check := resource.ComposeTestCheckFunc(
29+
resource.TestCheckResourceAttrSet(
30+
"data.github_actions_public_key.test", "key",
31+
),
32+
)
33+
34+
testCase := func(t *testing.T, mode string) {
35+
resource.Test(t, resource.TestCase{
36+
PreCheck: func() { skipUnlessMode(t, mode) },
37+
Providers: testAccProviders,
38+
Steps: []resource.TestStep{
39+
{
40+
Config: config,
41+
Check: check,
42+
},
43+
},
44+
})
45+
}
46+
47+
t.Run("with an anonymous account", func(t *testing.T) {
48+
t.Skip("anonymous account not supported for this operation")
49+
})
50+
51+
t.Run("with an individual account", func(t *testing.T) {
52+
testCase(t, individual)
53+
})
54+
55+
t.Run("with an organization account", func(t *testing.T) {
56+
testCase(t, organization)
57+
})
58+
59+
})
60+
}

github/provider.go

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -89,50 +89,54 @@ func Provider() terraform.ResourceProvider {
8989
},
9090

9191
ResourcesMap: map[string]*schema.Resource{
92-
"github_actions_environment_secret": resourceGithubActionsEnvironmentSecret(),
93-
"github_actions_organization_secret": resourceGithubActionsOrganizationSecret(),
94-
"github_actions_organization_secret_repositories": resourceGithubActionsOrganizationSecretRepositories(),
95-
"github_actions_organization_permissions": resourceGithubActionsOrganizationPermissions(),
96-
"github_actions_runner_group": resourceGithubActionsRunnerGroup(),
97-
"github_actions_secret": resourceGithubActionsSecret(),
98-
"github_app_installation_repository": resourceGithubAppInstallationRepository(),
99-
"github_branch": resourceGithubBranch(),
100-
"github_branch_protection": resourceGithubBranchProtection(),
101-
"github_branch_protection_v3": resourceGithubBranchProtectionV3(),
102-
"github_issue": resourceGithubIssue(),
103-
"github_issue_label": resourceGithubIssueLabel(),
104-
"github_membership": resourceGithubMembership(),
105-
"github_organization_block": resourceOrganizationBlock(),
106-
"github_organization_project": resourceGithubOrganizationProject(),
107-
"github_organization_webhook": resourceGithubOrganizationWebhook(),
108-
"github_project_card": resourceGithubProjectCard(),
109-
"github_project_column": resourceGithubProjectColumn(),
110-
"github_repository_autolink_reference": resourceGithubRepositoryAutolinkReference(),
111-
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
112-
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
113-
"github_repository_environment": resourceGithubRepositoryEnvironment(),
114-
"github_repository_file": resourceGithubRepositoryFile(),
115-
"github_repository_milestone": resourceGithubRepositoryMilestone(),
116-
"github_repository_project": resourceGithubRepositoryProject(),
117-
"github_repository_pull_request": resourceGithubRepositoryPullRequest(),
118-
"github_repository_webhook": resourceGithubRepositoryWebhook(),
119-
"github_repository": resourceGithubRepository(),
120-
"github_team_membership": resourceGithubTeamMembership(),
121-
"github_team_members": resourceGithubTeamMembers(),
122-
"github_team_repository": resourceGithubTeamRepository(),
123-
"github_team_sync_group_mapping": resourceGithubTeamSyncGroupMapping(),
124-
"github_emu_group_mapping": resourceGithubEMUGroupMapping(),
125-
"github_team": resourceGithubTeam(),
126-
"github_user_gpg_key": resourceGithubUserGpgKey(),
127-
"github_user_invitation_accepter": resourceGithubUserInvitationAccepter(),
128-
"github_user_ssh_key": resourceGithubUserSshKey(),
129-
"github_branch_default": resourceGithubBranchDefault(),
92+
"github_actions_environment_secret": resourceGithubActionsEnvironmentSecret(),
93+
"github_actions_organization_permissions": resourceGithubActionsOrganizationPermissions(),
94+
"github_actions_organization_secret": resourceGithubActionsOrganizationSecret(),
95+
"github_actions_organization_secret_repositories": resourceGithubActionsOrganizationSecretRepositories(),
96+
"github_actions_runner_group": resourceGithubActionsRunnerGroup(),
97+
"github_actions_secret": resourceGithubActionsSecret(),
98+
"github_app_installation_repository": resourceGithubAppInstallationRepository(),
99+
"github_branch": resourceGithubBranch(),
100+
"github_branch_default": resourceGithubBranchDefault(),
101+
"github_branch_protection": resourceGithubBranchProtection(),
102+
"github_branch_protection_v3": resourceGithubBranchProtectionV3(),
103+
"github_dependabot_organization_secret": resourceGithubDependabotOrganizationSecret(),
104+
"github_dependabot_organization_secret_repositories": resourceGithubDependabotOrganizationSecretRepositories(),
105+
"github_dependabot_secret": resourceGithubDependabotSecret(),
106+
"github_emu_group_mapping": resourceGithubEMUGroupMapping(),
107+
"github_issue": resourceGithubIssue(),
108+
"github_issue_label": resourceGithubIssueLabel(),
109+
"github_membership": resourceGithubMembership(),
110+
"github_organization_block": resourceOrganizationBlock(),
111+
"github_organization_project": resourceGithubOrganizationProject(),
112+
"github_organization_webhook": resourceGithubOrganizationWebhook(),
113+
"github_project_card": resourceGithubProjectCard(),
114+
"github_project_column": resourceGithubProjectColumn(),
115+
"github_repository": resourceGithubRepository(),
116+
"github_repository_autolink_reference": resourceGithubRepositoryAutolinkReference(),
117+
"github_repository_collaborator": resourceGithubRepositoryCollaborator(),
118+
"github_repository_deploy_key": resourceGithubRepositoryDeployKey(),
119+
"github_repository_environment": resourceGithubRepositoryEnvironment(),
120+
"github_repository_file": resourceGithubRepositoryFile(),
121+
"github_repository_milestone": resourceGithubRepositoryMilestone(),
122+
"github_repository_project": resourceGithubRepositoryProject(),
123+
"github_repository_pull_request": resourceGithubRepositoryPullRequest(),
124+
"github_repository_webhook": resourceGithubRepositoryWebhook(),
125+
"github_team": resourceGithubTeam(),
126+
"github_team_members": resourceGithubTeamMembers(),
127+
"github_team_membership": resourceGithubTeamMembership(),
128+
"github_team_repository": resourceGithubTeamRepository(),
129+
"github_team_sync_group_mapping": resourceGithubTeamSyncGroupMapping(),
130+
"github_user_gpg_key": resourceGithubUserGpgKey(),
131+
"github_user_invitation_accepter": resourceGithubUserInvitationAccepter(),
132+
"github_user_ssh_key": resourceGithubUserSshKey(),
130133
},
131134

132135
DataSourcesMap: map[string]*schema.Resource{
133136
"github_actions_public_key": dataSourceGithubActionsPublicKey(),
134137
"github_branch": dataSourceGithubBranch(),
135138
"github_collaborators": dataSourceGithubCollaborators(),
139+
"github_dependabot_public_key": dataSourceGithubDependabotPublicKey(),
136140
"github_ip_ranges": dataSourceGithubIpRanges(),
137141
"github_membership": dataSourceGithubMembership(),
138142
"github_organization": dataSourceGithubOrganization(),

github/resource_github_actions_environment_secret_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"strings"
67
"testing"
@@ -15,9 +16,8 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) {
1516
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
1617

1718
t.Run("creates and updates secrets without error", func(t *testing.T) {
18-
19-
secretValue := "super_secret_value"
20-
updatedSecretValue := "updated_super_secret_value"
19+
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))
20+
updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value"))
2121

2222
config := fmt.Sprintf(`
2323
resource "github_repository" "test" {
@@ -113,8 +113,7 @@ func TestAccGithubActionsEnvironmentSecret(t *testing.T) {
113113
})
114114

115115
t.Run("deletes secrets without error", func(t *testing.T) {
116-
117-
secretValue := "super_secret_value"
116+
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))
118117

119118
config := fmt.Sprintf(`
120119
resource "github_repository" "test" {

github/resource_github_actions_organization_secret_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"strings"
67
"testing"
@@ -10,8 +11,8 @@ import (
1011

1112
func TestAccGithubActionsOrganizationSecret(t *testing.T) {
1213
t.Run("creates and updates secrets without error", func(t *testing.T) {
13-
secretValue := "super_secret_value"
14-
updatedSecretValue := "updated_super_secret_value"
14+
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))
15+
updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value"))
1516

1617
config := fmt.Sprintf(`
1718
resource "github_actions_organization_secret" "plaintext_secret" {

github/resource_github_actions_secret_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package github
22

33
import (
4+
"encoding/base64"
45
"fmt"
56
"strings"
67
"testing"
@@ -65,9 +66,8 @@ func TestAccGithubActionsSecret(t *testing.T) {
6566
})
6667

6768
t.Run("creates and updates secrets without error", func(t *testing.T) {
68-
69-
secretValue := "super_secret_value"
70-
updatedSecretValue := "updated_super_secret_value"
69+
secretValue := base64.StdEncoding.EncodeToString([]byte("super_secret_value"))
70+
updatedSecretValue := base64.StdEncoding.EncodeToString([]byte("updated_super_secret_value"))
7171

7272
config := fmt.Sprintf(`
7373
resource "github_repository" "test" {

0 commit comments

Comments
 (0)