Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: enable IP type switching #338

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 1 addition & 23 deletions src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,7 @@ class CloudSQLInstanceMap extends Map {
// been setup there's no need to set it up again
if (this.has(instanceConnectionName)) {
const instance = this.get(instanceConnectionName);
if (instance.ipType && instance.ipType !== ipType) {
throw new CloudSQLConnectorError({
message:
`getOptions called for instance ${instanceConnectionName} with ipType ${ipType}, ` +
`but was previously called with ipType ${instance.ipType}. ` +
'If you require both for your use case, please use a new connector object.',
code: 'EMISMATCHIPTYPE',
});
} else if (instance.authType && instance.authType !== authType) {
if (instance.authType && instance.authType !== authType) {
throw new CloudSQLConnectorError({
message:
`getOptions called for instance ${instanceConnectionName} with authType ${authType}, ` +
Expand All @@ -107,11 +99,9 @@ class CloudSQLInstanceMap extends Map {

getInstance({
instanceConnectionName,
ipType,
authType,
}: {
instanceConnectionName: string;
ipType: IpAddressTypes;
authType: AuthTypes;
}): CloudSQLInstance {
const connectionInstance = this.get(instanceConnectionName);
Expand All @@ -120,17 +110,6 @@ class CloudSQLInstanceMap extends Map {
message: `Cannot find info for instance: ${instanceConnectionName}`,
code: 'ENOINSTANCEINFO',
});
} else if (
connectionInstance.ipType &&
connectionInstance.ipType !== ipType
) {
throw new CloudSQLConnectorError({
message:
`getOptions called for instance ${instanceConnectionName} with ipType ${ipType}, ` +
`but was previously called with ipType ${connectionInstance.ipType}. ` +
'If you require both for your use case, please use a new connector object.',
code: 'EMISMATCHIPTYPE',
});
} else if (
connectionInstance.authType &&
connectionInstance.authType !== authType
Expand Down Expand Up @@ -200,7 +179,6 @@ export class Connector {
stream() {
const cloudSqlInstance = instances.getInstance({
instanceConnectionName,
ipType,
authType,
});
const {
Expand Down
121 changes: 0 additions & 121 deletions test/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,72 +300,6 @@ t.test('Connector reusing instance on mismatching auth type', async t => {
);
});

t.test('Connector reusing instance on mismatching ip type', async t => {
setupCredentials(t); // setup google-auth credentials mocks
let hasInstance = false;

// mocks sql admin fetcher and generateKeys modules
// so that they can return a deterministic result
const {Connector} = t.mockRequire('../src/connector', {
'../src/sqladmin-fetcher': {
SQLAdminFetcher: class {
getInstanceMetadata() {
return Promise.resolve({
ipAddresses: {
public: '127.0.0.1',
},
serverCaCert: {
cert: CA_CERT,
expirationTime: '2033-01-06T10:00:00.232Z',
},
});
}
getEphemeralCertificate() {
return Promise.resolve({
cert: CLIENT_CERT,
expirationTime: '2033-01-06T10:00:00.232Z',
});
}
},
},
'../src/cloud-sql-instance': {
CloudSQLInstance: {
async getCloudSQLInstance() {
if (hasInstance) {
throw new Error('should only initialize once');
}
hasInstance = true;
return {
ipType: 'PUBLIC',
};
},
},
},
});

const connector = new Connector();
await connector.getOptions({
authType: 'PASSWORD',
ipType: 'PUBLIC',
instanceConnectionName: 'foo:bar:baz',
});
return t.rejects(
connector.getOptions({
authType: 'PASSWORD',
ipType: 'PRIVATE',
instanceConnectionName: 'foo:bar:baz',
}),
{
code: 'EMISMATCHIPTYPE',
message:
'getOptions called for instance foo:bar:baz with ipType PRIVATE, ' +
'but was previously called with ipType PUBLIC. ' +
'If you require both for your use case, please use a new connector object.',
},
'should throw error if retrieving existing instance with diff ip type'
);
});

t.test('Connector factory method mismatch auth type', async t => {
setupCredentials(t); // setup google-auth credentials mocks

Expand Down Expand Up @@ -422,61 +356,6 @@ t.test('Connector factory method mismatch auth type', async t => {
);
});

t.test('Connector factory method mismatch ip type', async t => {
setupCredentials(t); // setup google-auth credentials mocks

// mocks sql admin fetcher and generateKeys modules
// so that they can return a deterministic result
const {Connector} = t.mockRequire('../src/connector', {
'../src/sqladmin-fetcher': {
SQLAdminFetcher: class {
getInstanceMetadata() {
return Promise.resolve({
ipAddresses: {
public: '127.0.0.1',
},
serverCaCert: {
cert: CA_CERT,
expirationTime: '2033-01-06T10:00:00.232Z',
},
});
}
getEphemeralCertificate() {
return Promise.resolve({
cert: CLIENT_CERT,
expirationTime: '2033-01-06T10:00:00.232Z',
});
}
},
},
'../src/cloud-sql-instance': {
CloudSQLInstance: {
async getCloudSQLInstance() {
return {
ipType: 'PUBLIC',
};
},
},
},
});

const connector = new Connector();
const opts = await connector.getOptions({
authType: 'PASSWORD',
ipType: 'PRIVATE',
instanceConnectionName: 'foo:bar:baz',
});
t.throws(
() => {
opts.stream(); // calls factory method that returns new socket
},
{
code: 'EMISMATCHIPTYPE',
},
'should throw a mismatching ip type error'
);
});

t.test('Connector, supporting Tedious driver', async t => {
setupCredentials(t); // setup google-auth credentials mocks

Expand Down