-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathintegration_tofu_state_encryption_test.go
219 lines (185 loc) · 7.41 KB
/
integration_tofu_state_encryption_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
//go:build tofu
package test_test
import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"testing"
"github.com/gruntwork-io/terragrunt/test/helpers"
"github.com/gruntwork-io/terragrunt/util"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const (
testFixtureTofuStateEncryptionPBKDF2 = "fixtures/tofu-state-encryption/pbkdf2"
testFixtureTofuStateEncryptionGCPKMS = "fixtures/tofu-state-encryption/gcp-kms"
testFixtureTofuStateEncryptionAWSKMS = "fixtures/tofu-state-encryption/aws-kms"
testFixtureRenderJSONWithEncryption = "fixtures/render-json-with-encryption"
gcpKMSKeyID = "projects/terragrunt-test/locations/global/keyRings/terragrunt-test/cryptoKeys/terragrunt-test-key"
awsKMSKeyID = "bd372994-d969-464a-a261-6cc850c58a92"
stateFile = "terraform.tfstate"
awsKMSKeyRegion = "us-east-1"
)
var testFixtureRenderJSONWithEncryptionMainModulePath = filepath.Join(testFixtureRenderJSONWithEncryption, "main")
func TestTofuStateEncryptionPBKDF2(t *testing.T) {
t.Parallel()
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureTofuStateEncryptionPBKDF2)
workDir := util.JoinPath(tmpEnvPath, testFixtureTofuStateEncryptionPBKDF2)
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+workDir)
assert.True(t, helpers.FileIsInFolder(t, stateFile, workDir))
validateStateIsEncrypted(t, stateFile, workDir)
}
func TestTofuStateEncryptionGCPKMS(t *testing.T) {
t.Skip("Skipping test as the GCP KMS key is not available. You have to setup your own GCP KMS key to run this test.")
t.Parallel()
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureTofuStateEncryptionGCPKMS)
workDir := util.JoinPath(tmpEnvPath, testFixtureTofuStateEncryptionGCPKMS)
configPath := util.JoinPath(workDir, "terragrunt.hcl")
helpers.CopyAndFillMapPlaceholders(t, configPath, configPath, map[string]string{
"__FILL_IN_KMS_KEY_ID__": gcpKMSKeyID,
})
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+workDir)
assert.True(t, helpers.FileIsInFolder(t, stateFile, workDir))
validateStateIsEncrypted(t, stateFile, workDir)
}
func TestTofuStateEncryptionAWSKMS(t *testing.T) {
t.Parallel()
tmpEnvPath := helpers.CopyEnvironment(t, testFixtureTofuStateEncryptionAWSKMS)
workDir := util.JoinPath(tmpEnvPath, testFixtureTofuStateEncryptionAWSKMS)
configPath := util.JoinPath(workDir, "terragrunt.hcl")
helpers.CopyAndFillMapPlaceholders(t, configPath, configPath, map[string]string{
"__FILL_IN_KMS_KEY_ID__": awsKMSKeyID,
"__FILL_IN_AWS_REGION__": awsKMSKeyRegion,
})
helpers.RunTerragrunt(t, "terragrunt apply -auto-approve --terragrunt-non-interactive --terragrunt-working-dir "+workDir)
assert.True(t, helpers.FileIsInFolder(t, stateFile, workDir))
validateStateIsEncrypted(t, stateFile, workDir)
}
func TestTofuRenderJSONConfigWithEncryption(t *testing.T) {
t.Parallel()
tmpDir, err := os.MkdirTemp("", "terragrunt-render-json-*")
require.NoError(t, err)
jsonOut := filepath.Join(tmpDir, "terragrunt_rendered.json")
defer os.RemoveAll(tmpDir)
helpers.CleanupTerraformFolder(t, fixtureRenderJSONMainModulePath)
helpers.CleanupTerraformFolder(t, fixtureRenderJSONDepModulePath)
helpers.RunTerragrunt(t, "terragrunt run-all apply -auto-approve --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir "+testFixtureRenderJSONWithEncryption)
helpers.RunTerragrunt(t, fmt.Sprintf("terragrunt render-json --terragrunt-non-interactive --terragrunt-log-level trace --terragrunt-working-dir %s --terragrunt-json-out %s", testFixtureRenderJSONWithEncryptionMainModulePath, jsonOut))
jsonBytes, err := os.ReadFile(jsonOut)
require.NoError(t, err)
var rendered map[string]interface{}
require.NoError(t, json.Unmarshal(jsonBytes, &rendered))
// Make sure all terraform block is visible
terraformBlock, hasTerraform := rendered["terraform"]
if assert.True(t, hasTerraform) {
source, hasSource := terraformBlock.(map[string]interface{})["source"]
assert.True(t, hasSource)
assert.Equal(t, "./module", source)
}
// Make sure included remoteState is rendered out
remoteState, hasRemoteState := rendered["remote_state"]
if assert.True(t, hasRemoteState) {
assert.Equal(
t,
map[string]interface{}{
"backend": "local",
"generate": map[string]interface{}{
"path": "backend.tf",
"if_exists": "overwrite_terragrunt",
},
"config": map[string]interface{}{
"path": "foo.tfstate",
},
"encryption": map[string]interface{}{
"key_provider": "pbkdf2",
"passphrase": "correct-horse-battery-staple",
},
"disable_init": false,
"disable_dependency_optimization": false,
},
remoteState.(map[string]interface{}),
)
}
// Make sure dependency blocks are rendered out
dependencyBlocks, hasDependency := rendered["dependency"]
if assert.True(t, hasDependency) {
assert.Equal(
t,
map[string]interface{}{
"dep": map[string]interface{}{
"name": "dep",
"config_path": "../dep",
"outputs": nil,
"inputs": nil,
"mock_outputs": nil,
"enabled": nil,
"mock_outputs_allowed_terraform_commands": nil,
"mock_outputs_merge_strategy_with_state": nil,
"mock_outputs_merge_with_state": nil,
"skip": nil,
},
},
dependencyBlocks.(map[string]interface{}),
)
}
// Make sure included generate block is rendered out
generateBlocks, hasGenerate := rendered["generate"]
if assert.True(t, hasGenerate) {
assert.Equal(
t,
map[string]interface{}{
"provider": map[string]interface{}{
"path": "provider.tf",
"comment_prefix": "# ",
"disable_signature": false,
"disable": false,
"if_exists": "overwrite_terragrunt",
"if_disabled": "skip",
"contents": `provider "aws" {
region = "us-east-1"
}
`,
},
},
generateBlocks.(map[string]interface{}),
)
}
// Make sure all inputs are merged together
inputsBlock, hasInputs := rendered["inputs"]
if assert.True(t, hasInputs) {
assert.Equal(
t,
map[string]interface{}{
"env": "qa",
"name": "dep",
"type": "main",
"aws_region": "us-east-1",
},
inputsBlock.(map[string]interface{}),
)
}
}
// Check the statefile contains an encrypted_data key
// and that the encrypted_data is base64 encoded
func validateStateIsEncrypted(t *testing.T, fileName string, path string) {
t.Helper()
filePath := filepath.Join(path, fileName)
file, err := os.Open(filePath)
require.NoError(t, err)
defer file.Close()
byteValue, err := io.ReadAll(file)
require.NoError(t, err)
var result map[string]interface{}
err = json.Unmarshal(byteValue, &result)
require.NoError(t, err, "Error unmarshalling the state file '%s'", fileName)
encryptedData, exists := result["encrypted_data"]
assert.True(t, exists, "The key 'encrypted_data' should exist in the state '%s'", fileName)
// Check if the encrypted_data is base64 encoded (common for AES-256 encrypted data)
encryptedDataStr, ok := encryptedData.(string)
assert.True(t, ok, "The value of 'encrypted_data' should be a string")
_, err = base64.StdEncoding.DecodeString(encryptedDataStr)
assert.NoError(t, err, "The value of 'encrypted_data' should be base64 encoded, indicating AES-256 encryption")
}