Skip to content
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
1 change: 1 addition & 0 deletions common/errorCodes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
ERR_MISSING_PASSPHRASE: 'ERR_MISSING_PASSPHRASE',
ERR_OSSL_BAD_DECRYPT: 'ERR_OSSL_BAD_DECRYPT',
ERR_INVALID_USERNAME: '390144',
ERR_INVALID_KEY_FILE: 'ERR_INVALID_KEY_FILE',
};
2 changes: 2 additions & 0 deletions common/errorMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ module.exports = {
'Native Okta auth doesn\'t support MFA. Please, use the "Identity Provider SSO (via external browser)" auth instead',
KEY_PAIR_PASSPHRASE_ERROR: 'Please check that the passphrase is provided and matches the key.',
KEY_PAIR_USERNAME_ERROR: 'Incorrect username was specified.',
KEY_PAIR_INVALID_FILE_ERROR:
"The selected file is not a valid key. Please choose a valid key file, check it's passphrase and try again.",
};
1 change: 1 addition & 0 deletions common/getKeyPairConnectionErrorMessageByCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const ERROR_CODE_TO_MESSAGE = {
[errorCodes.ERR_MISSING_PASSPHRASE]: errorMessages.KEY_PAIR_PASSPHRASE_ERROR,
[errorCodes.ERR_OSSL_BAD_DECRYPT]: errorMessages.KEY_PAIR_PASSPHRASE_ERROR,
[errorCodes.ERR_INVALID_USERNAME]: errorMessages.KEY_PAIR_USERNAME_ERROR,
[errorCodes.ERR_INVALID_KEY_FILE]: errorMessages.KEY_PAIR_INVALID_FILE_ERROR,
};

const getKeyPairConnectionErrorMessageByCode = code => ERROR_CODE_TO_MESSAGE[code];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@
"inputKeyword": "privateKeyPath",
"inputType": "file",
"inputPlaceholder": "Private Key",
"extensions": ["p8"],
"extensions": ["p8", "pem", "cert", "crt"],
"dependency": {
"key": "authType",
"value": ["keyPair"]
Expand Down
12 changes: 12 additions & 0 deletions reverse_engineering/helpers/connections/connectionError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class ConnectionError extends Error {
code;

constructor(message, code) {
super(message);
this.code = code;
}
}

module.exports = {
ConnectionError,
};
35 changes: 34 additions & 1 deletion reverse_engineering/helpers/connections/keyPairConnection.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
const fs = require('fs');
const crypto = require('crypto');
const { connectWithTimeout } = require('./connection.js');
const errorCodes = require('../../../common/errorCodes.js');
const errorMessages = require('../../../common/errorMessages.js');
const { ConnectionError } = require('./connectionError.js');

const authByKeyPair = ({ account, role, timeout, username, privateKeyPath, privateKeyPass, logger }) => {
if (
!isValidPrivateKey({
privateKeyPath,
privateKeyPass,
logger,
})
) {
throw new ConnectionError(errorMessages.KEY_PAIR_INVALID_FILE_ERROR, errorCodes.ERR_INVALID_KEY_FILE);
}

const authByKeyPair = ({ account, role, timeout, username, privateKeyPath, privateKeyPass }) => {
return connectWithTimeout({
account,
role,
Expand All @@ -12,6 +27,24 @@ const authByKeyPair = ({ account, role, timeout, username, privateKeyPath, priva
});
};

const isValidPrivateKey = ({ privateKeyPath, privateKeyPass, logger }) => {
const textFilePrivateKeyFormat = 'pem';
const fileContent = fs.readFileSync(privateKeyPath, 'utf8');

try {
crypto.createPrivateKey({
key: fileContent,
format: textFilePrivateKeyFormat,
passphrase: privateKeyPass,
});

return true;
} catch (error) {
logger.log('error', { error }, 'Connection error');
return false;
}
};

module.exports = {
authByKeyPair,
};
2 changes: 1 addition & 1 deletion reverse_engineering/helpers/snowflakeHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ const connect = async (
logger,
});
} else if (authType === 'keyPair') {
authPromise = authByKeyPair({ account, role, timeout, username, privateKeyPath, privateKeyPass });
authPromise = authByKeyPair({ account, role, timeout, username, privateKeyPath, privateKeyPass, logger });
} else {
authPromise = authByCredentials({ account, username, password, role, warehouse, timeout });
}
Expand Down