manager-underlay.js 17 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
'use strict';

var U = require('./utils.js');
var E = require('./errors.js');

var warned = {};

// The purpose of this file is to try to auto-build
// partial managers so that the external API can be smaller.

module.exports.wrap = function(greenlock, gconf) {
    var myFind = gconf.find;
    delete gconf.find;

    var mega = mergeManager(gconf);

    greenlock.manager = {};
    greenlock.sites = {};
    //greenlock.accounts = {};
    //greenlock.certs = {};

    var allowed = [
        'accountKeyType', //: ["P-256", "RSA-2048"],
        'serverKeyType', //: ["RSA-2048", "P-256"],
        'store', // : { module, specific opts },
        'challenges', // : { "http-01", "dns-01", "tls-alpn-01" },
        'subscriberEmail',
        'agreeToTerms',
        'agreeTos',
        'customerEmail',
        'renewOffset',
        'renewStagger',
        'module', // not allowed, just ignored
        'manager'
    ];

    // get / set default site settings such as
    // subscriberEmail, store, challenges, renewOffset, renewStagger
    greenlock.manager.defaults = function(conf) {
        return greenlock._init().then(function() {
            if (!conf) {
                return mega.defaults();
            }

            if (conf.sites) {
                throw new Error('cannot set sites as global config');
            }
            if (conf.routes) {
                throw new Error('cannot set routes as global config');
            }

            // disallow keys we know to be bad
            [
                'subject',
                'deletedAt',
                'altnames',
                'lastAttemptAt',
                'expiresAt',
                'issuedAt',
                'renewAt',
                'sites',
                'routes'
            ].some(function(k) {
                if (k in conf) {
                    throw new Error(
                        '`' + k + '` not allowed as a default setting'
                    );
                }
            });
            Object.keys(conf).forEach(function(k) {
                if (!allowed.includes(k) && !warned[k]) {
                    warned[k] = true;
                    console.warn(
                        k +
                            " isn't a known key. Please open an issue and let us know the use case."
                    );
                }
            });

            Object.keys(conf).forEach(function(k) {
                if (-1 !== ['module', 'manager'].indexOf(k)) {
                    return;
                }

                if ('undefined' === typeof k) {
                    throw new Error(
                        "'" +
                            k +
                            "' should be set to a value, or `null`, but not left `undefined`"
                    );
                }
            });

            return mega.defaults(conf);
        });
    };

    greenlock.manager._defaults = function(opts) {
        return mega.defaults(opts);
    };

    greenlock.manager.add = function(args) {
        if (!args || !Array.isArray(args.altnames) || !args.altnames.length) {
            throw new Error(
                'you must specify `altnames` when adding a new site'
            );
        }
        if (args.renewAt) {
            throw new Error(
                'you cannot specify `renewAt` when adding a new site'
            );
        }

        return greenlock.manager.set(args);
    };

    // TODO agreeToTerms should be handled somewhere... maybe?

    // Add and update remains because I said I had locked the API
    greenlock.manager.set = greenlock.manager.update = function(args) {
        return greenlock._init().then(function() {
            // The goal is to make this decently easy to manage by hand without mistakes
            // but also reasonably easy to error check and correct
            // and to make deterministic auto-corrections

            args.subject = checkSubject(args);

            //var subscriberEmail = args.subscriberEmail;

            // TODO shortcut the other array checks when not necessary
            if (Array.isArray(args.altnames)) {
                args.altnames = checkAltnames(args.subject, args);
            }

            // at this point we know that subject is the first of altnames
            return Promise.all(
                (args.altnames || []).map(function(d) {
                    d = d.replace('*.', '');
                    return U._validDomain(d);
                })
            ).then(function() {
                if (!U._uniqueNames(args.altnames || [])) {
                    throw E.NOT_UNIQUE(
                        'add',
                        "'" + args.altnames.join("' '") + "'"
                    );
                }

                // durations
                if (args.renewOffset) {
                    args.renewOffset = U._parseDuration(args.renewOffset);
                }
                if (args.renewStagger) {
                    args.renewStagger = U._parseDuration(args.renewStagger);
                }

                return mega.set(args).then(function(result) {
                    if (!gconf._bin_mode) {
                        greenlock.renew({}).catch(function(err) {
                            if (!err.context) {
                                err.contxt = 'renew';
                            }
                            greenlock._notify('error', err);
                        });
                    }
                    return result;
                });
            });
        });
    };

    greenlock.manager.get = greenlock.sites.get = function(args) {
        return Promise.resolve().then(function() {
            if (args.subject) {
                throw new Error(
                    'get({ servername }) searches certificates by altnames, not by subject specifically'
                );
            }
            if (args.servernames || args.altnames || args.renewBefore) {
                throw new Error(
                    'get({ servername }) does not take arguments that could lead to multiple results'
                );
            }
            return mega.get(args);
        });
    };

    greenlock.manager.remove = function(args) {
        return Promise.resolve().then(function() {
            args.subject = checkSubject(args);
            if (args.servername) {
                throw new Error(
                    'remove() should be called with `subject` only, if you wish to remove altnames use `update()`'
                );
            }
            if (args.altnames) {
                throw new Error(
                    'remove() should be called with `subject` only, not `altnames`'
                );
            }
            // TODO check no altnames
            return mega.remove(args);
        });
    };

    /*
    {
        subject: site.subject,
        altnames: site.altnames,
        //issuedAt: site.issuedAt,
        //expiresAt: site.expiresAt,
        renewOffset: site.renewOffset,
        renewStagger: site.renewStagger,
        renewAt: site.renewAt,
        subscriberEmail: site.subscriberEmail,
        customerEmail: site.customerEmail,
        challenges: site.challenges,
        store: site.store
    };
    */

    // no transaction promise here because it calls set
    greenlock._find = async function(args) {
        args = _mangleFindArgs(args);
        var ours = await mega.find(args);
        if (!myFind) {
            return ours;
        }

        // if the user has an overlay find function we'll do a diff
        // between the managed state and the overlay, and choose
        // what was found.
        var theirs = await myFind(args);
        theirs = theirs.filter(function(site) {
            if (!site || 'string' !== typeof site.subject) {
                throw new Error('found site is missing subject');
            }
            if (
                !Array.isArray(site.altnames) ||
                !site.altnames.length ||
                !site.altnames[0] ||
                site.altnames[0] !== site.subject
            ) {
                throw new Error('missing or malformed altnames');
            }
            ['renewAt', 'issuedAt', 'expiresAt'].forEach(function(k) {
                if (site[k]) {
                    throw new Error(
                        '`' +
                            k +
                            '` should be updated by `set()`, not by `find()`'
                    );
                }
            });
            if (!site) {
                return;
            }
            if (args.subject && site.subject !== args.subject) {
                return false;
            }

            var servernames = args.servernames || args.altnames;
            if (
                servernames &&
                !site.altnames.some(function(altname) {
                    return servernames.includes(altname);
                })
            ) {
                return false;
            }

            return site.renewAt < (args.renewBefore || Infinity);
        });
        return _mergeFind(ours, theirs);
    };

    function _mergeFind(ours, theirs) {
        var toUpdate = [];
        theirs.forEach(function(_newer) {
            var hasCurrent = ours.some(function(_older) {
                var changed = false;
                if (_newer.subject !== _older.subject) {
                    return false;
                }

                // BE SURE TO SET THIS UNDEFINED AFTERWARDS
                _older._exists = true;

                _newer.deletedAt = _newer.deletedAt || 0;
                Object.keys(_newer).forEach(function(k) {
                    if (_older[k] !== _newer[k]) {
                        changed = true;
                        _older[k] = _newer[k];
                    }
                });
                if (changed) {
                    toUpdate.push(_older);
                }

                // handled the (only) match
                return true;
            });
            if (!hasCurrent) {
                toUpdate.push(_newer);
            }
        });

        // delete the things that are gone
        ours.forEach(function(_older) {
            if (!_older._exists) {
                _older.deletedAt = Date.now();
                toUpdate.push(_older);
            }
            _older._exists = undefined;
        });

        Promise.all(
            toUpdate.map(function(site) {
                return greenlock.sites.update(site).catch(function(err) {
                    console.error(
                        'Developer Error: cannot update sites from user-supplied `find()`:'
                    );
                    console.error(err);
                });
            })
        );

        // ours is updated from theirs
        return ours;
    }

    greenlock.manager.init = mega.init;
};

function checkSubject(args) {
    if (!args || !args.subject) {
        throw new Error('you must specify `subject` when configuring a site');
    }
    /*
		if (!args.subject) {
			throw E.NO_SUBJECT('add');
		}
    */

    var subject = (args.subject || '').toLowerCase();
    if (subject !== args.subject) {
        console.warn('`subject` must be lowercase', args.subject);
    }

    return U._encodeName(subject);
}

function checkAltnames(subject, args) {
    // the things we have to check and get right
    var altnames = (args.altnames || []).map(function(name) {
        return String(name || '').toLowerCase();
    });

    // punycode BEFORE validation
    // (set, find, remove)
    if (altnames.join() !== args.altnames.join()) {
        console.warn(
            'all domains in `altnames` must be lowercase:',
            args.altnames
        );
    }

    args.altnames = args.altnames.map(U._encodeName);
    if (
        !args.altnames.every(function(d) {
            return U._validName(d);
        })
    ) {
        throw E.INVALID_HOSTNAME('add', "'" + args.altnames.join("' '") + "'");
    }

    if (subject && subject !== args.altnames[0]) {
        throw E.BAD_ORDER(
            'add',
            '(' + args.subject + ") '" + args.altnames.join("' '") + "'"
        );
    }
    /*
    if (subject && subject !== altnames[0]) {
        throw new Error(
            '`subject` must be the first domain in `altnames`',
            args.subject,
            altnames.join(' ')
        );
    }
    */

    return altnames;
}

function loadManager(gconf) {
    var m;
    // 1. Get the manager
    // 2. Figure out if we need to wrap it

    if (!gconf.manager) {
        gconf.manager = '@greenlock/manager';
    }

    if ('string' !== typeof gconf.manager) {
        throw new Error(
            '`manager` should be a string representing the npm name or file path of the module'
        );
    }
    try {
        // wrap this to be safe for @greenlock/manager
        m = require(gconf.manager).create(gconf);
    } catch (e) {
        console.error('Error loading manager:');
        console.error(e.code);
        console.error(e.message);
    }

    if (!m) {
        console.error();
        console.error(
            'Failed to load manager plugin ',
            JSON.stringify(gconf.manager)
        );
        console.error();
        process.exit(1);
    }

    return m;
}

function mergeManager(gconf) {
    var mng;
    function m() {
        if (mng) {
            return mng;
        }
        mng = require('@greenlock/manager').create(gconf);
        return mng;
    }

    var mini = loadManager(gconf);
    var mega = {};
    // optional
    if (mini.defaults) {
        mega.defaults = function(opts) {
            return mini.defaults(opts);
        };
    } else {
        mega.defaults = m().defaults;
    }

    // optional
    if (mini.remove) {
        mega.remove = function(opts) {
            return mini.remove(opts);
        };
    } else {
        mega.remove = function(opts) {
            mega.get(opts).then(function(site) {
                if (!site) {
                    return null;
                }
                site.deletedAt = Date.now();
                return mega.set(site).then(function() {
                    return site;
                });
            });
        };
    }

    if (mini.find) {
        // without this there cannot be fully automatic renewal
        mega.find = function(opts) {
            return mini.find(opts);
        };
    }

    // set and (find and/or get) should be from the same set
    if (mini.set) {
        mega.set = function(opts) {
            if (!mini.find) {
                // TODO create the list so that find can be implemented
            }
            return mini.set(opts);
        };
    } else {
        mega.set = m().set;
        mega.get = m().get;
    }

    if (mini.get) {
        mega.get = function(opts) {
            return mini.get(opts);
        };
    } else if (mini.find) {
        mega.get = function(opts) {
            var servername = opts.servername;
            delete opts.servername;
            opts.servernames = (servername && [servername]) || undefined;
            return mini.find(opts).then(function(sites) {
                return sites.filter(function(site) {
                    return site.altnames.include(servername);
                })[0];
            });
        };
    } else if (mini.set) {
        throw new Error(
            gconf.manager + ' implements `set()`, but not `get()` or `find()`'
        );
    } else {
        mega.find = m().find;
        mega.get = m().get;
    }

    if (!mega.get) {
        mega.get = function(opts) {
            var servername = opts.servername;
            delete opts.servername;
            opts.servernames = (servername && [servername]) || undefined;
            return mega.find(opts).then(function(sites) {
                return sites.filter(function(site) {
                    return site.altnames.include(servername);
                })[0];
            });
        };
    }

    mega.init = function(deps) {
        if (mini.init) {
            return mini.init(deps).then(function() {
                if (mng) {
                    return mng.init(deps);
                }
            });
        } else if (mng) {
            return mng.init(deps);
        } else {
            return Promise.resolve(null);
        }
    };

    return mega;
}

function _mangleFindArgs(args) {
    var servernames = (args.servernames || [])
        .concat(args.altnames || [])
        .filter(Boolean)
        .slice(0);
    var modified = servernames.slice(0);

    // servername, wildname, and altnames are all the same
    ['wildname', 'servername'].forEach(function(k) {
        var altname = args[k] || '';
        if (altname && !modified.includes(altname)) {
            modified.push(altname);
        }
    });

    if (modified.length) {
        servernames = modified;
        servernames = servernames.map(U._encodeName);
        args.altnames = servernames;
        args.servernames = args.altnames = checkAltnames(false, args);
    }

    // documented as args.servernames
    // preserved as args.altnames for v3 beta backwards compat
    // my only hesitancy in this choice is that a "servername"
    // may NOT contain '*.', in which case `altnames` is a better choice.
    // However, `altnames` is ambiguous - as if it means to find a
    // certificate by that specific collection of altnames.
    // ... perhaps `domains` could work?
    return args;
}