Skip to content

Add payload hash func and default host var; Move fetching credentials func to sig lib #126

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

Merged
merged 4 commits into from
May 17, 2023

Conversation

shawnhankim
Copy link
Contributor

To enhance common AWS sig & credentials lib for services in AWS.

Description:

  • Add a payload hash function to support multiple services in AWS such as S3 and Lambda, and it can be used for not only GET but also POST method.
  • Add a NJS var of default host name to be used for Role Session Name at multiple services in AWS
  • Move reusable functions of fetching credentials into awscredentials.js
  • Replace name of env variables of AWS credentials and debug to support common AWS signature lib for multiple services in AWS

Misc.:

…edentials and debug

- Move reusable functions of fectching credentials into awscredentials.js
- Replace name of env variables of AWS credentials and debug to support common AWS signature lib for multiple services in AWS
- Add a NJS var of default host name to be used for Role Session Name at multiple services in AWS
- Add a payload hash function to support multiple services in AWS such as S3 and Lambda, and it can be used for not only GET but also POST method.
Copy link
Collaborator

@dekobon dekobon left a comment

Choose a reason for hiding this comment

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

Hi @shawnhankim thank you for the solid contribution. My apologies for the delay on the review. This all looks good and I had only a few comments/suggestions.

@@ -34,24 +68,26 @@ function sessionToken(r) {
}

/**
* Get the instance profile credentials needed to authenticated against S3 from
* Get the instance profile credentials needed to authenticate against S3 from
* a backend cache. If the credentials cannot be found, then return undefined.
* @param r {Request} HTTP request object (not used, but required for NGINX configuration)
* @returns {undefined|{accessKeyId: (string), secretAccessKey: (string), sessionToken: (string|null), expiration: (string|null)}} AWS instance profile credentials or undefined
*/
function readCredentials(r) {
// TODO: Change the generic constants naming for multiple AWS services.
Copy link
Collaborator

Choose a reason for hiding this comment

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

This TODO comment can be removed because this PR does what is mentioned.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for finding this. Addressed!

@@ -34,7 +34,7 @@ required=("S3_BUCKET_NAME" "S3_SERVER" "S3_SERVER_PORT" "S3_SERVER_PROTO"
if [[ -v AWS_CONTAINER_CREDENTIALS_RELATIVE_URI ]]; then
echo "Running inside an ECS task, using container credentials"

Copy link
Collaborator

Choose a reason for hiding this comment

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

Add a section to this script that detects the presence of S3_ environment variables, emits warnings that they are deprecated, and assigns those environment variables to the appropriate AWS_ environment variables.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It is a great point. Thanks! Addressed. Let me know if you have any additional comments.

@@ -23,6 +24,7 @@ js_var $indexIsEmpty true;
js_set $s3auth s3gateway.s3auth;
js_set $awsSessionToken awscredentials.sessionToken;
js_set $s3uri s3gateway.s3uri;
js_var $defaultHostName 'nginx-s3-gateway';
Copy link
Collaborator

Choose a reason for hiding this comment

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

What's the purpose of setting this variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is the purpose of option 2 in this link: #126 (comment)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have removed this variable, and you could find my answer in the #126 (comment).

*/
async function _fetchWebIdentityCredentials(r) {
const arn = process.env['AWS_ROLE_ARN'];
const name = process.env['HOSTNAME'] || 'nginx-s3-gateway';
Copy link
Collaborator

Choose a reason for hiding this comment

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

How is the hardcoded value nginx-s3-gateway being used? It would be useful to have a comment about it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was wondering what would be your suggestion.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • I have removed this hardcoded value in this NJS code, because this NJS can be used for not only S3 but also other services such as Lambda.
  • I have set this default value in the https://github.com/shawnhankim/nginx-s3-gateway/blob/awssig-env/common/docker-entrypoint.d/00-check-for-required-env.sh#L58 as the following example instead:
     elif [[ -v AWS_WEB_IDENTITY_TOKEN_FILE ]]; then
       echo "Running inside EKS with IAM roles for service accounts"
       if [[ ! -v HOSTNAME ]]; then
         # This environment value is used for Role Session Name. The default value is
         # set as a nginx-s3-gateway unless the value is defined.
         HOSTNAME="nginx-s3-gateway"
       fi
  • Let me know if you have other suggestion, or if you agree with this approach.

Copy link
Collaborator

@dekobon dekobon May 16, 2023

Choose a reason for hiding this comment

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

This approach is I believe is the best we can do. I was considering modifying the startup scripts such that if the environment variable HOSTNAME is not set that script would run hostname and then set HOSTNAME to the value returned.

That said, I'm a bit confused about the above comment that indicates that HOSTNAME maps to the "Role Session Name" because both of those concepts do not seem the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

  • Thanks for your opinion and original intent.
  • Yeah, I was also confused about the comment and variable. So I have replaced HOSTNAME with AWS_ROLE_SESSION_NAME and added the depreciation message of the variable of HOSTNAME.

- Set nginx-s3-gateway as a default value when HOSTNAME isn't set if AWS_WEB_IDENTITY_TOKEN_FILE is defined
- Change depreciation message and terminate the bootstrap when credentials and debug variables are set with the prefix of 'S3_'

fix: comment
@dekobon dekobon merged commit 449612b into nginx:master May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants