aboutsummaryrefslogtreecommitdiff
path: root/terraform
blob: b5ed3f6089a9dd469e623c2bb4b3400d2da92187 (plain)
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
96
97
98
99
#!/bin/sh

set -eu
TOP=$(dirname $0)

update_terraform()
{
    export TFVERS=0.11.15
    if [ ! -x $TOP/.bin/terraform_${TFVERS} ]
    then
        (
        mkdir -p $TOP/.bin/
        cd $TOP/.bin
        wget -q https://releases.hashicorp.com/terraform/${TFVERS}/terraform_${TFVERS}_linux_amd64.zip
        unzip -o terraform_${TFVERS}_linux_amd64.zip
        mv terraform terraform_${TFVERS}
        chmod a+x terraform_${TFVERS}
        ln -sf terraform_${TFVERS} terraform
        rm terraform_${TFVERS}_linux_amd64.zip
        )
    fi
}

update_terraform

conf=$TOP/.auth.conf
if [ -r "$conf" ]; then
    . "$conf"
fi

auth_awsv2()
{
    if [ -z "${aws_profile:-}" ]; then
        echo "E: \$aws_profile must be set in environment or $conf"
        exit 1
    fi

    eval "$(aws2-wrap --profile $aws_profile --export || echo exit 1)"
}

auth_awsv1()
{
    if [ -z "${aws_profile:-}" ] || [ -z "${aws_mfa_serial:-}" ]; then
        echo "E: Both \$aws_profile and \$aws_mfa_serial must be set."
        echo "I: Either set them in the environment, or in $conf"
        exit 1
    fi

    # assume role for terraform
    . $(pwd)/.role.conf
    credentials_dir=$HOME/.cache/systems.linaro.org
    credentials="$credentials_dir/admin_credentials"
    mkdir -p "$credentials_dir"
    chmod 700 "$credentials_dir"

    if [ -e "$credentials" ]; then
        now=$(date +%s)
        credentials_created=$(stat --format=%Y "$credentials")
        if [ $((now - credentials_created)) -gt 3600 ]; then
            rm -f "$credentials"
        fi
    fi
    duration=3600
    if [ ! -e "$credentials" ] || [ ! -s "$credentials" ]; then
        read -p "MFA token: " token
        touch "$credentials"
        chmod 600 "$credentials"
        aws --profile "$aws_profile" \
            --region us-east-1 \
            sts assume-role \
            --role-arn "$role"  \
            --role-session-name "`whoami`" \
            --duration-seconds "$duration" \
            --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
            --serial-number "$aws_mfa_serial" \
            --token-code "$token" \
            --output text \
            >> "$credentials"
    fi

    export AWS_ACCESS_KEY_ID="$(awk '{print($1)}' "$credentials")"
    export AWS_SECRET_ACCESS_KEY="$(awk '{print($2)}' "$credentials")"
    export AWS_SESSION_TOKEN="$(awk '{print($3)}' "$credentials")"
}

if [ -r ~/.scwrc ]; then
    export SCALEWAY_ORGANIZATION=$(jq -r .organization ~/.scwrc)
    export SCALEWAY_TOKEN=$(jq -r .token ~/.scwrc)
    export SCW_TOKEN=${SCALEWAY_TOKEN}
fi

if [ -z ${AWS_ACCESS_KEY_ID+x}${AWS_SECRET_ACCESS_KEY+x}${AWS_SECRET_ACCESS_KEY+x} ]
then
    which aws2 >/dev/null 2>&1 && auth_awsv2 || auth_awsv1
fi


$TOP/.bin/terraform "$@"