This repository was archived by the owner on Feb 2, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathaws-cli-session-manager
More file actions
executable file
·137 lines (118 loc) · 3.72 KB
/
aws-cli-session-manager
File metadata and controls
executable file
·137 lines (118 loc) · 3.72 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#! /usr/bin/env bash
declare engine image mountopt aws_cli_version force_tty read_write
engine="${ENGINE:-docker}" # for planned compatibility with podman
aws_cli_version="${AWS_CLI_VERSION:-2.23.7}"
image="${IMAGE:-ghcr.io/kineticcafe/aws-cli-session-manager:${aws_cli_version}}"
force_tty=false
read_write=false
mountopt=",readonly" # default to readonly config/credential mounting
declare -a args interactive mounts passopt
interactive=(--interactive)
passopt=(
--env PYTHONIOENCODING=utf-8
--env LANG=en_US.UTF-8
--env LC_ALL=en_US.UTF-8
--env LC_CTYPE=en_US.UTF-8
)
mounts=(
--mount "type=bind,src=$(pwd),dst=/aws"
)
while (($#)); do
case "$1" in
--non-interactive) interactive=() ;;
--force-tty) force_tty=true ;;
--read-write) read_write=true ;;
*) args+=("$1") ;;
esac
shift
done
set -- "${args[@]}"
if ! [[ -t 0 ]]; then
if [[ "${force_tty}" == "true" ]]; then
echo >&2 "Cannot --force-tty without a TTY."
exit 1
fi
interactive=()
elif [[ "${force_tty}" == "true" ]]; then
interactive=(--interactive --tty)
fi
if [[ "$(basename "$0")" == aws_completer ]]; then
[[ -n "${COMP_POINT}" ]] && passopt+=(--env COMP_POINT="${COMP_POINT}")
[[ -n "${COMP_LINE}" ]] && passopt+=(--env COMP_LINE="${COMP_LINE}")
[[ -n "${COMMAND_LINE}" ]] && passopt+=(--env COMMAND_LINE="${COMMAND_LINE}")
passopt+=(--entrypoint aws_completer)
elif [[ "$1" == sh ]] || [[ "$1" == shell ]]; then
if ! [[ -t 0 ]]; then
echo >&2 "Cannot run the shell without a TTY."
exit 1
fi
shift
passopt+=(--entrypoint=/bin/bash)
interactive=(--interactive --tty)
elif [[ "$1" == "configure" ]] || [[ "${read_write}" = "true" ]]; then
# Allow read/write of configuration when using `configure`
mountopt=
fi
if [[ -d "${HOME}"/.aws ]]; then
mounts+=(--mount "type=bind,src=${HOME}/.aws,dst=/root/.aws${mountopt}")
fi
if [[ -d "${HOME}"/.aws/cli/history ]]; then
mounts+=(--mount "type=bind,src=${HOME}/.aws/cli/history,dst=/root/.aws/cli/history")
fi
if [[ -n "${AWS_CONFIG_FILE}" ]]; then
if [[ "$1" == "configure" ]] && [[ ! -f "${AWS_CONFIG_FILE}" ]]; then
mkdir -p "$(dirname "${AWS_CONFIG_FILE}")"
touch "${AWS_CONFIG_FILE}"
fi
if [[ -f "${AWS_CONFIG_FILE}" ]]; then
mounts+=(--mount "type=bind,src=${AWS_CONFIG_FILE},dst=/root/.aws/config${mountopt}")
fi
fi
if [[ -n "${AWS_SHARED_CREDENTIALS_FILE}" ]]; then
if [[ "$1" == "configure" ]] && [[ ! -f "${AWS_SHARED_CREDENTIALS_FILE}" ]]; then
mkdir -p "$(dirname "${AWS_SHARED_CREDENTIALS_FILE}")"
touch "${AWS_SHARED_CREDENTIALS_FILE}"
fi
if [[ -f "${AWS_SHARED_CREDENTIALS_FILE}" ]]; then
mounts+=(--mount "type=bind,src=${AWS_SHARED_CREDENTIALS_FILE},dst=/root/.aws/credentials${mountopt}")
fi
fi
# - Need better support for AWS_CA_BUNDLE (also --ca-bundle / profile ca_bundle)
# - Need better support for AWS_DATA_PATH (`:` / `;` separated)
# - Need support for AWS_WEB_IDENTITY_TOKEN_FILE
for var in \
AWS_ACCESS_KEY_ID \
AWS_CA_BUNDLE \
AWS_CLI_AUTO_PROMPT \
AWS_CLI_FILE_ENCODING \
AWS_DEFAULT_OUTPUT \
AWS_DEFAULT_REGION \
AWS_EC2_METADATA_DISABLED \
AWS_IGNORE_CONFIGURED_ENDPOINT_URLS \
AWS_MAX_ATTEMPTS \
AWS_METADATA_SERVICE_NUM_ATTEMPTS \
AWS_METADATA_SERVICE_TIMEOUT \
AWS_PAGER \
AWS_PROFILE \
AWS_REGION \
AWS_RETRY_MODE \
AWS_ROLE_ARN \
AWS_ROLE_SESSION_NAME \
AWS_SECRET_ACCESS_KEY \
AWS_SESSION_TOKEN \
AWS_STS_REGIONAL_ENDPOINTS \
AWS_USE_FIPS_ENDPOINT \
AWS_WEB_IDENTITY_TOKEN_FILE; do
[[ -n "${!var}" ]] && passopt+=(--env "${var}=${!var}")
done
for var in $(compgen -v | grep AWS_ENDPOINT_URL); do
[[ -n "${!var}" ]] && passopt+=(--env "${var}=${!var}")
done
"${engine}" run \
"${interactive[@]}" \
--rm \
--network host \
"${mounts[@]}" \
"${passopt[@]}" \
"${image}" \
"$@"