diff --git a/docs/features/techdocs/configuration.md b/docs/features/techdocs/configuration.md index 869febdf5c..1190c219a8 100644 --- a/docs/features/techdocs/configuration.md +++ b/docs/features/techdocs/configuration.md @@ -76,7 +76,12 @@ techdocs: awsS3: # An API key is required to write to a storage bucket. credentials: - $file: '/path/to/aws_application_credentials.json', + accessKeyId: + $env: TECHDOCS_AWSS3_ACCESS_KEY_ID_CREDENTIAL + secretAccessKey: + $env: TECHDOCS_AWSS3_SECRET_ACCESS_KEY_CREDENTIAL + region: + $env: AWSS3_REGION # AWS S3 Bucket Name bucketName: 'techdocs-storage', diff --git a/docs/features/techdocs/using-cloud-storage.md b/docs/features/techdocs/using-cloud-storage.md index c1cea9c91b..a1bbcc6bbb 100644 --- a/docs/features/techdocs/using-cloud-storage.md +++ b/docs/features/techdocs/using-cloud-storage.md @@ -236,18 +236,18 @@ Create an inline policy for the TechDocs user by using the following policy: See more details in the section [Working with Inline Policies](https://docs.aws.amazon.com/IAM/latest/UserGuide/access_policies_manage.html). -Now you need to create your credentials file (`.json`) with the `TechDocs` User -credentials: +Now you need to fill in the environment variables with the `TechDocs` User +credentials. You can also specify a region if you want to accesses the resources +in a specific region. Otherwise no region will be selected by default. -```json -{ - "accessKeyId": "TECHDOCS_USER_ACCESS_KEY_ID", - "secretAccessKey": "TECHDOCS_USER_SECRET_ACCESS_KEY" -} +```properties +TECHDOCS_AWSS3_ACCESS_KEY_ID_CREDENTIAL="TECHDOCS_ACCESS_KEY_ID" +TECHDOCS_AWSS3_SECRET_ACCESS_KEY_CREDENTIAL="TECHDOCS_SECRET_ACCESS_KEY" +AWSS3_REGION="" // Optional ``` Make it available in your Backstage server and/or your local development server -and set it in the app config techdocs.publisher.awsS3.credentials. +and set it in the app config techdocs.publisher.awsS3. ```yaml techdocs: @@ -255,7 +255,12 @@ techdocs: type: 'awsS3' awsS3: credentials: - $file: '/path/to/aws_application_credentials.json' + accessKeyId: + $env: TECHDOCS_AWSS3_ACCESS_KEY_ID_CREDENTIAL + secretAccessKey: + $env: TECHDOCS_AWSS3_SECRET_ACCESS_KEY_CREDENTIAL + region: + $env: AWSS3_REGION ``` **3. That's it!** diff --git a/packages/techdocs-common/src/stages/publish/awsS3.test.ts b/packages/techdocs-common/src/stages/publish/awsS3.test.ts index 883674b332..06a19625f8 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.test.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.test.ts @@ -45,7 +45,10 @@ beforeEach(() => { publisher: { type: 'awsS3', awsS3: { - credentials: '{}', + credentials: { + accessKeyId: 'accessKeyId', + secretAccessKey: 'secretAccessKey', + }, bucketName: 'bucketName', }, }, diff --git a/packages/techdocs-common/src/stages/publish/awsS3.ts b/packages/techdocs-common/src/stages/publish/awsS3.ts index 959ffb87b3..d0877159dd 100644 --- a/packages/techdocs-common/src/stages/publish/awsS3.ts +++ b/packages/techdocs-common/src/stages/publish/awsS3.ts @@ -21,16 +21,23 @@ import { Entity, EntityName } from '@backstage/catalog-model'; import { Config } from '@backstage/config'; import { getHeadersForFileExtension, getFileTreeRecursively } from './helpers'; import { PublisherBase, PublishRequest } from './types'; -import { CredentialsOptions } from 'aws-sdk/lib/credentials'; import { ManagedUpload } from 'aws-sdk/clients/s3'; import fs from 'fs'; export class AwsS3Publish implements PublisherBase { static fromConfig(config: Config, logger: Logger): PublisherBase { - let credentials = ''; + let region = null; + let accessKeyId = null; + let secretAccessKey = null; let bucketName = ''; try { - credentials = config.getString('techdocs.publisher.awsS3.credentials'); + accessKeyId = config.getString( + 'techdocs.publisher.awsS3.credentials.accessKeyId', + ); + secretAccessKey = config.getString( + 'techdocs.publisher.awsS3.credentials.secretAccessKey', + ); + region = config.getOptionalString('techdocs.publisher.awsS3.region'); bucketName = config.getString('techdocs.publisher.awsS3.bucketName'); } catch (error) { throw new Error( @@ -40,17 +47,9 @@ export class AwsS3Publish implements PublisherBase { ); } - let credentialsJson = {}; - try { - credentialsJson = JSON.parse(credentials); - } catch (err) { - throw new Error( - 'Error in parsing techdocs.publisher.awsS3.credentials config to JSON.', - ); - } - const storageClient = new aws.S3({ - credentials: credentialsJson as CredentialsOptions, + credentials: { accessKeyId, secretAccessKey }, + ...(region && { region }), }); // Check if the defined bucket exists. Being able to connect means the configuration is good diff --git a/packages/techdocs-common/src/stages/publish/publish.test.ts b/packages/techdocs-common/src/stages/publish/publish.test.ts index b3fe98cce6..43c8101651 100644 --- a/packages/techdocs-common/src/stages/publish/publish.test.ts +++ b/packages/techdocs-common/src/stages/publish/publish.test.ts @@ -90,7 +90,10 @@ describe('Publisher', () => { publisher: { type: 'awsS3', awsS3: { - credentials: '{}', + credentials: { + accessKeyId: 'accessKeyId', + secretAccessKey: 'secretAccessKey', + }, bucketName: 'bucketName', }, },