Skip to content

Commit ed78668

Browse files
committed
initial commit
0 parents  commit ed78668

File tree

6 files changed

+258
-0
lines changed

6 files changed

+258
-0
lines changed

.gitignore

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
11+
# Exclude all .tfvars files, which are likely to contain sentitive data, such as
12+
# password, private keys, and other secrets. These should not be part of version
13+
# control as they are data points which are potentially sensitive and subject
14+
# to change depending on the environment.
15+
#
16+
*.tfvars
17+
18+
# Ignore override files as they are usually used to override resources locally and so
19+
# are not checked in
20+
override.tf
21+
override.tf.json
22+
*_override.tf
23+
*_override.tf.json
24+
25+
# Include override files you do wish to add to version control using negated pattern
26+
#
27+
# !example_override.tf
28+
29+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30+
# example: *tfplan*
31+
32+
# Ignore CLI configuration files
33+
.terraformrc
34+
terraform.rc

tf/provider.tf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
provider "google" {
2+
project = var.project_id
3+
request_timeout = "60s"
4+
region = "us-central1"
5+
}

tf/setup.tf

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Resources needed for all tests
2+
3+
resource "google_project_service" "secret_manager" {
4+
service = "secretmanager.googleapis.com"
5+
6+
timeouts {
7+
create = "30m"
8+
update = "40m"
9+
}
10+
11+
disable_dependent_services = true
12+
}
13+
14+
resource "google_service_account" "cloud_function_runner" {
15+
account_id = "cloud-function-service"
16+
display_name = "Testing Cloud Function Secrets integration"
17+
}
18+
19+
resource "google_storage_bucket" "cloud_functions" {
20+
name = "${var.project_id}-cloud-functions"
21+
location = "US"
22+
uniform_bucket_level_access = true
23+
}
24+
25+
26+
27+
28+
29+
30+

tf/test01.tf

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# Function Secrets Test 1: providing secret in secret_environment_variables block
2+
3+
resource "google_secret_manager_secret" "test_secret_01" {
4+
secret_id = "test-secret_01"
5+
6+
replication {
7+
user_managed {
8+
replicas {
9+
location = "us-central1"
10+
}
11+
replicas {
12+
location = "us-east1"
13+
}
14+
}
15+
}
16+
depends_on = [
17+
google_project_service.secret_manager
18+
]
19+
}
20+
21+
resource "google_secret_manager_secret_version" "test_secret_version_01a" {
22+
secret = google_secret_manager_secret.test_secret_01.id
23+
secret_data = "This is my secret for test 1."
24+
25+
26+
}
27+
28+
29+
# resource "google_secret_manager_secret_version" "test_secret_version_01b" {
30+
# secret = google_secret_manager_secret.test_secret_01.id
31+
# secret_data = "This is another version of my secret for test 1."
32+
# }
33+
34+
35+
resource "google_secret_manager_secret_iam_member" "cloud_function_sa_01" {
36+
secret_id = google_secret_manager_secret.test_secret_01.id
37+
role = "roles/secretmanager.secretAccessor"
38+
member = "serviceAccount:${google_service_account.cloud_function_runner.email}"
39+
}
40+
41+
data "archive_file" "cloud_function_1_zip" {
42+
type = "zip"
43+
output_path = "/tmp/cloud_function_1.zip"
44+
source {
45+
content = <<-EOF
46+
exports.echoSecret = (req, res) => {
47+
let message = req.query.message || req.body.message || "Secret: "+process.env.MY_SECRET;
48+
res.status(200).send(message);
49+
};
50+
EOF
51+
filename = "index.js"
52+
}
53+
}
54+
55+
resource "google_storage_bucket_object" "cloud_function_1_zip" {
56+
name = "cloud-function-1.zip"
57+
bucket = google_storage_bucket.cloud_functions.id
58+
source = data.archive_file.cloud_function_1_zip.output_path
59+
}
60+
61+
62+
63+
resource "google_cloudfunctions_function" "secrets_test" {
64+
name = "secrets-test-01"
65+
runtime = "nodejs14"
66+
service_account_email = google_service_account.cloud_function_runner.email
67+
entry_point = "echoSecret"
68+
source_archive_bucket = google_storage_bucket.cloud_functions.id
69+
source_archive_object = google_storage_bucket_object.cloud_function_1_zip.name
70+
trigger_http = true
71+
secret_environment_variables {
72+
key = "MY_SECRET"
73+
secret = google_secret_manager_secret.test_secret_01.secret_id // description for arg says 'name of secret', terraform keeps this value as "secret_id"
74+
version = "latest" // This value is not made available by the secret_manager_secret_version resource
75+
}
76+
}
77+
78+
resource "google_cloudfunctions_function_iam_member" "invoker" {
79+
cloud_function = google_cloudfunctions_function.secrets_test.name
80+
role = "roles/cloudfunctions.invoker"
81+
member = "allUsers"
82+
}
83+
output "functions_secrets_test_1_URL" {
84+
value = google_cloudfunctions_function.secrets_test.https_trigger_url
85+
}

tf/test02.tf

+100
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Function Secrets Test 2: providing secret in secret_volumes block
2+
3+
resource "google_secret_manager_secret" "test_secret_02" {
4+
secret_id = "test-secret_02"
5+
6+
replication {
7+
user_managed {
8+
replicas {
9+
location = "us-central1"
10+
}
11+
replicas {
12+
location = "us-east1"
13+
}
14+
}
15+
}
16+
depends_on = [
17+
google_project_service.secret_manager
18+
]
19+
}
20+
21+
resource "google_secret_manager_secret_version" "test_secret_version_02a" {
22+
secret = google_secret_manager_secret.test_secret_02.id
23+
secret_data = "This is my secret for test 2."
24+
}
25+
26+
27+
# resource "google_secret_manager_secret_version" "test_secret_version_02b" {
28+
# secret = google_secret_manager_secret.test_secret_02.id
29+
# secret_data = "This is another version of my secret for test 2."
30+
# }
31+
32+
33+
resource "google_secret_manager_secret_iam_member" "cloud_function_sa_02" {
34+
secret_id = google_secret_manager_secret.test_secret_02.id
35+
role = "roles/secretmanager.secretAccessor"
36+
member = "serviceAccount:${google_service_account.cloud_function_runner.email}"
37+
}
38+
39+
data "archive_file" "cloud_function_2_zip" {
40+
type = "zip"
41+
output_path = "/tmp/cloud_function_2.zip"
42+
source {
43+
content = <<-EOF
44+
const fs = require('fs')
45+
46+
exports.echoSecret = (req, res) => {
47+
const path = '/etc/secrets/test-secret'
48+
fs.access(path, fs.F_OK, (err) => {
49+
if (err) {
50+
console.error(err)
51+
res.status(200).send(err)
52+
return
53+
}
54+
fs.readFile(path, 'utf8', function(err,data) {
55+
res.status(200).send("Secret: "+data)
56+
return
57+
});
58+
})
59+
};
60+
EOF
61+
filename = "index.js"
62+
}
63+
}
64+
65+
66+
resource "google_cloudfunctions_function" "secrets_test_02" {
67+
name = "secrets-test-02"
68+
runtime = "nodejs14"
69+
service_account_email = google_service_account.cloud_function_runner.email
70+
entry_point = "echoSecret"
71+
source_archive_bucket = google_storage_bucket.cloud_functions.id
72+
source_archive_object = google_storage_bucket_object.cloud_function_2_zip.name
73+
trigger_http = true
74+
75+
secret_volumes {
76+
secret = google_secret_manager_secret.test_secret_02.secret_id
77+
mount_path = "/etc/secrets"
78+
versions { // code suggests this can be left empty, but still required
79+
version = "latest"
80+
path = "/test-secret"
81+
}
82+
}
83+
}
84+
85+
resource "google_storage_bucket_object" "cloud_function_2_zip" {
86+
name = "cloud-function-2.zip"
87+
bucket = google_storage_bucket.cloud_functions.id
88+
source = data.archive_file.cloud_function_2_zip.output_path
89+
}
90+
91+
resource "google_cloudfunctions_function_iam_member" "invoker_02" {
92+
cloud_function = google_cloudfunctions_function.secrets_test_02.name
93+
role = "roles/cloudfunctions.invoker"
94+
member = "allUsers"
95+
}
96+
output "functions_secrets_test_2_URL" {
97+
value = google_cloudfunctions_function.secrets_test_02.https_trigger_url
98+
}
99+
100+

tf/variables.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "project_id" {
2+
type = string
3+
description = "The project ID to use for Google provider."
4+
}

0 commit comments

Comments
 (0)