Skip to content

Commit

Permalink
Add additional comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dekobon committed May 17, 2022
1 parent b3eed9a commit 4e123f3
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion common/etc/nginx/include/s3gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ function readCredentials() {
* Creates an AWS authentication signature based on the global settings and
* the passed request parameter.
*
* @param r HTTP request
* @param r {Request} HTTP request object
* @returns {string} AWS authentication signature
*/
function s3auth(r) {
Expand Down Expand Up @@ -219,6 +219,14 @@ function s3SecurityToken() {
return '';
}

/**
* Build the base file path for a S3 request URI. This function allows for
* path style S3 URIs to be created that do not use a subdomain to specify
* the bucket name.
*
* @param r {Request} HTTP request object (not used, but required for NGINX configuration)
* @returns {string} start of the file path for the S3 object URI
*/
function s3BaseUri(r) {
var bucket = process.env['S3_BUCKET_NAME'];
var basePath;
Expand Down Expand Up @@ -260,6 +268,15 @@ function s3uri(r) {
return path;
}

/**
* Create and encode the query parameters needed to query S3 for an object
* listing.
*
* @param uriPath request URI path
* @param method request HTTP method
* @returns {string} query parameters to use with S3 request
* @private
*/
function _s3DirQueryParams(uriPath, method) {
if (!_isDirectory(uriPath) || method !== 'GET') {
return '';
Expand Down Expand Up @@ -364,6 +381,13 @@ function filterListResponse(r, data, flags) {
}
}

/**
* Creates a string containing the headers that need to be signed as part of v4
* signature authentication.
*
* @param sessionToken {string|undefined} AWS session token if present
* @returns {string} semicolon delimited string of the headers needed for signing
*/
function signedHeaders(sessionToken) {
var headers = defaultSignedHeaders;
if (sessionToken) {
Expand Down Expand Up @@ -740,6 +764,23 @@ function _require_env_var(envVarName) {
*/
var maxValidityOffsetMs = 4.5 * 60 * 100;

/**
* Get the credentials needed to create AWS signatures in order to authenticate
* to S3. If the gateway is being provided credentials via a instance profile
* credential as provided over the metadata endpoint, this function will:
* 1. Try to read the credentials from cache
* 2. Determine if the credentials are stale
* 3. If the cached credentials are missing or stale, it gets new credentials
* from the metadata endpoint.
* 4. If new credentials were pulled, it writes the credentials back to the
* cache.
*
* If the gateway is not using instance profile credentials, then this function
* quickly exits.
*
* @param r {Request} HTTP request object
* @returns {Promise<void>}
*/
async function fetchCredentials(r) {
var current = readCredentials();
if (current) {
Expand Down Expand Up @@ -789,6 +830,14 @@ async function fetchCredentials(r) {
r.return(200);
}

/**
* Get the credentials needed to generate AWS signatures from the ECS
* (Elastic Container Service) metadata endpoint.
*
* @param credentialsUri {string} endpoint to get credentials from
* @returns {Promise<{accessKeyId: (string), secretAccessKey: (string), sessionToken: (string), expiration: (string)}>}
* @private
*/
async function _fetchEcsRoleCredentials(credentialsUri) {
var resp = await ngx.fetch(credentialsUri);
if (!resp.ok) {
Expand All @@ -804,6 +853,13 @@ async function _fetchEcsRoleCredentials(credentialsUri) {
};
}

/**
* Get the credentials needed to generate AWS signatures from the EC2
* metadata endpoint.
*
* @returns {Promise<{accessKeyId: (string), secretAccessKey: (string), sessionToken: (string), expiration: (string)}>}
* @private
*/
async function _fetchEC2RoleCredentials() {
var tokenResp = await ngx.fetch(ec2ImdsTokenEndpoint, {
headers: {
Expand Down

0 comments on commit 4e123f3

Please sign in to comment.