Skip to content

Commit

Permalink
fix(providers): aws credentials (#18812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian committed Nov 17, 2023
1 parent 3ba8a83 commit b77b41c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
16 changes: 13 additions & 3 deletions packages/providers/upload-aws-s3/src/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const defaultOptions = {

describe('Utils', () => {
describe('Extract credentials for V4 different aws provider configurations', () => {
test('[Legacy] Credentials directly in the options', async () => {
test('[Legacy] Credentials directly in the options', () => {
const options: InitOptions = {
accessKeyId,
secretAccessKey,
Expand All @@ -30,7 +30,7 @@ describe('Utils', () => {
});
});

test('[Legacy] credentials directly in s3Options', async () => {
test('[Legacy] credentials directly in s3Options', () => {
const options: InitOptions = {
s3Options: {
accessKeyId,
Expand All @@ -46,7 +46,7 @@ describe('Utils', () => {
});
});

test('Credentials in credentials object inside s3Options', async () => {
test('Credentials in credentials object inside s3Options', () => {
const options: InitOptions = {
s3Options: {
credentials: {
Expand All @@ -63,5 +63,15 @@ describe('Utils', () => {
secretAccessKey,
});
});
test('Does not throw an error when credentials are not present', () => {
const options: InitOptions = {
s3Options: {
...defaultOptions,
},
};
const credentials = extractCredentials(options);

expect(credentials).toEqual(null);
});
});
});
4 changes: 2 additions & 2 deletions packages/providers/upload-aws-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ const getConfig = ({ baseUrl, rootPath, s3Options, ...legacyS3Options }: InitOpt
"S3 configuration options passed at root level of the plugin's providerOptions is deprecated and will be removed in a future release. Please wrap them inside the 's3Options:{}' property."
);
}

const credentials = extractCredentials({ s3Options, ...legacyS3Options });
const config = {
...s3Options,
...legacyS3Options,
credentials: extractCredentials({ s3Options, ...legacyS3Options }),
...(credentials ? { credentials } : {}),
};

config.params.ACL = getOr(ObjectCannedACL.public_read, ['params', 'ACL'], config);
Expand Down
4 changes: 2 additions & 2 deletions packages/providers/upload-aws-s3/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function getBucketFromAwsUrl(fileUrl: string): BucketInfo {
}

// TODO Remove this in V5 since we will only support the new config structure
export const extractCredentials = (options: InitOptions): AwsCredentialIdentity => {
export const extractCredentials = (options: InitOptions): AwsCredentialIdentity | null => {
// legacy
if (options.accessKeyId && options.secretAccessKey) {
return {
Expand All @@ -115,5 +115,5 @@ export const extractCredentials = (options: InitOptions): AwsCredentialIdentity
};
}

throw new Error("Couldn't find AWS credentials.");
return null;
};

0 comments on commit b77b41c

Please sign in to comment.