backtrace-service
Advanced tools
Comparing version 1.2.8 to 1.2.9
@@ -19,3 +19,3 @@ import { IServiceDescriptor } from '../model/serviceDescriptor'; | ||
*/ | ||
export declare function getDescriptor(serviceName: string, defaultPort: number, opts?: IDescriptorOpts, createDir?: boolean): IServiceDescriptor; | ||
export declare function getDescriptor(serviceName: string, defaultPort: number, opts?: IDescriptorOpts): IServiceDescriptor; | ||
/** | ||
@@ -22,0 +22,0 @@ * getConfig fetches from the expected place on the machine (outside the service). |
@@ -19,2 +19,4 @@ "use strict"; | ||
var log = __importStar(require("../log/log")); | ||
// XXX: better default? | ||
var defaultCoronerdHostname = '0.0.0.0'; | ||
/** | ||
@@ -56,2 +58,26 @@ * getBackupConfig fetches a config file provided by the service itself. | ||
exports.getProperConfig = getProperConfig; | ||
function getCoronerdHostname() { | ||
var alldescPath = process.env.ALL_DESCRIPTOR_PATH || | ||
"/var/run/coronerd/svclayer/_all.json"; | ||
// if coronerd has supplied its REST URL, then use it. | ||
if (fs.existsSync(alldescPath)) { | ||
try { | ||
var alldesc = JSON.parse(fs.readFileSync(alldescPath, { encoding: 'utf8' })); | ||
if (alldesc && typeof (alldesc.coronerd) === 'string') { | ||
try { | ||
var crUrl = url.parse(alldesc.coronerd); | ||
if (crUrl.hostname) | ||
return crUrl.hostname; | ||
} | ||
catch (err) { | ||
// ignore errors and use fallback below | ||
} | ||
} | ||
} | ||
catch (err) { | ||
// ignore errors and use fallback below | ||
} | ||
} | ||
return defaultCoronerdHostname; | ||
} | ||
/** | ||
@@ -63,5 +89,4 @@ * getDescriptor fetches the service's integration parameters. | ||
*/ | ||
function getDescriptor(serviceName, defaultPort, opts, createDir) { | ||
function getDescriptor(serviceName, defaultPort, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
if (createDir === void 0) { createDir = true; } | ||
var varprefix = process.env.SERVICE_DESCRIPTOR_VARPREFIX || '/var/run/coronerd/services.d'; | ||
@@ -71,30 +96,37 @@ var varPath = process.env.SERVICE_DESCRIPTOR_VARPATH || | ||
var paths = getDescriptorPaths(serviceName, varPath); | ||
var desc = readDescriptorConfiguration(paths); | ||
var generateConf = !desc; | ||
// if descriptor doesn't exists in descriptor paths | ||
// generate new descriptor file with basic configuration | ||
var foundPath = ""; | ||
var desc; | ||
for (var _i = 0, paths_1 = paths; _i < paths_1.length; _i++) { | ||
var descriptorPath = paths_1[_i]; | ||
desc = readDescriptorConfiguration(descriptorPath); | ||
if (desc) { | ||
foundPath = descriptorPath; | ||
break; | ||
} | ||
} | ||
// if descriptor doesn't exist in the descriptor search path, | ||
// generate new descriptor file with default configuration in the | ||
// var path, where it can own the file. | ||
if (!desc) { | ||
desc = getDefaultDescriptor(serviceName, defaultPort, opts); | ||
mkdirp_1.default(path_1.default.basename(varPath), function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
}); | ||
} | ||
var surl = url.parse(desc.server); | ||
if (!surl.port && surl.protocol !== 'https:') { | ||
throw new Error('Unspecified port number requires https'); | ||
} | ||
// generate configuration after checking port and url | ||
// to solve problem when user path invalid server and port in options | ||
if (generateConf) { | ||
var dirPath = path_1.default.basename(varPath); | ||
if (fs.existsSync(dirPath)) { | ||
var surl = new url.URL(desc.server); | ||
// if the descriptor owned by the service was read in and happens to | ||
// contain the invalid-for-SSL default hostname "0.0.0.0" that was | ||
// written out previously, see whether it can be automatically fixed up. | ||
if (foundPath === varPath && surl.hostname === defaultCoronerdHostname) { | ||
var hostname = getCoronerdHostname(); | ||
if (hostname !== defaultCoronerdHostname) { | ||
surl.hostname = hostname; | ||
desc.server = surl.toString(); | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
} | ||
else if (createDir) { | ||
mkdirp_1.default(dirPath, function (err) { | ||
if (err) { | ||
throw err; | ||
} | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
}); | ||
} | ||
} | ||
var port = 443; | ||
var port = (surl.protocol === 'https:') ? 443 : 80; | ||
if (surl.port) { | ||
@@ -108,2 +140,3 @@ port = parseInt(surl.port, 10); | ||
port: port, | ||
protocol: surl.protocol.split(':')[0], | ||
}; | ||
@@ -113,22 +146,21 @@ } | ||
function getDefaultDescriptor(serviceName, defaultPort, opts) { | ||
var protocol = opts.protocol ? opts.protocol : 'http'; | ||
var hostname = getCoronerdHostname(); | ||
return { | ||
name: serviceName, | ||
secret: crypto_1.randomBytes(32).toString('hex'), | ||
server: opts.server || "https://0.0.0.0:" + defaultPort, | ||
server: opts.server || protocol + "://" + hostname + ":" + defaultPort, | ||
resource: opts.resource || "/api/" + serviceName, | ||
proxy: opts.proxy || false, | ||
proxy: opts.proxy !== undefined ? opts.proxy : true, | ||
enabled: true, | ||
}; | ||
} | ||
function readDescriptorConfiguration(paths) { | ||
for (var _i = 0, paths_1 = paths; _i < paths_1.length; _i++) { | ||
var descriptorPath = paths_1[_i]; | ||
try { | ||
var conf = JSON.parse(fs.readFileSync(descriptorPath, { encoding: 'utf8' })); | ||
return conf; | ||
} | ||
catch (err) { | ||
continue; | ||
} | ||
function readDescriptorConfiguration(descriptorPath) { | ||
try { | ||
var conf = JSON.parse(fs.readFileSync(descriptorPath, { encoding: 'utf8' })); | ||
return conf; | ||
} | ||
catch (err) { | ||
// ignore error | ||
} | ||
return undefined; | ||
@@ -135,0 +167,0 @@ } |
@@ -0,0 +0,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=coronerDescriptor.js.map |
@@ -5,2 +5,3 @@ export interface IDescriptorOpts { | ||
proxy?: boolean; | ||
protocol?: string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=descriptorOptions.js.map |
@@ -0,0 +0,0 @@ import { NextFunction, Request, Response } from 'express'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ export interface ICoronerRequestOption { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=authRequestOptions.js.map |
@@ -0,0 +0,0 @@ export interface IConfig { |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=serverConfiguration.js.map |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ "use strict"; |
export declare function info(log: string): void; | ||
export declare function warning(log: string): void; | ||
export declare function error(log: string): void; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { Response } from 'express'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -6,2 +6,3 @@ export interface IServiceDescriptor { | ||
secret: string; | ||
protocol: string; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=serviceDescriptor.js.map |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "backtrace-service", | ||
"version": "1.2.8", | ||
"version": "1.2.9", | ||
"description": "Common tools for Backtrace Node services", | ||
@@ -5,0 +5,0 @@ "author": "Backtrace", |
@@ -0,0 +0,0 @@ # Backtrace Service Layer nodejs library |
@@ -10,2 +10,6 @@ import { randomBytes } from 'crypto'; | ||
import { IDescriptorOpts } from './model/descriptorOptions'; | ||
// XXX: better default? | ||
const defaultCoronerdHostname = '0.0.0.0'; | ||
/** | ||
@@ -52,2 +56,27 @@ * getBackupConfig fetches a config file provided by the service itself. | ||
function getCoronerdHostname() { | ||
const alldescPath = process.env.ALL_DESCRIPTOR_PATH || | ||
"/var/run/coronerd/svclayer/_all.json"; | ||
// if coronerd has supplied its REST URL, then use it. | ||
if (fs.existsSync(alldescPath)) { | ||
try { | ||
const alldesc = JSON.parse( | ||
fs.readFileSync(alldescPath, { encoding: 'utf8' }), | ||
); | ||
if (alldesc && typeof(alldesc.coronerd) === 'string') { | ||
try { | ||
const crUrl = url.parse(alldesc.coronerd); | ||
if (crUrl.hostname) | ||
return crUrl.hostname; | ||
} catch (err) { | ||
// ignore errors and use fallback below | ||
} | ||
} | ||
} catch (err) { | ||
// ignore errors and use fallback below | ||
} | ||
} | ||
return defaultCoronerdHostname; | ||
} | ||
/** | ||
@@ -63,3 +92,2 @@ * getDescriptor fetches the service's integration parameters. | ||
opts: IDescriptorOpts = {}, | ||
createDir: boolean = true, | ||
): IServiceDescriptor { | ||
@@ -73,31 +101,39 @@ const varprefix = | ||
const paths: string[] = getDescriptorPaths(serviceName, varPath); | ||
let desc = readDescriptorConfiguration(paths); | ||
const generateConf = !desc; | ||
// if descriptor doesn't exists in descriptor paths | ||
// generate new descriptor file with basic configuration | ||
let foundPath = ""; | ||
let desc: ICoronerDescriptor | undefined; | ||
for (const descriptorPath of paths) { | ||
desc = readDescriptorConfiguration(descriptorPath); | ||
if (desc) { | ||
foundPath = descriptorPath; | ||
break; | ||
} | ||
} | ||
// if descriptor doesn't exist in the descriptor search path, | ||
// generate new descriptor file with default configuration in the | ||
// var path, where it can own the file. | ||
if (!desc) { | ||
desc = getDefaultDescriptor(serviceName, defaultPort, opts); | ||
mkdirp(path.basename(varPath), (err: Error) => { | ||
if (err) { | ||
throw err; | ||
} | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
}); | ||
} | ||
const surl = url.parse(desc.server); | ||
if (!surl.port && surl.protocol !== 'https:') { | ||
throw new Error('Unspecified port number requires https'); | ||
} | ||
let surl = new url.URL(desc.server); | ||
// generate configuration after checking port and url | ||
// to solve problem when user path invalid server and port in options | ||
if (generateConf) { | ||
const dirPath = path.basename(varPath); | ||
if (fs.existsSync(dirPath)) { | ||
// if the descriptor owned by the service was read in and happens to | ||
// contain the invalid-for-SSL default hostname "0.0.0.0" that was | ||
// written out previously, see whether it can be automatically fixed up. | ||
if (foundPath === varPath && surl.hostname === defaultCoronerdHostname) { | ||
const hostname = getCoronerdHostname(); | ||
if (hostname !== defaultCoronerdHostname) { | ||
surl.hostname = hostname; | ||
desc.server = surl.toString(); | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
} else if (createDir) { | ||
mkdirp(dirPath, (err: Error) => { | ||
if (err) { | ||
throw err; | ||
} | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
}); | ||
} | ||
} | ||
let port: number = 443; | ||
let port: number = (surl.protocol === 'https:') ? 443 : 80; | ||
if (surl.port) { | ||
@@ -111,3 +147,4 @@ port = parseInt(surl.port, 10); | ||
secret: desc.secret, | ||
port, | ||
port: port, | ||
protocol: surl.protocol.split(':')[0], | ||
}; | ||
@@ -121,26 +158,28 @@ } | ||
) { | ||
const protocol = opts.protocol ? opts.protocol : 'http'; | ||
const hostname = getCoronerdHostname(); | ||
return { | ||
name: serviceName, | ||
secret: randomBytes(32).toString('hex'), | ||
server: opts.server || `https://0.0.0.0:${defaultPort}`, | ||
server: opts.server || `${protocol}://${hostname}:${defaultPort}`, | ||
resource: opts.resource || `/api/${serviceName}`, | ||
proxy: opts.proxy || false, | ||
proxy: opts.proxy !== undefined ? opts.proxy : true, | ||
enabled: true, | ||
}; | ||
} | ||
function readDescriptorConfiguration( | ||
paths: string[], | ||
descriptorPath: string, | ||
): ICoronerDescriptor | undefined { | ||
for (const descriptorPath of paths) { | ||
try { | ||
const conf = JSON.parse( | ||
fs.readFileSync(descriptorPath, { encoding: 'utf8' }), | ||
); | ||
return conf; | ||
} catch (err) { | ||
continue; | ||
} | ||
try { | ||
const conf = JSON.parse( | ||
fs.readFileSync(descriptorPath, { encoding: 'utf8' }), | ||
); | ||
return conf; | ||
} catch (err) { | ||
// ignore error | ||
} | ||
return undefined; | ||
} | ||
function getDescriptorPaths(serviceName: string, varPath: string): string[] { | ||
@@ -147,0 +186,0 @@ return !process.env.SERVICE_DESCRIPTOR_PATH |
@@ -0,0 +0,0 @@ /** |
@@ -5,2 +5,3 @@ export interface IDescriptorOpts { | ||
proxy?: boolean; | ||
protocol?: string; | ||
} |
@@ -0,0 +0,0 @@ import axios from 'axios'; |
@@ -0,0 +0,0 @@ export interface ICoronerRequestOption { |
@@ -0,0 +0,0 @@ export interface IConfig { |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import chalk from 'chalk'; |
@@ -7,2 +7,3 @@ /* A service descriptor, for service use. */ | ||
secret: string; | ||
protocol: string; | ||
} |
@@ -0,0 +0,0 @@ import * as url from 'url'; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
90934
50
1568
10