UNPKG

mecano

Version:

Common functions for system deployment.

137 lines (132 loc) 4.19 kB
// Generated by CoffeeScript 1.11.1 var misc, path, string; module.exports = function(options) { var keytab, princ; if (!options.principal) { throw Error('Property principal is required'); } if (!options.keytab) { throw Error('Property keytab is required'); } if (/^\S+@\S+$/.test(options.kadmin_principal)) { if (options.realm == null) { options.realm = options.kadmin_principal.split('@')[1]; } } else { if (!options.realm) { throw Error('Property "realm" is required unless present in principal'); } options.principal = options.principal + "@" + options.realm; } keytab = {}; princ = {}; this.execute({ cmd: "export TZ=GMT; klist -kt " + options.keytab, code_skipped: 1, shy: true }, function(err, exists, stdout, stderr) { var _, i, kvno, len, line, match, mdate, principal, ref, results; if (err) { throw err; } if (!exists) { return; } options.log({ message: "Keytab exists, check kvno validity", level: 'DEBUG', module: 'mecano/krb5/ktadd' }); ref = string.lines(stdout); results = []; for (i = 0, len = ref.length; i < len; i++) { line = ref[i]; if (!(match = /^\s*(\d+)\s+([\d\/:]+\s+[\d\/:]+)\s+(.*)\s*$/.exec(line))) { continue; } _ = match[0], kvno = match[1], mdate = match[2], principal = match[3]; kvno = parseInt(kvno, 10); mdate = Date.parse(mdate + " GMT"); if (!keytab[principal] || keytab[principal].kvno < kvno) { results.push(keytab[principal] = { kvno: kvno, mdate: mdate }); } else { results.push(void 0); } } return results; }); this.execute({ cmd: misc.kadmin(options, "getprinc -terse " + options.principal), shy: true, "if": function() { return keytab[options.principal] != null; } }, function(err, executed, stdout, stderr) { var kvno, mdate, ref, ref1, values; if (err) { return err; } if (!executed) { return; } values = string.lines(stdout)[1]; if (!values) { throw Error("Principal does not exist: '" + options.principal + "'"); } values = values.split('\t'); mdate = parseInt(values[2], 10) * 1000; kvno = parseInt(values[8], 10); princ = { mdate: mdate, kvno: kvno }; options.log({ message: "Keytab kvno '" + ((ref = keytab[options.principal]) != null ? ref.kvno : void 0) + "', principal kvno '" + princ.kvno + "'", level: 'INFO', module: 'mecano/krb5/ktadd' }); return options.log({ message: "Keytab mdate '" + (new Date((ref1 = keytab[options.principal]) != null ? ref1.mdate : void 0)) + "', principal mdate '" + (new Date(princ.mdate)) + "'", level: 'INFO', module: 'mecano/krb5/ktadd' }); }); this.execute({ cmd: misc.kadmin(options, "ktremove -k " + options.keytab + " " + options.principal), "if": function() { var ref; return (keytab[options.principal] != null) && (((ref = keytab[options.principal]) != null ? ref.kvno : void 0) !== princ.kvno || keytab[options.principal].mdate !== princ.mdate); } }); this.mkdir({ target: "" + (path.dirname(options.keytab)), "if": function() { var ref; return (keytab[options.principal] == null) || (((ref = keytab[options.principal]) != null ? ref.kvno : void 0) !== princ.kvno || keytab[options.principal].mdate !== princ.mdate); } }); this.execute({ cmd: misc.kadmin(options, "ktadd -k " + options.keytab + " " + options.principal), "if": function() { var ref; return (keytab[options.principal] == null) || (((ref = keytab[options.principal]) != null ? ref.kvno : void 0) !== princ.kvno || keytab[options.principal].mdate !== princ.mdate); } }); this.chown({ target: options.keytab, uid: options.uid, gid: options.gid, "if": (options.uid != null) || (options.gid != null) }); return this.chmod({ target: options.keytab, mode: options.mode, "if": options.mode != null }); }; path = require('path'); misc = require('../misc'); string = require('../misc/string');