forked from mdahwireng/GitHub_Analyzer_API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenv_setup.sh
executable file
·96 lines (82 loc) · 2.3 KB
/
env_setup.sh
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
pexist=
if [ -f ~/.aws/config ]; then
pexist=$(grep tenac ~/.aws/config)
fi
function get_aws_profile() {
region=${1:-"us-east-1"}
if [ ! -z ${pexist} ]; then
prof="--profile kifiya --region $region"
if [ ! -z $profile_name ]; then prof="--profile $profile_name --region $region"; fi
else
prof="--region $region"
fi
echo $prof
}
function get_ssm_secret() {
#echo "reading key=$1 from aws secret manager"
res=$(aws secretsmanager get-secret-value \
--secret-id $1 \
--query SecretString \
--output text $prof || echo "")
echo $res
}
function gen_ssm_secret() {
#echo "generating random password from aws secret manager"
#--require-each-included-type \
res=$(aws secretsmanager get-random-password \
--exclude-punctuation \
--password-length ${1:-20} $prof | jq -r '.RandomPassword')
echo $res
}
function save_ssm_secret() {
echo "saving key=$2, value=$1 to aws secret manager"
res=$(aws secretsmanager create-secret \
--name $2 \
--secret-string $1 $prof)
}
function get_api_key(){
# app_key salt
e=${1:-"dev"}
key=${2:-"API_KEY"}
ssmappkey="${e}/csengine-api-key"
appkey=$(get_ssm_secret $ssmappkey)
if [ -z $appkey ]; then
echo "Generating Random APP_KEY and saving it in AWS secret manager"
appkey=$(gen_ssm_secret 20)
string="{\"$key\":\"$appkey\"}"
save_ssm_secret $string $ssmappkey
fi
echo ""
echo "{\"$key\":\"$appkey\"}"
echo ""
}
curdir=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
efs_path="/mnt/efs/autograde"
#determine rootdir for env and cred files
if [ ! -d ${efs_path} ]; then
rootdir=$(dirname $curdir)
else
rootdir=${efs_path}
fi
envdir=$rootdir/.env
mkdir -p $envdir
#fastapi vars
envfile="$envdir/.envdev"
if [ ! -f $envfile ]; then
keyname='tenx/env/dev'
region="us-east-1"
prof=$(get_aws_profile $region)
echo "using prof=$prof .."
echo "reading $keyname from AWS SM .."
res=$(get_ssm_secret $keyname)
echo "#" > $envfile
for x in $res; do
echo "$x" >> $envfile
done
echo "secret saved to ${envfile}"
else
echo "using existing ${envfile}"
fi
echo "===============cat ${envfile}============"
cat $envfile
echo "========================================="