Skip to content

Commit c6290d4

Browse files
authored
feat: Add better error for misshaped credentials (#22)
1 parent b616de9 commit c6290d4

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"scripts": {
77
"build": "ncc build src/main.ts",
88
"lint": "eslint . --ext .ts,.tsx",
9-
"format": "prettier --write **/*.ts",
10-
"test": "mocha -r ts-node/register -t 30s 'setupGcloudSDK/tests/*.test.ts'"
9+
"format": "prettier --write **/**/*.ts",
10+
"test": "mocha -r ts-node/register -t 90s 'setupGcloudSDK/tests/*.test.ts'"
1111
},
1212
"repository": {
1313
"type": "git",

setupGcloudSDK/src/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,30 @@ export function parseServiceAccountKey(
145145
if (!serviceAccountKey.trim().startsWith('{')) {
146146
serviceAccount = Buffer.from(serviceAccountKey, 'base64').toString('utf8');
147147
}
148-
return JSON.parse(serviceAccount);
148+
try {
149+
return JSON.parse(serviceAccount);
150+
} catch (error) {
151+
const keyFormat = `
152+
{
153+
"type": "service_account",
154+
"project_id": "project-id",
155+
"private_key_id": "key-id",
156+
"private_key": "-----BEGIN PRIVATE KEY-----\\nprivate-key\\n-----END PRIVATE KEY-----\\n",
157+
"client_email": "service-account-email",
158+
"client_id": "client-id",
159+
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
160+
"token_uri": "https://accounts.google.com/o/oauth2/token",
161+
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
162+
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/service-account-email"
163+
}
164+
`;
165+
const message =
166+
'Error parsing credentials: ' +
167+
error.message +
168+
'\nEnsure your credentials are base64 encoded or validate JSON format: ' +
169+
keyFormat;
170+
throw new Error(message);
171+
}
149172
}
150173

151174
/**

setupGcloudSDK/tests/download-util.test.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ describe('#downloadAndExtractTool', function() {
3939
});
4040

4141
afterEach(async function() {
42-
await io.rmRF(toolDir);
43-
await io.rmRF(tempDir);
42+
try {
43+
await io.rmRF(toolDir);
44+
await io.rmRF(tempDir);
45+
} catch (err) {
46+
console.log('Error occurred during cleanup: ' + err);
47+
}
4448
});
4549

4650
it('downloads and extracts linux version', async function() {

0 commit comments

Comments
 (0)