Rename AwsCredentialsProvider.getCredentials -> AwsCredentialsManager.getCredentialProvider

Signed-off-by: Clare Liguori <liguori@amazon.com>
This commit is contained in:
Clare Liguori
2022-12-01 12:06:32 -08:00
parent e40790d0c2
commit f248b75bde
8 changed files with 211 additions and 184 deletions
@@ -28,9 +28,9 @@ import { getVoidLogger } from '@backstage/backend-common';
import { Entity, DEFAULT_NAMESPACE } from '@backstage/catalog-model';
import { ConfigReader } from '@backstage/config';
import {
AwsCredentials,
AwsCredentialsProviderOptions,
DefaultAwsCredentialsProvider,
AwsCredentialProvider,
AwsCredentialProviderOptions,
DefaultAwsCredentialsManager,
} from '@backstage/integration-aws-node';
import { mockClient, AwsClientStub } from 'aws-sdk-client-mock';
import express from 'express';
@@ -45,9 +45,9 @@ import { Readable } from 'stream';
const env = process.env;
let s3Mock: AwsClientStub<S3Client>;
function getMockCredentials(): Promise<AwsCredentials> {
function getMockCredentialProvider(): Promise<AwsCredentialProvider> {
return Promise.resolve({
provider: async () => {
sdkCredentialProvider: async () => {
return Promise.resolve({
accessKeyId: 'MY_ACCESS_KEY_ID',
secretAccessKey: 'MY_SECRET_ACCESS_KEY',
@@ -55,9 +55,9 @@ function getMockCredentials(): Promise<AwsCredentials> {
},
});
}
const credsProviderMock = jest.spyOn(
DefaultAwsCredentialsProvider.prototype,
'getCredentials',
const getCredProviderMock = jest.spyOn(
DefaultAwsCredentialsManager.prototype,
'getCredentialProvider',
);
const getEntityRootDir = (entity: Entity) => {
@@ -178,8 +178,8 @@ describe('AwsS3Publish', () => {
process.env.AWS_REGION = 'us-west-2';
jest.resetAllMocks();
credsProviderMock.mockImplementation((_?: AwsCredentialsProviderOptions) =>
getMockCredentials(),
getCredProviderMock.mockImplementation((_?: AwsCredentialProviderOptions) =>
getMockCredentialProvider(),
);
mockFs({
@@ -249,10 +249,10 @@ describe('AwsS3Publish', () => {
describe('buildCredentials', () => {
it('should retrieve credentials for a specific account ID', async () => {
await createPublisherFromConfig();
expect(credsProviderMock).toHaveBeenCalledWith({
expect(getCredProviderMock).toHaveBeenCalledWith({
accountId: '111111111111',
});
expect(credsProviderMock).toHaveBeenCalledTimes(1);
expect(getCredProviderMock).toHaveBeenCalledTimes(1);
});
it('should retrieve default credentials when no config is present', async () => {
@@ -268,8 +268,8 @@ describe('AwsS3Publish', () => {
});
await AwsS3Publish.fromConfig(mockConfig, logger);
expect(credsProviderMock).toHaveBeenCalledWith();
expect(credsProviderMock).toHaveBeenCalledTimes(1);
expect(getCredProviderMock).toHaveBeenCalledWith();
expect(getCredProviderMock).toHaveBeenCalledTimes(1);
});
it('should fall back to deprecated method of retrieving credentials', async () => {
@@ -290,7 +290,7 @@ describe('AwsS3Publish', () => {
});
await AwsS3Publish.fromConfig(mockConfig, logger);
expect(credsProviderMock).toHaveBeenCalledTimes(0);
expect(getCredProviderMock).toHaveBeenCalledTimes(0);
});
});
@@ -17,8 +17,8 @@ import { Entity, CompoundEntityRef } from '@backstage/catalog-model';
import { Config } from '@backstage/config';
import { assertError, ForwardedError } from '@backstage/errors';
import {
AwsCredentialsProvider,
DefaultAwsCredentialsProvider,
AwsCredentialsManager,
DefaultAwsCredentialsManager,
} from '@backstage/integration-aws-node';
import {
GetObjectCommand,
@@ -136,9 +136,9 @@ export class AwsS3Publish implements PublisherBase {
const credentialsConfig = config.getOptionalConfig(
'techdocs.publisher.awsS3.credentials',
);
const credsProvider = DefaultAwsCredentialsProvider.fromConfig(config);
const credentials = await AwsS3Publish.buildCredentials(
credsProvider,
const credsManager = DefaultAwsCredentialsManager.fromConfig(config);
const sdkCredentialProvider = await AwsS3Publish.buildCredentials(
credsManager,
accountId,
credentialsConfig,
region,
@@ -158,7 +158,7 @@ export class AwsS3Publish implements PublisherBase {
const storageClient = new S3Client({
customUserAgent: 'backstage-aws-techdocs-s3-publisher',
credentialDefaultProvider: () => credentials,
credentialDefaultProvider: () => sdkCredentialProvider,
...(region && { region }),
...(endpoint && { endpoint }),
...(s3ForcePathStyle && { s3ForcePathStyle }),
@@ -192,20 +192,21 @@ export class AwsS3Publish implements PublisherBase {
}
private static async buildCredentials(
credsProvider: AwsCredentialsProvider,
credsManager: AwsCredentialsManager,
accountId?: string,
config?: Config,
region?: string,
): Promise<AwsCredentialIdentityProvider> {
// Pull credentials for the specified account ID from the 'aws' config section
if (accountId) {
return (await credsProvider.getCredentials({ accountId })).provider;
return (await credsManager.getCredentialProvider({ accountId }))
.sdkCredentialProvider;
}
// Fall back to the default credential chain if neither account ID
// nor explicit credentials are provided
if (!config) {
return (await credsProvider.getCredentials()).provider;
return (await credsManager.getCredentialProvider()).sdkCredentialProvider;
}
// Pull credentials from the techdocs config section (deprecated)
@@ -214,7 +215,7 @@ export class AwsS3Publish implements PublisherBase {
const explicitCredentials: AwsCredentialIdentityProvider =
accessKeyId && secretAccessKey
? AwsS3Publish.buildStaticCredentials(accessKeyId, secretAccessKey)
: (await credsProvider.getCredentials()).provider;
: (await credsManager.getCredentialProvider()).sdkCredentialProvider;
const roleArn = config.getOptionalString('roleArn');
if (roleArn) {