Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manage cors headers in terraform #4115

Merged
merged 30 commits into from
Jul 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
be474a9
Initial cors script test
asteel-gsa Jul 24, 2024
2723469
add depends_on
asteel-gsa Jul 24, 2024
8a7cf7f
Trying out the cors json via jsonencode
asteel-gsa Jul 24, 2024
d8ba02a
add var
asteel-gsa Jul 24, 2024
0be1723
testing
asteel-gsa Jul 24, 2024
1f5b6b3
give interpreter and working_dir
asteel-gsa Jul 24, 2024
5cbd60c
add cf target command
asteel-gsa Jul 25, 2024
e82377d
add aws
asteel-gsa Jul 25, 2024
0348d44
trim space
asteel-gsa Jul 25, 2024
6a92529
give proper path for aws
asteel-gsa Jul 25, 2024
6b23296
force delete key
asteel-gsa Jul 25, 2024
df6cef2
set the actual domain
asteel-gsa Jul 25, 2024
1f16167
trying decoded json
asteel-gsa Jul 25, 2024
7d232a5
more testing
asteel-gsa Jul 25, 2024
7695d3a
trying map
asteel-gsa Jul 25, 2024
2399040
Testing decoded_json local
asteel-gsa Jul 25, 2024
b59bd27
Testing moving the string json into module
asteel-gsa Jul 25, 2024
3b3b712
typo fix
asteel-gsa Jul 25, 2024
c431a0f
Undo the decode
asteel-gsa Jul 25, 2024
26bfdfa
Make executable
asteel-gsa Jul 25, 2024
e96a734
Test with json file
asteel-gsa Jul 25, 2024
a01e12d
change script_path
asteel-gsa Jul 25, 2024
090fe92
add trigger
asteel-gsa Jul 25, 2024
d548668
Fix the jq
asteel-gsa Jul 25, 2024
ca31829
Add env specific CORS files
asteel-gsa Jul 25, 2024
40f3369
Add info statements for logs
asteel-gsa Jul 25, 2024
95fbed4
Add a small sleep for CF API
asteel-gsa Jul 25, 2024
a74368b
Quiet unzip output
asteel-gsa Jul 25, 2024
ecf351c
rename resource
asteel-gsa Jul 25, 2024
4b99623
Add an informational comment on why the trigger={} was added
asteel-gsa Jul 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions terraform/shared/modules/cors/cors-script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

curl -L "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip -q awscliv2.zip && rm awscliv2.zip
./aws/install -i ~/usr -b ~/bin
/github/home/bin/aws --version

cf t -o "$1" -s "$2"
SERVICE_INSTANCE_NAME=fac-public-s3;
KEY_NAME=fac-public-s3-key;
cf create-service-key "${SERVICE_INSTANCE_NAME}" "${KEY_NAME}";
echo "Sleeping for CF API"
sleep 10
S3_CREDENTIALS=$(cf service-key "${SERVICE_INSTANCE_NAME}" "${KEY_NAME}" | tail -n +2);
export AWS_ACCESS_KEY_ID="$(echo "$S3_CREDENTIALS" | jq -r .credentials.access_key_id)";
export AWS_SECRET_ACCESS_KEY="$(echo "$S3_CREDENTIALS" | jq -r .credentials.secret_access_key)";
export BUCKET_NAME="$(echo "$S3_CREDENTIALS" | jq -r .credentials.bucket)";
export AWS_DEFAULT_REGION="$(echo "$S3_CREDENTIALS" | jq -r .credentials.region)";
echo "Bucket: $BUCKET_NAME";
echo "INFO: Putting CORS config in bucket..."
/github/home/bin/aws s3api put-bucket-cors --bucket "$BUCKET_NAME" --cors-configuration file://"$3";
echo "INFO: aws s3api get-bucket-cors output..."
/github/home/bin/aws s3api get-bucket-cors --bucket "$BUCKET_NAME";
cf delete-service-key -f "${SERVICE_INSTANCE_NAME}" "${KEY_NAME}";
17 changes: 17 additions & 0 deletions terraform/shared/modules/cors/cors.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
locals {
script_path = "${var.cf_space_name}-cors.json"
}
resource "null_resource" "cors_header" {
provisioner "local-exec" {
working_dir = path.module
interpreter = ["/bin/bash", "-c"]
command = "./cors-script.sh ${var.cf_org_name} ${var.cf_space_name} ${local.script_path}"
}
# https://github.com/hashicorp/terraform/issues/8266#issuecomment-454377049
# A clever way to get this to run every time, otherwise we would be relying on
# an md5 hash, which, once this goes into the system, will rarely (if ever)
# be updated
triggers = {
always_run = "${timestamp()}"
}
}
19 changes: 19 additions & 0 deletions terraform/shared/modules/cors/dev-cors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"CORSRules": [
{
"AllowedHeaders": [
"Authorization"
],
"AllowedMethods": [
"HEAD",
"GET"
],
"AllowedOrigins": [
"https://fac-dev.app.cloud.gov"
],
"ExposeHeaders": [
"ETag"
]
}
]
}
19 changes: 19 additions & 0 deletions terraform/shared/modules/cors/preview-cors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"CORSRules": [
{
"AllowedHeaders": [
"Authorization"
],
"AllowedMethods": [
"HEAD",
"GET"
],
"AllowedOrigins": [
"https://fac-preview.app.cloud.gov"
],
"ExposeHeaders": [
"ETag"
]
}
]
}
19 changes: 19 additions & 0 deletions terraform/shared/modules/cors/production-cors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"CORSRules": [
{
"AllowedHeaders": [
"Authorization"
],
"AllowedMethods": [
"HEAD",
"GET"
],
"AllowedOrigins": [
"https://app.cloud.gov"
],
"ExposeHeaders": [
"ETag"
]
}
]
}
19 changes: 19 additions & 0 deletions terraform/shared/modules/cors/staging-cors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"CORSRules": [
{
"AllowedHeaders": [
"Authorization"
],
"AllowedMethods": [
"HEAD",
"GET"
],
"AllowedOrigins": [
"https://fac-staging.app.cloud.gov"
],
"ExposeHeaders": [
"ETag"
]
}
]
}
12 changes: 12 additions & 0 deletions terraform/shared/modules/cors/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
variable "cf_org_name" {
type = string
description = "name of the organization to configure"
default = "gsa-tts-oros-fac"
}

variable "cf_space_name" {
type = string
description = "name of the space to configure"
# No default... The calling module knows which env is for which space and we
# shouldn't assume it!
}
6 changes: 6 additions & 0 deletions terraform/shared/modules/env/cors.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module "cors" {
source = "../cors"
cf_org_name = var.cf_org_name
cf_space_name = var.cf_space_name
depends_on = [module.s3-public]
}
2 changes: 1 addition & 1 deletion terraform/shared/modules/env/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ variable "new_relic_account_id" {
variable "new_relic_api_key" {
type = string
description = "New Relic API key"
}
}
Loading