Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ Wrapper recognizes these non-curl arguments:
- `--region` - AWS region name, if can't be automatically detected from host or
if not explicitly provided in `AWS_DEFAULT_REGION` environment variable
- `--ec2-creds` - use attached to EC2 credentials (instance role)
- `--ecs-creds` - use attached to ECS credentials (task role)

### Response format

Expand Down Expand Up @@ -307,10 +308,15 @@ including access key, secret key, session token and region.

Just import from the shell as `source ec2-import-creds`.

Or you can use `--ec2-creds` options of `aws-cli` to get the same effect, but
Or you can use `--ec2-creds` option of `aws-curl` to get the same effect, but
importing credentials once in beginning is faster than importing for every
`aws-curl` invocation.

## ECS attached role

When your service runs as ECS task you can import the attached credentials
using `source ecs-import-creds` or `--ecs-creds` option.

## Platforms

The script has been tested on bash in posix mode on macOS and Linux. It should
Expand Down
35 changes: 30 additions & 5 deletions aws-curl
100755 → 100644
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh
# shellcheck disable=SC2155,SC2001

VERSION="1.0.9"
VERSION="1.0.10"

DATE_CMD="date"
SED_CMD="sed"
Expand All @@ -21,7 +21,7 @@ fi
# safe url string
##
urlsafe() {
echo "$1" | sed \
echo "$1" | $SED_CMD \
-e 's!\x09!%09!g' \
-e 's!\x0A!%0A!g' \
-e 's!\x0B!%0B!g' \
Expand Down Expand Up @@ -336,15 +336,15 @@ sigv4_authorization_header() {
}

##
# Extracts key value from pretty-printed JSON-like structure.
# Extracts key value from JSON-like structure.
# Arguments:
# $1 payload
# $2 key name
# Returns:
# key value
##
get_key_value() {
echo "$1" | grep "$2" | cut -d ':' -f 2 | cut -d '"' -f 2
echo "$1" | $SED_CMD -n -e "s/.*\"$2\": *\"\([^\"]*\)\".*/\1/p"
}

##
Expand All @@ -355,7 +355,7 @@ get_key_value() {
# region without az suffix
##
strip_az_suffix() {
echo "$1" | sed -e 's![a-z]$!!'
echo "$1" | $SED_CMD -e 's![a-z]$!!'
}

##
Expand All @@ -379,6 +379,21 @@ ec2_import_creds() {
fi
}

##
# Imports credentials attached to ECS task
##
ecs_import_creds() {
curl_opts="--silent --connect-timeout 1 --fail"
ecs_creds_url="http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI"
credentials=$(curl $curl_opts "$ecs_creds_url")

if [ -n "$credentials" ]; then
AWS_ACCESS_KEY_ID=$(get_key_value "$credentials" "AccessKeyId")
AWS_SECRET_ACCESS_KEY=$(get_key_value "$credentials" "SecretAccessKey")
AWS_SESSION_TOKEN=$(get_key_value "$credentials" "Token")
fi
}

##
# Show help
##
Expand All @@ -405,6 +420,7 @@ CURL_VERBOSE=""
AWS_REGION=""
AWS_SERVICE=""
EC2_CREDS="0"
ECS_CREDS="0"

# read command line arguments
while [ "$#" != 0 ]; do
Expand Down Expand Up @@ -460,6 +476,10 @@ while [ "$#" != 0 ]; do
shift
EC2_CREDS="1"
;;
--ecs-creds )
shift
ECS_CREDS="1"
;;
--data-ascii | --data-raw | --data-urlencode )
echo "Option $1 is not supported at this time."
exit 1
Expand Down Expand Up @@ -500,6 +520,11 @@ if [ "$EC2_CREDS" = 1 ]; then
ec2_import_creds
fi

# import attached ecs credentials if enabled
if [ "$ECS_CREDS" = 1 ]; then
ecs_import_creds
fi

# check mandatory environment variables
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
echo "AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are not set."
Expand Down
2 changes: 1 addition & 1 deletion ec2-import-creds
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ credentials=$([ -n "$attached_role_name" ] && curl -m 1 -s "http://169.254.169.2
availability_zone="$(curl -m 1 -s "http://169.254.169.254/latest/meta-data/placement/availability-zone")"

get_key_value() {
echo "$1" | grep "$2" | cut -d ':' -f 2 | cut -d '"' -f 2
echo "$1" | sed -n -e "s/.*\"$2\": *\"\([^\"]*\)\".*/\1/p"
}

strip_az_suffix() {
Expand Down
14 changes: 14 additions & 0 deletions ecs-import-creds
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/sh
# shellcheck disable=SC2155

credentials=$(curl -m 1 -s "http://169.254.170.2$AWS_CONTAINER_CREDENTIALS_RELATIVE_URI")

get_key_value() {
echo "$1" | sed -n -e "s/.*\"$2\": *\"\([^\"]*\)\".*/\1/p"
}

if [ -n "$credentials" ]; then
export AWS_ACCESS_KEY_ID=$(get_key_value "$credentials" "AccessKeyId")
export AWS_SECRET_ACCESS_KEY=$(get_key_value "$credentials" "SecretAccessKey")
export AWS_SESSION_TOKEN=$(get_key_value "$credentials" "Token")
fi