Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
rtpascual committed Sep 23, 2024
1 parent 12db2ca commit 69dbc7b
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,6 @@ void describe('StorageAccessOrchestrator', () => {
);
assert.deepStrictEqual(storageAccessDefinitionOutput, {
'foo/*': {
'auth-with-id': ['write', 'delete'],
auth: ['get'],
},
[`foo/${entityIdSubstitution}/*`]: {
Expand Down Expand Up @@ -657,7 +656,6 @@ void describe('StorageAccessOrchestrator', () => {
},
'other/*': {
stub1: ['get'],
stub2: ['get', 'write', 'delete'],
},
[`other/${entityIdSubstitution}/*`]: {
stub2: ['get', 'write', 'delete'],
Expand Down
4 changes: 2 additions & 2 deletions packages/backend-storage/src/storage_access_orchestrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ export class StorageAccessOrchestrator {
([s3Prefix, accessPermissions]) => {
const uniqueDefinitionIdSet = new Set<string>();
// iterate over all of the access definitions for a given prefix
const accessConfig: StorageAccessConfig = {};
accessPermissions.forEach((permission) => {
const accessConfig: StorageAccessConfig = {};
// replace "read" with "get" and "list" in actions
const replaceReadWithGetAndList = permission.actions.flatMap(
(action) => (action === 'read' ? ['get', 'list'] : [action])
Expand Down Expand Up @@ -291,7 +291,7 @@ const placeholderSubstitution = (
idSubstitution
) as StoragePath;

// for auth/unauth roles where prefix ends with '/*/*' remove the last wildcard
// for owner paths where prefix ends with '/*/*' remove the last wildcard
if (prefix.endsWith('/*/*')) {
return prefix.slice(0, -2) as StoragePath;
}
Expand Down
44 changes: 44 additions & 0 deletions packages/backend-storage/src/storage_outputs_aspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { StorageOutput } from '@aws-amplify/backend-output-schemas';
import { App, Stack } from 'aws-cdk-lib';
import { IConstruct } from 'constructs';
import { AmplifyUserError } from '@aws-amplify/platform-core';
import { StorageAccessDefinitionOutput } from './private_types.js';

void describe('StorageOutputsAspect', () => {
let app: App;
Expand Down Expand Up @@ -129,6 +130,49 @@ void describe('StorageOutputsAspect', () => {
})
);
});

void it('should add access paths if the storage has access rules configured', () => {
const accessDefinition = {
'path/*': {
authenticated: ['get', 'list', 'write', 'delete'],
guest: ['get', 'list'],
},
};
const node = new AmplifyStorage(stack, 'test', { name: 'testName' });
node.addAccessDefinition(
accessDefinition as StorageAccessDefinitionOutput
);
aspect = new StorageOutputsAspect(outputStorageStrategy);
aspect.visit(node);

assert.equal(
addBackendOutputEntryMock.mock.calls[0].arguments[0],
'AWS::Amplify::Storage'
);
assert.equal(
addBackendOutputEntryMock.mock.calls[0].arguments[1].payload
.storageRegion,
Stack.of(node).region
);
assert.equal(
addBackendOutputEntryMock.mock.calls[0].arguments[1].payload.bucketName,
node.resources.bucket.bucketName
);
assert.equal(
appendToBackendOutputListMock.mock.calls[0].arguments[0],
'AWS::Amplify::Storage'
);
assert.equal(
appendToBackendOutputListMock.mock.calls[0].arguments[1].payload
.buckets,
JSON.stringify({
name: node.name,
bucketName: node.resources.bucket.bucketName,
storageRegion: Stack.of(node).region,
paths: accessDefinition,
})
);
});
});

void describe('Validate', () => {
Expand Down
19 changes: 13 additions & 6 deletions packages/backend-storage/src/storage_outputs_aspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { BackendOutputStorageStrategy } from '@aws-amplify/plugin-types';
import { IAspect, Stack } from 'aws-cdk-lib';
import { IConstruct } from 'constructs';
import { AmplifyStorage } from './construct.js';
import { StorageAccessDefinitionOutput } from './private_types.js';

/**
* Aspect to store the storage outputs in the backend
Expand Down Expand Up @@ -95,16 +96,22 @@ export class StorageOutputsAspect implements IAspect {
},
});
}
const bucketsPayload: Record<
string,
string | StorageAccessDefinitionOutput
> = {
name: node.name,
bucketName: node.resources.bucket.bucketName,
storageRegion: Stack.of(node).region,
};
if (node.accessDefinition) {
bucketsPayload.paths = node.accessDefinition;
}
// both default and non-default buckets should have the name, bucket name, and storage region stored in `buckets` field
outputStorageStrategy.appendToBackendOutputList(storageOutputKey, {
version: '1',
payload: {
buckets: JSON.stringify({
name: node.name,
bucketName: node.resources.bucket.bucketName,
storageRegion: Stack.of(node).region,
paths: node.accessDefinition,
}),
buckets: JSON.stringify(bucketsPayload),
},
});
};
Expand Down
2 changes: 1 addition & 1 deletion packages/client-config/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type AmazonCognitoStandardAttributes_3 = 'address' | 'birthdate' | 'email' | 'fa

// @public
interface AmazonLocationServiceConfig {
name?: string;
style?: string;
}

Expand Down Expand Up @@ -128,7 +129,6 @@ export type AuthClientConfig = {

// @public
interface AWSAmplifyBackendOutputs {
$schema?: string;
analytics?: {
amazon_pinpoint?: {
aws_region: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,8 @@ void describe('storage client config contributor v1', () => {
storageRegion: 'testRegion',
paths: {
'path/*': {
guest: ['get'],
guest: ['get', 'list'],
authenticated: ['read', 'write', 'delete'],
},
},
}),
Expand All @@ -569,7 +570,8 @@ void describe('storage client config contributor v1', () => {
aws_region: 'testRegion',
paths: {
'path/*': {
guest: ['get'],
guest: ['get', 'list'],
authenticated: ['read', 'write', 'delete'],
},
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ export type AmplifyStorageAccessActions =
* Config format for Amplify Gen 2 client libraries to communicate with backend services.
*/
export interface AWSAmplifyBackendOutputs {
/**
* JSON schema
*/
$schema?: string;
/**
* Version of this schema
*/
Expand Down Expand Up @@ -246,6 +242,10 @@ export interface AWSAmplifyBackendOutputs {
* via the `patternProperty` ".*".
*/
export interface AmazonLocationServiceConfig {
/**
* Map resource name
*/
name?: string;
/**
* Map style
*/
Expand Down

0 comments on commit 69dbc7b

Please sign in to comment.