-
Notifications
You must be signed in to change notification settings - Fork 10
Allow linking and unlinking lambdas to applications #8
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
base: main
Are you sure you want to change the base?
Changes from 7 commits
af6229d
41acd70
f1ff5a8
dd66e33
611c858
ae7e5fd
86f9acf
c3e5f64
89584e3
c99ac2e
eee381b
517ef18
880d627
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| import {Command} from '@commander-js/extra-typings'; | ||
| import {FusionAuthClient} from '@fusionauth/typescript-client'; | ||
| import {readFile} from 'fs/promises'; | ||
| import chalk from 'chalk'; | ||
| import {join} from 'path'; | ||
| import {errorAndExit} from '../utils.js'; | ||
| import {apiKeyOption, hostOption} from "../options.js"; | ||
| import {load as loadYaml} from 'js-yaml'; | ||
|
|
||
| const action = async function (lambdaId: string, {input, key: apiKey, host}: { | ||
| input: string; | ||
| key: string; | ||
| host: string | ||
| }): Promise<void> { | ||
| console.log(`Creating lambda ${lambdaId} on ${host}`); | ||
| try { | ||
| const filename = join(input, lambdaId + ".yaml"); | ||
| const data = await readFile(filename, 'utf-8'); | ||
| const lambda = loadYaml(data) as object; | ||
| const fusionAuthClient = new FusionAuthClient(apiKey, host); | ||
| const clientResponse = await fusionAuthClient.createLambda(lambdaId, { lambda }); | ||
| if (!clientResponse.wasSuccessful()) | ||
| errorAndExit(`Error creating lambda: `, clientResponse); | ||
| console.log(chalk.green(`Lambda created`)); | ||
| } | ||
| catch (e: unknown) { | ||
| errorAndExit(`Error creating lambda: `, e); | ||
| } | ||
| } | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
| export const lambdaCreate = new Command('lambda:create') | ||
| .description(`Create a lambda on FusionAuth. | ||
| Example lambda .yaml file: | ||
|
|
||
| body: | | ||
| function populate(jwt, user, registration) { | ||
| jwt.message = 'Hello World!'; | ||
| console.info('Hello World!'); | ||
| } | ||
| debug: true | ||
| engineType: GraalJS | ||
| id: f3b3b547-7754-452d-8729-21b50d111505 | ||
| insertInstant: 1692177291178 | ||
| lastUpdateInstant: 1692211131823 | ||
| name: '[ATestLambda]' | ||
| type: JWTPopulate | ||
| `) | ||
| .argument('<lambdaId>', 'The lambda id to create. The lambda is read from the file <id>.yaml in the <input> directory.') | ||
| .option('-i, --input <input>', 'The input directory', './lambdas/') | ||
| .addOption(apiKeyOption) | ||
| .addOption(hostOption) | ||
| .action(action); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import {Command} from '@commander-js/extra-typings'; | ||
| import {FusionAuthClient, ApplicationRequest} from '@fusionauth/typescript-client'; | ||
| import chalk from 'chalk'; | ||
| import {errorAndExit, getApplicationName} from '../utils.js'; | ||
| import {apiKeyOption, hostOption} from "../options.js"; | ||
|
|
||
| const action = async function ( applicationId: string, | ||
| lambdaId: string, | ||
| {key: apiKey, host}: | ||
| { | ||
| key: string; | ||
| host: string | ||
| } | ||
| ): Promise<void> | ||
| { | ||
| console.log(`Linking lambda ${lambdaId} to application ${applicationId} on ${host}`); | ||
| try { | ||
| const applicationName = await getApplicationName(applicationId, {key: apiKey, host}); | ||
| const request: ApplicationRequest = { | ||
| "application": { | ||
| "name": applicationName, | ||
| "lambdaConfiguration": { | ||
| "accessTokenPopulateId": lambdaId, | ||
| "idTokenPopulateId": lambdaId | ||
| } | ||
| } | ||
| }; | ||
| const fusionAuthClient = new FusionAuthClient(apiKey, host); | ||
| const clientResponse = await fusionAuthClient.updateApplication(applicationId, request) | ||
|
||
| if (!clientResponse.wasSuccessful()) | ||
| errorAndExit(`Error linking lambda: `, clientResponse); | ||
| console.log(chalk.green(`Lambda linked`)); | ||
| } | ||
| catch (e: unknown) { | ||
| errorAndExit(`Error linking lambda: `, e); | ||
| } | ||
| } | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
| export const lambdaLinkToApplication = new Command('lambda:link-to-application') | ||
| .description(`Link an existing lambda to an application on FusionAuth as both the "Access Token populate lambda" and the "Id Token populate lambda". | ||
| Example use: | ||
| npx fusionauth lambda:link-to-application e9fdb985-9173-4e01-9d73-ac2d60d1dc8e f3b3b547-7754-452d-8729-21b50d111505 --key lambda_testing_key;`) | ||
| .argument('<applicationId>', 'The application id to update.') | ||
| .argument('<lambdaId>', 'The lambda id to link to the application.') | ||
| .addOption(apiKeyOption) | ||
| .addOption(hostOption) | ||
| .action(action); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| import {Command} from '@commander-js/extra-typings'; | ||
| import {FusionAuthClient, ApplicationRequest} from '@fusionauth/typescript-client'; | ||
| import chalk from 'chalk'; | ||
| import {errorAndExit, getApplication} from '../utils.js'; | ||
| import {apiKeyOption, hostOption} from "../options.js"; | ||
|
|
||
| const action = async function ( applicationId: string, | ||
| lambdaId: string, | ||
| {key: apiKey, host}: | ||
| { | ||
| key: string; | ||
| host: string | ||
| } | ||
| ): Promise<void> | ||
| { | ||
| console.log(`Unlinking lambda ${lambdaId} from application ${applicationId} on ${host}`); | ||
| try { | ||
| const application = await getApplication(applicationId, {key: apiKey, host}); | ||
| const request: ApplicationRequest = { | ||
| "application": { | ||
| "name": application.name, | ||
| "lambdaConfiguration": {} | ||
| } | ||
| }; | ||
| const accessTokenPopulateId = application.lambdaConfiguration?.accessTokenPopulateId; | ||
| const idTokenPopulateId = application.lambdaConfiguration?.idTokenPopulateId; | ||
|
|
||
| if (!accessTokenPopulateId && !idTokenPopulateId) | ||
| errorAndExit(`No existing lambdas were linked`); | ||
| if (accessTokenPopulateId && accessTokenPopulateId == lambdaId) | ||
| request.application!.lambdaConfiguration!.accessTokenPopulateId = null; | ||
| else | ||
| request.application!.lambdaConfiguration!.accessTokenPopulateId = accessTokenPopulateId; | ||
| if (idTokenPopulateId && idTokenPopulateId == lambdaId) | ||
| request.application!.lambdaConfiguration!.idTokenPopulateId = null; | ||
|
||
| else | ||
| request.application!.lambdaConfiguration!.idTokenPopulateId = idTokenPopulateId; | ||
|
|
||
| const fusionAuthClient = new FusionAuthClient(apiKey, host); | ||
| const clientResponse = await fusionAuthClient.updateApplication(applicationId, request) | ||
|
||
| if (!clientResponse.wasSuccessful()) | ||
| errorAndExit(`Error unlinking lambda: `, clientResponse); | ||
| console.log(chalk.green(`Lambda unlinked`)); | ||
| } | ||
| catch (e: unknown) { | ||
| errorAndExit(`Error unlinking lambda: `, e); | ||
| } | ||
| } | ||
|
|
||
| // noinspection JSUnusedGlobalSymbols | ||
| export const lambdaUnlinkFromApplication = new Command('lambda:unlink-from-application') | ||
| .description(`Unlink an existing lambda from an application on FusionAuth from both "Access Token populate lambda" and the "Id Token populate lambda" if it was used as either or both. | ||
| Example use: | ||
| npx fusionauth lambda:unlink-from-application e9fdb985-9173-4e01-9d73-ac2d60d1dc8e f3b3b547-7754-452d-8729-21b50d111505 --key lambda_testing_key;`) | ||
| .argument('<applicationId>', 'The application id to update.') | ||
| .argument('<lambdaId>', 'The lambda id to unlink from the application.') | ||
| .addOption(apiKeyOption) | ||
| .addOption(hostOption) | ||
| .action(action); | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a short
.summary('Create a lambda on FusionAuth')for commands with a large description. (lamdba:create,lambda:link-to-application,lamdba:unlink-from-application)