-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(lambda): initial user migration trigger support
Adds support for the UserMigration_Authentication trigger.
- Loading branch information
Showing
25 changed files
with
738 additions
and
65 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
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,10 +1,12 @@ | ||
import { Triggers } from "./triggers"; | ||
import { CodeDelivery } from "./codeDelivery/codeDelivery"; | ||
import { UserPool } from "./userPool"; | ||
|
||
export { UserPool } from "./userPool"; | ||
export { createCodeDelivery, CodeDelivery } from "./codeDelivery/codeDelivery"; | ||
|
||
export interface Services { | ||
storage: UserPool; | ||
userPool: UserPool; | ||
codeDelivery: CodeDelivery; | ||
triggers: Triggers; | ||
} |
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,140 @@ | ||
import { createLambda } from "./lambda"; | ||
import * as AWS from "aws-sdk"; | ||
|
||
describe("Lambda function invoker", () => { | ||
let mockLambdaClient: jest.Mocked<AWS.Lambda>; | ||
|
||
beforeEach(() => { | ||
mockLambdaClient = { | ||
invoke: jest.fn(), | ||
} as any; | ||
}); | ||
|
||
describe("enabled", () => { | ||
it("returns true if lambda is configured", () => { | ||
const lambda = createLambda( | ||
{ | ||
UserMigration: "MyLambdaName", | ||
}, | ||
mockLambdaClient | ||
); | ||
|
||
expect(lambda.enabled("UserMigration")).toBe(true); | ||
}); | ||
|
||
it("returns false if lambda is not configured", () => { | ||
const lambda = createLambda({}, mockLambdaClient); | ||
|
||
expect(lambda.enabled("UserMigration")).toBe(false); | ||
}); | ||
}); | ||
|
||
describe("invoke", () => { | ||
it("throws if lambda is not configured", async () => { | ||
const lambda = createLambda({}, mockLambdaClient); | ||
|
||
await expect( | ||
lambda.invoke("UserMigration", { | ||
clientId: "clientId", | ||
password: "password", | ||
triggerSource: "UserMigration_Authentication", | ||
username: "username", | ||
userPoolId: "userPoolId", | ||
}) | ||
).rejects.toEqual(new Error("UserMigration trigger not configured")); | ||
}); | ||
|
||
it("invokes the lambda", async () => { | ||
const response = Promise.resolve({ | ||
StatusCode: 200, | ||
Payload: '{ "some": "json" }', | ||
}); | ||
mockLambdaClient.invoke.mockReturnValue({ | ||
promise: () => response, | ||
} as any); | ||
const lambda = createLambda( | ||
{ | ||
UserMigration: "MyLambdaName", | ||
}, | ||
mockLambdaClient | ||
); | ||
|
||
await lambda.invoke("UserMigration", { | ||
clientId: "clientId", | ||
password: "password", | ||
triggerSource: "UserMigration_Authentication", | ||
username: "username", | ||
userPoolId: "userPoolId", | ||
}); | ||
|
||
expect(mockLambdaClient.invoke).toHaveBeenCalledWith({ | ||
FunctionName: "MyLambdaName", | ||
InvocationType: "RequestResponse", | ||
Payload: JSON.stringify({ | ||
version: 0, | ||
userName: "username", | ||
callerContext: { awsSdkVersion: "2.656.0", clientId: "clientId" }, | ||
region: "local", | ||
userPoolId: "userPoolId", | ||
triggerSource: "UserMigration_Authentication", | ||
request: { userAttributes: {} }, | ||
response: {}, | ||
}), | ||
}); | ||
}); | ||
|
||
describe("when lambda successful", () => { | ||
it("returns string payload as json", async () => { | ||
const response = Promise.resolve({ | ||
StatusCode: 200, | ||
Payload: '{ "response": "value" }', | ||
}); | ||
mockLambdaClient.invoke.mockReturnValue({ | ||
promise: () => response, | ||
} as any); | ||
const lambda = createLambda( | ||
{ | ||
UserMigration: "MyLambdaName", | ||
}, | ||
mockLambdaClient | ||
); | ||
|
||
const result = await lambda.invoke("UserMigration", { | ||
clientId: "clientId", | ||
password: "password", | ||
triggerSource: "UserMigration_Authentication", | ||
username: "username", | ||
userPoolId: "userPoolId", | ||
}); | ||
|
||
expect(result).toEqual("value"); | ||
}); | ||
|
||
it("returns Buffer payload as json", async () => { | ||
const response = Promise.resolve({ | ||
StatusCode: 200, | ||
Payload: Buffer.from('{ "response": "value" }'), | ||
}); | ||
mockLambdaClient.invoke.mockReturnValue({ | ||
promise: () => response, | ||
} as any); | ||
const lambda = createLambda( | ||
{ | ||
UserMigration: "MyLambdaName", | ||
}, | ||
mockLambdaClient | ||
); | ||
|
||
const result = await lambda.invoke("UserMigration", { | ||
clientId: "clientId", | ||
password: "password", | ||
triggerSource: "UserMigration_Authentication", | ||
username: "username", | ||
userPoolId: "userPoolId", | ||
}); | ||
|
||
expect(result).toEqual("value"); | ||
}); | ||
}); | ||
}); | ||
}); |
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,80 @@ | ||
import { CognitoUserPoolEvent } from "aws-lambda"; | ||
import * as AWS from "aws-sdk"; | ||
import { UnexpectedLambdaExceptionError } from "../errors"; | ||
|
||
interface UserMigrationEvent { | ||
userPoolId: string; | ||
clientId: string; | ||
username: string; | ||
password: string; | ||
triggerSource: "UserMigration_Authentication"; | ||
} | ||
|
||
export type CognitoUserPoolResponse = CognitoUserPoolEvent["response"]; | ||
|
||
export interface Lambda { | ||
invoke( | ||
lambda: "UserMigration", | ||
event: UserMigrationEvent | ||
): Promise<CognitoUserPoolResponse>; | ||
enabled(lambda: "UserMigration"): boolean; | ||
} | ||
|
||
export interface FunctionConfig { | ||
UserMigration?: string; | ||
} | ||
|
||
export type CreateLambda = ( | ||
config: FunctionConfig, | ||
lambdaClient: AWS.Lambda | ||
) => Lambda; | ||
|
||
export const createLambda: CreateLambda = (config, lambdaClient) => ({ | ||
enabled: (lambda) => !!config[lambda], | ||
async invoke(lambda, event) { | ||
const lambdaName = config[lambda]; | ||
if (!lambdaName) { | ||
throw new Error(`${lambda} trigger not configured`); | ||
} | ||
|
||
const lambdaEvent: CognitoUserPoolEvent = { | ||
version: 0, // TODO: how do we know what this is? | ||
userName: event.username, | ||
callerContext: { | ||
awsSdkVersion: "2.656.0", // TODO: this isn't correct | ||
clientId: event.clientId, | ||
}, | ||
region: "local", // TODO: pull from above, | ||
userPoolId: event.userPoolId, | ||
triggerSource: event.triggerSource, | ||
request: { | ||
userAttributes: {}, | ||
}, | ||
response: {}, | ||
}; | ||
|
||
console.log( | ||
`Invoking "${lambdaName}" with event`, | ||
JSON.stringify(lambdaEvent, undefined, 2) | ||
); | ||
const result = await lambdaClient | ||
.invoke({ | ||
FunctionName: lambdaName, | ||
InvocationType: "RequestResponse", | ||
Payload: JSON.stringify(lambdaEvent), | ||
}) | ||
.promise(); | ||
|
||
console.log( | ||
`Lambda completed with StatusCode=${result.StatusCode} and FunctionError=${result.FunctionError}` | ||
); | ||
if (result.StatusCode === 200) { | ||
const parsedPayload = JSON.parse(result.Payload as string); | ||
|
||
return parsedPayload.response as CognitoUserPoolResponse; | ||
} else { | ||
console.error(result.FunctionError); | ||
throw new UnexpectedLambdaExceptionError(); | ||
} | ||
}, | ||
}); |
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 @@ | ||
export { Triggers, createTriggers } from "./triggers"; |
Oops, something went wrong.