-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(client-s3): compatibility for S3Express and httpsigning midware
- Loading branch information
Showing
9 changed files
with
209 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export { AwsSdkSigV4Signer, AWSSDKSigV4Signer } from "./AwsSdkSigV4Signer"; | ||
export { AwsSdkSigV4Signer, AWSSDKSigV4Signer, validateSigningProperties } from "./AwsSdkSigV4Signer"; | ||
export { AwsSdkSigV4ASigner } from "./AwsSdkSigV4ASigner"; | ||
export * from "./resolveAwsSdkSigV4Config"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
packages/middleware-sdk-s3/src/s3-express/functions/s3ExpressHttpSigningMiddleware.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { httpSigningMiddlewareOptions } from "@smithy/core"; | ||
import { HttpRequest, IHttpRequest } from "@smithy/protocol-http"; | ||
import { | ||
AuthScheme, | ||
AwsCredentialIdentity, | ||
ErrorHandler, | ||
FinalizeHandler, | ||
FinalizeHandlerArguments, | ||
FinalizeHandlerOutput, | ||
FinalizeRequestMiddleware, | ||
HandlerExecutionContext, | ||
Pluggable, | ||
RequestSigner, | ||
SelectedHttpAuthScheme, | ||
SMITHY_CONTEXT_KEY, | ||
SuccessHandler, | ||
} from "@smithy/types"; | ||
import { getSmithyContext } from "@smithy/util-middleware"; | ||
|
||
import { signS3Express } from "./signS3Express"; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface HttpSigningMiddlewareSmithyContext extends Record<string, unknown> { | ||
selectedHttpAuthScheme?: SelectedHttpAuthScheme; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
interface HttpSigningMiddlewareHandlerExecutionContext extends HandlerExecutionContext { | ||
[SMITHY_CONTEXT_KEY]?: HttpSigningMiddlewareSmithyContext; | ||
} | ||
|
||
const defaultErrorHandler: ErrorHandler = (signingProperties) => (error) => { | ||
throw error; | ||
}; | ||
|
||
const defaultSuccessHandler: SuccessHandler = ( | ||
httpResponse: unknown, | ||
signingProperties: Record<string, unknown> | ||
): void => {}; | ||
|
||
interface SigningProperties { | ||
signingRegion: string; | ||
signingDate: Date; | ||
signingService: string; | ||
} | ||
|
||
interface PreviouslyResolved { | ||
signer: (authScheme?: AuthScheme | undefined) => Promise< | ||
RequestSigner & { | ||
signWithCredentials( | ||
req: IHttpRequest, | ||
identity: AwsCredentialIdentity, | ||
opts?: Partial<SigningProperties> | ||
): Promise<IHttpRequest>; | ||
} | ||
>; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const s3ExpressHttpSigningMiddlewareOptions = httpSigningMiddlewareOptions; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const s3ExpressHttpSigningMiddleware = | ||
<Input extends object, Output extends object>(config: PreviouslyResolved): FinalizeRequestMiddleware<any, any> => | ||
(next: FinalizeHandler<any, any>, context: HttpSigningMiddlewareHandlerExecutionContext): FinalizeHandler<any, any> => | ||
async (args: FinalizeHandlerArguments<any>): Promise<FinalizeHandlerOutput<any>> => { | ||
if (!HttpRequest.isInstance(args.request)) { | ||
return next(args); | ||
} | ||
|
||
const smithyContext: HttpSigningMiddlewareSmithyContext = getSmithyContext(context); | ||
const scheme = smithyContext.selectedHttpAuthScheme; | ||
if (!scheme) { | ||
throw new Error(`No HttpAuthScheme was selected: unable to sign request`); | ||
} | ||
const { | ||
httpAuthOption: { signingProperties = {} }, | ||
identity, | ||
signer, | ||
} = scheme; | ||
|
||
let request: IHttpRequest; | ||
|
||
if (context.s3ExpressIdentity) { | ||
request = await signS3Express( | ||
context.s3ExpressIdentity, | ||
signingProperties as unknown as SigningProperties, | ||
args.request, | ||
await config.signer() | ||
); | ||
} else { | ||
request = await signer.sign(args.request, identity, signingProperties); | ||
} | ||
|
||
const output = await next({ | ||
...args, | ||
request, | ||
}).catch((signer.errorHandler || defaultErrorHandler)(signingProperties)); | ||
(signer.successHandler || defaultSuccessHandler)(output.response, signingProperties); | ||
return output; | ||
}; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
export const getS3ExpressHttpSigningPlugin = (config: { | ||
signer: (authScheme?: AuthScheme | undefined) => Promise<RequestSigner>; | ||
}): Pluggable<any, any> => ({ | ||
applyToStack: (clientStack) => { | ||
clientStack.addRelativeTo( | ||
s3ExpressHttpSigningMiddleware(config as PreviouslyResolved), | ||
httpSigningMiddlewareOptions | ||
); | ||
}, | ||
}); |
29 changes: 29 additions & 0 deletions
29
packages/middleware-sdk-s3/src/s3-express/functions/signS3Express.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { AwsCredentialIdentity, HttpRequest as IHttpRequest } from "@smithy/types"; | ||
|
||
import { S3ExpressIdentity } from "../interfaces/S3ExpressIdentity"; | ||
|
||
export const signS3Express = async ( | ||
s3ExpressIdentity: S3ExpressIdentity, | ||
signingOptions: { | ||
signingDate: Date; | ||
signingRegion: string; | ||
signingService: string; | ||
}, | ||
request: IHttpRequest, | ||
sigV4MultiRegionSigner: { | ||
signWithCredentials( | ||
req: IHttpRequest, | ||
identity: AwsCredentialIdentity, | ||
opts?: Partial<typeof signingOptions> | ||
): Promise<IHttpRequest>; | ||
} | ||
) => { | ||
// the signer is expected to be SignatureV4MultiRegion for S3. | ||
const signedRequest = await sigV4MultiRegionSigner.signWithCredentials(request, s3ExpressIdentity, {}); | ||
|
||
if (signedRequest.headers["X-Amz-Security-Token"] || signedRequest.headers["x-amz-security-token"]) { | ||
throw new Error("X-Amz-Security-Token must not be set for s3-express requests."); | ||
} | ||
|
||
return signedRequest; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters