From becd6744f6b628c819590ffdd49d9c19081351f3 Mon Sep 17 00:00:00 2001 From: nolash Date: Mon, 6 Dec 2021 18:55:36 +0100 Subject: [PATCH] Add option to skip ssl validation on rpc --- CHANGELOG | 4 +++- chainlib/chain.py | 9 ++++++++- chainlib/cli/rpc.py | 2 +- chainlib/connection.py | 16 ++++++++++++++-- chainlib/data/config/config.ini | 1 + setup.cfg | 2 +- 6 files changed, 28 insertions(+), 6 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index 39b43be..57ce577 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,4 +1,6 @@ -- 0.0.5-pending +- 0.0.14 + * Add option to skip ssl verification on rpc +- 0.0.5 * Move eth code to separate package - 0.0.4-unreleased * Add pack tx from already signed tx struct diff --git a/chainlib/chain.py b/chainlib/chain.py index 71c5e1f..e72f806 100644 --- a/chainlib/chain.py +++ b/chainlib/chain.py @@ -186,10 +186,17 @@ class ChainSpec: return r - def __str__(self): + def as_string(self, skip_optional=False): s = '{}:{}:{}'.format(self.o['arch'], self.o['fork'], self.o['network_id']) + if skip_optional: + return s + if self.o.get('common_name'): s += ':' + self.o['common_name'] if self.o.get('custom'): s += ':' + ':'.join(self.o['custom']) return s + + + def __str__(self): + return self.as_string() diff --git a/chainlib/cli/rpc.py b/chainlib/cli/rpc.py index fd76b01..69ead3b 100644 --- a/chainlib/cli/rpc.py +++ b/chainlib/cli/rpc.py @@ -61,7 +61,7 @@ class Rpc: self.id_generator = IntSequenceGenerator() self.chain_spec = config.get('CHAIN_SPEC') - self.conn = self.constructor(url=config.get('RPC_PROVIDER'), chain_spec=self.chain_spec, auth=auth) + self.conn = self.constructor(url=config.get('RPC_PROVIDER'), chain_spec=self.chain_spec, auth=auth, verify_identity=config.true('RPC_VERIFY')) return self.conn diff --git a/chainlib/connection.py b/chainlib/connection.py index 786fda0..b56b403 100644 --- a/chainlib/connection.py +++ b/chainlib/connection.py @@ -102,10 +102,13 @@ class RPCConnection: } __constructors_for_chains = {} - def __init__(self, url=None, chain_spec=None, auth=None): + def __init__(self, url=None, chain_spec=None, auth=None, verify_identity=True): self.chain_spec = chain_spec self.location = None self.basic = None + self.verify_identity = verify_identity + if not self.verify_identity: + logg.warning('RPC host identity verification is OFF. Beware, you will be easy to cheat') if url == None: return self.auth = auth @@ -287,6 +290,11 @@ class JSONRPCHTTPConnection(HTTPConnection): :returns: Result value part of JSON RPC response :todo: Invalid response exception from invalid json response """ + ssl_ctx = None + if not self.verify_identity: + import ssl + ssl_ctx = ssl.SSLContext() + ssl_ctx.verify_mode = ssl.CERT_NONE req = Request( self.location, method='POST', @@ -313,7 +321,11 @@ class JSONRPCHTTPConnection(HTTPConnection): install_opener(ho) try: - r = urlopen(req, data=data.encode('utf-8')) + r = urlopen( + req, + data=data.encode('utf-8'), + context=ssl_ctx, + ) except URLError as e: raise RPCException(e) diff --git a/chainlib/data/config/config.ini b/chainlib/data/config/config.ini index 3b03253..fe99281 100644 --- a/chainlib/data/config/config.ini +++ b/chainlib/data/config/config.ini @@ -4,6 +4,7 @@ auth = credentials = dialect = default scheme = http +verify = 1 [chain] spec = diff --git a/setup.cfg b/setup.cfg index 503fcee..156ad40 100644 --- a/setup.cfg +++ b/setup.cfg @@ -6,7 +6,7 @@ name=chainlib license=WTFPL2 author_email=dev@holbrook.no description=Generic blockchain access library and tooling -version=0.0.12 +version=0.0.14 url=https://gitlab.com/chaintools/chainlib author=Louis Holbrook