Allow passing ip type to cloud-sql-connector

Signed-off-by: Tomasz Szuba <tszuba@box.com>
This commit is contained in:
Tomasz Szuba
2025-01-31 12:08:15 +01:00
parent 72f9a9d1ff
commit c7609de702
4 changed files with 67 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/backend-defaults': patch
---
Allow passing IP type to use with cloud-sql-connector
+4
View File
@@ -429,6 +429,10 @@ export interface Config {
* The instance connection name for the cloudsql instance, e.g. `project:region:instance`
*/
instance: string;
/**
* The ip address type to use for the connection. Defaults to 'PUBLIC'
*/
ipAddressType?: 'PUBLIC' | 'PRIVATE' | 'PSC';
}
| {
/**
@@ -221,6 +221,63 @@ describe('postgres', () => {
});
});
it('passes default settings to cloud-sql-connector', async () => {
const { Connector } = jest.requireMock(
'@google-cloud/cloud-sql-connector',
) as jest.Mocked<typeof import('@google-cloud/cloud-sql-connector')>;
const mockStream = (): any => {};
Connector.prototype.getOptions.mockResolvedValue({ stream: mockStream });
await buildPgDatabaseConfig(
new ConfigReader({
client: 'pg',
connection: {
type: 'cloudsql',
user: 'ben@gke.com',
instance: 'project:region:instance',
port: 5423,
},
}),
{ connection: { database: 'other_db' } },
);
expect(Connector.prototype.getOptions).toHaveBeenCalledWith({
authType: 'IAM',
instanceConnectionName: 'project:region:instance',
ipType: 'PUBLIC',
});
});
it('passes ip settings to cloud-sql-connector', async () => {
const { Connector } = jest.requireMock(
'@google-cloud/cloud-sql-connector',
) as jest.Mocked<typeof import('@google-cloud/cloud-sql-connector')>;
const mockStream = (): any => {};
Connector.prototype.getOptions.mockResolvedValue({ stream: mockStream });
await buildPgDatabaseConfig(
new ConfigReader({
client: 'pg',
connection: {
type: 'cloudsql',
user: 'ben@gke.com',
instance: 'project:region:instance',
ipAddressType: 'PRIVATE',
port: 5423,
},
}),
{ connection: { database: 'other_db' } },
);
expect(Connector.prototype.getOptions).toHaveBeenCalledWith({
authType: 'IAM',
instanceConnectionName: 'project:region:instance',
ipType: 'PRIVATE',
});
});
it('throws an error when the connection type is not supported', async () => {
await expect(
buildPgDatabaseConfig(
@@ -107,7 +107,7 @@ export async function buildPgDatabaseConfig(
const connector = new CloudSqlConnector();
const clientOpts = await connector.getOptions({
instanceConnectionName: config.connection.instance,
ipType: IpAddressTypes.PUBLIC,
ipType: config.connection.ipAddressType ?? IpAddressTypes.PUBLIC,
authType: AuthTypes.IAM,
});