Skip to content

[DRAFT] feat: Phase 1 - create standalone storage-construct package with grantAccess method #2870

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

Draft
wants to merge 11 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
12 changes: 12 additions & 0 deletions .changeset/grumpy-icons-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@aws-amplify/storage-construct': major
---

Initial release of standalone AmplifyStorage construct package

- Create new `@aws-amplify/storage-construct` as standalone CDK L3 construct
- Migrate AmplifyStorage implementation with CDK-native triggers
- Add `grantAccess(auth, accessDefinition)` method for access control
- Add `StorageAccessDefinition` type for structured access configuration
- Support all S3 bucket features (CORS, versioning, SSL enforcement, auto-delete)
- Provide comprehensive TypeScript declarations and API documentation
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions packages/storage-construct/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Be very careful editing this file. It is crafted to work around [this issue](https://github.com/npm/npm/issues/4479)

# First ignore everything
**/*

# Then add back in transpiled js and ts declaration files
!lib/**/*.js
!lib/**/*.d.ts

# Then ignore test js and ts declaration files
*.test.js
*.test.d.ts

# This leaves us with including only js and ts declaration files of functional code
59 changes: 59 additions & 0 deletions packages/storage-construct/API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## API Report File for "@aws-amplify/storage-construct"

> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).

```ts

import { CfnBucket } from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';
import { EventType } from 'aws-cdk-lib/aws-s3';
import { IBucket } from 'aws-cdk-lib/aws-s3';
import { IFunction } from 'aws-cdk-lib/aws-lambda';
import { Stack } from 'aws-cdk-lib';

// @public
export class AmplifyStorage extends Construct {
constructor(scope: Construct, id: string, props: AmplifyStorageProps);
addTrigger: (events: EventType[], handler: IFunction) => void;
grantAccess: (_auth: unknown, _access: StorageAccessDefinition) => void;
// (undocumented)
readonly isDefault: boolean;
// (undocumented)
readonly name: string;
// (undocumented)
readonly resources: StorageResources;
// (undocumented)
readonly stack: Stack;
}

// @public (undocumented)
export type AmplifyStorageProps = {
isDefault?: boolean;
name: string;
versioned?: boolean;
triggers?: Partial<Record<AmplifyStorageTriggerEvent, IFunction>>;
};

// @public (undocumented)
export type AmplifyStorageTriggerEvent = 'onDelete' | 'onUpload';

// @public (undocumented)
export type StorageAccessDefinition = {
[path: string]: Array<{
type: 'authenticated' | 'guest' | 'owner' | 'groups';
actions: Array<'read' | 'write' | 'delete'>;
groups?: string[];
}>;
};

// @public (undocumented)
export type StorageResources = {
bucket: IBucket;
cfnResources: {
cfnBucket: CfnBucket;
};
};

// (No @packageDocumentation comment for this package)

```
3 changes: 3 additions & 0 deletions packages/storage-construct/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Description

Replace with a description of this package
4 changes: 4 additions & 0 deletions packages/storage-construct/api-extractor.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../api-extractor.base.json",
"mainEntryPointFilePath": "<projectFolder>/lib/index.d.ts"
}
31 changes: 31 additions & 0 deletions packages/storage-construct/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "@aws-amplify/storage-construct",
"version": "0.1.0",
"type": "module",
"publishConfig": {
"access": "public"
},
"exports": {
".": {
"types": "./lib/index.d.ts",
"import": "./lib/index.js",
"require": "./lib/index.js"
}
},
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc",
"clean": "rimraf lib",
"test": "node --test lib/construct.test.js",
"update:api": "api-extractor run --local"
},
"license": "Apache-2.0",
"dependencies": {
"@aws-amplify/backend-output-storage": "^1.3.1"
},
"peerDependencies": {
"aws-cdk-lib": "^2.189.1",
"constructs": "^10.0.0"
}
}
145 changes: 145 additions & 0 deletions packages/storage-construct/src/construct.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
import { describe, it } from 'node:test';
import { AmplifyStorage } from './construct.js';
import { App, Stack } from 'aws-cdk-lib';
import { Capture, Template } from 'aws-cdk-lib/assertions';
import assert from 'node:assert';

void describe('AmplifyStorage', () => {
void it('creates a bucket', () => {
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'test', { name: 'testName' });
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::S3::Bucket', 1);
});

void it('turns versioning on if specified', () => {
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'test', { versioned: true, name: 'testName' });
const template = Template.fromStack(stack);
template.resourceCountIs('AWS::S3::Bucket', 1);
template.hasResourceProperties('AWS::S3::Bucket', {
VersioningConfiguration: { Status: 'Enabled' },
});
});

void it('stores attribution data in stack', () => {
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'testAuth', { name: 'testName' });

const template = Template.fromStack(stack);
assert.equal(
JSON.parse(template.toJSON().Description).stackType,
'storage-S3',
);
});

void it('enables cors on the bucket', () => {
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'testAuth', { name: 'testName' });

const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::S3::Bucket', {
CorsConfiguration: {
CorsRules: [
{
AllowedHeaders: ['*'],
AllowedMethods: ['GET', 'HEAD', 'PUT', 'POST', 'DELETE'],
AllowedOrigins: ['*'],
ExposedHeaders: [
'x-amz-server-side-encryption',
'x-amz-request-id',
'x-amz-id-2',
'ETag',
],
MaxAge: 3000,
},
],
},
});
});

void it('sets destroy retain policy and auto-delete objects true', () => {
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'testBucketId', { name: 'testName' });

const template = Template.fromStack(stack);
const buckets = template.findResources('AWS::S3::Bucket');
const bucketLogicalIds = Object.keys(buckets);
assert.equal(bucketLogicalIds.length, 1);
const bucket = buckets[bucketLogicalIds[0]];
assert.equal(bucket.DeletionPolicy, 'Delete');
assert.equal(bucket.UpdateReplacePolicy, 'Delete');

template.hasResourceProperties('Custom::S3AutoDeleteObjects', {
BucketName: {
Ref: 'testBucketIdBucket3B30067A',
},
});
});

void it('forces SSL', () => {
const app = new App();
const stack = new Stack(app);
new AmplifyStorage(stack, 'testBucketId', { name: 'testName' });

const template = Template.fromStack(stack);

const policyCapture = new Capture();
template.hasResourceProperties('AWS::S3::BucketPolicy', {
Bucket: { Ref: 'testBucketIdBucket3B30067A' },
PolicyDocument: policyCapture,
});

assert.match(
JSON.stringify(policyCapture.asObject()),
/"aws:SecureTransport":"false"/,
);
});

void it('has grantAccess method', () => {
const app = new App();
const stack = new Stack(app);
const storage = new AmplifyStorage(stack, 'test', { name: 'testName' });

// Test that grantAccess method exists and can be called
assert.equal(typeof storage.grantAccess, 'function');

// Test calling grantAccess (currently just logs warning)
const mockAuth = {};
const accessDefinition = {
'photos/*': [
{
type: 'authenticated' as const,
actions: ['read' as const, 'write' as const],
},
],
};

// Should not throw
storage.grantAccess(mockAuth, accessDefinition);
});

void describe('storage overrides', () => {
void it('can override bucket properties', () => {
const app = new App();
const stack = new Stack(app);

const bucket = new AmplifyStorage(stack, 'test', { name: 'testName' });
bucket.resources.cfnResources.cfnBucket.accelerateConfiguration = {
accelerationStatus: 'Enabled',
};

const template = Template.fromStack(stack);
template.hasResourceProperties('AWS::S3::Bucket', {
AccelerateConfiguration: {
AccelerationStatus: 'Enabled',
},
});
});
});
});
Loading
Loading