Skip to content

Commit

Permalink
Testing various password storage techniques.
Browse files Browse the repository at this point in the history
  • Loading branch information
jensenbox committed Nov 30, 2017
1 parent b0625ed commit fff5f4c
Show file tree
Hide file tree
Showing 22 changed files with 425 additions and 63 deletions.
25 changes: 0 additions & 25 deletions agent/agent.py

This file was deleted.

8 changes: 7 additions & 1 deletion agent/agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,14 @@ case "$1" in
noop)
;;

vault)
export PYTHONPATH=/code
python3 sn_agent/cli.py vault
;;

run)
python3 agent.py
export PYTHONPATH=/code
python3 sn_agent/cli.py run
;;

docs)
Expand Down
17 changes: 17 additions & 0 deletions agent/demo_config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---

services:

- service: deadbeef-aaaa-bbbb-cccc-000000000101
module: examples.simple_adapter.SimpleAdapter

- service: deadbeef-aaaa-bbbb-cccc-111111111101
module: adapters.tensorflow.mnist.TensorflowMNIST

- service: deadbeef-aaaa-bbbb-cccc-000000000202
module: adapters.opencog.relex.RelexAdapter

- service: deadbeef-aaaa-bbbb-cccc-100000000001
module: adapters.aigents.AigentsAdapter


8 changes: 5 additions & 3 deletions agent/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ jsonrpcserver
rdflib
pyaml

# For some reason jsonrpcclient is using this even though we are using the aiohttp implementation - perhaps under the covers?
requests

# Testing
pytest
mock
pytest-cov
coveralls

# Documentation
sphinx
sphinx-autobuild
sphinx_rtd_theme
Expand All @@ -29,3 +28,6 @@ tensorflow

web3
requests

fire
hvac
76 changes: 76 additions & 0 deletions agent/sn_agent/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import ssl
from getpass import getpass

import fire
import sys

from aiohttp import web

from sn_agent.app import create_app
from sn_agent.network import NetworkSettings

import logging

import hvac
import os

logger = logging.getLogger(__name__)


class Agent(object):
def run(self):
network_settings = NetworkSettings()

app = create_app()

sslcontext = None

if network_settings.SSL_CERTIFICATE_FILE and network_settings.SSL_KEYFILE:
sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
sslcontext.load_cert_chain('server.crt', 'server.key')

logger.info('Host setting: %s', network_settings.WEB_HOST)

web.run_app(app, port=network_settings.WEB_PORT, ssl_context=sslcontext)

def vault_init(self, client):

shares = 1
threshold = 1

result = client.initialize(shares, threshold)

print("The following is extremely important. It will never be shown again.")
print("-------------------------------------------------------------------")
print(result)
print("-------------------------------------------------------------------")

def unseal(self, client):

while True:
key = getpass()
client.unseal(key)
if not client.is_sealed():
break
print("Try again")

def vault(self):

client = hvac.Client(url='http://vault:8200')

if not client.is_initialized():
self.vault_init(client)
return

if client.is_sealed():
self.unseal(client)

client = hvac.Client(url='http://vault:8200', token=os.environ['VAULT_TOKEN'])
client.write('secret/foo', baz='bar', lease='1h')
print(client.read('secret/foo'))
client.delete('secret/foo')

#| {'keys': ['86dfb323fc678f993af9d8876209297e56d6ab880dc056944f635b7949c816ca'], 'keys_base64': ['ht+zI/xnj5k6+diHYgkpflbWq4gNwFaUT2NbeUnIFso='], 'root_token': '71e608b7-ae2f-0c80-bb99-d1a4848cf549'}

if __name__ == '__main__':
fire.Fire(Agent)
7 changes: 7 additions & 0 deletions agent/sn_agent/network/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
from sn_agent.ontology.service_descriptor import ServiceDescriptor


class ResolverABC(ABC):
@abstractmethod
def resolve(self, agent_id):
"""Resolve the url for an agent given only its ID"""
pass


class NetworkABC(ABC):
def __init__(self, app):
self.app = app
Expand Down
6 changes: 5 additions & 1 deletion agent/sn_agent/network/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class NetworkSettings(SettingsBase):
def __init__(self, **custom_settings):


self._ENV_PREFIX = 'SN_NETWORK_'

self.GATEWAY = '0.0.0.0'
Expand All @@ -21,8 +23,10 @@ def __init__(self, **custom_settings):
self.WEB_HOST = "0.0.0.0"
self.WEB_PORT = 8000

self.SSL_CERTIFICATE_FILE = None
self.SSL_KEYFILE = None

self.AGENT_URL_LOOKUP_FILE = os.path.join(THIS_DIR, 'data', 'agent_to_url_lookup.json')
self.COINBASE = '0x633a490e1d3022a90e49cfb79ff8789d264ae753'

super().__init__(**custom_settings)

Expand Down
58 changes: 40 additions & 18 deletions agent/sn_agent/network/sn.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,35 @@

from sn_agent.agent.base import AgentABC
from sn_agent.network import NetworkSettings
from sn_agent.network.base import NetworkABC
from sn_agent.network.base import NetworkABC, ResolverABC
from sn_agent.network.enum import NetworkStatus
from sn_agent.ontology.service_descriptor import ServiceDescriptor

logger = logging.getLogger(__name__)


class UnresolvedAgentException(Exception):
pass


class FileResolver(ResolverABC):
def __init__(self, lookup_file):
self.lookup_file = lookup_file

def resolve(self, agent_id):
filename = self.lookup_file

with open(filename, encoding='utf-8') as data_file:
agent_urls = json.loads(data_file.read())

return agent_urls.get(agent_id)


class DHTResolver(ResolverABC):
def resolve(self, agent_id):
return None


class SNNetwork(NetworkABC):
def __init__(self, app):
super().__init__(app)
Expand All @@ -23,17 +45,24 @@ def __init__(self, app):
self.payload = None
self.agent = None

self.resolvers = []
self.resolvers.append(FileResolver(self.settings.AGENT_URL_LOOKUP_FILE))
self.resolvers.append(DHTResolver())

async def startup(self):
logger.debug('Registering agent on DHT')
logger.debug('Starting up the network')

self.agent = self.app['agent']

try:
account_address = self.client_connection.eth.coinbase

logger.debug('Using account: %s', account_address)

self.payload = {
'from': self.client_connection.eth.coinbase,
'from': account_address,
'gas': 1500000,
'gasPrice': 30000000000000
'gasPrice': 30000000
}
current_block = self.client_connection.eth.blockNumber
logger.debug('Current client blocknumber: %s', current_block)
Expand All @@ -46,8 +75,8 @@ async def startup(self):
# Implemented methods
def join_network(self):
logger.debug('Joining Network')
contract = self.get_agent_factory_contract()
contract.transact(self.payload).create()
# contract = self.get_agent_factory_contract()
# contract.transact(self.payload).create()
logger.debug('Joined network')

def advertise_service(self, service: ServiceDescriptor):
Expand All @@ -66,19 +95,12 @@ def find_service_providers(self, service: ServiceDescriptor) -> list:

def get_url_for_agent(self, agent_id):

filename = self.settings.AGENT_URL_LOOKUP_FILE

with open(filename, encoding='utf-8') as data_file:
agent_urls = json.loads(data_file.read())

url = agent_urls.get(agent_id)

if url is None:
# Fallback to blockchain if none specified in the lookup file
blockchain_result = None
# TODO implement grabbing from blockchain
for resolver in self.resolvers:
agent_url = resolver.resolve(agent_id)
if agent_url:
return agent_url

return url
raise UnresolvedAgentException(agent_id)

# TODO: Unimplemented methods

Expand Down
10 changes: 10 additions & 0 deletions agent/sn_agent/version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

#: MAJOR, MINOR, RELEASE, STATUS [alpha, beta, final], VERSION
VERSION = (0, 0, 0, 'alpha', 0)


def get_version():
"""
Returns a string representation of the version information of this project.
"""
return '.'.join([str(i) for i in VERSION])
8 changes: 8 additions & 0 deletions data/vault-data/config/standard.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
backend "file" {
path = "/vault/file"
}

listener "tcp" {
address = "0.0.0.0:8200"
tls_disable = 1
}
2 changes: 0 additions & 2 deletions docker-compose.demo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ services:
- SN_WEB_COOKIE_SECRET=kubr6DvIuYj4GREdgXq5CCoL5qHQWglj8IECTsI79mY=
- PYTHONPATH=/code



parity:
build: parity
volumes:
Expand Down
Loading

0 comments on commit fff5f4c

Please sign in to comment.