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 storage access rules to outputs #1927

Merged
merged 15 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
10 changes: 10 additions & 0 deletions .changeset/two-sloths-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@aws-amplify/client-config': minor
'@aws-amplify/backend-output-schemas': patch
'@aws-amplify/integration-tests': patch
'@aws-amplify/backend-storage': patch
'@aws-amplify/backend': patch
'@aws-amplify/backend-cli': patch
---

add storage access rules to outputs
20 changes: 20 additions & 0 deletions packages/backend-output-schemas/src/storage/v1.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
import { z } from 'zod';

const storageAccessActionEnum = z.enum([
'read',
'get',
'list',
'write',
'delete',
]);

const pathSchema = z.record(
z.string(),
z.object({
guest: z.array(storageAccessActionEnum).optional(),
authenticated: z.array(storageAccessActionEnum).optional(),
groups: z.array(storageAccessActionEnum).optional(),
entity: z.array(storageAccessActionEnum).optional(),
resource: z.array(storageAccessActionEnum).optional(),
})
);

const bucketSchema = z.object({
name: z.string(),
bucketName: z.string(),
storageRegion: z.string(),
paths: pathSchema.optional(),
});

export const storageOutputSchema = z.object({
Expand Down
3 changes: 2 additions & 1 deletion packages/backend-storage/src/access_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ResourceProvider,
} from '@aws-amplify/plugin-types';
import { StorageAccessBuilder } from './types.js';
import { entityIdSubstitution } from './constants.js';

export const roleAccessBuilder: StorageAccessBuilder = {
authenticated: {
Expand Down Expand Up @@ -69,7 +70,7 @@ export const roleAccessBuilder: StorageAccessBuilder = {
},
],
actions,
idSubstitution: '${cognito-identity.amazonaws.com:sub}',
idSubstitution: entityIdSubstitution,
}),
}),
resource: (other) => ({
Expand Down
1 change: 1 addition & 0 deletions packages/backend-storage/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export const entityIdPathToken = '{entity_id}';
export const entityIdSubstitution = '${cognito-identity.amazonaws.com:sub}';
9 changes: 9 additions & 0 deletions packages/backend-storage/src/construct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { AttributionMetadataStorage } from '@aws-amplify/backend-output-storage'
import { fileURLToPath } from 'node:url';
import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { S3EventSourceV2 } from 'aws-cdk-lib/aws-lambda-event-sources';
import { StorageAccessDefinitionOutput } from './private_types.js';

// Be very careful editing this value. It is the string that is used to attribute stacks to Amplify Storage in BI metrics
const storageStackType = 'storage-S3';
Expand Down Expand Up @@ -84,6 +85,7 @@ export class AmplifyStorage
readonly resources: StorageResources;
readonly isDefault: boolean;
readonly name: string;
accessDefinition: StorageAccessDefinitionOutput;
/**
* Create a new AmplifyStorage instance
*/
Expand Down Expand Up @@ -146,4 +148,11 @@ export class AmplifyStorage
new S3EventSourceV2(this.resources.bucket, { events })
);
};

/**
* Add access definitions to storage
*/
addAccessDefinition = (accessOutput: StorageAccessDefinitionOutput) => {
this.accessDefinition = accessOutput;
};
}
6 changes: 6 additions & 0 deletions packages/backend-storage/src/private_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ export type StorageError =
* StorageAction type intended to be used after mapping "read" to "get" and "list"
*/
export type InternalStorageAction = Exclude<StorageAction, 'read'>;

/**
* Storage access types intended to be used to map storage access to storage outputs
*/
export type StorageAccessConfig = Record<string, InternalStorageAction[]>;
export type StorageAccessDefinitionOutput = Record<string, StorageAccessConfig>;
Loading
Loading