Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update to s3 assume role #62

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,5 @@ NEXT_PUBLIC_USER_POOL_CLIENT_ID=
GITHUB_PRIVATE_KEY=
AWS_REGION=
NEXT_PUBLIC_AWS_S3_BUCKET_NAME=
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
NEXT_PUBLIC_ENABLE_THUMBNAIL_UPLOAD=
AWS_ASSUME_ROLE_ARN=
NEXT_PUBLIC_ENABLE_THUMBNAIL_UPLOAD=
3 changes: 0 additions & 3 deletions components/MenuBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ const filteredItems =
)
: items;

console.log(process.env.NEXT_PUBLIC_ENABLE_THUMBNAIL_UPLOAD);
console.log(filteredItems);

const MenuBar = () => {
const pathname = usePathname();

Expand Down
66 changes: 53 additions & 13 deletions utils/s3.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,67 @@
import { S3Client, HeadObjectCommand } from '@aws-sdk/client-s3';
import { STSClient, AssumeRoleCommand } from '@aws-sdk/client-sts';
import { S3RequestPresigner } from '@aws-sdk/s3-request-presigner';
import { HttpRequest } from '@smithy/protocol-http';
import { parseUrl } from '@smithy/url-parser';
import { formatUrl } from '@aws-sdk/util-format-url';
import { Hash } from '@smithy/hash-node';

const s3 = new S3Client({
region: process.env.AWS_REGION!,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID!,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY!,
},
});
const bucketName = process.env.NEXT_PUBLIC_AWS_S3_BUCKET_NAME!;
const region = process.env.AWS_REGION || 'us-west-2';
const RoleArn = process.env.AWS_ASSUME_ROLE_ARN;
const timestamp = Date.now();

const presigner = new S3RequestPresigner({
credentials: s3.config.credentials,
region: process.env.AWS_REGION!,
sha256: Hash.bind(null, 'sha256'),
});
async function assumeRole() {
const sts = new STSClient({ region });

const bucketName = process.env.NEXT_PUBLIC_AWS_S3_BUCKET_NAME!;
const roleParams = {
RoleArn,
RoleSessionName: `veda-ingest-ui-${timestamp}`,
DurationSeconds: 900,
};

const command = new AssumeRoleCommand(roleParams);
console.log({ command });
const response = await sts.send(command);

if (
!response.Credentials ||
!response.Credentials.AccessKeyId ||
!response.Credentials.SecretAccessKey ||
!response.Credentials.SessionToken
) {
throw new Error(
'Failed to assume role: Missing credentials from STS response.'
);
}

return {
accessKeyId: response.Credentials.AccessKeyId,
secretAccessKey: response.Credentials.SecretAccessKey,
sessionToken: response.Credentials.SessionToken,
};
}

async function createS3Client() {
const credentials = await assumeRole();
return new S3Client({
region,
credentials,
});
}

async function createPresigner() {
const credentials = await assumeRole();
return new S3RequestPresigner({
credentials,
region,
sha256: Hash.bind(null, 'sha256'),
});
}

export async function checkFileExists(filename: string): Promise<boolean> {
try {
const s3 = await createS3Client();
await s3.send(new HeadObjectCommand({ Bucket: bucketName, Key: filename }));
return true;
} catch (error: any) {
Expand All @@ -37,6 +76,7 @@ export async function generateSignedUrl(
filename: string,
filetype: string
): Promise<string> {
const presigner = await createPresigner();
const url = parseUrl(
`https://${bucketName}.s3.${process.env.AWS_REGION}.amazonaws.com/${filename}`
);
Expand Down