Skip to content

Commit 0530b7e

Browse files
authored
[MAINT] Add regression test for #2614 (#3062)
* Separate Unit tests to own Test func Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Add test to ensure that `destroy_on_drift` and `ignore_changes` together prevents replacement of externally modified secret Signed-off-by: Timo Sand <timo.sand@f-secure.com> * Upgrade to `go-github` v81 Signed-off-by: Timo Sand <timo.sand@f-secure.com> --------- Signed-off-by: Timo Sand <timo.sand@f-secure.com>
1 parent 92478b7 commit 0530b7e

File tree

1 file changed

+100
-8
lines changed

1 file changed

+100
-8
lines changed

github/resource_github_actions_organization_secret_test.go

Lines changed: 100 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ import (
66
"strings"
77
"testing"
88

9+
"github.com/google/go-github/v81/github"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
911
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1114
)
1215

1316
func TestAccGithubActionsOrganizationSecret(t *testing.T) {
@@ -17,16 +20,16 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) {
1720

1821
config := fmt.Sprintf(`
1922
resource "github_actions_organization_secret" "plaintext_secret" {
20-
secret_name = "test_plaintext_secret"
21-
plaintext_value = "%s"
22-
visibility = "private"
23+
secret_name = "test_plaintext_secret"
24+
plaintext_value = "%s"
25+
visibility = "private"
2326
}
2427
2528
resource "github_actions_organization_secret" "encrypted_secret" {
26-
secret_name = "test_encrypted_secret"
27-
encrypted_value = "%s"
28-
visibility = "private"
29-
destroy_on_drift = false
29+
secret_name = "test_encrypted_secret"
30+
encrypted_value = "%s"
31+
visibility = "private"
32+
destroy_on_drift = false
3033
}
3134
`, secretValue, secretValue)
3235

@@ -143,8 +146,79 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) {
143146
},
144147
})
145148
})
149+
}
150+
151+
func TestAccGithubActionsOrganizationSecret_DestroyOnDrift(t *testing.T) {
152+
t.Run("destroyOnDrift false", func(t *testing.T) {
153+
destroyOnDrift := false
154+
t.Run("should ignore drift when ignore_changes lifecycle is configured", func(t *testing.T) {
155+
// Verify https://github.com/integrations/terraform-provider-github/issues/2614
156+
randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
157+
config := fmt.Sprintf(`
158+
resource "github_actions_organization_secret" "test_secret" {
159+
secret_name = "test_secret_%s"
160+
plaintext_value = "test_value"
161+
visibility = "private"
162+
163+
destroy_on_drift = %t
164+
lifecycle {
165+
ignore_changes = [plaintext_value]
166+
}
167+
}
168+
`, randomID, destroyOnDrift)
169+
170+
resource.Test(t, resource.TestCase{
171+
PreCheck: func() { skipUnlessHasOrgs(t) },
172+
Providers: testAccProviders,
173+
Steps: []resource.TestStep{
174+
{
175+
Config: config,
176+
},
177+
{
178+
Config: config,
179+
Check: resource.ComposeTestCheckFunc(
180+
func(s *terraform.State) error {
181+
rs, ok := s.RootModule().Resources["github_actions_organization_secret.test_secret"]
182+
if !ok {
183+
t.Errorf("not found: github_actions_organization_secret.test_secret")
184+
}
185+
// Now that the secret is created, update it to trigger a drift.
186+
client := testAccProvider.Meta().(*Owner).v3client
187+
owner := testAccProvider.Meta().(*Owner).name
188+
ctx := t.Context()
189+
190+
keyId, publicKey, err := getOrganizationPublicKeyDetails(owner, testAccProvider.Meta().(*Owner))
191+
if err != nil {
192+
t.Errorf("Failed to get organization public key details: %v", err)
193+
}
146194

147-
// Unit tests for drift detection behavior
195+
encryptedSecret, err := createEncryptedSecret(rs.Primary, "foo", keyId, publicKey)
196+
if err != nil {
197+
t.Errorf("Failed to create encrypted secret: %v", err)
198+
}
199+
_, err = client.Actions.CreateOrUpdateOrgSecret(ctx, owner, encryptedSecret)
200+
if err != nil {
201+
t.Errorf("Failed to create or update organization secret: %v", err)
202+
}
203+
return err
204+
},
205+
),
206+
},
207+
{
208+
Config: config,
209+
PlanOnly: true,
210+
ExpectNonEmptyPlan: false,
211+
},
212+
},
213+
})
214+
})
215+
})
216+
// t.Run("destroyOnDrift true", func(t *testing.T) {
217+
// destroyOnDrift := true
218+
// })
219+
}
220+
221+
func TestGithubActionsOrganizationSecret_DestroyOnDrift(t *testing.T) {
148222
t.Run("destroyOnDrift false clears sensitive values instead of recreating", func(t *testing.T) {
149223
originalTimestamp := "2023-01-01T00:00:00Z"
150224
newTimestamp := "2023-01-02T00:00:00Z"
@@ -248,3 +322,21 @@ func TestAccGithubActionsOrganizationSecret(t *testing.T) {
248322
}
249323
})
250324
}
325+
326+
func createEncryptedSecret(is *terraform.InstanceState, plaintextValue, keyId, publicKey string) (*github.EncryptedSecret, error) {
327+
secretName := is.Attributes["secret_name"]
328+
visibility := is.Attributes["visibility"]
329+
330+
encryptedBytes, err := encryptPlaintext(plaintextValue, publicKey)
331+
if err != nil {
332+
return nil, err
333+
}
334+
encryptedValue := base64.StdEncoding.EncodeToString(encryptedBytes)
335+
336+
return &github.EncryptedSecret{
337+
Name: secretName,
338+
KeyID: keyId,
339+
Visibility: visibility,
340+
EncryptedValue: encryptedValue,
341+
}, nil
342+
}

0 commit comments

Comments
 (0)