Skip to content

Commit 02ec798

Browse files
Refactor checksum verification logic to handle 404 errors gracefully and improve logging
1 parent 6ce556d commit 02ec798

File tree

5 files changed

+29
-6
lines changed

5 files changed

+29
-6
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ coverage/
44
.DS_Store
55
*.log
66
.env
7+
8+
bin/
9+
10+
*.exe

bin/cloudsqlctl.exe

512 Bytes
Binary file not shown.

src/commands/install.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
import { Command } from 'commander';
22
import { getLatestVersion, downloadProxy } from '../core/updater.js';
33
import { logger } from '../core/logger.js';
4+
import { isRunning, stopProxy } from '../core/proxy.js';
45

56
export const installCommand = new Command('install')
67
.description('Download and install Cloud SQL Proxy')
78
.option('-v, --version <version>', 'Specific version to install')
89
.action(async (options) => {
910
try {
11+
if (await isRunning()) {
12+
logger.info('Stopping running proxy before installation...');
13+
await stopProxy();
14+
}
15+
1016
let version = options.version;
1117
if (!version) {
1218
logger.info('Fetching latest version...');

src/commands/update.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { Command } from 'commander';
22
import { getLatestVersion, downloadProxy } from '../core/updater.js';
33
import { logger } from '../core/logger.js';
4+
import { isRunning, stopProxy } from '../core/proxy.js';
45

56
export const updateCommand = new Command('update')
67
.description('Update Cloud SQL Proxy to the latest version')
78
.action(async () => {
89
try {
10+
if (await isRunning()) {
11+
logger.info('Stopping running proxy before update...');
12+
await stopProxy();
13+
}
14+
915
logger.info('Checking for updates...');
1016
const version = await getLatestVersion();
1117
logger.info(`Latest version is ${version}. Updating...`);

src/core/updater.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export async function downloadProxy(version: string) {
4040
const releaseUrl = `https://api.github.com/repos/${GITHUB_REPO}/releases/tags/${version}`;
4141
const response = await axios.get(releaseUrl);
4242
const assets = response.data.assets;
43-
logger.info(`Found ${assets.length} assets: ${assets.map((a: any) => a.name).join(', ')}`);
4443
// eslint-disable-next-line @typescript-eslint/no-explicit-any
4544
const asset = assets.find((a: any) => a.name === ASSET_NAME);
4645
// eslint-disable-next-line @typescript-eslint/no-explicit-any
@@ -93,11 +92,19 @@ export async function downloadProxy(version: string) {
9392
}
9493
logger.info('Checksum verified successfully');
9594
} catch (error) {
96-
logger.warn('Checksum verification skipped or failed: ' + error);
97-
// Decide if we want to fail hard or warn. For security, failing hard is better if checksum was expected.
98-
// But if checksum file is missing, maybe warn.
99-
// Given requirements "Secure", let's fail if we found a checksum url but it failed.
100-
if (checksumUrl) throw error;
95+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
96+
if (axios.isAxiosError(error) && error.response?.status === 404) {
97+
logger.warn('Checksum file not found, skipping verification.');
98+
} else {
99+
logger.warn('Checksum verification failed: ' + error);
100+
// If it was a verification failure (not 404), we should probably fail.
101+
// But if it was a network error fetching the checksum, maybe we should also fail?
102+
// For now, if we have a checksum URL and it fails to verify (mismatch), we threw Error above.
103+
// If axios failed with other than 404, we rethrow.
104+
if (!(axios.isAxiosError(error) && error.response?.status === 404)) {
105+
throw error;
106+
}
107+
}
101108
}
102109
}
103110

0 commit comments

Comments
 (0)