TechDocs/GCS: Enable authentication using environemnt variables

GOOGLE_APPLICATION_CREDENTIALS is the environment variable used by the client. Compute Engine, Google Kubernetes Engine, App Engine, Cloud Run, and Cloud Functions automatically provide a default service account and set this env variable.
https://cloud.google.com/docs/authentication/production
This commit is contained in:
Himanshu Mishra
2021-01-09 16:07:39 +01:00
parent 70b6384c26
commit 1ef5d0f13c
3 changed files with 43 additions and 38 deletions
@@ -27,57 +27,60 @@ import { getHeadersForFileExtension, getFileTreeRecursively } from './helpers';
import { PublisherBase, PublishRequest } from './types';
export class GoogleGCSPublish implements PublisherBase {
static fromConfig(config: Config, logger: Logger): PublisherBase {
let credentials = '';
let projectId = '';
static async fromConfig(
config: Config,
logger: Logger,
): Promise<PublisherBase> {
let bucketName = '';
try {
credentials = config.getString(
'techdocs.publisher.googleGcs.credentials',
);
projectId = config.getString('techdocs.publisher.googleGcs.projectId');
bucketName = config.getString('techdocs.publisher.googleGcs.bucketName');
} catch (error) {
throw new Error(
"Since techdocs.publisher.type is set to 'googleGcs' in your app config, " +
'credentials, projectId and bucketName are required in techdocs.publisher.googleGcs ' +
'required to authenticate with Google Cloud Storage.',
'techdocs.publisher.googleGcs.bucketName is required.',
);
}
// Credentials is an optional config. If missing, default GCS environment variables will be used.
// Read more here https://cloud.google.com/docs/authentication/production
const credentials = config.getOptionalString(
'techdocs.publisher.googleGcs.credentials',
);
let credentialsJson = {};
try {
credentialsJson = JSON.parse(credentials);
} catch (err) {
throw new Error(
'Error in parsing techdocs.publisher.googleGcs.credentials config to JSON.',
);
if (credentials) {
try {
credentialsJson = JSON.parse(credentials);
} catch (err) {
throw new Error(
'Error in parsing techdocs.publisher.googleGcs.credentials config to JSON.',
);
}
}
const projectId = config.getOptionalString(
'techdocs.publisher.googleGcs.projectId',
);
const storageClient = new Storage({
credentials: credentialsJson,
projectId: projectId,
...(credentials && {
credentials: credentialsJson,
}),
...(projectId && { projectId }),
});
// Check if the defined bucket exists. Being able to connect means the configuration is good
// and the storage client will work.
storageClient
.bucket(bucketName)
.getMetadata()
.then(() => {
logger.info(
`Successfully connected to the GCS bucket ${bucketName} in the GCP project ${projectId}.`,
);
})
.catch(reason => {
logger.error(
`Could not retrieve metadata about the GCS bucket ${bucketName} in the GCP project ${projectId}. ` +
'Make sure the GCP project and the bucket exists and the access key located at the path ' +
"techdocs.publisher.googleGcs.credentials defined in app config has the role 'Storage Object Creator'. " +
'Refer to https://backstage.io/docs/features/techdocs/using-cloud-storage',
);
throw new Error(`from GCS client library: ${reason.message}`);
});
try {
await storageClient.bucket(bucketName).getMetadata();
logger.info(`Successfully connected to the GCS bucket ${bucketName}.`);
} catch (err) {
logger.error(
`Could not retrieve metadata about the GCS bucket ${bucketName}. ` +
'Make sure the bucket exists. Also make sure that authentication is setup either by explicitly defining ' +
'techdocs.publisher.googleGcs.credentials in app config or by using environment variables' +
'Refer to https://backstage.io/docs/features/techdocs/using-cloud-storage',
);
throw new Error(err.message);
}
return new GoogleGCSPublish(storageClient, bucketName, logger);
}
@@ -43,7 +43,7 @@ export class Publisher {
switch (publisherType) {
case 'googleGcs':
logger.info('Creating Google Storage Bucket publisher for TechDocs');
return GoogleGCSPublish.fromConfig(config, logger);
return await GoogleGCSPublish.fromConfig(config, logger);
case 'awsS3':
logger.info('Creating AWS S3 Bucket publisher for TechDocs');
return AwsS3Publish.fromConfig(config, logger);