Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for AWS_PROFILE and --json and --xml options #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Set AWS credentials and region using standard AWS CLI environment variables:
- `AWS_SESSION_TOKEN` - temporary token received from STS or from EC2 metadata
- `AWS_DEFAULT_REGION` - AWS default region, in case if region is not provided
in URL or as command line argument `--region`.

- `AWS_PROFILE` - AWS_PROFILE, will read the above and default format from ~/.aws/credentials
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: wrap ~/.aws/credentials as ~/.aws/credentials

nit: extraneous AWS_PROFILE

You can read more about AWS CLI environment variables here:
<https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html>

Expand Down Expand Up @@ -129,8 +129,9 @@ aws-curl --request POST \
NOTE: Region can't be detected from URL, so it should be explicitly provided as
argument or as `AWS_DEFAULT_REGION` env variable.

NOTE: This API has xml response format by default, pass
`Accept: application/json` header to change response format.
NOTE: This API has xml response format by default, pass `Accept:
application/json` header or give "--json" argument to change response
format.

### Example 3: S3

Expand Down Expand Up @@ -234,13 +235,15 @@ 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)
- `--json` - force output in json format
- `--xml` - force output in xml format

### Response format

APIs for different services have different default response format. Sometimes it
is json, sometimes xml. For most APIs you could enforce json output format by
adding header `Accept: application/json` and xml output format by adding header
`Accept: application/xml`.
`Accept: application/xml` or use the --xml or --json arguments to specify this.

## Automatically computed headers

Expand Down
26 changes: 25 additions & 1 deletion aws-curl
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,14 @@ while [ "$#" != 0 ]; do
REQUEST_METHOD="$1"
shift
;;
--json )
shift
OUTPUT_FORMAT="application/json"
;;
--xml )
shift
OUTPUT_FORMAT="application/xml"
;;
-H | --header )
shift
REQUEST_HEADERS=$(printf "%s\n%s" "$REQUEST_HEADERS" "$1")
Expand Down Expand Up @@ -499,6 +507,22 @@ fi
if [ "$EC2_CREDS" = 1 ]; then
ec2_import_creds
fi
get_cred_value() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to name it as ini_get_value, please assign named local variables for $1 and $2, format it similar to other functions in this file and move up to function.

I guess credentials file can have zero or more spaces around = sign and this code works just for one specific scenario. Suggest to adjust syntax to tolerate extraneous spaces around equal sign, also, tolerate if there are no any spaces.

echo "$1" | grep "$2 =" | cut -d ' ' -f 3-
}

if [ -n "$AWS_PROFILE" ] && [ -z "$AWS_ACCESS_KEY_ID" ]; then
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest to move body of this condition into function profile_import_creds that will have profile name as parameter

# this can work with AWS SSO based connections
block=$(sed -n '/\['$AWS_PROFILE'/,/^$/p' ~/.aws/credentials)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mac needs gsed, there is SED_CMD env variable that is populated on mac with gsed that you can use instead of referencing to sed.

There is missing closing \].

Selecting lines from section start to first empty line likely is not like aws cli parses this file. Likely section end condition is one of 1) start of new section 2) end of file. This regexp need a fix.

AWS_ACCESS_KEY_ID=$(get_cred_value "$block" aws_access_key_id)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code doesn't handle situation when profile doesn't have values for parameters. It blindly overwrites context with what it is profile, but if profile has no values and values were passed in script in alternative way, then these values will be zeroed.

AWS_SECRET_ACCESS_KEY=$(get_cred_value "$block" aws_secret_access_key)
AWS_SESSION_TOKEN=$(get_cred_value "$block" aws_session_token)
AWS_DEFAULT_REGION=$(get_cred_value "$block" region)
output=$(get_cred_value "$block" output)
if [ -n "$output" ]; then
OUTPUT_FORMAT=application/$output
fi
fi

# check mandatory environment variables
if [ -z "$AWS_ACCESS_KEY_ID" ] || [ -z "$AWS_SECRET_ACCESS_KEY" ]; then
Expand Down Expand Up @@ -617,6 +641,6 @@ echo "$CURL_ARGS" \
| xargs -0 curl --request "$REQUEST_METHOD" \
--header "$AUTHORIZATION_HEADER" \
--header "User-Agent:" \
--header "Accept:" \
--header "Accept: $OUTPUT_FORMAT" \
--header "Content-Type:" \
--data-binary "$REQUEST_PAYLOAD"