async function resolveCertificate()

in lib/CertificateTools.js [310:380]


async function resolveCertificate() {
    let options = {};
    let keyPath = path.join(__dirname, '..', config.server.privateKey);
    let certPath = path.join(__dirname, '..', config.server.certificate);
    let pfxPath = path.join(__dirname, '..', config.server.pfx);

    if (config.server.passphrase) {
        options.passphrase = config.server.passphrase;
    }
    if (await fs.exists(keyPath)) {
        options.key = await fs.readFile(keyPath);
    }
    if (await fs.exists(certPath)) {
        options.cert = await fs.readFile(certPath);
    }
    if (await fs.exists(pfxPath)) {
        options.pfx = await fs.readFile(pfxPath);
    }

    let CertFileVerified = await veryfyCertFile(keyPath, certPath, pfxPath, options.passphrase);

    if ((!options.cert && !options.pfx) || !CertFileVerified) {
        ConsoleWriter.warn("Local valid certificate not found.");
        ConsoleWriter.info("Checking global instance of pbiviz certificate...");
        let globalPbivizOptions = await getGlobalPbivizCerts();

        if (!globalPbivizOptions.cert && !globalPbivizOptions.pfx) {
            await createCertFile(config, true);
            if (!(await getCertFile(config, true))) {
                ConsoleWriter.error('Certificate wasn\'t created');
                throw new Error("Call `pbiviz --install-cert` command to create the new certificate");
            }
            else {
                if (config.server.passphrase) {
                    options.passphrase = config.server.passphrase;
                }
                if (await fs.exists(keyPath)) {
                    options.key = await fs.readFile(keyPath);
                }
                if (await fs.exists(certPath)) {
                    options.cert = await fs.readFile(certPath);
                }
                if (await fs.exists(pfxPath)) {
                    options.pfx = await fs.readFile(pfxPath);
                }
            }
        }
        else {
            // copy certs to local instance
            ConsoleWriter.info("Copy server certificate from global instance of pbiviz...");
            if (globalPbivizOptions.cert) {
                await fs.copyFile(globalPbivizOptions.cert, path.join(__dirname, '..', config.server.certificate));
                options.certificate = config.server.certificate;
            }
            if (globalPbivizOptions.key) {
                await fs.copyFile(globalPbivizOptions.key, path.join(__dirname, '..', config.server.privateKey));
                options.privateKey = config.server.privateKey;
            }
            if (globalPbivizOptions.pfx) {
                await fs.copyFile(globalPbivizOptions.pfx, path.join(__dirname, '..', config.server.pfx));
                // need to pass whole file instead patho to file
                options.pfx = await fs.readFile(path.join(__dirname, '..', config.server.pfx));
                options.passphrase = globalPbivizOptions.passphrase;
                // eslint-disable-next-line require-atomic-updates
                config.server.passphrase = globalPbivizOptions.passphrase;
            }
            await fs.writeFile(path.join(__dirname, confPath), JSON.stringify(config));
        }
    }
    return options;
}