Skip to content
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

add user groups to outputs #2081

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/strange-eggs-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@aws-amplify/backend-output-schemas': minor
'@aws-amplify/client-config': minor
'@aws-amplify/auth-construct': patch
'@aws-amplify/backend': patch
'@aws-amplify/backend-cli': patch
---

add user groups to outputs
25 changes: 25 additions & 0 deletions packages/auth-construct/src/construct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1098,6 +1098,7 @@ void describe('Auth construct', () => {
'oauthRedirectSignOut',
'oauthResponseType',
'oauthClientId',
'groups',
],
},
},
Expand Down Expand Up @@ -1480,6 +1481,30 @@ void describe('Auth construct', () => {
const outputs = template.findOutputs('*');
assert.equal(outputs['socialProviders']['Value'], `["GOOGLE"]`);
});
void it('can override group precedence and correctly updates stored output', () => {
const app = new App();
const stack = new Stack(app);
const auth = new AmplifyAuth(stack, 'test', {
loginWith: { email: true },
groups: ['admins', 'managers'],
});
auth.resources.groups['admins'].cfnUserGroup.precedence = 2;
const expectedGroups = {
admins: {
precedence: 2,
},
managers: {
precedence: 1,
},
};
const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::Cognito::UserPoolGroup', {
GroupName: 'admins',
Precedence: 2,
});
const outputs = template.findOutputs('*');
assert.equal(outputs['groups']['Value'], JSON.stringify(expectedGroups));
});
});

void describe('Auth external login', () => {
Expand Down
17 changes: 17 additions & 0 deletions packages/auth-construct/src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,23 @@ export class AmplifyAuth
},
});

// user group precedence can be overwritten, so they are exposed via cdk LAZY
output.groups = Lazy.string({
produce: () => {
const groupsObj = Object.keys(this.resources.groups).reduce(
(result, key) => {
const precedence =
this.resources.groups[key].cfnUserGroup.precedence;
result[key] = { precedence };
return result;
},
{} as Record<string, { precedence?: number }>
);

return JSON.stringify(groupsObj);
},
});

outputStorageStrategy.addBackendOutputEntry(authOutputKey, {
version: '1',
payload: output,
Expand Down
12 changes: 12 additions & 0 deletions packages/backend-output-schemas/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut: z.ZodOptional<z.ZodString>;
oauthClientId: z.ZodOptional<z.ZodString>;
oauthResponseType: z.ZodOptional<z.ZodString>;
groups: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
authRegion: string;
userPoolId: string;
Expand All @@ -153,6 +154,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
}, {
authRegion: string;
userPoolId: string;
Expand All @@ -173,6 +175,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
}>;
}, "strip", z.ZodTypeAny, {
version: "1";
Expand All @@ -196,6 +199,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
};
}, {
version: "1";
Expand All @@ -219,6 +223,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
};
}>]>>;
"AWS::Amplify::GraphQL": z.ZodOptional<z.ZodDiscriminatedUnion<"version", [z.ZodObject<{
Expand Down Expand Up @@ -402,6 +407,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
};
} | undefined;
"AWS::Amplify::GraphQL"?: {
Expand Down Expand Up @@ -473,6 +479,7 @@ export const unifiedBackendOutputSchema: z.ZodObject<{
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
};
} | undefined;
"AWS::Amplify::GraphQL"?: {
Expand Down Expand Up @@ -555,6 +562,7 @@ export const versionedAuthOutputSchema: z.ZodDiscriminatedUnion<"version", [z.Zo
oauthRedirectSignOut: z.ZodOptional<z.ZodString>;
oauthClientId: z.ZodOptional<z.ZodString>;
oauthResponseType: z.ZodOptional<z.ZodString>;
groups: z.ZodOptional<z.ZodString>;
}, "strip", z.ZodTypeAny, {
authRegion: string;
userPoolId: string;
Expand All @@ -575,6 +583,7 @@ export const versionedAuthOutputSchema: z.ZodDiscriminatedUnion<"version", [z.Zo
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
}, {
authRegion: string;
userPoolId: string;
Expand All @@ -595,6 +604,7 @@ export const versionedAuthOutputSchema: z.ZodDiscriminatedUnion<"version", [z.Zo
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
}>;
}, "strip", z.ZodTypeAny, {
version: "1";
Expand All @@ -618,6 +628,7 @@ export const versionedAuthOutputSchema: z.ZodDiscriminatedUnion<"version", [z.Zo
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
};
}, {
version: "1";
Expand All @@ -641,6 +652,7 @@ export const versionedAuthOutputSchema: z.ZodDiscriminatedUnion<"version", [z.Zo
oauthRedirectSignOut?: string | undefined;
oauthClientId?: string | undefined;
oauthResponseType?: string | undefined;
groups?: string | undefined;
};
}>]>;

Expand Down
1 change: 1 addition & 0 deletions packages/backend-output-schemas/src/auth/v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export const authOutputSchema = z.object({
oauthRedirectSignOut: z.string().optional(),
oauthClientId: z.string().optional(),
oauthResponseType: z.string().optional(),
groups: z.string().optional(), // JSON array as string
}),
});
2 changes: 1 addition & 1 deletion packages/backend/src/backend_factory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ void describe('Backend', () => {
const backend = new BackendFactory({}, rootStack);
const clientConfigPartial: DeepPartialAmplifyGeneratedConfigs<ClientConfig> =
{
version: '1.2',
version: '1.3',
custom: {
someCustomOutput: 'someCustomOutputValue',
},
Expand Down
2 changes: 1 addition & 1 deletion packages/backend/src/backend_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const rootStackTypeIdentifier = 'root';

// Client config version that is used by `backend.addOutput()`
const DEFAULT_CLIENT_CONFIG_VERSION_FOR_BACKEND_ADD_OUTPUT =
ClientConfigVersionOption.V1_2;
ClientConfigVersionOption.V1_3;

/**
* Factory that collects and instantiates all the Amplify backend constructs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ void describe('Custom outputs accumulator', () => {
);

const configPart1: DeepPartialAmplifyGeneratedConfigs<ClientConfig> = {
version: '1.2',
version: '1.3',
custom: { output1: 'val1' },
};
const configPart2: DeepPartialAmplifyGeneratedConfigs<ClientConfig> = {
version: '1.2',
version: '1.3',
custom: { output2: 'val2' },
};
accumulator.addOutput(configPart1);
Expand Down Expand Up @@ -115,7 +115,7 @@ void describe('Custom outputs accumulator', () => {

assert.throws(
() =>
accumulator.addOutput({ version: '1.2', custom: { output1: 'val1' } }),
accumulator.addOutput({ version: '1.3', custom: { output1: 'val1' } }),
(error: AmplifyUserError) => {
assert.strictEqual(
error.message,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void describe('generate outputs command', () => {
assert.equal(generateClientConfigMock.mock.callCount(), 1);
assert.deepEqual(
generateClientConfigMock.mock.calls[0].arguments[1],
'1.2' // default version
'1.3' // default version
);
assert.deepEqual(
generateClientConfigMock.mock.calls[0].arguments[2],
Expand All @@ -97,7 +97,7 @@ void describe('generate outputs command', () => {
assert.equal(generateClientConfigMock.mock.callCount(), 1);
assert.deepEqual(
generateClientConfigMock.mock.calls[0].arguments[1],
'1.2' // default version
'1.3' // default version
);
assert.deepStrictEqual(
generateClientConfigMock.mock.calls[0].arguments[2],
Expand All @@ -118,7 +118,7 @@ void describe('generate outputs command', () => {
namespace: 'app_id',
type: 'branch',
},
'1.2',
'1.3',
'/foo/bar',
undefined,
]
Expand All @@ -136,7 +136,7 @@ void describe('generate outputs command', () => {
{
stackName: 'stack_name',
},
'1.2',
'1.3',
'/foo/bar',
undefined,
]
Expand All @@ -154,7 +154,7 @@ void describe('generate outputs command', () => {
{
stackName: 'stack_name',
},
'1.2',
'1.3',
'foo/bar',
undefined,
]
Expand All @@ -172,7 +172,7 @@ void describe('generate outputs command', () => {
{
stackName: 'stack_name',
},
'1.2',
'1.3',
'foo/bar',
ClientConfigFormat.DART,
]
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/commands/sandbox/sandbox_command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,15 +427,15 @@ void describe('sandbox command', () => {
);
});

void it('sandbox creates an empty client config file if one does not already exist for version 1.2', async (contextual) => {
void it('sandbox creates an empty client config file if one does not already exist for version 1.3', async (contextual) => {
contextual.mock.method(fs, 'existsSync', () => false);
const writeFileMock = contextual.mock.method(fsp, 'writeFile', () => true);
await commandRunner.runCommand('sandbox --outputs-version 1.2');
await commandRunner.runCommand('sandbox --outputs-version 1.3');
assert.equal(sandboxStartMock.mock.callCount(), 1);
assert.equal(writeFileMock.mock.callCount(), 1);
assert.deepStrictEqual(
writeFileMock.mock.calls[0].arguments[1],
`{\n "version": "1.2"\n}`
`{\n "version": "1.3"\n}`
);
assert.deepStrictEqual(
writeFileMock.mock.calls[0].arguments[0],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void describe('sandbox_event_handler_factory', () => {
} as unknown as ClientConfigGeneratorAdapter;
const clientConfigLifecycleHandler = new ClientConfigLifecycleHandler(
clientConfigGeneratorAdapterMock,
'1.2',
'1.3',
'test-out',
ClientConfigFormat.JSON
);
Expand Down Expand Up @@ -73,7 +73,7 @@ void describe('sandbox_event_handler_factory', () => {
namespace: 'test',
name: 'name',
},
'1.2',
'1.3',
'test-out',
'json',
]);
Expand Down Expand Up @@ -185,7 +185,7 @@ void describe('sandbox_event_handler_factory', () => {
namespace: 'test',
name: 'name',
},
'1.2',
'1.3',
'test-out',
'json',
]);
Expand Down
Loading
Loading