Skip to content

Commit 83292f6

Browse files
committed
feat: Add GCS Authorized buckets list
1 parent 39de076 commit 83292f6

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

lib/actions/google/gcs/google_cloud_storage.js

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,29 @@ class GoogleCloudStorageAction extends Hub.Action {
3838
required: true,
3939
sensitive: false,
4040
description: "The Project Id for your GCS project from https://console.cloud.google.com/apis/credentials",
41-
},
41+
}, {
42+
name: "authorized_buckets",
43+
label: "Authorized Buckets",
44+
required: true,
45+
sensitive: false,
46+
description: "List of authorized Buckets for the Users (semicolon separated)",
47+
}
4248
];
4349
}
4450
async execute(request) {
4551
const response = new Hub.ActionResponse();
4652
if (!request.formParams.bucket) {
53+
const selectedBucket = request.formParams.bucket;
54+
const authorizedBuckets = (request.params.authorized_buckets || "")
55+
.split(";")
56+
.map((s) => s.trim());
57+
if (!authorizedBuckets.includes(selectedBucket)) {
58+
const error = (0, action_response_1.errorWith)(http_errors_1.HTTP_ERROR.bad_request, `${LOG_PREFIX} Selected bucket "${selectedBucket}" is not in the list of authorized buckets.`);
59+
response.success = false;
60+
response.error = error;
61+
response.message = error.message;
62+
return response;
63+
}
4764
const error = (0, action_response_1.errorWith)(http_errors_1.HTTP_ERROR.bad_request, `${LOG_PREFIX} needs a GCS bucket specified.`);
4865
response.success = false;
4966
response.error = error;
@@ -118,16 +135,26 @@ class GoogleCloudStorageAction extends Hub.Action {
118135
winston.error(`${LOG_PREFIX} No buckets in account`, { webhookId: request.webhookId });
119136
return form;
120137
}
121-
const buckets = results[0];
138+
const allBuckets = results[0];
139+
const authorizedBuckets = (request.params.authorized_buckets || "")
140+
.split(";")
141+
.map((s) => s.trim())
142+
.filter((s) => s.length > 0);
143+
const filteredBuckets = allBuckets.filter((b) => authorizedBuckets.includes(b.name));
144+
if (filteredBuckets.length === 0) {
145+
form.error = "None of the authorized buckets were found in your GCS account.";
146+
winston.error(`${LOG_PREFIX} No authorized buckets found`, { webhookId: request.webhookId });
147+
return form;
148+
}
122149
form.fields = [{
123150
label: "Bucket",
124151
name: "bucket",
125152
required: true,
126-
options: buckets.map((b) => {
153+
options: filteredBuckets.map((b) => {
127154
return { name: b.id, label: b.name };
128155
}),
156+
default: filteredBuckets[0].id,
129157
type: "select",
130-
default: buckets[0].id,
131158
}, {
132159
label: "Filename",
133160
name: "filename",

src/actions/google/gcs/google_cloud_storage.ts

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,37 @@ export class GoogleCloudStorageAction extends Hub.Action {
3737
required: true,
3838
sensitive: false,
3939
description: "The Project Id for your GCS project from https://console.cloud.google.com/apis/credentials",
40-
},
40+
}, {
41+
name: "authorized_buckets",
42+
label: "Authorized Buckets",
43+
required: true,
44+
sensitive: false,
45+
description: "List of authorized Buckets for the Users (semicolon separated)",
46+
}
4147
]
4248

4349
async execute(request: Hub.ActionRequest) {
4450
const response = new Hub.ActionResponse()
4551

4652
if (!request.formParams.bucket) {
53+
const selectedBucket = request.formParams.bucket!
54+
const authorizedBuckets = (request.params.authorized_buckets || "")
55+
.split(";")
56+
.map((s: string) => s.trim())
57+
58+
if (!authorizedBuckets.includes(selectedBucket)) {
59+
const error: Error = errorWith(
60+
HTTP_ERROR.bad_request,
61+
`${LOG_PREFIX} Selected bucket "${selectedBucket}" is not in the list of authorized buckets.`
62+
)
63+
response.success = false
64+
response.error = error
65+
response.message = error.message
66+
return response
67+
}
4768
const error: Error = errorWith(
48-
HTTP_ERROR.bad_request,
49-
`${LOG_PREFIX} needs a GCS bucket specified.`,
69+
HTTP_ERROR.bad_request,
70+
`${LOG_PREFIX} needs a GCS bucket specified.`,
5071
)
5172
response.success = false
5273
response.error = error
@@ -131,8 +152,8 @@ export class GoogleCloudStorageAction extends Hub.Action {
131152
132153
Google SDK Error: "${e.message}"`
133154
winston.error(
134-
`${LOG_PREFIX} An error occurred while fetching the bucket list. Google SDK Error: ${e.message} `,
135-
{webhookId: request.webhookId},
155+
`${LOG_PREFIX} An error occurred while fetching the bucket list. Google SDK Error: ${e.message} `,
156+
{webhookId: request.webhookId},
136157
)
137158
return form
138159
}
@@ -143,17 +164,31 @@ export class GoogleCloudStorageAction extends Hub.Action {
143164
return form
144165
}
145166

146-
const buckets = results[0]
167+
const allBuckets = results[0]
168+
const authorizedBuckets = (request.params.authorized_buckets || "")
169+
.split(";")
170+
.map((s: string) => s.trim())
171+
.filter((s: string) => s.length > 0)
172+
173+
const filteredBuckets = allBuckets.filter((b: any) =>
174+
authorizedBuckets.includes(b.name)
175+
)
176+
177+
if (filteredBuckets.length === 0) {
178+
form.error = "None of the authorized buckets were found in your GCS account."
179+
winston.error(`${LOG_PREFIX} No authorized buckets found`, { webhookId: request.webhookId })
180+
return form
181+
}
147182

148183
form.fields = [{
149184
label: "Bucket",
150185
name: "bucket",
151186
required: true,
152-
options: buckets.map((b: any) => {
153-
return {name: b.id, label: b.name}
154-
}),
187+
options: filteredBuckets.map((b: any) => {
188+
return { name: b.id, label: b.name }
189+
}),
190+
default: filteredBuckets[0].id,
155191
type: "select",
156-
default: buckets[0].id,
157192
}, {
158193
label: "Filename",
159194
name: "filename",
@@ -164,7 +199,7 @@ export class GoogleCloudStorageAction extends Hub.Action {
164199
options: [{label: "Yes", name: "yes"}, {label: "No", name: "no"}],
165200
default: "yes",
166201
description: "If Overwrite is enabled, will use the title or filename and overwrite existing data." +
167-
" If disabled, a date time will be appended to the name to make the file unique.",
202+
" If disabled, a date time will be appended to the name to make the file unique.",
168203
}]
169204

170205
return form

0 commit comments

Comments
 (0)