Skip to content

Commit 414f994

Browse files
committed
Updating bucket naming logic
1 parent 16874e5 commit 414f994

File tree

3 files changed

+90
-79
lines changed

3 files changed

+90
-79
lines changed

operations/_scripts/deploy/deploy.sh

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ set -x
44
echo "In deploy.sh"
55
GITHUB_REPO_NAME=$(echo $GITHUB_REPOSITORY | sed 's/^.*\///')
66

7+
# Generate buckets identifiers and check them agains AWS Rules
8+
export TF_STATE_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh tf | xargs)"
9+
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/deploy/check_bucket_name.sh $TF_STATE_BUCKET
10+
export LB_LOGS_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh lb | xargs)"
11+
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/deploy/check_bucket_name.sh $LB_LOGS_BUCKET
12+
713
# Generate buckets identifiers
8-
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh
14+
export TF_STATE_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh tf | xargs)"
15+
export LB_LOGS_BUCKET="$(/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_buckets_identifiers.sh lb | xargs)"
916

1017
# Generate subdomain
1118
/bin/bash $GITHUB_ACTION_PATH/operations/_scripts/generate/generate_subdomain.sh
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
echo "In check_bucket_name.sh"
3+
4+
set -e
5+
6+
### S3 Buckets name must follow AWS rules. Info below.
7+
### https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucketnamingrules.html
8+
9+
function checkBucket() {
10+
# check length of bucket name
11+
if [[ ${#1} -lt 3 || ${#1} -gt 63 ]]; then
12+
echo "::error::Bucket name must be between 3 and 63 characters long."
13+
exit 1
14+
fi
15+
16+
# check that bucket name consists only of lowercase letters, numbers, dots (.), and hyphens (-)
17+
if [[ ! $1 =~ ^[a-z0-9.-]+$ ]]; then
18+
echo "::error::Bucket name can only consist of lowercase letters, numbers, dots (.), and hyphens (-)."
19+
exit 1
20+
fi
21+
22+
# check that bucket name begins and ends with a letter or number
23+
if [[ ! $1 =~ ^[a-zA-Z0-9] ]]; then
24+
echo "::error::Bucket name must begin with a letter or number."
25+
exit 1
26+
fi
27+
if [[ ! $1 =~ [a-zA-Z0-9]$ ]]; then
28+
echo "::error::Bucket name must end with a letter or number."
29+
exit 1
30+
fi
31+
32+
# check that bucket name does not contain two adjacent periods
33+
if [[ $1 =~ \.\. ]]; then
34+
echo "::error::Bucket name cannot contain two adjacent periods."
35+
exit 1
36+
fi
37+
38+
# check that bucket name is not formatted as an IP address
39+
if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
40+
echo "::error::Bucket name cannot be formatted as an IP address."
41+
exit 1
42+
fi
43+
44+
# check that bucket name does not start with the prefix xn--
45+
if [[ $1 =~ ^xn-- ]]; then
46+
echo "::error::Bucket name cannot start with the prefix xn--."
47+
exit 1
48+
fi
49+
50+
# check that bucket name does not end with the suffix -s3alias
51+
if [[ $1 =~ -s3alias$ ]]; then
52+
echo "::error::Bucket name cannot end with the suffix -s3alias."
53+
exit 1
54+
fi
55+
}
56+
57+
checkBucket $1

operations/_scripts/generate/generate_buckets_identifiers.sh

Lines changed: 25 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,83 +2,30 @@
22

33
set -e
44

5-
GITHUB_IDENTIFIER="$(echo $($GITHUB_ACTION_PATH/operations/_scripts/generate/generate_identifier.sh) | tr '[:upper:]' '[:lower:]')"
5+
GITHUB_IDENTIFIER="$(echo $($GITHUB_ACTION_PATH/operations/_scripts/generate/generate_identifier.sh) | tr '[:upper:]' '[:lower:]' | tr '_' '-' )"
66

7-
# Function: generate_identifiers
8-
# Description: Generate all identifiers needs to poulate the IaC (terraform and ansible). Identifiers are a required component as cloud resources often have strict usage conventions that need to be appied to dynamic user input.
9-
function generate_identifiers () {
10-
# Generate TF_STATE_BUCKET ID if empty
11-
if [ -z "${TF_STATE_BUCKET}" ]; then
12-
if [[ ${#GITHUB_IDENTIFIER} < 55 ]]; then
13-
TF_STATE_BUCKET="${GITHUB_IDENTIFIER}-tf-state"
14-
else
15-
TF_STATE_BUCKET="${GITHUB_IDENTIFIER}-tf"
16-
fi
17-
else
18-
export TF_STATE_BUCKET=${TF_STATE_BUCKET}
19-
fi
20-
# Generate LB_LOGS_BUCKET ID
21-
if [[ ${#GITHUB_IDENTIFIER} < 59 ]]; then
22-
export LB_LOGS_BUCKET="${GITHUB_IDENTIFIER}-logs"
23-
else
24-
export LB_LOGS_BUCKET="${GITHUB_IDENTIFIER}-lg"
25-
fi
26-
}
27-
# Function: check_bucket_names
28-
# Description: Runs a handful of RegEx to ensure strict usage requirements are followed.
29-
function generate_bucket_names() {
30-
31-
# check length of bucket name
32-
if [[ ${#1} -lt 3 || ${#1} -gt 63 ]]; then
33-
echo "Bucket name must be between 3 and 63 characters long."
34-
exit 1
35-
fi
36-
37-
# check that bucket name consists only of lowercase letters, numbers, dots (.), and hyphens (-)
38-
if [[ ! $1 =~ ^[a-z0-9.-]+$ ]]; then
39-
echo "Bucket name can only consist of lowercase letters, numbers, dots (.), and hyphens (-)."
40-
exit 1
41-
fi
42-
43-
# check that bucket name begins and ends with a letter or number
44-
if [[ ! $1 =~ ^[a-zA-Z0-9] ]]; then
45-
echo "Bucket name must begin with a letter or number."
46-
exit 1
47-
fi
48-
if [[ ! $1 =~ [a-zA-Z0-9]$ ]]; then
49-
echo "Bucket name must end with a letter or number."
50-
exit 1
51-
fi
52-
53-
# check that bucket name does not contain two adjacent periods
54-
if [[ $1 =~ \.\. ]]; then
55-
echo "Bucket name cannot contain two adjacent periods."
56-
exit 1
57-
fi
58-
59-
# check that bucket name is not formatted as an IP address
60-
if [[ $1 =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
61-
echo "Bucket name cannot be formatted as an IP address."
62-
exit 1
63-
fi
64-
65-
# check that bucket name does not start with the prefix xn--
66-
if [[ $1 =~ ^xn-- ]]; then
67-
echo "Bucket name cannot start with the prefix xn--."
68-
exit 1
69-
fi
70-
71-
# check that bucket name does not end with the suffix -s3alias
72-
if [[ $1 =~ -s3alias$ ]]; then
73-
echo "Bucket name cannot end with the suffix -s3alias."
74-
exit 1
75-
fi
76-
}
7+
case $1 in
8+
tf)
9+
# Generate TF_STATE_BUCKET ID if empty
10+
if [ -z "${TF_STATE_BUCKET}" ]; then
11+
# Add trailing id depending on name length - See AWS S3 bucket naming rules
12+
if [[ ${#GITHUB_IDENTIFIER} < 55 ]]; then
13+
TF_STATE_BUCKET="${GITHUB_IDENTIFIER}-tf-state"
14+
else
15+
TF_STATE_BUCKET="${GITHUB_IDENTIFIER}-tf"
16+
fi
17+
fi
18+
echo "$TF_STATE_BUCKET"
7719

78-
generate_identifiers
79-
80-
generate_bucket_names $TF_STATE_BUCKET
81-
generate_bucket_names $LB_LOGS_BUCKET
82-
83-
export TF_STATE_BUCKET=${TF_STATE_BUCKET}
84-
export LB_LOGS_BUCKET=${LB_LOGS_BUCKET}
20+
;;
21+
lb)
22+
# Generate LB_LOGS_BUCKET ID
23+
# Add trailing id depending on name length - See AWS S3 bucket naming rules
24+
if [[ ${#GITHUB_IDENTIFIER} < 59 ]]; then
25+
LB_LOGS_BUCKET="${GITHUB_IDENTIFIER}-logs"
26+
else
27+
LB_LOGS_BUCKET="${GITHUB_IDENTIFIER}-lg"
28+
fi
29+
echo "$LB_LOGS_BUCKET"
30+
;;
31+
esac

0 commit comments

Comments
 (0)