Skip to content

Commit

Permalink
admin script added
Browse files Browse the repository at this point in the history
  • Loading branch information
zLukas committed Dec 19, 2023
1 parent e1c8b58 commit 3819ad9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ cython_debug/


#terrafrom
*.tfvars
*.tfvars*
.terraform*
*.tfstate*

Expand Down
69 changes: 69 additions & 0 deletions admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#! /bin/bash -e


TERRAFORM_DIR=terraform
VAR_FILE="vars2.tfvars.json"

function terraform_init() {

terraform init
KEY_ID=$(cat ~/.aws/credentials | awk 'BEGIN{FS=" "}/key_id/{print $3}')
SECRET_KEY=$(cat ~/.aws/credentials | awk 'BEGIN{FS=" "}/secret_access/{print $3}')
echo "{ \"access_key\": \"${KEY_ID}\", \"secret_key\": \"${SECRET_KEY}\", \"clients\": []}" > ${TERRAFORM_DIR}/${VAR_FILE}
cd ${TERRAFORM_DIR}
terraform apply --var-file=${VAR_FILE}
cd ..
}
function terrafrom_apply(){
cd ${TERRAFORM_DIR}
terraform apply ${VAR_FILE}
cd ..
}

function add_new_clients(){
for client in "$@"
do
cat ${TERRAFORM_DIR}/${VAR_FILE}| jq '.clients[.clients| length ] += "'$client'" ' > ${TERRAFORM_DIR}/tmp_vars.tfvars.json
mv ${TERRAFORM_DIR}/tmp_vars.tfvars.json ${TERRAFORM_DIR}/${VAR_FILE}
done
}

function remove_clients(){
for client in "$@"
do
cat ${TERRAFORM_DIR}/${VAR_FILE}| jq '.clients[.clients| length ] += "'$client'" ' > ${TERRAFORM_DIR}/tmp_vars.tfvars.json
mv ${TERRAFORM_DIR}/tmp_vars.tfvars.json ${TERRAFORM_DIR}/${VAR_FILE}
done
}



function list_clients(){
aws iam list-users --output json | jq '.Users[] | select(.Path | contains("/certClient/"))| .UserName'
}

function usage(){
echo "admin [flag] [values]:"
echo " -i : terraform init/apply"
echo " -d : terraform destroy"
echo " -n <client1, client2,...> : add new clients"
echo " -r <client1, client2,...> : remove clients"
echo " -l : list current clients"
}

while getopts ":inrl:" arg; do
case "${arg}" in
i)
terraform_init
;;
n)
shift 1
add_new_client "$@"
;;
*)
echo "unknow option: ${arg}"
echo "usage"
usage
;;
esac
done
3 changes: 1 addition & 2 deletions terraform/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ resource "aws_iam_access_key" "clents_acces_keys" {


resource "aws_iam_policy_attachment" "ClientsPolicy" {
for_each=aws_iam_user.client_users
name="clients-db-policy"
users=[each.value.name]
users=var.clients
policy_arn="arn:aws:iam::aws:policy/AmazonDynamoDBReadOnlyAccess"
}

0 comments on commit 3819ad9

Please sign in to comment.