Skip to content
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion web/src/core/adapters/s3Client/s3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
getNewlyRequestedOrCachedTokenFactory,
createSessionStorageTokenPersistence
} from "core/tools/getNewlyRequestedOrCachedToken";
import { assert, is, type Equals } from "tsafe/assert";
import { assert, is, typeGuard, type Equals } from "tsafe";
import type { Oidc } from "core/ports/Oidc";
import { getS3UriKey, parseS3Uri } from "core/tools/S3Uri";
import { exclude, id } from "tsafe";
Expand Down Expand Up @@ -219,6 +219,60 @@ export function createS3Client(
})();

const s3Client: S3Client = {
getBucketPolicies: async ({ bucket }) => {
const { getAwsS3Client } = await prApi;

const { awsS3Client } = await getAwsS3Client();

const { GetBucketPolicyCommand, S3ServiceException } = await import(
"@aws-sdk/client-s3"
);

let policy: string | undefined;

try {
({ Policy: policy } = await awsS3Client.send(
new GetBucketPolicyCommand({
Bucket: bucket
})
));
} catch (error) {
if (error instanceof S3ServiceException) {
const httpStatusCode = error.$metadata?.httpStatusCode;

if (
httpStatusCode === 403 ||
httpStatusCode === 404 ||
httpStatusCode === 405 ||
httpStatusCode === 501 ||
error.name === "NoSuchBucketPolicy" ||
error.name === "NotImplemented" ||
error.name === "NotSupported"
) {
return undefined;
}
}

throw error;
}

if (policy === undefined) {
return undefined;
}

const bucketPolicies: unknown = JSON.parse(policy);

assert(
typeGuard<S3Client.BucketPolicies>(
bucketPolicies,
typeof bucketPolicies === "object" &&
bucketPolicies !== null &&
!Array.isArray(bucketPolicies)
)
);

return bucketPolicies;
},
getToken: async ({ doForceRenew }) => {
const { getNewlyRequestedOrCachedToken, clearCachedToken } = await prApi;

Expand Down
6 changes: 6 additions & 0 deletions web/src/core/ports/S3Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ export type S3Client = {
errorMessage: string;
}
>;

getBucketPolicies: (params: {
bucket: string;
}) => Promise<S3Client.BucketPolicies | undefined>;
};

export namespace S3Client {
Expand Down Expand Up @@ -86,4 +90,6 @@ export namespace S3Client {
errorCase: "access denied" | "no such bucket";
};
}

export type BucketPolicies = Record<string, unknown>;
}
4 changes: 2 additions & 2 deletions web/src/core/usecases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as userAuthentication from "./userAuthentication";
import * as userProfileForm from "./userProfileForm";
import * as userConfigs from "./userConfigs";
import * as secretsEditor from "./secretsEditor";
import * as s3CodeSnippets from "./s3CodeSnippets";
import * as s3ProfilesDetailsUiController from "./s3ProfilesDetailsUiController";
import * as k8sCodeSnippets from "./k8sCodeSnippets";
import * as vaultCredentials from "./vaultCredentials";
import * as sqlOlapShell from "./sqlOlapShell";
Expand Down Expand Up @@ -40,7 +40,7 @@ export const usecases = {
userProfileForm,
userConfigs,
secretsEditor,
s3CodeSnippets,
s3ProfilesDetailsUiController,
k8sCodeSnippets,
vaultCredentials,
sqlOlapShell,
Expand Down
Loading