Skip to content

Commit e3d9b23

Browse files
committed
patch(feat): octo-aws-cdk s3-storage-access overlay.
1 parent ad29df8 commit e3d9b23

10 files changed

+60
-36
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { AAnchor, Anchor } from '@quadnix/octo';
2+
import { S3StorageService } from '../models/service/s3-storage/s3-storage.service.model.js';
3+
4+
@Anchor()
5+
export class S3DirectoryAnchor extends AAnchor {
6+
constructor(anchorId: string, parent: S3StorageService) {
7+
super(anchorId, parent);
8+
}
9+
}

packages/octo-aws-cdk/src/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import './anchors/iam-role.anchor.js';
22
import './anchors/iam-user.anchor.js';
3+
import './anchors/s3-directory.anchor.js';
34

45
import './factories/aws/ec2.aws.factory.js';
56
import './factories/aws/ecr.aws.factory.js';
@@ -42,9 +43,7 @@ import './models/service/s3-static-website/actions/update-source-paths-s3-static
4243

4344
export { S3StorageAccess, S3StorageService } from './models/service/s3-storage/s3-storage.service.model.js';
4445
import './models/service/s3-storage/actions/add-s3-storage.model.action.js';
45-
import './models/service/s3-storage/actions/add-s3-storage-access.overlay.action.js';
4646
import './models/service/s3-storage/actions/delete-s3-storage.model.action.js';
47-
import './models/service/s3-storage/actions/delete-s3-storage-access.overlay.action.js';
4847

4948
import './models/subnet/actions/add-subnet.model.action.js';
5049
import './models/subnet/actions/delete-subnet.model.action.js';
@@ -53,6 +52,8 @@ import './models/subnet/actions/update-subnet-association.model.action.js';
5352
export { AwsRegionSharedEfsModule } from './modules/aws-region-shared-efs.module.js';
5453
export { S3WebsiteSaveManifestModule } from './modules/s3-website-save-manifest.module.js';
5554

55+
import './overlays/s3-storage-access/actions/add-s3-storage-access.overlay.action.js';
56+
import './overlays/s3-storage-access/actions/delete-s3-storage-access.overlay.action.js';
5657
import './overlays/security-group/actions/add-security-group.overlay.action.js';
5758
import './overlays/security-group/actions/update-security-group.overlay.action.js';
5859

packages/octo-aws-cdk/src/models/service/s3-storage/s3-storage-access.overlay.ts

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/octo-aws-cdk/src/models/service/s3-storage/s3-storage.service.model.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Container, Model, OverlayService, Service } from '@quadnix/octo';
2+
import { IamRoleAnchor } from '../../../anchors/iam-role.anchor.js';
3+
import { S3DirectoryAnchor } from '../../../anchors/s3-directory.anchor.js';
24
import { CommonUtility } from '../../../utilities/common/common.utility.js';
35
import { AwsRegion, RegionId } from '../../region/aws.region.model.js';
46
import { AwsServer } from '../../server/aws.server.model.js';
5-
import { S3StorageAccessOverlay } from './s3-storage-access.overlay.js';
7+
import { S3StorageAccessOverlay } from '../../../overlays/s3-storage-access/s3-storage-access.overlay.js';
68
import { IS3StorageService } from './s3-storage.service.interface.js';
79

810
export enum S3StorageAccess {
@@ -17,7 +19,7 @@ export class S3StorageService extends Service {
1719

1820
readonly bucketName: string;
1921

20-
readonly directories: { remoteDirectoryPath: string }[] = [];
22+
readonly directories: { directoryAnchorName: string; remoteDirectoryPath: string }[] = [];
2123

2224
constructor(regionId: RegionId, bucketName: string) {
2325
super(`${bucketName}-s3-storage`);
@@ -31,7 +33,11 @@ export class S3StorageService extends Service {
3133
throw new Error('Remote directory already added in S3 bucket!');
3234
}
3335

34-
this.directories.push({ remoteDirectoryPath });
36+
const directoryAnchorName = `${CommonUtility.hash(remoteDirectoryPath).substring(0, 12)}Directory`;
37+
const directoryAnchor = new S3DirectoryAnchor(directoryAnchorName, this);
38+
this.anchors.push(directoryAnchor);
39+
40+
this.directories.push({ directoryAnchorName, remoteDirectoryPath });
3541
}
3642

3743
async allowDirectoryAccess(
@@ -44,15 +50,16 @@ export class S3StorageService extends Service {
4450
throw new Error('Cannot find remote directory!');
4551
}
4652

47-
const principal = server.getAnchors()[0];
48-
const overlayId = CommonUtility.hash(principal.anchorId, directory.remoteDirectoryPath, accessLevel);
49-
5053
const allowRead = accessLevel === S3StorageAccess.READ || accessLevel === S3StorageAccess.READ_WRITE;
5154
const allowWrite = accessLevel === S3StorageAccess.WRITE || accessLevel === S3StorageAccess.READ_WRITE;
5255
if (!allowRead && !allowWrite) {
5356
return;
5457
}
5558

59+
const directoryAnchor = this.anchors.find((a) => a.anchorId === directory.directoryAnchorName)!;
60+
const serverAnchor = server.getAnchors().find((a) => a instanceof IamRoleAnchor)!;
61+
const overlayId = CommonUtility.hash(serverAnchor.anchorId, directory.remoteDirectoryPath, accessLevel);
62+
5663
const overlayService = await Container.get(OverlayService);
5764
const s3StorageAccessOverlay = new S3StorageAccessOverlay(
5865
overlayId,
@@ -62,7 +69,7 @@ export class S3StorageService extends Service {
6269
bucketName: this.bucketName,
6370
remoteDirectoryPath: directory.remoteDirectoryPath,
6471
},
65-
[principal],
72+
[serverAnchor, directoryAnchor],
6673
);
6774
await overlayService.addOverlay(s3StorageAccessOverlay);
6875
}
@@ -77,15 +84,12 @@ export class S3StorageService extends Service {
7784
throw new Error('Cannot find remote directory!');
7885
}
7986

80-
const principal = server.getAnchors()[0];
81-
const overlayId = CommonUtility.hash(principal.anchorId, directory.remoteDirectoryPath, accessLevel);
87+
const serverAnchor = server.getAnchors().find((a) => a instanceof IamRoleAnchor)!;
88+
const overlayId = CommonUtility.hash(serverAnchor.anchorId, directory.remoteDirectoryPath, accessLevel);
8289

8390
const overlayService = await Container.get(OverlayService);
8491
const overlay = await overlayService.getOverlayById(overlayId);
85-
if (!overlay) {
86-
throw new Error('Cannot find overlay!');
87-
}
88-
await overlayService.removeOverlay(overlay);
92+
await overlayService.removeOverlay(overlay!);
8993
}
9094

9195
async removeDirectory(remoteDirectoryPath: string): Promise<void> {

packages/octo-aws-cdk/src/models/service/s3-storage/actions/add-s3-storage-access.overlay.action.ts renamed to packages/octo-aws-cdk/src/overlays/s3-storage-access/actions/add-s3-storage-access.overlay.action.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Action, ActionInputs, ActionOutputs, Diff, DiffAction, Factory, IModelAction, ModelType } from '@quadnix/octo';
2-
import { IamRoleAnchor } from '../../../../anchors/iam-role.anchor.js';
3-
import { IamRole } from '../../../../resources/iam/iam-role.resource.js';
2+
import { IamRoleAnchor } from '../../../anchors/iam-role.anchor.js';
3+
import { IamRole } from '../../../resources/iam/iam-role.resource.js';
44
import { S3StorageAccessOverlay } from '../s3-storage-access.overlay.js';
55

66
@Action(ModelType.OVERLAY)
@@ -16,7 +16,9 @@ export class AddS3StorageAccessOverlayAction implements IModelAction {
1616

1717
filter(diff: Diff): boolean {
1818
return (
19-
diff.action === DiffAction.ADD && diff.model.MODEL_NAME === 's3-storage-access' && diff.field === 'overlayId'
19+
diff.action === DiffAction.ADD &&
20+
diff.model.MODEL_NAME === 's3-storage-access-overlay' &&
21+
diff.field === 'overlayId'
2022
);
2123
}
2224

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Action, ActionInputs, ActionOutputs, Diff, DiffAction, Factory, IModelAction, ModelType } from '@quadnix/octo';
2-
import { IamRoleAnchor } from '../../../../anchors/iam-role.anchor.js';
3-
import { IamRole } from '../../../../resources/iam/iam-role.resource.js';
2+
import { IamRoleAnchor } from '../../../anchors/iam-role.anchor.js';
3+
import { IamRole } from '../../../resources/iam/iam-role.resource.js';
44
import { S3StorageAccessOverlay } from '../s3-storage-access.overlay.js';
55

66
@Action(ModelType.OVERLAY)
@@ -16,7 +16,9 @@ export class DeleteS3StorageAccessOverlayAction implements IModelAction {
1616

1717
filter(diff: Diff): boolean {
1818
return (
19-
diff.action === DiffAction.DELETE && diff.model.MODEL_NAME === 's3-storage-access' && diff.field === 'overlayId'
19+
diff.action === DiffAction.DELETE &&
20+
diff.model.MODEL_NAME === 's3-storage-access-overlay' &&
21+
diff.field === 'overlayId'
2022
);
2123
}
2224

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { AOverlay, IOverlay, IResource, Overlay } from '@quadnix/octo';
2+
import { IamRoleAnchor } from '../../anchors/iam-role.anchor.js';
3+
import { S3DirectoryAnchor } from '../../anchors/s3-directory.anchor.js';
4+
import { IS3StorageAccessOverlayProperties } from './s3-storage-access.overlay.interface.js';
5+
6+
@Overlay()
7+
export class S3StorageAccessOverlay extends AOverlay<S3StorageAccessOverlay> {
8+
override readonly MODEL_NAME: string = 's3-storage-access-overlay';
9+
10+
constructor(
11+
overlayId: IOverlay['overlayId'],
12+
properties: IS3StorageAccessOverlayProperties,
13+
anchors: [IamRoleAnchor, S3DirectoryAnchor],
14+
) {
15+
super(overlayId, properties as unknown as IResource['properties'], anchors);
16+
}
17+
}

packages/octo-aws-cdk/src/resources/iam/actions/update-iam-role-with-s3-storage-policy.resource.action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
IAMClient,
77
} from '@aws-sdk/client-iam';
88
import { Action, Container, Diff, DiffAction, Factory, IResourceAction, ModelType } from '@quadnix/octo';
9-
import { IS3StorageAccessOverlayProperties } from '../../../models/service/s3-storage/s3-storage-access.overlay.interface.js';
9+
import { IS3StorageAccessOverlayProperties } from '../../../overlays/s3-storage-access/s3-storage-access.overlay.interface.js';
1010
import { IIamRoleResponse } from '../iam-role.interface.js';
1111
import { IamRole, IamRolePolicyDiff } from '../iam-role.resource.js';
1212

@@ -18,7 +18,7 @@ export class UpdateIamRoleWithS3StoragePolicyResourceAction implements IResource
1818
return (
1919
diff.action === DiffAction.UPDATE &&
2020
diff.model.MODEL_NAME === 'iam-role' &&
21-
(diff.value as IamRolePolicyDiff['key']).overlay.MODEL_NAME === 's3-storage-access'
21+
(diff.value as IamRolePolicyDiff['key']).overlay.MODEL_NAME === 's3-storage-access-overlay'
2222
);
2323
}
2424

packages/octo-aws-cdk/src/resources/iam/actions/update-iam-user-with-s3-storage-policy.resource.action.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
IAMClient,
77
} from '@aws-sdk/client-iam';
88
import { Action, Container, Diff, DiffAction, Factory, IResourceAction, ModelType } from '@quadnix/octo';
9-
import { IS3StorageAccessOverlayProperties } from '../../../models/service/s3-storage/s3-storage-access.overlay.interface.js';
9+
import { IS3StorageAccessOverlayProperties } from '../../../overlays/s3-storage-access/s3-storage-access.overlay.interface.js';
1010
import { IIamUserResponse } from '../iam-user.interface.js';
1111
import { IamUser, IamUserPolicyDiff } from '../iam-user.resource.js';
1212

@@ -18,7 +18,7 @@ export class UpdateIamUserWithS3StoragePolicyResourceAction implements IResource
1818
return (
1919
diff.action === DiffAction.UPDATE &&
2020
diff.model.MODEL_NAME === 'iam-user' &&
21-
(diff.value as IamUserPolicyDiff['key']).overlay.MODEL_NAME === 's3-storage-access'
21+
(diff.value as IamUserPolicyDiff['key']).overlay.MODEL_NAME === 's3-storage-access-overlay'
2222
);
2323
}
2424

0 commit comments

Comments
 (0)