Skip to content

Commit

Permalink
feat: location constraint migration (#497)
Browse files Browse the repository at this point in the history
* feat: rename location-constraint

* feat: migrate locationConstraintMiddleware

* feat: apply LocationConstraint plugin
  • Loading branch information
Chase Coalwell authored and trivikr committed Jan 3, 2020
1 parent 5e1e3a7 commit ad7f978
Show file tree
Hide file tree
Showing 16 changed files with 116 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ public List<RuntimeClientPlugin> getClientPlugins() {
.withConventions(AwsDependency.SSEC_MIDDLEWARE.dependency, "Ssec", HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.operationPredicate((m, s, o) -> testContainsMember(m, o, SSEC_OPERATIONS))
.build(),
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.LOCATION_CONSTRAINT.dependency, "LocationConstraint",
HAS_MIDDLEWARE)
.servicePredicate((m, s) -> testServiceId(s, "S3"))
.operationPredicate((m, s, o) -> o.getId().getName().equals("CreateBucket"))
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public enum AwsDependency implements SymbolDependencyContainer {
VALIDATE_BUCKET_NAME(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-s3", "^0.1.0-preview.2"),
ADD_EXPECT_CONTINUE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-expect-continue", "^0.1.0-preview.5"),
ADD_GLACIER_API_VERSION(NORMAL_DEPENDENCY, "@aws-sdk/middleware-sdk-glacier", "^0.1.0-preview.7"),
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5");
SSEC_MIDDLEWARE(NORMAL_DEPENDENCY, "@aws-sdk/middleware-ssec", "^0.1.0-preview.5"),
LOCATION_CONSTRAINT(NORMAL_DEPENDENCY, "@aws-sdk/middleware-location-constraint", "^0.1.0-preview.5");

public final String packageName;
public final String version;
Expand Down
4 changes: 0 additions & 4 deletions packages/location-constraint-middleware/README.md

This file was deleted.

47 changes: 0 additions & 47 deletions packages/location-constraint-middleware/src/index.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/middleware-location-constraint/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# @aws-sdk/middleware-location-constraint

[![NPM version](https://img.shields.io/npm/v/@aws-sdk/middleware-location-constraint/preview.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-location-constraint)
[![NPM downloads](https://img.shields.io/npm/dm/@aws-sdk/middleware-location-constraint.svg)](https://www.npmjs.com/package/@aws-sdk/middleware-location-constraint)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@aws-sdk/location-constraint-middleware",
"version": "0.1.0-preview.7",
"name": "@aws-sdk/middleware-location-constraint",
"version": "0.1.0-preview.5",
"scripts": {
"prepublishOnly": "tsc",
"pretest": "tsc -p tsconfig.test.json",
Expand All @@ -22,4 +22,4 @@
"jest": "^24.7.1",
"typescript": "~3.4.0"
}
}
}
16 changes: 16 additions & 0 deletions packages/middleware-location-constraint/src/configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Provider } from "@aws-sdk/types";

export interface LocationConstraintInputConfig {}

interface PreviouslyResolved {
region: Provider<string>;
}

export interface LocationConstraintResolvedConfig {
region: Provider<string>;
}
export function resolveLocationConstraintConfig<T>(
input: T & LocationConstraintInputConfig & PreviouslyResolved
): T & LocationConstraintResolvedConfig {
return { ...input };
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { locationConstraintMiddleware } from "./";

describe("locationConstrainMiddleware", () => {
const next = jest.fn();

beforeEach(() => {
jest.clearAllMocks();
});

it("should remove any CreateBucketConfiguration from requests directed at us-east-1", async () => {
const mw = locationConstraintMiddleware(async () => "us-east-1");
const next = jest.fn();
const handler = locationConstraintMiddleware({
region: "us-east-1"
})(next, {} as any);
const input = {
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
foo: "bar"
};

await mw(next, {} as any)({ input });
await handler({ input });

expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toEqual({
Expand All @@ -21,13 +27,14 @@ describe("locationConstrainMiddleware", () => {
});

it("should apply a CreateBucketConfiguration with a LocationConstraint of the target region for requests directed outside of us-east-1", async () => {
const mw = locationConstraintMiddleware(async () => "us-east-2");
const next = jest.fn();
const handler = locationConstraintMiddleware({
region: "us-east-2"
})(next, {} as any);
const input = {
foo: "bar"
};

await mw(next, {} as any)({ input });
await handler({ input });

expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toEqual({
Expand All @@ -39,14 +46,15 @@ describe("locationConstrainMiddleware", () => {
});

it("should do nothing if a LocationConstraint had already been set on a request directed outside of us-east-1", async () => {
const mw = locationConstraintMiddleware(async () => "us-east-2");
const next = jest.fn();
const handler = locationConstraintMiddleware({
region: "us-east-2"
})(next, {} as any);
const input = {
CreateBucketConfiguration: { LocationConstraint: "us-east-1" },
foo: "bar"
};

await mw(next, {} as any)({ input });
await handler({ input });

expect(next.mock.calls.length).toBe(1);
expect(next.mock.calls[0][0]).toEqual({ input });
Expand Down
67 changes: 67 additions & 0 deletions packages/middleware-location-constraint/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
InitializeHandler,
InitializeMiddleware,
InitializeHandlerArguments,
InitializeHandlerOptions,
InitializeHandlerOutput,
MetadataBearer,
Pluggable
} from "@aws-sdk/types";
import { LocationConstraintResolvedConfig } from "./configuration";

/**
* This middleware modifies the input on S3 CreateBucket requests. If the LocationConstraint has not been set, this
* middleware will set a LocationConstraint to match the configured region. The CreateBucketConfiguration will be
* removed entirely on requests to the us-east-1 region.
*/

export function locationConstraintMiddleware(
options: LocationConstraintResolvedConfig
): InitializeMiddleware<any, any> {
return <Output extends MetadataBearer>(
next: InitializeHandler<any, Output>
): InitializeHandler<any, Output> => async (
args: InitializeHandlerArguments<any>
): Promise<InitializeHandlerOutput<Output>> => {
const { CreateBucketConfiguration } = args.input;
if (
!CreateBucketConfiguration ||
!CreateBucketConfiguration.LocationConstraint
) {
args = {
...args,
input: {
...args.input,
CreateBucketConfiguration: { LocationConstraint: options.region }
}
};
} else if (options.region === "us-east-1") {
args = {
...args,
input: {
...args.input,
CreateBucketConfiguration: undefined
}
};
}

return next(args);
};
}

export const locationConstraintMiddlewareOptions: InitializeHandlerOptions = {
step: "initialize",
tags: ["LOCATION_CONSTRAINT", "CREATE_BUCKET_CONFIGURATION"],
name: "locationConstraintMiddleware"
};

export const getLocationConstraintPlugin = (
config: LocationConstraintResolvedConfig
): Pluggable<any, any> => ({
applyToStack: clientStack => {
clientStack.add(
locationConstraintMiddleware(config),
locationConstraintMiddlewareOptions
);
}
});

0 comments on commit ad7f978

Please sign in to comment.