backtrace-service
Advanced tools
Comparing version 1.2.1 to 1.2.2
import { IServiceDescriptor } from '../model/descriptor'; | ||
export interface ICoronerDescriptor { | ||
name: string; | ||
server: string; | ||
secret: string; | ||
resource: string; | ||
proxy: boolean; | ||
enabled: boolean; | ||
} | ||
import { IDescriptorOpts } from './model/descriptorOptions'; | ||
/** | ||
@@ -20,7 +13,2 @@ * getBackupConfig fetches a config file provided by the service itself. | ||
export declare function getProperConfig(serviceName: string): object | undefined; | ||
export interface IDescriptorOpts { | ||
server?: string; | ||
resource?: string; | ||
proxy?: boolean; | ||
} | ||
/** | ||
@@ -30,2 +18,3 @@ * getDescriptor fetches the service's integration parameters. | ||
* @param defaultPort - the default port number for the service. | ||
* @param opts - descriptor options | ||
*/ | ||
@@ -32,0 +21,0 @@ export declare function getDescriptor(serviceName: string, defaultPort: number, opts?: IDescriptorOpts): IServiceDescriptor; |
@@ -10,7 +10,6 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var crypto_1 = require("crypto"); | ||
var fs = __importStar(require("fs")); | ||
var url = __importStar(require("url")); | ||
var log = __importStar(require("../log/log")); | ||
var crypto_1 = require("crypto"); | ||
var url = __importStar(require("url")); | ||
; | ||
/** | ||
@@ -22,3 +21,5 @@ * getBackupConfig fetches a config file provided by the service itself. | ||
try { | ||
var backupJson = fs.readFileSync(process.cwd() + "/" + serviceName + ".conf", { encoding: 'utf8' }); | ||
var backupJson = fs.readFileSync(process.cwd() + "/" + serviceName + ".conf", { | ||
encoding: 'utf8', | ||
}); | ||
return JSON.parse(backupJson); | ||
@@ -40,7 +41,9 @@ } | ||
try { | ||
var serviceConfigJson = fs.readFileSync("/etc/backtrace/" + serviceName + "/" + serviceName + ".conf", { encoding: 'utf8' }); | ||
var serviceConfigJson = fs.readFileSync("/etc/backtrace/" + serviceName + "/" + serviceName + ".conf", { | ||
encoding: 'utf8', | ||
}); | ||
return JSON.parse(serviceConfigJson); | ||
} | ||
catch (error) { | ||
log.warning("No configuration file found at /etc/backtrace/" + serviceName + "/" + serviceName + ".conf.\n\n The primary configuration should be located /etc/backtrace/" + serviceName + "/" + serviceName + ".conf without the proper configuration file the " + serviceName + " service may not work as intended"); | ||
log.warning("No configuration file found at /etc/backtrace/" + serviceName + "/" + serviceName + ".conf.\n\n The primary configuration should be located /etc/backtrace/" + serviceName + "/" + serviceName + ". \n\n conf without the proper configuration file the " + serviceName + " service may not work as intended"); | ||
return undefined; | ||
@@ -50,3 +53,2 @@ } | ||
exports.getProperConfig = getProperConfig; | ||
; | ||
/** | ||
@@ -56,47 +58,34 @@ * getDescriptor fetches the service's integration parameters. | ||
* @param defaultPort - the default port number for the service. | ||
* @param opts - descriptor options | ||
*/ | ||
function getDescriptor(serviceName, defaultPort, opts) { | ||
if (opts === void 0) { opts = {}; } | ||
var varprefix = process.env.SERVICE_DESCRIPTOR_VARPREFIX || | ||
'/var/run/coronerd/services.d'; | ||
var varpath = process.env.SERVICE_DESCRIPTOR_VARPATH || | ||
var varprefix = process.env.SERVICE_DESCRIPTOR_VARPREFIX || '/var/run/coronerd/services.d'; | ||
var varPath = process.env.SERVICE_DESCRIPTOR_VARPATH || | ||
varprefix + "/" + serviceName + ".json"; | ||
var descr = {}; | ||
var paths = []; | ||
var i = 0; | ||
if (!process.env.SERVICE_DESCRIPTOR_PATH) { | ||
paths.push("/etc/coronerd/services.d/" + serviceName + ".json"); | ||
paths.push(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 | ||
if (!desc) { | ||
desc = getDefaultDescriptor(serviceName, defaultPort, opts); | ||
} | ||
else { | ||
paths.push(process.env.SERVICE_DESCRIPTOR_PATH); | ||
var surl = url.parse(desc.server); | ||
if (!surl.port && surl.protocol !== 'https:') { | ||
throw new Error('Unspecified port number requires https'); | ||
} | ||
for (; i < paths.length; i++) { | ||
try { | ||
descr = JSON.parse(fs.readFileSync(paths[i], { encoding: 'utf8' })); | ||
break; | ||
} | ||
catch (error) { | ||
} | ||
// generate configuration after checking port and url | ||
// to solve problem when user path invalid server and port in options | ||
if (generateConf) { | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
} | ||
if (i === paths.length) { | ||
log.info("Generating service integration for " + serviceName + " in " + varpath); | ||
descr.name = serviceName; | ||
descr.secret = crypto_1.randomBytes(32).toString('hex'); | ||
descr.server = opts.server || "https://0.0.0.0:" + defaultPort; | ||
descr.resource = opts.resource || "/api/" + serviceName; | ||
descr.proxy = opts.proxy === false ? false : true; | ||
descr.enabled = true; | ||
fs.writeFileSync(varpath, JSON.stringify(descr, null, 2)); | ||
var port = 443; | ||
if (surl.port) { | ||
port = parseInt(surl.port, 10); | ||
} | ||
var surl = url.parse(descr.server); | ||
if (!surl.port && surl.protocol !== 'https:') | ||
throw new Error("unspecified port number requires https"); | ||
var port = 443; | ||
if (surl.port) | ||
port = parseInt(surl.port); | ||
return { | ||
name: descr.name, | ||
resource: descr.resource, | ||
secret: descr.secret, | ||
name: desc.name, | ||
resource: desc.resource, | ||
secret: desc.secret, | ||
port: port, | ||
@@ -106,2 +95,30 @@ }; | ||
exports.getDescriptor = getDescriptor; | ||
function getDefaultDescriptor(serviceName, defaultPort, opts) { | ||
return { | ||
name: serviceName, | ||
secret: crypto_1.randomBytes(32).toString('hex'), | ||
server: opts.server || "https://0.0.0.0:" + defaultPort, | ||
resource: opts.resource || "/api/" + serviceName, | ||
proxy: opts.proxy || false, | ||
enabled: true, | ||
}; | ||
} | ||
function readDescriptorConfiguration(paths) { | ||
for (var _i = 0, paths_1 = paths; _i < paths_1.length; _i++) { | ||
var path = paths_1[_i]; | ||
try { | ||
var conf = JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })); | ||
return conf; | ||
} | ||
catch (err) { | ||
continue; | ||
} | ||
} | ||
return undefined; | ||
} | ||
function getDescriptorPaths(serviceName, varPath) { | ||
return !process.env.SERVICE_DESCRIPTOR_PATH | ||
? ["/etc/coronerd/services.d/" + serviceName + ".json", varPath] | ||
: [process.env.SERVICE_DESCRIPTOR_PATH]; | ||
} | ||
/** | ||
@@ -108,0 +125,0 @@ * getConfig fetches from the expected place on the machine (outside the service). |
import { NextFunction, Request, Response } from 'express'; | ||
import { IServiceDescriptor } from '../model/descriptor'; | ||
import { ICoronerRequestOption } from './model/authRequestOptions'; | ||
import { IServerConfiguration } from './model/serverConfiguration'; | ||
import { IServiceDescriptor } from '../model/descriptor'; | ||
/** | ||
@@ -6,0 +6,0 @@ * Identity manager |
@@ -87,3 +87,3 @@ "use strict"; | ||
if (!url || !nonce || !hmac) { | ||
responseResult_1.ResponseResult.badRequest(response, "missing parameters \n " + (!url ? 'url' : '') + " \n " + (!nonce ? 'nonce' : '') + " \n " + (!hmac ? 'hmac' : '') + " "); | ||
responseResult_1.ResponseResult.badRequest(response, "missing parameters\n " + (!url ? 'url' : '') + "\n " + (!nonce ? 'nonce' : '') + "\n " + (!hmac ? 'hmac' : '')); | ||
} | ||
@@ -272,3 +272,4 @@ if (!_this.checkHmac(_this.descr.secret, nonce, hmac)) { | ||
token: request.get('X-Coroner-Token'), | ||
url: request.get('X-Coroner-Location') || request.get('origin'), | ||
url: request.get('X-Coroner-Location') || | ||
request.get('origin'), | ||
}; | ||
@@ -275,0 +276,0 @@ request.coronerAuth = data; |
@@ -5,3 +5,3 @@ /** | ||
export { IdentityManager } from './identity/identity'; | ||
export { IAuthRequestOptions, ICoronerRequestOption } from './identity/model/authRequestOptions'; | ||
export { IAuthRequestOptions, ICoronerRequestOption, } from './identity/model/authRequestOptions'; | ||
/** | ||
@@ -15,2 +15,7 @@ * Logging | ||
export { UniverseHelper } from './universe/backtraceUniverseHelper'; | ||
export { getBackupConfig, getConfig, getDescriptor, getProperConfig } from './config/config'; | ||
/** | ||
* Service configuration helpers | ||
*/ | ||
export { getBackupConfig, getConfig, getDescriptor, getProperConfig, } from './config/config'; | ||
export { ICoronerDescriptor } from './config/model/coronerDescriptor'; | ||
export { IDescriptorOpts } from './config/model/descriptorOptions'; |
@@ -20,2 +20,5 @@ "use strict"; | ||
exports.UniverseHelper = backtraceUniverseHelper_1.UniverseHelper; | ||
/** | ||
* Service configuration helpers | ||
*/ | ||
var config_1 = require("./config/config"); | ||
@@ -22,0 +25,0 @@ exports.getBackupConfig = config_1.getBackupConfig; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
; | ||
//# sourceMappingURL=descriptor.js.map |
{ | ||
"name": "backtrace-service", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Common tools for Backtrace Node services", | ||
@@ -5,0 +5,0 @@ "author": "Backtrace", |
@@ -0,24 +1,18 @@ | ||
import { randomBytes } from 'crypto'; | ||
import * as fs from 'fs'; | ||
import * as url from 'url'; | ||
import * as log from '../log/log'; | ||
import { randomBytes } from 'crypto'; | ||
import * as url from 'url'; | ||
import { IServiceDescriptor } from '../model/descriptor'; | ||
import { ICoronerDescriptor } from './model/coronerDescriptor'; | ||
import { IDescriptorOpts } from './model/descriptorOptions'; | ||
/* What coronerd sees; not intended for service consumption. */ | ||
export interface ICoronerDescriptor { | ||
name: string, | ||
server: string, | ||
secret: string, | ||
resource: string, | ||
proxy: boolean, | ||
enabled: boolean, | ||
}; | ||
/** | ||
* getBackupConfig fetches a config file provided by the service itself. | ||
* @param serviceName - service name - for example: share/saml/comments | ||
* @param serviceName - service name - for example: share/saml/comments | ||
*/ | ||
export function getBackupConfig(serviceName: string): object { | ||
try { | ||
const backupJson = fs.readFileSync(`${process.cwd()}/${serviceName}.conf`, { encoding: 'utf8' }); | ||
const backupJson = fs.readFileSync(`${process.cwd()}/${serviceName}.conf`, { | ||
encoding: 'utf8', | ||
}); | ||
return JSON.parse(backupJson); | ||
@@ -35,7 +29,12 @@ } catch (error) { | ||
* getProperConfig fetches from the expected place on the machine (outside the service). | ||
* @param serviceName - service name - for example: share/saml/comments | ||
* @param serviceName - service name - for example: share/saml/comments | ||
*/ | ||
export function getProperConfig(serviceName: string): object | undefined{ | ||
export function getProperConfig(serviceName: string): object | undefined { | ||
try { | ||
const serviceConfigJson = fs.readFileSync(`/etc/backtrace/${serviceName}/${serviceName}.conf`, { encoding: 'utf8' }); | ||
const serviceConfigJson = fs.readFileSync( | ||
`/etc/backtrace/${serviceName}/${serviceName}.conf`, | ||
{ | ||
encoding: 'utf8', | ||
}, | ||
); | ||
return JSON.parse(serviceConfigJson); | ||
@@ -45,3 +44,4 @@ } catch (error) { | ||
`No configuration file found at /etc/backtrace/${serviceName}/${serviceName}.conf.\n | ||
The primary configuration should be located /etc/backtrace/${serviceName}/${serviceName}.conf without the proper configuration file the ${serviceName} service may not work as intended`, | ||
The primary configuration should be located /etc/backtrace/${serviceName}/${serviceName}. \n | ||
conf without the proper configuration file the ${serviceName} service may not work as intended`, | ||
); | ||
@@ -52,8 +52,2 @@ return undefined; | ||
export interface IDescriptorOpts { | ||
server?: string, | ||
resource?: string, | ||
proxy?: boolean, | ||
}; | ||
/** | ||
@@ -63,61 +57,84 @@ * getDescriptor fetches the service's integration parameters. | ||
* @param defaultPort - the default port number for the service. | ||
* @param opts - descriptor options | ||
*/ | ||
export function getDescriptor(serviceName: string, defaultPort: number, | ||
opts: IDescriptorOpts = {}): IServiceDescriptor { | ||
const varprefix = process.env.SERVICE_DESCRIPTOR_VARPREFIX || | ||
'/var/run/coronerd/services.d'; | ||
const varpath = process.env.SERVICE_DESCRIPTOR_VARPATH || | ||
export function getDescriptor( | ||
serviceName: string, | ||
defaultPort: number, | ||
opts: IDescriptorOpts = {}, | ||
): IServiceDescriptor { | ||
const varprefix = | ||
process.env.SERVICE_DESCRIPTOR_VARPREFIX || '/var/run/coronerd/services.d'; | ||
const varPath = | ||
process.env.SERVICE_DESCRIPTOR_VARPATH || | ||
`${varprefix}/${serviceName}.json`; | ||
let descr: ICoronerDescriptor = <ICoronerDescriptor> {}; | ||
let paths = []; | ||
let i = 0; | ||
if (!process.env.SERVICE_DESCRIPTOR_PATH) { | ||
paths.push(`/etc/coronerd/services.d/${serviceName}.json`); | ||
paths.push(varpath); | ||
} else { | ||
paths.push(process.env.SERVICE_DESCRIPTOR_PATH); | ||
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 | ||
if (!desc) { | ||
desc = getDefaultDescriptor(serviceName, defaultPort, opts); | ||
} | ||
for (; i < paths.length; i++) { | ||
try { | ||
descr = JSON.parse(fs.readFileSync(paths[i], { encoding: 'utf8' })); | ||
break; | ||
} catch (error) { | ||
} | ||
const surl = url.parse(desc.server); | ||
if (!surl.port && surl.protocol !== 'https:') { | ||
throw new Error('Unspecified port number requires https'); | ||
} | ||
if (i === paths.length) { | ||
log.info(`Generating service integration for ${serviceName} in ${varpath}`); | ||
descr.name = serviceName; | ||
descr.secret = randomBytes(32).toString('hex'); | ||
descr.server = opts.server || `https://0.0.0.0:${defaultPort}`; | ||
descr.resource = opts.resource || `/api/${serviceName}`; | ||
descr.proxy = opts.proxy === false ? false : true; | ||
descr.enabled = true; | ||
fs.writeFileSync(varpath, JSON.stringify(descr, null, 2)); | ||
// generate configuration after checking port and url | ||
// to solve problem when user path invalid server and port in options | ||
if (generateConf) { | ||
fs.writeFileSync(varPath, JSON.stringify(desc, null, 2)); | ||
} | ||
const surl = url.parse(descr.server); | ||
if (!surl.port && surl.protocol !== 'https:') | ||
throw new Error("unspecified port number requires https"); | ||
let port: number = 443; | ||
if (surl.port) | ||
port = parseInt(surl.port); | ||
if (surl.port) { | ||
port = parseInt(surl.port, 10); | ||
} | ||
return { | ||
name: descr.name, | ||
resource: descr.resource, | ||
secret: descr.secret, | ||
port: port, | ||
name: desc.name, | ||
resource: desc.resource, | ||
secret: desc.secret, | ||
port, | ||
}; | ||
} | ||
function getDefaultDescriptor( | ||
serviceName: string, | ||
defaultPort: number, | ||
opts: IDescriptorOpts, | ||
) { | ||
return { | ||
name: serviceName, | ||
secret: randomBytes(32).toString('hex'), | ||
server: opts.server || `https://0.0.0.0:${defaultPort}`, | ||
resource: opts.resource || `/api/${serviceName}`, | ||
proxy: opts.proxy || false, | ||
enabled: true, | ||
}; | ||
} | ||
function readDescriptorConfiguration( | ||
paths: string[], | ||
): ICoronerDescriptor | undefined { | ||
for (const path of paths) { | ||
try { | ||
const conf = JSON.parse(fs.readFileSync(path, { encoding: 'utf8' })); | ||
return conf; | ||
} catch (err) { | ||
continue; | ||
} | ||
} | ||
return undefined; | ||
} | ||
function getDescriptorPaths(serviceName: string, varPath: string): string[] { | ||
return !process.env.SERVICE_DESCRIPTOR_PATH | ||
? [`/etc/coronerd/services.d/${serviceName}.json`, varPath] | ||
: [process.env.SERVICE_DESCRIPTOR_PATH]; | ||
} | ||
/** | ||
* getConfig fetches from the expected place on the machine (outside the service). | ||
* getConfig fetches from the expected place on the machine (outside the service). | ||
* If configuration doesn't exists then getConfig method will try to fetch configuration from internal service path | ||
* @param serviceName - service name - for example: share/saml/comments | ||
* @param serviceName - service name - for example: share/saml/comments | ||
*/ | ||
@@ -124,0 +141,0 @@ |
@@ -5,5 +5,5 @@ import axios from 'axios'; | ||
import { NextFunction, Request, Response } from 'express'; | ||
import { IServiceDescriptor } from '../model/descriptor'; | ||
import { ResponseResult } from '../model/responseResult'; | ||
import { | ||
IAuthRequestOptions, | ||
ICoronerAuth, | ||
@@ -13,3 +13,2 @@ ICoronerRequestOption, | ||
import { IServerConfiguration } from './model/serverConfiguration'; | ||
import { IServiceDescriptor } from '../model/descriptor'; | ||
@@ -20,3 +19,5 @@ /** | ||
export class IdentityManager { | ||
private logger: { log: (level: string, log: string) => void } | undefined = undefined; | ||
private logger: | ||
| { log: (level: string, log: string) => void } | ||
| undefined = undefined; | ||
@@ -63,6 +64,6 @@ constructor(private descr: IServiceDescriptor) {} | ||
response, | ||
`missing parameters | ||
${!url ? 'url' : ''} | ||
${!nonce ? 'nonce' : ''} | ||
${!hmac ? 'hmac' : ''} `, | ||
`missing parameters | ||
${!url ? 'url' : ''} | ||
${!nonce ? 'nonce' : ''} | ||
${!hmac ? 'hmac' : ''}`, | ||
); | ||
@@ -104,3 +105,8 @@ } | ||
if (!token || !url) { | ||
this.log('error', `${request.ip}: missing internal params token: ${token} || url: ${url}`); | ||
this.log( | ||
'error', | ||
`${ | ||
request.ip | ||
}: missing internal params token: ${token} || url: ${url}`, | ||
); | ||
ResponseResult.badRequest(response, 'missing parameters'); | ||
@@ -121,3 +127,5 @@ return; | ||
if (result.status !== 200) { | ||
next(new Error(`Invalid server response code ${response.statusCode}`)); | ||
next( | ||
new Error(`Invalid server response code ${response.statusCode}`), | ||
); | ||
return; | ||
@@ -130,3 +138,5 @@ } | ||
if (!!coronerd_nonce && !!coronerd_hmac) { | ||
if (!this.checkHmac(this.descr.secret, coronerd_nonce, coronerd_hmac)) { | ||
if ( | ||
!this.checkHmac(this.descr.secret, coronerd_nonce, coronerd_hmac) | ||
) { | ||
ResponseResult.badRequest(response, 'missing parameters'); | ||
@@ -149,3 +159,6 @@ next(new Error('Invalid server generated HMAC')); | ||
.catch((err) => { | ||
ResponseResult.badRequest(response, 'Cannot retrieve information from server'); | ||
ResponseResult.badRequest( | ||
response, | ||
'Cannot retrieve information from server', | ||
); | ||
next(err); | ||
@@ -160,3 +173,5 @@ }); | ||
*/ | ||
public async loginCoronerd(coronerdUrl: string): Promise<IServerConfiguration | undefined> { | ||
public async loginCoronerd( | ||
coronerdUrl: string, | ||
): Promise<IServerConfiguration | undefined> { | ||
const prefix = coronerdUrl.endsWith('/') ? '' : '/'; | ||
@@ -176,3 +191,6 @@ const requestUrl = `${coronerdUrl}${prefix}api/login`; | ||
} catch (err) { | ||
this.log('error', `Cannot login to coronerd. Reason ${JSON.stringify(err)}`); | ||
this.log( | ||
'error', | ||
`Cannot login to coronerd. Reason ${JSON.stringify(err)}`, | ||
); | ||
return undefined; | ||
@@ -187,3 +205,6 @@ } | ||
*/ | ||
public async isValidToken(universeUrl: string, authToken: string): Promise<boolean> { | ||
public async isValidToken( | ||
universeUrl: string, | ||
authToken: string, | ||
): Promise<boolean> { | ||
const result = await this.getConfiguration(universeUrl, authToken); | ||
@@ -239,3 +260,5 @@ return result !== undefined; | ||
token: request.get('X-Coroner-Token') as string, | ||
url: (request.get('X-Coroner-Location') as string) || (request.get('origin') as string), | ||
url: | ||
(request.get('X-Coroner-Location') as string) || | ||
(request.get('origin') as string), | ||
}; | ||
@@ -242,0 +265,0 @@ (request as any).coronerAuth = data; |
@@ -5,3 +5,6 @@ /** | ||
export { IdentityManager } from './identity/identity'; | ||
export { IAuthRequestOptions, ICoronerRequestOption } from './identity/model/authRequestOptions'; | ||
export { | ||
IAuthRequestOptions, | ||
ICoronerRequestOption, | ||
} from './identity/model/authRequestOptions'; | ||
@@ -17,2 +20,13 @@ /** | ||
export { UniverseHelper } from './universe/backtraceUniverseHelper'; | ||
export { getBackupConfig, getConfig, getDescriptor, getProperConfig } from './config/config'; | ||
/** | ||
* Service configuration helpers | ||
*/ | ||
export { | ||
getBackupConfig, | ||
getConfig, | ||
getDescriptor, | ||
getProperConfig, | ||
} from './config/config'; | ||
export { ICoronerDescriptor } from './config/model/coronerDescriptor'; | ||
export { IDescriptorOpts } from './config/model/descriptorOptions'; |
/* A service descriptor, for service use. */ | ||
export interface IServiceDescriptor { | ||
name: string, | ||
port: number, | ||
resource: string, | ||
secret: string, | ||
}; | ||
name: string; | ||
port: number; | ||
resource: string; | ||
secret: string; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
84099
47
1458