From 4d3826d29b3ed4d13e2b1e91e921791b30305628 Mon Sep 17 00:00:00 2001 From: Maksim Fomenkov Date: Mon, 17 Oct 2022 10:49:57 +0300 Subject: [PATCH] add TLS support --- dshackle-cli/README.md | 23 +++++++++++++++++++---- dshackle-cli/package-lock.json | 4 ++-- dshackle-cli/package.json | 2 +- dshackle-cli/src/cli.js | 12 +++++++++--- dshackle-cli/src/grpc-clent.js | 15 +++++++++++++-- 5 files changed, 44 insertions(+), 12 deletions(-) diff --git a/dshackle-cli/README.md b/dshackle-cli/README.md index 2aba0f3c..b43b6f94 100644 --- a/dshackle-cli/README.md +++ b/dshackle-cli/README.md @@ -1,21 +1,36 @@ # DSHACKLE CLI -A CLI tool to verify the [dshacle](https://github.com/emeraldpay/dshackle) installation. +A CLI tool to verify the [dshackle](https://github.com/emeraldpay/dshackle) installation. ## Usage ``` -npx dchackle-cli-tool [-p|--print] +npx dshackle-cli-tool [-p|--print][--ca][--cert][--key] ``` ### Options --p | --print - will print the describe response as is +```-p | --print``` will print the describe response as is + +### TLS +```--ca``` the root certificate data + +```--cert``` the client certificate key chain, if available +```--key``` the client certificate private key, if available ## Example ``` -> dchackle-cli-tool localhost:2449 +> dshackle-cli-tool localhost:2449 Connecting to: localhost:2449... Connected to localhost:2449 CHAIN_ETHEREUM -> AVAIL_OK CHAIN_KOVAN -> AVAIL_OK +``` + +With TLS +``` +>dshackle-cli % dshackle-cli-tool --ca ./../out/ca.myhost.dev.crt --cert ./../out/client_1.crt --key ./../out/client_1.key 127.0.0.1:2450 +Using TLS +Connecting to: 127.0.0.1:2450... +Connected to 127.0.0.1:2450 +CHAIN_ETHEREUM -> AVAIL_OK ``` \ No newline at end of file diff --git a/dshackle-cli/package-lock.json b/dshackle-cli/package-lock.json index edee9a4a..f19665b2 100644 --- a/dshackle-cli/package-lock.json +++ b/dshackle-cli/package-lock.json @@ -1,12 +1,12 @@ { "name": "dshackle-cli-tool", - "version": "1.0.3", + "version": "1.0.7", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "dshackle-cli-tool", - "version": "1.0.3", + "version": "1.0.7", "license": "MIT", "dependencies": { "@grpc/grpc-js": "^1.6.12", diff --git a/dshackle-cli/package.json b/dshackle-cli/package.json index 29bcd25e..55836293 100644 --- a/dshackle-cli/package.json +++ b/dshackle-cli/package.json @@ -1,6 +1,6 @@ { "name": "dshackle-cli-tool", - "version": "1.0.4", + "version": "1.0.7", "description": "", "main": "src/index.js", "scripts": { diff --git a/dshackle-cli/src/cli.js b/dshackle-cli/src/cli.js index 9f408310..ca83abc8 100644 --- a/dshackle-cli/src/cli.js +++ b/dshackle-cli/src/cli.js @@ -6,10 +6,10 @@ import util from "util"; export function cli(args) { let opts = parseArgumentsIntoOptions(args); if (!opts.url) { - console.log("Err: URL not specified!") + console.log("Err: URL not specified!!!") return } - describe(opts.url, (error, response) => { + describe(opts.url, opts.ca, opts.cert, opts.key, (error, response) => { if (error) { console.error(clc.red('Connection to ' + opts.url + ' failed! [' + error.message + ']')); } else { @@ -42,6 +42,9 @@ function parseArgumentsIntoOptions(rawArgs) { const args = arg( { '--print': Boolean, + '--ca': String, + '--cert': String, + '--key': String, '-p': '--print' }, { @@ -50,7 +53,10 @@ function parseArgumentsIntoOptions(rawArgs) { ); return { print: args['--print'] || false, - url: args._[0] + url: args._[0], + ca: args['--ca'], + cert: args['--cert'], + key: args['--key'] }; } diff --git a/dshackle-cli/src/grpc-clent.js b/dshackle-cli/src/grpc-clent.js index f4a32c64..4f435d2b 100644 --- a/dshackle-cli/src/grpc-clent.js +++ b/dshackle-cli/src/grpc-clent.js @@ -1,6 +1,7 @@ const grpc = require("@grpc/grpc-js"); const path = require('path') const protoLoader = require("@grpc/proto-loader"); +const fs = require('fs'); const PROTO_PATH = path.join(__dirname, "../grpc/proto/blockchain.proto"); @@ -15,11 +16,21 @@ const options = { const packageDefinition = protoLoader.loadSync(PROTO_PATH, options); const emerald = grpc.loadPackageDefinition(packageDefinition).emerald -export function describe(url, handler) { +export function describe(url, ca, cert, key, handler) { + let credentials = grpc.credentials.createInsecure() + if (ca || cert || key) { + console.log("Using TLS") + credentials = grpc.credentials.createSsl( + ca ? fs.readFileSync(ca) : null, + key ? fs.readFileSync(key) : null, + cert ? fs.readFileSync(cert) : null + ); + } const client = new emerald.Blockchain( url, - grpc.credentials.createInsecure() + credentials ); + console.log('Connecting to: ' + url + '...') client.Describe({}, handler); }