@streamr/autocertifier-client
Advanced tools
Comparing version 100.0.0-pretestnet.6 to 100.0.0-rc.0
@@ -38,2 +38,18 @@ "use strict"; | ||
const logger = new utils_2.Logger(module); | ||
const ensureConfigFileWritable = (directory) => { | ||
const baseDirectory = getBaseDirectory(directory); | ||
fs_1.default.accessSync(baseDirectory, fs_1.default.constants.W_OK | fs_1.default.constants.R_OK); | ||
logger.trace(`Directory ${baseDirectory} is readable and writable`); | ||
}; | ||
const getBaseDirectory = (directory) => { | ||
const subDirs = directory.split(path_1.default.sep); | ||
do { | ||
const current = subDirs.join(path_1.default.sep); | ||
if (fs_1.default.existsSync(current)) { | ||
return current; | ||
} | ||
subDirs.pop(); | ||
} while (subDirs.length > 0); | ||
return path_1.default.sep; | ||
}; | ||
exports.SERVICE_ID = 'system/auto-certificer'; | ||
@@ -46,54 +62,9 @@ const ONE_DAY = 1000 * 60 * 60 * 24; | ||
class AutoCertifierClient extends eventemitter3_1.EventEmitter { | ||
updateTimeout; | ||
restClient; | ||
configFile; | ||
streamrWebSocketPort; | ||
ongoingSessions = new Set(); | ||
constructor(configFile, streamrWebSocketPort, restApiUrl, registerRpcMethod) { | ||
super(); | ||
this.ongoingSessions = new Set(); | ||
this.createCertificate = async () => { | ||
const sessionId = await this.restClient.createSession(); | ||
let certifiedSubdomain; | ||
this.ongoingSessions.add(sessionId); | ||
try { | ||
certifiedSubdomain = await this.restClient.createSubdomainAndCertificate(this.streamrWebSocketPort, sessionId); | ||
} | ||
finally { | ||
this.ongoingSessions.delete(sessionId); | ||
} | ||
const dir = path_1.default.dirname(this.configFile); | ||
// TODO: use async fs methods? | ||
if (!fs_1.default.existsSync(dir)) { | ||
fs_1.default.mkdirSync(dir, { recursive: true }); | ||
} | ||
fs_1.default.writeFileSync(this.configFile, JSON.stringify(certifiedSubdomain)); | ||
const certObj = forge.pki.certificateFromPem(certifiedSubdomain.certificate); | ||
const expirationTimestamp = certObj.validity.notAfter.getTime(); | ||
this.scheduleCertificateUpdate(expirationTimestamp); | ||
this.emit('updatedCertificate', certifiedSubdomain); | ||
}; | ||
this.updateCertificate = async () => { | ||
const sessionId = await this.restClient.createSession(); | ||
this.ongoingSessions.add(sessionId); | ||
const oldCertifiedSubdomain = JSON.parse(fs_1.default.readFileSync(this.configFile, 'utf8')); | ||
const updatedCertifiedSubdomain = await this.restClient.updateCertificate(oldCertifiedSubdomain.fqdn.split('.')[0], this.streamrWebSocketPort, oldCertifiedSubdomain.authenticationToken, sessionId); | ||
this.ongoingSessions.delete(sessionId); | ||
// TODO: use async fs methods? | ||
fs_1.default.writeFileSync(this.configFile, JSON.stringify(updatedCertifiedSubdomain)); | ||
const certObj = forge.pki.certificateFromPem(updatedCertifiedSubdomain.certificate); | ||
const expirationTimestamp = certObj.validity.notAfter.getTime(); | ||
this.scheduleCertificateUpdate(expirationTimestamp); | ||
// TODO: if the certificate was not updated there's no need to emit the event. Could compare certificates? | ||
this.emit('updatedCertificate', updatedCertifiedSubdomain); | ||
}; | ||
// This method should be called whenever the IP address or port of the node changes | ||
this.updateSubdomainIp = async () => { | ||
if (!fs_1.default.existsSync(this.configFile)) { | ||
logger.warn('updateSubdomainIp() called while subdomain file does not exist'); | ||
return; | ||
} | ||
// TODO: use async fs methods? | ||
const oldSubdomain = JSON.parse(fs_1.default.readFileSync(this.configFile, 'utf8')); | ||
logger.info('updateSubdomainIp() called for ' + oldSubdomain.fqdn); | ||
const sessionId = await this.restClient.createSession(); | ||
this.ongoingSessions.add(sessionId); | ||
await this.restClient.updateSubdomainIp(oldSubdomain.fqdn.split('.')[0], this.streamrWebSocketPort, sessionId, oldSubdomain.authenticationToken); | ||
this.ongoingSessions.delete(sessionId); | ||
}; | ||
this.restClient = new RestClient_1.RestClient(restApiUrl); | ||
@@ -154,8 +125,58 @@ this.configFile = (0, utils_1.filePathToNodeFormat)(configFile); | ||
// TODO: use tooling from @streamr/utils to set the timeout with an abortController. | ||
this.updateTimeout = setTimeout(this.ensureCertificateValidity, updateIn); | ||
this.updateTimeout = setTimeout(() => this.ensureCertificateValidity(), updateIn); | ||
} | ||
createCertificate = async () => { | ||
const dir = path_1.default.dirname(this.configFile); | ||
ensureConfigFileWritable(dir); | ||
const sessionId = await this.restClient.createSession(); | ||
let certifiedSubdomain; | ||
this.ongoingSessions.add(sessionId); | ||
try { | ||
certifiedSubdomain = await this.restClient.createSubdomainAndCertificate(this.streamrWebSocketPort, sessionId); | ||
} | ||
finally { | ||
this.ongoingSessions.delete(sessionId); | ||
} | ||
// TODO: use async fs methods? | ||
if (!fs_1.default.existsSync(dir)) { | ||
fs_1.default.mkdirSync(dir, { recursive: true }); | ||
} | ||
fs_1.default.writeFileSync(this.configFile, JSON.stringify(certifiedSubdomain)); | ||
const certObj = forge.pki.certificateFromPem(certifiedSubdomain.certificate); | ||
const expirationTimestamp = certObj.validity.notAfter.getTime(); | ||
this.scheduleCertificateUpdate(expirationTimestamp); | ||
this.emit('updatedCertificate', certifiedSubdomain); | ||
}; | ||
updateCertificate = async () => { | ||
const sessionId = await this.restClient.createSession(); | ||
this.ongoingSessions.add(sessionId); | ||
const oldCertifiedSubdomain = JSON.parse(fs_1.default.readFileSync(this.configFile, 'utf8')); | ||
const updatedCertifiedSubdomain = await this.restClient.updateCertificate(oldCertifiedSubdomain.fqdn.split('.')[0], this.streamrWebSocketPort, sessionId, oldCertifiedSubdomain.authenticationToken); | ||
this.ongoingSessions.delete(sessionId); | ||
// TODO: use async fs methods? | ||
fs_1.default.writeFileSync(this.configFile, JSON.stringify(updatedCertifiedSubdomain)); | ||
const certObj = forge.pki.certificateFromPem(updatedCertifiedSubdomain.certificate); | ||
const expirationTimestamp = certObj.validity.notAfter.getTime(); | ||
this.scheduleCertificateUpdate(expirationTimestamp); | ||
// TODO: if the certificate was not updated there's no need to emit the event. Could compare certificates? | ||
this.emit('updatedCertificate', updatedCertifiedSubdomain); | ||
}; | ||
// This method should be called whenever the IP address or port of the node changes | ||
updateSubdomainIp = async () => { | ||
if (!fs_1.default.existsSync(this.configFile)) { | ||
logger.warn('updateSubdomainIp() called while subdomain file does not exist'); | ||
return; | ||
} | ||
// TODO: use async fs methods? | ||
const oldSubdomain = JSON.parse(fs_1.default.readFileSync(this.configFile, 'utf8')); | ||
logger.info('updateSubdomainIp() called for ' + oldSubdomain.fqdn); | ||
const sessionId = await this.restClient.createSession(); | ||
this.ongoingSessions.add(sessionId); | ||
await this.restClient.updateSubdomainIp(oldSubdomain.fqdn.split('.')[0], this.streamrWebSocketPort, sessionId, oldSubdomain.authenticationToken); | ||
this.ongoingSessions.delete(sessionId); | ||
}; | ||
// IAutoCertifierRpc implementation | ||
// TODO: could move to the DHT package or move all rpc related logic here from AutoCertifierClientFacade in DHT | ||
async hasSession(request) { | ||
logger.trace('hasSession() called ' + this.ongoingSessions.size + ' ongoing sessions'); | ||
logger.info('hasSession() called ' + this.ongoingSessions.size + ' ongoing sessions'); | ||
if (this.ongoingSessions.has(request.sessionId)) { | ||
@@ -165,3 +186,3 @@ return { sessionId: request.sessionId }; | ||
else { | ||
throw `Session not found ${request.sessionId}`; | ||
throw new Error(`Session not found ${request.sessionId}`); | ||
} | ||
@@ -168,0 +189,0 @@ } |
@@ -19,2 +19,6 @@ "use strict"; | ||
class Err extends Error { | ||
code; | ||
// TODO: could remove httpStatus since we already have a higher level error? | ||
httpStatus; | ||
originalError; | ||
constructor(code, httpStatus, message, originalError) { | ||
@@ -21,0 +25,0 @@ super(message); |
@@ -10,7 +10,8 @@ "use strict"; | ||
class AutoCertifierRpcClient { | ||
_transport; | ||
typeName = AutoCertifier_1.AutoCertifierRpc.typeName; | ||
methods = AutoCertifier_1.AutoCertifierRpc.methods; | ||
options = AutoCertifier_1.AutoCertifierRpc.options; | ||
constructor(_transport) { | ||
this._transport = _transport; | ||
this.typeName = AutoCertifier_1.AutoCertifierRpc.typeName; | ||
this.methods = AutoCertifier_1.AutoCertifierRpc.methods; | ||
this.options = AutoCertifier_1.AutoCertifierRpc.options; | ||
} | ||
@@ -17,0 +18,0 @@ /** |
@@ -13,2 +13,3 @@ "use strict"; | ||
class RestClient { | ||
baseUrl; | ||
constructor(baseUrl) { | ||
@@ -37,3 +38,3 @@ this.baseUrl = baseUrl; | ||
}; | ||
const response = await this.patch(url, body); | ||
const response = await this.patch(url, body, 2 * 60 * 1000); | ||
return response; | ||
@@ -95,5 +96,5 @@ } | ||
// eslint-disable-next-line class-methods-use-this | ||
patch(url, body) { | ||
patch(url, body, timeout) { | ||
return new Promise((resolve, reject) => { | ||
request_1.default.patch(url, { json: body, rejectUnauthorized: false }, (error, response, body) => { | ||
request_1.default.patch(url, { json: body, rejectUnauthorized: false, timeout }, (error, response, body) => { | ||
if (error) { | ||
@@ -100,0 +101,0 @@ reject(error); |
{ | ||
"name": "@streamr/autocertifier-client", | ||
"version": "100.0.0-pretestnet.6", | ||
"version": "100.0.0-rc.0", | ||
"description": "Autocertifier Client for Streamr Network", | ||
@@ -19,8 +19,7 @@ "repository": { | ||
"clean": "jest --clearCache || true; rm -rf dist *.tsbuildinfo node_modules/.cache || true", | ||
"eslint": "eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'", | ||
"prepublishOnly": "npm run clean && NODE_ENV=production tsc -b tsconfig.node.json" | ||
"eslint": "eslint --cache --cache-location=node_modules/.cache/.eslintcache/ '*/**/*.{js,ts}'" | ||
}, | ||
"dependencies": { | ||
"@protobuf-ts/runtime-rpc": "^2.8.2", | ||
"@streamr/utils": "100.0.0-pretestnet.6", | ||
"@streamr/utils": "100.0.0-rc.0", | ||
"eventemitter3": "^5.0.0", | ||
@@ -31,6 +30,5 @@ "node-forge": "^1.3.1", | ||
"devDependencies": { | ||
"@types/node-forge": "^1.3.5", | ||
"@types/request": "^2.48.8", | ||
"ts-node": "^10.9.1" | ||
"@types/node-forge": "^1.3.11", | ||
"@types/request": "^2.48.8" | ||
} | ||
} |
@@ -21,2 +21,20 @@ import { EventEmitter } from 'eventemitter3' | ||
const ensureConfigFileWritable = (directory: string): void => { | ||
const baseDirectory = getBaseDirectory(directory) | ||
fs.accessSync(baseDirectory, fs.constants.W_OK | fs.constants.R_OK) | ||
logger.trace(`Directory ${baseDirectory} is readable and writable`) | ||
} | ||
const getBaseDirectory = (directory: string): string => { | ||
const subDirs = directory.split(path.sep) | ||
do { | ||
const current = subDirs.join(path.sep) | ||
if (fs.existsSync(current)) { | ||
return current | ||
} | ||
subDirs.pop() | ||
} while (subDirs.length > 0) | ||
return path.sep | ||
} | ||
export const SERVICE_ID = 'system/auto-certificer' | ||
@@ -108,6 +126,9 @@ const ONE_DAY = 1000 * 60 * 60 * 24 | ||
// TODO: use tooling from @streamr/utils to set the timeout with an abortController. | ||
this.updateTimeout = setTimeout(this.ensureCertificateValidity, updateIn) | ||
this.updateTimeout = setTimeout(() => this.ensureCertificateValidity(), updateIn) | ||
} | ||
private createCertificate = async (): Promise<void> => { | ||
const dir = path.dirname(this.configFile) | ||
ensureConfigFileWritable(dir) | ||
const sessionId = await this.restClient.createSession() | ||
@@ -123,3 +144,2 @@ let certifiedSubdomain: CertifiedSubdomain | ||
} | ||
const dir = path.dirname(this.configFile) | ||
// TODO: use async fs methods? | ||
@@ -144,3 +164,3 @@ if (!fs.existsSync(dir)) { | ||
const updatedCertifiedSubdomain = await this.restClient.updateCertificate(oldCertifiedSubdomain.fqdn.split('.')[0], | ||
this.streamrWebSocketPort, oldCertifiedSubdomain.authenticationToken, sessionId) | ||
this.streamrWebSocketPort, sessionId, oldCertifiedSubdomain.authenticationToken) | ||
@@ -183,9 +203,9 @@ this.ongoingSessions.delete(sessionId) | ||
async hasSession(request: HasSessionRequest): Promise<HasSessionResponse> { | ||
logger.trace('hasSession() called ' + this.ongoingSessions.size + ' ongoing sessions') | ||
logger.info('hasSession() called ' + this.ongoingSessions.size + ' ongoing sessions') | ||
if (this.ongoingSessions.has(request.sessionId)) { | ||
return { sessionId: request.sessionId } | ||
} else { | ||
throw `Session not found ${request.sessionId}` | ||
throw new Error(`Session not found ${request.sessionId}`) | ||
} | ||
} | ||
} |
@@ -41,3 +41,3 @@ import { Session } from './data/Session' | ||
} | ||
const response = await this.patch<CertifiedSubdomain>(url, body) | ||
const response = await this.patch<CertifiedSubdomain>(url, body, 2 * 60 * 1000) | ||
return response | ||
@@ -100,5 +100,5 @@ } | ||
// eslint-disable-next-line class-methods-use-this | ||
private patch<T>(url: string, body: any): Promise<T> { | ||
private patch<T>(url: string, body: any, timeout?: number): Promise<T> { | ||
return new Promise((resolve, reject) => { | ||
request.patch(url, { json: body, rejectUnauthorized: false }, (error: any, response: Response, body: any) => { | ||
request.patch(url, { json: body, rejectUnauthorized: false, timeout }, (error: any, response: Response, body: any) => { | ||
if (error) { | ||
@@ -105,0 +105,0 @@ reject(error) |
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
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
84518
2
1399
+ Added@streamr/utils@100.0.0-rc.0(transitive)
+ Addedbn.js@4.12.1(transitive)
+ Addedbrorand@1.1.0(transitive)
+ Addedelliptic@6.6.1(transitive)
+ Addedhash.js@1.1.7(transitive)
+ Addedhmac-drbg@1.0.1(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedminimalistic-assert@1.0.1(transitive)
+ Addedminimalistic-crypto-utils@1.0.1(transitive)
+ Addednode-addon-api@5.1.0(transitive)
+ Addednode-gyp-build@4.8.4(transitive)
+ Addedsecp256k1@5.0.1(transitive)
- Removed@streamr/utils@100.0.0-pretestnet.6(transitive)
Updated@streamr/utils@100.0.0-rc.0