aws-sdk#S3Control TypeScript Examples

The following examples show how to use aws-sdk#S3Control. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: handlers.ts    From aws-resource-providers with MIT License 6 votes vote down vote up
private async upsertAccountPublicAccessBlock(action: Action, service: S3Control, logger: Logger, model: ResourceModel, accountId: string): Promise<ResourceModel> {
        const request: S3Control.PutPublicAccessBlockRequest = {
            AccountId: accountId,
            PublicAccessBlockConfiguration: {
                IgnorePublicAcls: model.ignorePublicAcls,
                BlockPublicAcls: model.blockPublicAcls,
                BlockPublicPolicy: model.blockPublicPolicy,
                RestrictPublicBuckets: model.restrictPublicBuckets,
            },
        };

        logger.log({
            action,
            message: 'before invoke putPublicAccessBlock',
            request,
        });
        const response = await service.putPublicAccessBlock(request).promise();
        logger.log({
            action,
            message: 'after invoke putPublicAccessBlock',
            response,
        });

        logger.log({ action, message: 'done', model });
        return model;
    }
Example #2
Source File: handlers.ts    From aws-resource-providers with MIT License 6 votes vote down vote up
@handlerEvent(Action.Delete)
    @commonAws({ serviceName: 'S3Control', debug: true })
    public async delete(action: Action, args: HandlerArgs<ResourceModel>, service: S3Control): Promise<null> {
        const request: S3Control.DeletePublicAccessBlockRequest = {
            AccountId: args.request.awsAccountId,
        };

        args.logger.log({
            action,
            message: 'before invoke deletePublicAccessBlock',
            request,
        });
        const response = await service.deletePublicAccessBlock(request).promise();
        args.logger.log({
            action,
            message: 'after invoke deletePublicAccessBlock',
            response,
        });

        args.logger.log({ action, message: 'done' });

        return Promise.resolve(null);
    }
Example #3
Source File: handlers.ts    From aws-resource-providers with MIT License 6 votes vote down vote up
@handlerEvent(Action.Read)
    @commonAws({ serviceName: 'S3Control', debug: true })
    public async read(action: Action, args: HandlerArgs<ResourceModel>, service: S3Control): Promise<ResourceModel> {
        const accountId = args.request.awsAccountId;

        const request: S3Control.GetPublicAccessBlockRequest = {
            AccountId: accountId,
        };

        args.logger.log({
            action,
            message: 'before invoke getPublicAccessBlock',
            request,
        });
        try {
            const response = await service.getPublicAccessBlock(request).promise();
            args.logger.log({
                action,
                message: 'after invoke getPublicAccessBlock',
                response,
            });

            const publicAccessBlock = response.PublicAccessBlockConfiguration;

            const result = ResourceModel.deserialize(publicAccessBlock);
            result.resourceId = accountId;

            args.logger.log({ action, message: 'done', result });

            return Promise.resolve(result);
        } catch (err) {
            if (err && err.code === 'NoSuchPublicAccessBlockConfiguration') {
                throw new exceptions.NotFound(ResourceModel.TYPE_NAME, request.AccountId);
            } else {
                // Raise the original exception
                throw err;
            }
        }
    }
Example #4
Source File: handlers.ts    From aws-resource-providers with MIT License 5 votes vote down vote up
@handlerEvent(Action.Create)
    @commonAws({ serviceName: 'S3Control', debug: true })
    public async create(action: Action, args: HandlerArgs<ResourceModel>, service: S3Control, model: ResourceModel): Promise<ResourceModel> {
        const accountId = args.request.awsAccountId;
        model.resourceId = accountId;
        return this.upsertAccountPublicAccessBlock(action, service, args.logger, model, accountId);
    }
Example #5
Source File: handlers.ts    From aws-resource-providers with MIT License 5 votes vote down vote up
@handlerEvent(Action.Update)
    @commonAws({ serviceName: 'S3Control', debug: true })
    public async update(action: Action, args: HandlerArgs<ResourceModel>, service: S3Control, model: ResourceModel): Promise<ResourceModel> {
        const accountId = args.request.awsAccountId;
        return this.upsertAccountPublicAccessBlock(action, service, args.logger, model, accountId);
    }
Example #6
Source File: handlers.test.ts    From aws-resource-providers with MIT License 4 votes vote down vote up
describe('when calling handler', () => {
    let testEntrypointPayload: any;
    let spySession: jest.SpyInstance;
    let spySessionClient: jest.SpyInstance;
    let s3control: AwsServiceMockBuilder<S3Control>;
    let fixtureMap: Map<Action, Record<string, any>>;

    beforeAll(() => {
        fixtureMap = new Map<Action, Record<string, any>>();
        fixtureMap.set(Action.Create, createFixture);
        fixtureMap.set(Action.Read, readFixture);
        fixtureMap.set(Action.Update, updateFixture);
        fixtureMap.set(Action.Delete, deleteFixture);
    });

    beforeEach(async () => {
        s3control = on(S3Control, { snapshot: false });
        s3control.mock('getPublicAccessBlock').resolve({
            PublicAccessBlockConfiguration: {
                BlockPublicAcls: true,
            },
        });
        s3control.mock('putPublicAccessBlock').resolve({});
        s3control.mock('deletePublicAccessBlock').resolve({});
        spySession = jest.spyOn(SessionProxy, 'getSession');
        spySessionClient = jest.spyOn<any, any>(SessionProxy.prototype, 'client');
        spySessionClient.mockReturnValue(s3control.instance);
        testEntrypointPayload = {
            credentials: { accessKeyId: '', secretAccessKey: '', sessionToken: '' },
            region: 'us-east-1',
            action: 'CREATE',
        };
    });

    afterEach(() => {
        jest.clearAllMocks();
        jest.restoreAllMocks();
    });

    test('create operation successful - s3 public access block', async () => {
        const request = fixtureMap.get(Action.Create);
        const progress = await resource.testEntrypoint({ ...testEntrypointPayload, action: Action.Create, request }, null);
        expect(progress).toMatchObject({ status: OperationStatus.Success, message: '', callbackDelaySeconds: 0 });
        expect(progress.resourceModel.serialize()).toMatchObject({ ...request.desiredResourceState, ResourceId: IDENTIFIER });
    });

    test('update operation successful - s3 public access block', async () => {
        const request = fixtureMap.get(Action.Update);
        const progress = await resource.testEntrypoint({ ...testEntrypointPayload, action: Action.Update, request }, null);
        expect(progress).toMatchObject({ status: OperationStatus.Success, message: '', callbackDelaySeconds: 0 });
        expect(progress.resourceModel.serialize()).toMatchObject(request.desiredResourceState);
    });

    test('delete operation successful - s3 public access block', async () => {
        const request = fixtureMap.get(Action.Delete);
        const progress = await resource.testEntrypoint({ ...testEntrypointPayload, action: Action.Delete, request }, null);
        expect(progress).toMatchObject({ status: OperationStatus.Success, message: '', callbackDelaySeconds: 0 });
        expect(progress.resourceModel).toBeNull();
    });

    test('read operation successful - s3 public access block', async () => {
        const request = fixtureMap.get(Action.Read);
        const progress = await resource.testEntrypoint({ ...testEntrypointPayload, action: Action.Read, request }, null);
        expect(progress).toMatchObject({ status: OperationStatus.Success, message: '', callbackDelaySeconds: 0 });
        expect(progress.resourceModel.serialize()).toMatchObject(request.desiredResourceState);
    });

    test('read operation fail not found - s3 public access block', async () => {
        const mockGet = s3control.mock('getPublicAccessBlock').reject({ ...new Error(), code: 'NoSuchPublicAccessBlockConfiguration' });
        const request = fixtureMap.get(Action.Read);
        const progress = await resource.testEntrypoint({ ...testEntrypointPayload, action: Action.Read, request }, null);
        expect(progress).toMatchObject({ status: OperationStatus.Failed, errorCode: exceptions.NotFound.name });
        expect(mockGet.mock).toHaveBeenCalledTimes(1);
    });

    test('all operations fail without session - s3 public access block', async () => {
        expect.assertions(fixtureMap.size);
        spySession.mockReturnValue(null);
        for (const [action, request] of fixtureMap) {
            const progress = await resource.testEntrypoint({ ...testEntrypointPayload, action, request }, null);
            expect(progress.errorCode).toBe(exceptions.InvalidCredentials.name);
        }
    });
});