Skip to content

Commit

Permalink
fix: only add PSC ipType if PSC is enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
jackwotherspoon committed Sep 23, 2024
1 parent 680c2e9 commit 50e68b8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
31 changes: 0 additions & 31 deletions src/ip-addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import {sqladmin_v1beta4} from '@googleapis/sqladmin';
import {CloudSQLConnectorError} from './errors';

export enum IpAddressTypes {
Expand Down Expand Up @@ -57,36 +56,6 @@ const getPSCIpAddress = (ipAddresses: IpAddresses) => {
return ipAddresses.psc;
};

export function parseIpAddresses(
ipResponse: sqladmin_v1beta4.Schema$IpMapping[] | undefined,
dnsName: string | null | undefined
): IpAddresses {
const ipAddresses: IpAddresses = {};
if (ipResponse) {
for (const ip of ipResponse) {
if (ip.type === 'PRIMARY' && ip.ipAddress) {
ipAddresses.public = ip.ipAddress;
}
if (ip.type === 'PRIVATE' && ip.ipAddress) {
ipAddresses.private = ip.ipAddress;
}
}
}

if (dnsName) {
ipAddresses.psc = dnsName;
}

if (!ipAddresses.public && !ipAddresses.private && !ipAddresses.psc) {
throw new CloudSQLConnectorError({
message: 'Cannot connect to instance, it has no supported IP addresses',
code: 'ENOSQLADMINIPADDRESS',
});
}

return ipAddresses;
}

export function selectIpAddress(
ipAddresses: IpAddresses,
type: IpAddressTypes | unknown
Expand Down
29 changes: 25 additions & 4 deletions src/sqladmin-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,31 @@ export class SQLAdminFetcher {
});
}

const ipAddresses = parseIpAddresses(
res.data.ipAddresses,
res.data.dnsName
);
const ipAddresses: IpAddresses = {};
if (res.data.ipAddresses) {
for (const ip of res.data.ipAddresses) {
if (ip.type === 'PRIMARY' && ip.ipAddress) {
ipAddresses.public = ip.ipAddress;
}
if (ip.type === 'PRIVATE' && ip.ipAddress) {
ipAddresses.private = ip.ipAddress;
}
}
}

// Resolve dnsName into IP address for PSC enabled instances.
// Note that we have to check for PSC enablement also because CAS instances
// also set the dnsName field.
if (res.data.dnsName && res.data.pscEnabled) {
ipAddresses.psc = res.data.dnsName;
}

if (!ipAddresses.public && !ipAddresses.private && !ipAddresses.psc) {
throw new CloudSQLConnectorError({
message: 'Cannot connect to instance, it has no supported IP addresses',
code: 'ENOSQLADMINIPADDRESS',
});
}

const {serverCaCert} = res.data;
if (!serverCaCert || !serverCaCert.cert || !serverCaCert.expirationTime) {
Expand Down

0 comments on commit 50e68b8

Please sign in to comment.