Only allow one googleGcs config object

Signed-off-by: Martina Iglesias Fernandez <martina@roadie.io>
This commit is contained in:
Martina Iglesias Fernandez
2021-03-19 17:50:25 +01:00
parent 5bd790984e
commit d5cb51331a
8 changed files with 45 additions and 81 deletions
+4 -4
View File
@@ -123,10 +123,10 @@ kafka:
integrations:
googleGcs:
- clientEmail:
$env: GCS_CLIENT_EMAIL
privateKey:
$env: GCS_PRIVATE_KEY
clientEmail:
$env: GCS_CLIENT_EMAIL
privateKey:
$env: GCS_PRIVATE_KEY
github:
- host: github.com
@@ -24,10 +24,10 @@ Explicit credentials can be set in the following format:
```yaml
integrations:
googleGcs:
- clientEmail:
$env: GCS_CLIENT_EMAIL
privateKey:
$env: GCS_PRIVATE_KEY
clientEmail:
$env: GCS_CLIENT_EMAIL
privateKey:
$env: GCS_PRIVATE_KEY
```
Then make sure the environment variables `GCS_CLIENT_EMAIL` and
@@ -41,26 +41,19 @@ describe('GcsUrlReader', () => {
it('creates a reader with credentials correctly configured', () => {
const entries = createReader({
integrations: {
googleGcs: [
{
privateKey: '--- BEGIN KEY ---- fakekey --- END KEY ---',
clientEmail: 'someone@example.com',
},
{
host: 'proxy.storage.cloud.google.com',
privateKey: '--- BEGIN KEY ---- fakekey2 --- END KEY ---',
clientEmail: 'someone2@example.com',
},
],
googleGcs: {
privateKey: '--- BEGIN KEY ---- fakekey --- END KEY ---',
clientEmail: 'someone@example.com',
},
},
});
expect(entries).toHaveLength(2);
expect(entries).toHaveLength(1);
});
it('creates a reader with default credentials provider', () => {
const entries = createReader({
integrations: {
googleGcs: [{}],
googleGcs: {},
},
});
expect(entries).toHaveLength(1);
@@ -69,7 +62,7 @@ describe('GcsUrlReader', () => {
describe('predicates', () => {
const readers = createReader({
integrations: {
googleGcs: [{}],
googleGcs: {},
},
});
const predicate = readers[0].predicate;
@@ -24,7 +24,7 @@ import {
import getRawBody from 'raw-body';
import {
GOOGLE_GCS_HOST,
readGoogleGcsIntegrationConfigs,
readGoogleGcsIntegrationConfig,
} from '@backstage/integration';
const parseURL = (
@@ -49,28 +49,26 @@ export class GoogleGcsUrlReader implements UrlReader {
if (!config.has('integrations.googleGcs')) {
return [];
}
const configs = readGoogleGcsIntegrationConfigs(
config.getOptionalConfigArray('integrations.googleGcs') ?? [],
const gcsConfig = readGoogleGcsIntegrationConfig(
config.getConfig('integrations.googleGcs'),
);
return configs.map(integration => {
let storage: Storage;
if (!integration.clientEmail || !integration.privateKey) {
logger.warn(
'googleGcs credentials not found in config. Using default credentials provider.',
);
storage = new Storage();
} else {
storage = new Storage({
credentials: {
client_email: integration.clientEmail || undefined,
private_key: integration.privateKey || undefined,
},
});
}
const reader = new GoogleGcsUrlReader(storage);
const predicate = (url: URL) => url.host === GOOGLE_GCS_HOST;
return { reader, predicate };
});
let storage: Storage;
if (!gcsConfig.clientEmail || !gcsConfig.privateKey) {
logger.warn(
'googleGcs credentials not found in config. Using default credentials provider.',
);
storage = new Storage();
} else {
storage = new Storage({
credentials: {
client_email: gcsConfig.clientEmail || undefined,
private_key: gcsConfig.privateKey || undefined,
},
});
}
const reader = new GoogleGcsUrlReader(storage);
const predicate = (url: URL) => url.host === GOOGLE_GCS_HOST;
return [{ reader, predicate }];
};
constructor(private readonly storage: Storage) {}
+2 -2
View File
@@ -151,7 +151,7 @@ export interface Config {
}>;
/** Integration configuration for Google Cloud Storage */
googleGcs?: Array<{
googleGcs?: {
/**
* Service account email used to authenticate requests.
* @visibility secret
@@ -162,6 +162,6 @@ export interface Config {
* @visibility secret
*/
privateKey?: string;
}>;
};
};
}
@@ -18,7 +18,6 @@ import { Config, ConfigReader } from '@backstage/config';
import {
GoogleGcsIntegrationConfig,
readGoogleGcsIntegrationConfig,
readGoogleGcsIntegrationConfigs,
} from './config';
describe('readGoogleGcsIntegrationConfig', () => {
@@ -38,25 +37,9 @@ describe('readGoogleGcsIntegrationConfig', () => {
token: 'someone@example.com',
});
});
});
describe('readGoogleGcsIntegrationConfigs', () => {
function buildConfig(data: Partial<GoogleGcsIntegrationConfig>[]): Config[] {
return data.map(item => new ConfigReader(item));
}
it('reads all values', () => {
const output = readGoogleGcsIntegrationConfigs(
buildConfig([
{
privateKey: 'fake-key',
clientEmail: 'someone@example.com',
},
]),
);
expect(output).toContainEqual({
privateKey: 'fake-key',
clientEmail: 'someone@example.com',
});
it('does not fail when config is not set', () => {
const output = readGoogleGcsIntegrationConfig(buildConfig({}));
expect(output).toEqual({});
});
});
+4 -11
View File
@@ -38,6 +38,10 @@ export type GoogleGcsIntegrationConfig = {
export function readGoogleGcsIntegrationConfig(
config: Config,
): GoogleGcsIntegrationConfig {
if (!config) {
return {};
}
if (!config.has('clientEmail') || !config.has('privateKey')) {
return {};
}
@@ -50,14 +54,3 @@ export function readGoogleGcsIntegrationConfig(
const clientEmail = config.getOptionalString('clientEmail');
return { clientEmail: clientEmail, privateKey: privateKey };
}
/**
* Reads a set of Google Cloud Storage integration configs.
*
* @param configs All of the integration config objects
*/
export function readGoogleGcsIntegrationConfigs(
configs: Config[],
): GoogleGcsIntegrationConfig[] {
return configs.map(readGoogleGcsIntegrationConfig);
}
+1 -4
View File
@@ -14,9 +14,6 @@
* limitations under the License.
*/
export {
readGoogleGcsIntegrationConfig,
readGoogleGcsIntegrationConfigs,
} from './config';
export { readGoogleGcsIntegrationConfig } from './config';
export type { GoogleGcsIntegrationConfig } from './config';
export const GOOGLE_GCS_HOST = 'storage.cloud.google.com';