Skip to content

Commit

Permalink
Update api to get/save ssh key fingerprints
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketeerbkw committed Mar 11, 2019
1 parent 64b747f commit 9fedd48
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
6 changes: 6 additions & 0 deletions services/api/src/resources/sshKey/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ const validateSshKey = (key /* : string */) /* : boolean */ => {
}
};

const getSshKeyFingerprint = (key /* : string */) /* : string */ => {
const parsed = sshpk.parseKey(key, 'ssh');
return parsed.fingerprint('sha256', 'ssh').toString();
};

module.exports = {
validateSshKey,
getSshKeyFingerprint,
};
22 changes: 14 additions & 8 deletions services/api/src/resources/sshKey/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
const R = require('ramda');
const sqlClient = require('../../clients/sqlClient');
const { isPatchEmpty, prepare, query } = require('../../util/db');
const { validateSshKey } = require('.');
const { validateSshKey, getSshKeyFingerprint } = require('.');
const Sql = require('./sql');

/* ::
Expand Down Expand Up @@ -66,8 +66,9 @@ const addSshKey = async (
{ credentials: { role, userId: credentialsUserId } },
) => {
const keyType = sshKeyTypeToString(unformattedKeyType);
const keyFormatted = formatSshKey({ keyType, keyValue });

if (!validateSshKey(formatSshKey({ keyType, keyValue }))) {
if (!validateSshKey(keyFormatted)) {
throw new Error('Invalid SSH key format! Please verify keyType + keyValue');
}

Expand All @@ -86,6 +87,7 @@ const addSshKey = async (
name,
keyValue,
keyType,
keyFingerprint: getSshKeyFingerprint(keyFormatted),
}),
);
await query(sqlClient, Sql.addSshKeyToUser({ sshKeyId: insertId, userId }));
Expand Down Expand Up @@ -125,16 +127,20 @@ const updateSshKey = async (
throw new Error('Input patch requires at least 1 attribute');
}

if (
(keyType || keyValue) &&
!validateSshKey(formatSshKey({ keyType, keyValue }))
) {
throw new Error('Invalid SSH key format! Please verify keyType + keyValue');
let keyFingerprint = null;
if ((keyType || keyValue)) {
const keyFormatted = formatSshKey({ keyType, keyValue });

if (!validateSshKey(keyFormatted)) {
throw new Error('Invalid SSH key format! Please verify keyType + keyValue');
}

keyFingerprint = getSshKeyFingerprint(keyFormatted);
}

await query(
sqlClient,
Sql.updateSshKey({ id, patch: { name, keyType, keyValue } }),
Sql.updateSshKey({ id, patch: { name, keyType, keyValue, keyFingerprint } }),
);
const rows = await query(sqlClient, Sql.selectSshKey(id));

Expand Down
3 changes: 3 additions & 0 deletions services/api/src/resources/sshKey/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ const Sql /* : SqlObj */ = {
name,
keyValue,
keyType,
keyFingerprint,
} /* : {
id: number,
name: string,
keyValue: string,
keyType: string,
keyFingerprint: string,
} */,
) =>
knex('ssh_key')
Expand All @@ -71,6 +73,7 @@ const Sql /* : SqlObj */ = {
name,
key_value: keyValue,
key_type: keyType,
key_fingerprint: keyFingerprint,
})
.toString(),
addSshKeyToUser: (
Expand Down
1 change: 1 addition & 0 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const typeDefs = gql`
name: String
keyValue: String
keyType: String
keyFingerprint: String
created: String
}
Expand Down

0 comments on commit 9fedd48

Please sign in to comment.