Skip to content

Instantly share code, notes, and snippets.

@voxpelli
Last active February 2, 2018 06:31
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save voxpelli/9db1a2b0d8ae1254cf0c to your computer and use it in GitHub Desktop.
Save voxpelli/9db1a2b0d8ae1254cf0c to your computer and use it in GitHub Desktop.
Check the ssh key length of specified users
// Replace this list with the result of to-run-on-org-page.js
// And remember to do an "npm install"
var users = ["voxpelli"];
var https = require('https');
var fs = require('fs');
var exec = require('child_process').exec;
var tmp = require('temporary-directory')
var cuid = require('cuid');
var chalk = require('chalk');
tmp(function created (err, dir, cleanup) {
if (err) return console.error('Error creating tmpdir!', err)
Promise.all(users.map(function (username) {
return new Promise(function (resolve, reject) {
https.get('https://github.com/' + username + '.keys', function (res) {
var body = '';
res.on('data', function(d) {
body += d;
});
res.on('end', function() {
// Data reception is done, do whatever with it!
resolve(body);
});
}).on('error', function (e) {
reject(e);
});
});
})).then(function (keys) {
var checks = [];
keys.forEach(function (data, position) {
var splitted = data.split('\n');
splitted.forEach(function (key, keyPosition) {
key = key.trim();
if (!key) {
return;
}
var id = cuid();
var next = new Promise(function (resolve, reject) {
var filename = dir + '/' + id + '.pub';
fs.writeFile(filename, key, function (err) {
if (err) return reject(err);
resolve(filename);
});
});
next = next.then(function (filename) {
return new Promise(function (resolve, reject) {
exec('ssh-keygen -l -f ' + filename, function (err, stdout, stderr) {
if (err) {
return reject(err);
}
var parts = stdout.toString('utf8').split(' ');
resolve({
username: users[position],
bitsize: parseInt(parts[0]),
type: parts[parts.length - 1].trim().slice(1, -1),
keyPosition: keyPosition,
});
});
});
});
checks.push(next);
});
});
return Promise.all(checks);
}).then(function (results) {
results.sort(function (a, b) {
if (a.bitsize < b.bitsize) {
return -1;
}
if (a.bitsize > b.bitsize) {
return 1;
}
return 0;
});
results.forEach(function (result) {
var color = 'bgRed';
if (result.type.toLowerCase() !== 'rsa') {
color = 'bgBlack';
} else if (result.bitsize >= 4096) {
color = 'bgGreen';
}
console.log(chalk.dim(result.username + ':'), chalk[color](result.type + ' ' + result.bitsize), chalk.dim('(' + result.keyPosition + ')'));
});
}).catch(function (e) {
console.log("Got error: " + e.message);
}).then(function () {
// later, when you wanna destroy the tmpdir:
cleanup(function cleanedUp (err) {
if (err) console.error('Error removing tmpdir!', err)
})
});
});
{
"name": "fetchkeys",
"version": "1.0.0",
"description": "",
"main": "fetchkeys.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Pelle Wessman <pelle@kodfabrik.se> (http://kodfabrik.se/)",
"license": "MIT",
"dependencies": {
"chalk": "^1.1.3",
"temporary-directory": "^1.0.1"
},
"devDependencies": {
"cuid": "^1.2.5"
}
}
// Run in web console on all pages on https://github.com/orgs/<orgname>/people
JSON.stringify(Array.prototype.reduce.call(
document.getElementsByClassName('member-username'),
function (previousValue, currentValue) {
previousValue.push(currentValue.textContent);
return previousValue;
},
[]
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment