Skip to content

Commit

Permalink
Merge pull request #629 from reg-viz/feat/remove_create_bucket_on_pre…
Browse files Browse the repository at this point in the history
…pare

feat(s3-plugin): Remove prepare option to create bucket
  • Loading branch information
Quramy authored Jan 19, 2024
2 parents 509a8df + 4147b30 commit 6f23123
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 261 deletions.
43 changes: 13 additions & 30 deletions packages/reg-publish-s3-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,9 @@ npm i reg-publish-s3-plugin -D
reg-suit prepare -p publish-s3
```

## AWS Credentials
## Requirements

This plugin needs AWS credentials to access S3. You can set them by the following 2 methods.

### Environment values

```sh
export AWS_ACCESS_KEY_ID=<your-access-key>
export AWS_SECRET_ACCESS_KEY=<your-secret-key>
```

### Create INI file

Create a file at `~/.aws/credentials` and edit it. For example:

```ini
[default]
aws_access_key_id = <your-access-key>
aws_secret_access_key = <your-secret-key>
```
To use this plugin, you need to create an S3 bucket and configure to allow to access it from your CI.

## Configure

Expand All @@ -40,7 +23,7 @@ aws_secret_access_key = <your-secret-key>
sseKMSKeyId?: string;
customDomain?: string;
pathPrefix?: string;
sdkOptions?: S3.Types.ClientConfiguration;
sdkOptions?: S3ClientConfig;
}
```

Expand All @@ -51,19 +34,19 @@ aws_secret_access_key = <your-secret-key>
- `sseKMSKeyId` - _Optional_ - Specify server-side encryption KMS KEY ID. If provided, is passed as SSEKMSKeyId to s3.putObject.
- `customDomain` - _Optional_ - Set if you have your domain and host S3 on it. If set, the HTML report will be published with this custom domain(e.g. `https://your-sub.example.com/...`).
- `pathPrefix` - _Optional_ - Specify paths. For example if you set `some_dir`, the report is published with URL such as `https://your-backet-name.s3.amazonaws.com/some_dir/xxxxxxxxx/index.html`.
- `sdkOptions` - _Optional_ - Specify SDK options to pass to the S3 client. For details about the options, refer to the [AWS JavaScript SDK docs](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details).
- `sdkOptions` - _Optional_ - Specify options to pass to `S3Client` constructor. For details about the options, refer to the [AWS JavaScript SDK docs](https://www.npmjs.com/package/@aws-sdk/client-s3#usage).

## IAM Role Policy

This plugin needs follwings role policy.
This plugin needs following role policy.

```
"Action": [
"s3:DeleteObject",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:ListBucket"
]
"Action": [
"s3:DeleteObject",
"s3:GetObject",
"s3:GetObjectAcl",
"s3:PutObject",
"s3:PutObjectAcl",
"s3:ListObject"
]
```
7 changes: 0 additions & 7 deletions packages/reg-publish-s3-plugin/e2e/clean-up-buckets.sh

This file was deleted.

Binary file not shown.
Binary file not shown.
16 changes: 0 additions & 16 deletions packages/reg-publish-s3-plugin/e2e/report-fixture/index.html

This file was deleted.

122 changes: 0 additions & 122 deletions packages/reg-publish-s3-plugin/e2e/script.ts

This file was deleted.

2 changes: 0 additions & 2 deletions packages/reg-publish-s3-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
"main": "lib/index.js",
"scripts": {
"prepublish": "tsc -p tsconfig.build.json",
"clean:e2e": "rimraf e2e/report-fixture-expected",
"test.bk": "npm run clean:e2e && ts-node e2e/script.ts",
"test": "echo skip"
},
"keywords": [
Expand Down
91 changes: 8 additions & 83 deletions packages/reg-publish-s3-plugin/src/s3-bucket-preparer.ts
Original file line number Diff line number Diff line change
@@ -1,104 +1,29 @@
import { v4 as uuid } from "uuid";
import { CreateBucketCommand, PutBucketPolicyCommand, S3Client } from "@aws-sdk/client-s3";
import { PluginPreparer, PluginCreateOptions, PluginLogger } from "reg-suit-interface";
import { PluginConfig } from "./s3-publisher-plugin";

export interface SetupInquireResult {
createBucket: boolean;
bucketName?: string;
bucketName: string;
}

function createPolicy(bucketName: string) {
return {
Version: "2012-10-17",
Id: "Policy1498486961145",
Statement: [
{
Sid: "Stmt1498486956732",
Effect: "Allow",
Principal: "*",
Action: "s3:GetObject",
Resource: `arn:aws:s3:::${bucketName}/*`,
},
],
};
}

const BUCKET_PREFIX = "reg-publish-bucket";

export class S3BucketPreparer implements PluginPreparer<SetupInquireResult, PluginConfig> {
private _s3client = new S3Client();
_logger!: PluginLogger;

inquire() {
return [
{
name: "createBucket",
type: "confirm",
message: "Create a new S3 bucket",
default: true,
},
{
name: "bucketName",
type: "input",
message: "Existing bucket name",
when: (ctx: any) => !(ctx as { createBucket: boolean }).createBucket,
message: "Bucket name",
},
];
}

prepare(config: PluginCreateOptions<SetupInquireResult>) {
async prepare(config: PluginCreateOptions<SetupInquireResult>) {
this._logger = config.logger;
const ir = config.options;
if (!ir.createBucket) {
return Promise.resolve({
bucketName: ir.bucketName as string,
});
} else {
const id = uuid();
const bucketName = `${BUCKET_PREFIX}-${id}`;
if (config.noEmit) {
this._logger.info(`Skip to create S3 bucket ${bucketName} because noEmit option.`);
return Promise.resolve({ bucketName });
}
this._logger.info(`Create new S3 bucket: ${this._logger.colors.magenta(bucketName)}`);
const spinner = this._logger.getSpinner(`creating bucket...`);
spinner.start();
return this._createBucket(bucketName)
.then(bucketName => {
return this._updatePolicy(bucketName);
})
.then(bucketName => {
spinner.stop();
return { bucketName };
});
}
}

_updatePolicy(bucketName: string) {
return new Promise<string>((resolve, reject) => {
this._s3client
.send(
new PutBucketPolicyCommand({
Bucket: bucketName,
Policy: JSON.stringify(createPolicy(bucketName)),
}),
)
.then(() => resolve(bucketName))
.catch(err => reject(err));
});
}

_createBucket(bucketName: string) {
return new Promise<string>((resolve, reject) => {
this._s3client
.send(
new CreateBucketCommand({
Bucket: bucketName,
}),
)
.then(() => resolve(bucketName))
.catch(err => reject(err));
});
const { bucketName } = config.options;
const pluginConfig: PluginConfig = {
bucketName,
};
return pluginConfig;
}
}
2 changes: 1 addition & 1 deletion packages/reg-suit-core-testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"reg-notify-gitlab-plugin": "^0.12.3",
"reg-notify-slack-plugin": "^0.12.2",
"reg-publish-gcs-plugin": "^0.12.2",
"reg-publish-s3-plugin": "^0.12.2",
"reg-simple-keygen-plugin": "^0.12.2",
"reg-suit-core": "^0.12.2"
},
Expand Down

0 comments on commit 6f23123

Please sign in to comment.