Skip to content

Commit d77b328

Browse files
committed
use shared resources to avoid CANNOT_DISABLE_ENCRYPTION_AT_REST_DUE_TO_PRIVATE_ENDPOINTS
1 parent 72aca36 commit d77b328

File tree

3 files changed

+103
-24
lines changed

3 files changed

+103
-24
lines changed

internal/service/encryptionatrestprivateendpoint/resource_test.go

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,18 @@ func TestAccEncryptionAtRestPrivateEndpoint_Azure_basic(t *testing.T) {
3535

3636
func TestAccEncryptionAtRestPrivateEndpoint_createTimeoutWithDeleteOnCreate(t *testing.T) {
3737
var (
38-
projectID = os.Getenv("MONGODB_ATLAS_PROJECT_EAR_PE_AWS_ID")
3938
createTimeout = "1s"
4039
deleteOnCreateTimeout = true
41-
awsKms = admin.AWSKMSConfiguration{
42-
Enabled: conversion.Pointer(true),
43-
CustomerMasterKeyID: conversion.StringPtr(os.Getenv("AWS_CUSTOMER_MASTER_KEY_ID")),
44-
Region: conversion.StringPtr(conversion.AWSRegionToMongoDBRegion(os.Getenv("AWS_REGION"))),
45-
RoleId: conversion.StringPtr(os.Getenv("AWS_EAR_ROLE_ID")),
46-
RequirePrivateNetworking: conversion.Pointer(false),
47-
}
48-
awsKmsPrivateNetworking = admin.AWSKMSConfiguration{
49-
Enabled: conversion.Pointer(true),
50-
CustomerMasterKeyID: conversion.StringPtr(os.Getenv("AWS_CUSTOMER_MASTER_KEY_ID")),
51-
Region: conversion.StringPtr(conversion.AWSRegionToMongoDBRegion(os.Getenv("AWS_REGION"))),
52-
RoleId: conversion.StringPtr(os.Getenv("AWS_EAR_ROLE_ID")),
53-
RequirePrivateNetworking: conversion.Pointer(true),
54-
}
55-
region = conversion.AWSRegionToMongoDBRegion(os.Getenv("AWS_REGION"))
40+
region = conversion.AWSRegionToMongoDBRegion(os.Getenv("AWS_REGION"))
41+
// Create encryption at rest configuration outside of test configuration to avoid cleanup issues
42+
projectID = acc.EncryptionAtRestExecution(t)
5643
)
5744
resource.ParallelTest(t, resource.TestCase{
5845
PreCheck: func() { acc.PreCheckEncryptionAtRestEnvAWS(t) },
5946
ProtoV6ProviderFactories: acc.TestAccProviderV6Factories,
6047
Steps: []resource.TestStep{
6148
{
62-
Config: acc.ConfigAwsKms(projectID, &awsKms, false, true, false),
63-
Check: resource.ComposeAggregateTestCheckFunc(
64-
resource.TestCheckResourceAttr(earResourceName, "aws_kms_config.0.enabled", "true"),
65-
resource.TestCheckResourceAttr(earResourceName, "aws_kms_config.0.require_private_networking", "false"),
66-
),
67-
},
68-
{
69-
Config: configAWSBasicWithTimeout(projectID, &awsKmsPrivateNetworking, region, acc.TimeoutConfig(&createTimeout, nil, nil, true), &deleteOnCreateTimeout),
49+
Config: configEARPrivateEndpointWithTimeout(projectID, region, acc.TimeoutConfig(&createTimeout, nil, nil, true), &deleteOnCreateTimeout),
7050
ExpectError: regexp.MustCompile("will run cleanup because delete_on_create_timeout is true"),
7151
},
7252
},
@@ -388,6 +368,30 @@ func configAWSBasicWithTimeout(projectID string, awsKms *admin.AWSKMSConfigurati
388368
return config
389369
}
390370

371+
func configEARPrivateEndpointWithTimeout(projectID, region, timeoutConfig string, deleteOnCreateTimeout *bool) string {
372+
deleteOnCreateTimeoutConfig := ""
373+
if deleteOnCreateTimeout != nil {
374+
deleteOnCreateTimeoutConfig = fmt.Sprintf(`
375+
delete_on_create_timeout = %[1]t
376+
`, *deleteOnCreateTimeout)
377+
}
378+
379+
config := fmt.Sprintf(`
380+
resource "mongodbatlas_encryption_at_rest_private_endpoint" "test" {
381+
project_id = %[1]q
382+
cloud_provider = "AWS"
383+
region_name = %[2]q
384+
%[3]s
385+
%[4]s
386+
}
387+
388+
%[5]s
389+
390+
`, projectID, region, deleteOnCreateTimeoutConfig, timeoutConfig, configDS())
391+
392+
return config
393+
}
394+
391395
func configDS() string {
392396
return `
393397
data "mongodbatlas_encryption_at_rest_private_endpoint" "test" {

internal/testutil/acc/encryption_at_rest.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ package acc
33
import (
44
"context"
55
"fmt"
6+
"os"
67
"strconv"
8+
"testing"
79

810
"go.mongodb.org/atlas-sdk/v20250312005/admin"
911

1012
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1113
"github.com/hashicorp/terraform-plugin-testing/terraform"
14+
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
15+
"github.com/stretchr/testify/require"
1216
)
1317

1418
func ConfigEARAzureKeyVault(projectID string, azure *admin.AzureKeyVault, useRequirePrivateNetworking, useDatasource bool) string {
@@ -153,3 +157,65 @@ func EARImportStateIDFunc(resourceName string) resource.ImportStateIdFunc {
153157
return rs.Primary.ID, nil
154158
}
155159
}
160+
161+
// EncryptionAtRestExecution creates an encryption at rest configuration for test execution.
162+
func EncryptionAtRestExecution(tb testing.TB) string {
163+
tb.Helper()
164+
SkipInUnitTest(tb)
165+
require.True(tb, sharedInfo.init, "SetupSharedResources must called from TestMain test package")
166+
167+
projectID := ProjectIDExecution(tb)
168+
169+
sharedInfo.mu.Lock()
170+
defer sharedInfo.mu.Unlock()
171+
172+
// lazy creation so it's only done if really needed
173+
if !sharedInfo.encryptionAtRestEnabled {
174+
tb.Logf("Creating execution encryption at rest configuration for project: %s\n", projectID)
175+
176+
// Create encryption at rest configuration using environment variables
177+
awsKms := &admin.AWSKMSConfiguration{
178+
Enabled: conversion.Pointer(true),
179+
CustomerMasterKeyID: conversion.StringPtr(os.Getenv("AWS_CUSTOMER_MASTER_KEY_ID")),
180+
Region: conversion.StringPtr(conversion.AWSRegionToMongoDBRegion(os.Getenv("AWS_REGION"))),
181+
RoleId: conversion.StringPtr(os.Getenv("AWS_EAR_ROLE_ID")),
182+
RequirePrivateNetworking: conversion.Pointer(true),
183+
}
184+
185+
createEncryptionAtRest(tb, projectID, awsKms)
186+
sharedInfo.encryptionAtRestEnabled = true
187+
}
188+
189+
return projectID
190+
}
191+
192+
func createEncryptionAtRest(tb testing.TB, projectID string, aws *admin.AWSKMSConfiguration) {
193+
tb.Helper()
194+
195+
encryptionAtRestReq := &admin.EncryptionAtRest{
196+
AwsKms: aws,
197+
}
198+
199+
_, _, err := ConnV2().EncryptionAtRestUsingCustomerKeyManagementApi.UpdateEncryptionAtRest(tb.Context(), projectID, encryptionAtRestReq).Execute()
200+
require.NoError(tb, err, "Failed to create encryption at rest configuration for project: %s", projectID)
201+
}
202+
203+
func deleteEncryptionAtRest(projectID string) {
204+
// Disable encryption at rest by setting all providers to disabled
205+
encryptionAtRestReq := &admin.EncryptionAtRest{
206+
AwsKms: &admin.AWSKMSConfiguration{
207+
Enabled: conversion.Pointer(false),
208+
},
209+
AzureKeyVault: &admin.AzureKeyVault{
210+
Enabled: conversion.Pointer(false),
211+
},
212+
GoogleCloudKms: &admin.GoogleCloudKMS{
213+
Enabled: conversion.Pointer(false),
214+
},
215+
}
216+
217+
_, _, err := ConnV2().EncryptionAtRestUsingCustomerKeyManagementApi.UpdateEncryptionAtRest(context.Background(), projectID, encryptionAtRestReq).Execute()
218+
if err != nil {
219+
fmt.Printf("Failed to delete encryption at rest for project %s: %s\n", projectID, err)
220+
}
221+
}

internal/testutil/acc/shared_resource.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,14 @@ func cleanupSharedResources() {
5252
fmt.Printf("Deleting execution private link endpoint: %s, project id: %s, provider: %s\n", sharedInfo.privateLinkEndpointID, projectID, sharedInfo.privateLinkProviderName)
5353
deletePrivateLinkEndpoint(projectID, sharedInfo.privateLinkProviderName, sharedInfo.privateLinkEndpointID)
5454
}
55+
if sharedInfo.encryptionAtRestEnabled {
56+
projectID := sharedInfo.projectID
57+
if projectID == "" {
58+
projectID = projectIDLocal()
59+
}
60+
fmt.Printf("Deleting execution encryption at rest: project id: %s\n", projectID)
61+
deleteEncryptionAtRest(projectID)
62+
}
5563
if sharedInfo.projectID != "" {
5664
fmt.Printf("Deleting execution project: %s, id: %s\n", sharedInfo.projectName, sharedInfo.projectID)
5765
deleteProject(sharedInfo.projectID)
@@ -217,6 +225,7 @@ var sharedInfo = struct {
217225
projects []projectInfo
218226
mu sync.Mutex
219227
muSleep sync.Mutex
228+
encryptionAtRestEnabled bool
220229
init bool
221230
}{
222231
projects: []projectInfo{},

0 commit comments

Comments
 (0)