Skip to content

Commit afae9ba

Browse files
committed
fix issue with deleting buckets and permissions
1 parent 948953e commit afae9ba

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

src/packages/server/compute/cloud/google-cloud/policy.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export async function addBucketPolicyBinding({
7272
});
7373
}
7474

75-
async function getCloudResourceManager() {
75+
export async function getCloudResourceManager() {
7676
const { credentials, projectId } = await getCredentials();
7777
const jwtClient = new JWT({
7878
email: credentials.client_email,
@@ -89,6 +89,15 @@ async function getCloudResourceManager() {
8989
return { cloudresourcemanager, projectId };
9090
}
9191

92+
export async function getProjectNumber(): Promise<number> {
93+
const { cloudresourcemanager, projectId } = await getCloudResourceManager();
94+
const response = await cloudresourcemanager.projects.get({ projectId });
95+
if (response.data.projectNumber == null) {
96+
throw Error("no project number known");
97+
}
98+
return parseInt(response.data.projectNumber);
99+
}
100+
92101
function getBucketPolicyBinding({ serviceAccountId, bucketName, projectId }) {
93102
return {
94103
role: "roles/storage.objectAdmin",

src/packages/server/compute/cloud/google-cloud/storage.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { CreateBucketRequest, Storage } from "@google-cloud/storage";
99
import { StorageTransferServiceClient } from "@google-cloud/storage-transfer";
1010
import { uuid } from "@cocalc/util/misc";
1111
import { getGoogleCloudPrefix } from "./index";
12-
import { addStorageTransferPolicy } from "./policy";
12+
import { addStorageTransferPolicy, getProjectNumber } from "./policy";
1313
import type { GoogleCloudBucketStorageClass } from "@cocalc/util/db-schema/cloud-filesystems";
1414
import { GOOGLE_CLOUD_BUCKET_STORAGE_CLASSES } from "@cocalc/util/db-schema/cloud-filesystems";
1515

@@ -73,11 +73,15 @@ export async function deleteFilesUsingTransferService(
7373
// We fix this by doing just that via the api as given below.
7474
// We only need to do this once, which is why the awkward style of this code,
7575
// where it fails and tries this.
76-
const [serviceAccount] = await transferClient.getGoogleServiceAccount();
77-
const { accountEmail } = serviceAccount;
78-
if (!accountEmail) {
79-
throw Error("unable to get storage transfer service email");
80-
}
76+
// const [serviceAccount] = await transferClient.getGoogleServiceAccount();
77+
// const { accountEmail } = serviceAccount;
78+
// if (!accountEmail) {
79+
// throw Error("unable to get storage transfer service email");
80+
// }
81+
// I can't get the above to work ! -- at least with all the permissions and api's I know about enabling.
82+
// Fortunately the service account email for the transfer client follows a predictable pattern,
83+
// so we just use that:
84+
const accountEmail = `project-${await getProjectNumber()}@storage-transfer-service.iam.gserviceaccount.com`;
8185
await addStorageTransferPolicy(accountEmail);
8286
}
8387

0 commit comments

Comments
 (0)