Skip to content

Commit eb96865

Browse files
committed
[NRL-1922] Use env for AWS_REGION in scripts. Fix other Sonar warnings
1 parent f198826 commit eb96865

5 files changed

Lines changed: 25 additions & 19 deletions

File tree

scripts/aws_session_assume.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#!/usr/bin/env python
2+
import os
3+
24
import boto3
35

46
_AWS_ACCOUNT_FOR_ENV = {
@@ -13,6 +15,8 @@
1315
"prod": "prod",
1416
}
1517

18+
AWS_REGION = os.getenv("AWS_REGION", "eu-west-2")
19+
1620

1721
def get_account_name(env: str):
1822
if env not in _AWS_ACCOUNT_FOR_ENV:
@@ -23,7 +27,7 @@ def get_account_name(env: str):
2327

2428
def get_account_id(env: str):
2529
account_name = get_account_name(env)
26-
secretsmanager = boto3.client("secretsmanager", region_name="eu-west-2")
30+
secretsmanager = boto3.client("secretsmanager", region_name=AWS_REGION)
2731
secret_id = f"nhsd-nrlf--mgmt--{account_name}-account-id"
2832
result = secretsmanager.get_secret_value(SecretId=secret_id)
2933
account_id = result["SecretString"]
@@ -34,7 +38,7 @@ def get_account_id(env: str):
3438
def get_boto_session(env: str) -> boto3.Session:
3539
account_id = get_account_id(env)
3640

37-
sts = boto3.client("sts", region_name="eu-west-2")
41+
sts = boto3.client("sts", region_name=AWS_REGION)
3842
result = sts.assume_role(
3943
RoleArn=f"arn:aws:iam::{account_id}:role/terraform",
4044
RoleSessionName="get-account-id",

scripts/bootstrap.sh

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,6 @@ function _bootstrap() {
122122
#----------------
123123
"destroy-non-mgmt")
124124
_check_non_mgmt || return 1
125-
# TODO: Reintroduce the admin check - but should be fine for all developers
126-
# if [[ "$(aws sts get-caller-identity)" != *dev* || "$(aws sts get-caller-identity)" != *NHSDAdminRole* ]]; then
127-
# echo "Please log in as dev with an Admin account" >&2
128-
# return 1
129-
# fi
130-
131125
local workspace
132126
workspace=$2
133127
# Fetch the resources using the AWS CLI command
@@ -148,7 +142,7 @@ function _bootstrap() {
148142
;;
149143
arn:aws:logs* )
150144
echo "Deleting... : $arn"
151-
new_var=$(echo "$arn" | awk -F':' '{print $NF}')
145+
new_var=$(echo "$arn" | awk -F':' '{print $NF}') # NOSONAR (S1192) NF is not a env var
152146
aws logs delete-log-group --log-group-name $new_var
153147
;;
154148
arn:aws:secretsmanager* )
@@ -164,13 +158,13 @@ function _bootstrap() {
164158
;;
165159
arn:aws:dynamodb* )
166160
echo "Deleting... : $arn"
167-
new_var=$(echo "$arn" | awk -F':' '{print $NF}')
168-
table=$(echo "$arn" | awk -F'/' '{print $NF}')
161+
new_var=$(echo "$arn" | awk -F':' '{print $NF}') # NOSONAR (S1192) NF is not a env var
162+
table=$(echo "$arn" | awk -F'/' '{print $NF}') # NOSONAR (S1192) NF is not a env var
169163
aws dynamodb delete-table --table-name $table
170164
;;
171165
arn:aws:s3* )
172166
echo "Deleting... : $arn"
173-
new_var=$(echo "$arn" | awk -F':' '{print $NF}')
167+
new_var=$(echo "$arn" | awk -F':' '{print $NF}') # NOSONAR (S1192) NF is not a env var
174168
local versioned_objects
175169
versioned_objects=$(aws s3api list-object-versions \
176170
--bucket "${new_var}" \
@@ -184,9 +178,9 @@ function _bootstrap() {
184178
;;
185179
arn:aws:ssm* )
186180
echo "Deleting... : $arn"
187-
new_var=$(echo "$arn" | awk -F':' '{print $NF}')
188-
suffix=$(echo "$arn" | awk -F'/' '{print $NF}')
189-
name=$(echo "$new_var" | awk -F'/' '{print $(NF-1)}')
181+
new_var=$(echo "$arn" | awk -F':' '{print $NF}') # NOSONAR (S1192) NF is not a env var
182+
suffix=$(echo "$arn" | awk -F'/' '{print $NF}') # NOSONAR (S1192) NF is not a env var
183+
name=$(echo "$new_var" | awk -F'/' '{print $(NF-1)}') # NOSONAR (S1192) NF is not a env var
190184
aws ssm delete-parameter --name $name/$suffix
191185
;;
192186
arn:aws:acm* )
@@ -195,8 +189,8 @@ function _bootstrap() {
195189
;;
196190
arn:aws:firehose* )
197191
echo "Deleting... : $arn"
198-
new_var=$(echo "$arn" | awk -F':' '{print $NF}')
199-
name=$(echo "$new_var" | awk -F'/' '{print $NF}')
192+
new_var=$(echo "$arn" | awk -F':' '{print $NF}') # NOSONAR (S1192) NF is not a env var
193+
name=$(echo "$new_var" | awk -F'/' '{print $NF}') # NOSONAR (S1192) NF is not a env var
200194
aws firehose delete-delivery-stream --delivery-stream-name $name
201195
;;
202196
* )

scripts/set_smoketest_permissions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
#!/usr/bin/env python
22
import json
3+
import os
34

45
import fire
56
from aws_session_assume import get_boto_session
67

8+
AWS_REGION = os.getenv("AWS_REGION", "eu-west-2")
9+
710

811
def main(secret_env_name: str = "dev", bucket_env_name: str = "dev", env: str = "dev"):
912
boto_session = get_boto_session(env)
1013

1114
print("Getting smoke test parameters from AWS....") # noqa
1215
smoke_test_params_name = f"nhsd-nrlf--{secret_env_name}--smoke-test-parameters"
13-
secretsmanager = boto_session.client("secretsmanager", region_name="eu-west-2")
16+
secretsmanager = boto_session.client("secretsmanager", region_name=AWS_REGION)
1417
smoke_test_params_value = secretsmanager.get_secret_value(
1518
SecretId=smoke_test_params_name
1619
)

scripts/truststore.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ function _truststore_help() {
3434
echo " restore-archived-ca <env> - restore an archived certificate authority"
3535
echo " restore-archived-cert <env> - restore an archived client certificate"
3636
echo
37+
return 0
3738
}
3839

3940
# read an input file and substitute all the ${} entries
@@ -400,6 +401,8 @@ function _truststore() {
400401
"restore-archived-cert") _restore_archived_cert $args ;;
401402
*) _truststore_help $args ;;
402403
esac
404+
405+
return 0
403406
}
404407

405408
_truststore $@

terraform/account-wide-infrastructure/modules/glue/LogSchemaGeneration/LogSchemaGeneration.ipynb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
")\n",
1313
"import json\n",
1414
"from datetime import datetime, timedelta\n",
15+
"import os\n",
1516
"from typing import List, Dict, Any"
1617
]
1718
},
@@ -22,7 +23,8 @@
2223
"outputs": [],
2324
"source": [
2425
"# Initialize CloudWatch client\n",
25-
"session = boto3.Session(profile_name=\"nhsd-nrlf-dev\")\n",
26+
"AWS_REGION = os.getenv(\"AWS_REGION\", \"eu-west-2\")\n",
27+
"session = boto3.Session(profile_name=\"nhsd-nrlf-dev\", region_name=AWS_REGION)\n",
2628
"client = session.client(\"logs\")"
2729
]
2830
},

0 commit comments

Comments
 (0)