def test_forwarding(volttron_instance1_encrypt, volttron_instance2_encrypt): global FORWARDER_CONFIG tf = tempfile.NamedTemporaryFile() tf2 = tempfile.NamedTemporaryFile() tf3 = tempfile.NamedTemporaryFile() ks = KeyStore(tf.name) ks.generate() ks2 = KeyStore(tf2.name) ks2.generate() ks3 = KeyStore(tf2.name) ks3.generate() wrap1 = volttron_instance1_encrypt wrap2 = volttron_instance2_encrypt authfile1 = AuthFile(wrap1.volttron_home+"/auth.json") entry1 = AuthEntry( credentials="CURVE:{}".format(ks3.public()) ) authfile1.add(entry1) authfile = AuthFile(wrap2.volttron_home+"/auth.json") entry = AuthEntry( credentials="CURVE:{}".format(ks.public())) authfile.add(entry) entry = AuthEntry( credentials="CURVE:{}".format(ks2.public())) authfile.add(entry) forward_to_vip = "{}?serverkey={}&publickey={}&secretkey={}".format( wrap2.vip_address, wrap2.publickey, ks.public(), ks.secret() ) FORWARDER_CONFIG["destination-vip"] = forward_to_vip forwarder_config = FORWARDER_CONFIG print("THE CONFIG = {}".format(forwarder_config)) wrap1.install_agent( agent_dir="services/core/ForwardHistorian", config_file=forwarder_config ) connect_to_wrap2 = "{}?serverkey={}&publickey={}&secretkey={}".format( wrap2.vip_address, wrap2.publickey, ks2.public(), ks2.secret() ) connect_to_wrap1 = "{}?serverkey={}&publickey={}&secretkey={}".format( wrap1.vip_address, wrap1.publickey, ks3.public(), ks3.secret() ) agent_connected1 = wrap1.build_agent(address=connect_to_wrap1) agent_connected2 = wrap2.build_agent(address=connect_to_wrap2) message = '' agent_connected2.vip.pubsub.subscribe('pubsub', '', callback=onmessage) gevent.sleep(0.2) do_publish(agent1=agent_connected1) gevent.sleep(1) assert allforwardedmessage
def test_forwarding(volttron_instance1_encrypt, volttron_instance2_encrypt): global FORWARDER_CONFIG tf = tempfile.NamedTemporaryFile() tf2 = tempfile.NamedTemporaryFile() tf3 = tempfile.NamedTemporaryFile() ks = KeyStore(tf.name) ks.generate() ks2 = KeyStore(tf2.name) ks2.generate() ks3 = KeyStore(tf2.name) ks3.generate() wrap1 = volttron_instance1_encrypt wrap2 = volttron_instance2_encrypt authfile1 = AuthFile(wrap1.volttron_home + "/auth.json") entry1 = AuthEntry(credentials="CURVE:{}".format(ks3.public())) authfile1.add(entry1) authfile = AuthFile(wrap2.volttron_home + "/auth.json") entry = AuthEntry(credentials="CURVE:{}".format(ks.public())) authfile.add(entry) entry = AuthEntry(credentials="CURVE:{}".format(ks2.public())) authfile.add(entry) forward_to_vip = "{}?serverkey={}&publickey={}&secretkey={}".format( wrap2.vip_address, wrap2.publickey, ks.public(), ks.secret()) FORWARDER_CONFIG["destination-vip"] = forward_to_vip forwarder_config = FORWARDER_CONFIG print("THE CONFIG = {}".format(forwarder_config)) wrap1.install_agent(agent_dir="services/core/ForwardHistorian", config_file=forwarder_config) connect_to_wrap2 = "{}?serverkey={}&publickey={}&secretkey={}".format( wrap2.vip_address, wrap2.publickey, ks2.public(), ks2.secret()) connect_to_wrap1 = "{}?serverkey={}&publickey={}&secretkey={}".format( wrap1.vip_address, wrap1.publickey, ks3.public(), ks3.secret()) agent_connected1 = wrap1.build_agent(address=connect_to_wrap1) agent_connected2 = wrap2.build_agent(address=connect_to_wrap2) message = '' agent_connected2.vip.pubsub.subscribe('pubsub', '', callback=onmessage) gevent.sleep(0.2) do_publish(agent1=agent_connected1) gevent.sleep(1) assert allforwardedmessage
def forwarder(request, volttron_instances): #print "Fixture forwarder" global volttron_instance1, volttron_instance2 global forwarder_uuid, forwarder_config # 1. Update destination address in forwarder configuration if volttron_instance1.encrypt: tf = tempfile.NamedTemporaryFile() ks = KeyStore(tf.name) # generate public private key pair for instance1 ks.generate() # add public key of instance1 to instance2 auth file authfile = AuthFile(volttron_instance2.volttron_home + "/auth.json") entry = AuthEntry(credentials=ks.public()) authfile.add(entry) # setup destination address to include keys forwarder_config["destination-vip"] =\ "{}?serverkey={}&publickey={}&secretkey={}".format( volttron_instance2.vip_address, volttron_instance2.serverkey, ks.public(), ks.secret()) else: forwarder_config["destination-vip"] = volttron_instance2.vip_address # 1: Install historian agent # Install and start sqlhistorian agent in instance2 forwarder_uuid = volttron_instance1.install_agent( agent_dir="services/core/ForwardHistorian", config_file=forwarder_config, start=True) print("forwarder agent id: ", forwarder_uuid)
def build_connection(self, peer=None, address=None, identity=None, publickey=None, secretkey=None, serverkey=None, **kwargs): if self.encrypt: self.allow_all_connections() if address is None: address = self.vip_address serverkey = self.serverkey if publickey is None or secretkey is None: self.logit('generating new public secret key pair') keyfile = tempfile.mktemp(".keys", "agent", self.volttron_home) keys = KeyStore(keyfile) keys.generate() publickey = keys.public() secretkey = keys.secret() if self.encrypt: conn = Connection(address=address, peer=peer, publickey=publickey, secretkey=secretkey, serverkey=serverkey, volttron_home=self.volttron_home) else: conn = Connection(address=self.local_vip_address, peer=peer, volttron_home=self.volttron_home) return conn
def setup_control_connection(request, get_volttron_instances): """ Creates a single instance of VOLTTRON for testing purposes """ global wrapper, control_connection wrapper = get_volttron_instances(1) request.addfinalizer(wrapper.shutdown_platform) assert wrapper assert wrapper.is_running() if get_volttron_instances.param == 'encrypted': if wrapper.encrypt: wrapper.allow_all_connections() # Connect using keys ks = KeyStore() ks.generate() control_connection = Connection(address=wrapper.vip_address, peer=CONTROL, serverkey=wrapper.serverkey, publickey=ks.public(), secretkey=ks.secret()) else: control_connection = Connection(address=wrapper.local_vip_address, peer=CONTROL) # Sleep a couple seconds to wait for things to startup gevent.sleep(2) return wrapper, control_connection
def build_agent(self, address=None, should_spawn=True, identity=None, publickey=None, secretkey=None, serverkey=None, generatekeys=False, **kwargs): """ Build an agent connnected to the passed bus. By default the current instance that this class wraps will be the vip address of the agent. :param address: :param should_spawn: :param identity: :param publickey: :param secretkey: :param serverkey: :return: """ self.logit("Building generic agent.") use_ipc = kwargs.pop('use_ipc', False) if address is None: if use_ipc: self.logit('Using IPC vip-address') address = "ipc://@"+self.volttron_home+"/run/vip.socket" else: self.logit('Using vip-address '+self.vip_address) address = self.vip_address if generatekeys: self.logit('generating new public secret key pair') tf = tempfile.NamedTemporaryFile() ks = KeyStore(tf.name) ks.generate() publickey = ks.public() secretkey = ks.secret() if publickey and not serverkey: self.logit('using instance serverkey: {}'.format(self.publickey)) serverkey = self.publickey agent = Agent(address=address, identity=identity, publickey=publickey, secretkey=secretkey, serverkey=serverkey, **kwargs) self.logit('platformwrapper.build_agent.address: {}'.format(address)) # Automatically add agent's credentials to auth.json file if publickey: self.logit('Adding publickey to auth.json') gevent.spawn(self._append_allow_curve_key, publickey) gevent.sleep(0.1) if should_spawn: self.logit('platformwrapper.build_agent spawning') event = gevent.event.Event() gevent.spawn(agent.core.run, event)#.join(0) event.wait(timeout=2) hello = agent.vip.hello().get(timeout=.3) self.logit('Got hello response {}'.format(hello)) return agent
def _get_keys_from_keystore(self): '''Returns agent's public and secret key from keystore''' if self.agent_uuid: # this is an installed agent keystore_dir = os.curdir elif self.identity: if not self.volttron_home: raise ValueError('VOLTTRON_HOME must be specified.') keystore_dir = os.path.join(self.volttron_home, 'keystores', self.identity) if not os.path.exists(keystore_dir): os.makedirs(keystore_dir) else: # the agent is not installed and its identity was not set return None, None keystore_path = os.path.join(keystore_dir, 'keystore.json') keystore = KeyStore(keystore_path) return keystore.public(), keystore.secret()
def build_agent_with_key(platform, identity=None): """Create an agent instance that has a generated public and private key. The passed platform will be the vip-address of the agent and the identity will be set. If the identity is set to None then a random identity will be created. """ keys = KeyStore(os.path.join(platform.volttron_home, identity + '.keys')) keys.generate() agent = platform.build_agent(identity=identity, serverkey=platform.publickey, publickey=keys.public(), secretkey=keys.secret()) # Make publickey easily accessible for these tests agent.publickey = keys.public() gevent.sleep(0.1) # switch context for a bit return agent
def _get_keys_from_keystore(self): '''Returns agent's public and secret key from keystore''' if self.agent_uuid: # this is an installed agent keystore_dir = os.curdir elif self.identity: if not os.environ.get('VOLTTRON_HOME'): raise ValueError('VOLTTRON_HOME must be specified.') keystore_dir = os.path.join( os.environ.get('VOLTTRON_HOME'), 'keystores', self.identity) if not os.path.exists(keystore_dir): os.makedirs(keystore_dir) else: # the agent is not installed and its identity was not set return None, None keystore_path = os.path.join(keystore_dir, 'keystore.json') keystore = KeyStore(keystore_path) return keystore.public(), keystore.secret()
def update_curve_key(curve_key_path, no_warn=False): try: with open(curve_key_path, 'r') as curve_file: public, secret = read_curve_key(curve_file) except IOError as e: print e return keystore_path = os.path.join(os.path.dirname(curve_key_path), 'keystore') if os.path.exists(keystore_path) and not no_warn: response = raw_input("{} already exists. " "Overwrite? [y/N]: ".format(keystore_path)) if not response.lower().startswith('y'): print "Key update aborted." return keystore = KeyStore(keystore_path) keystore.public = public keystore.secret = secret print "Keys from {} have been transfered to {}".format(curve_key_path, keystore.filename)
import os import sys import gevent from volttron.platform import get_address from volttron.platform.agent.known_identities import VOLTTRON_CENTRAL_PLATFORM from volttron.platform.keystore import KeyStore from volttron.platform.messaging import topics from volttron.platform.vip.agent import Agent keystore = KeyStore() agent = Agent(address=get_address(), identity="blahagent", publickey=keystore.public(), secretkey=keystore.secret(), enable_store=False) event = gevent.event.Event() config_store_task = gevent.spawn(agent.core.run, event) event.wait() del event if VOLTTRON_CENTRAL_PLATFORM not in agent.vip.peerlist().get(): agent.core.stop() print('no vcp availablel') sys.exit() def receive_platform_data(peer, sender, bus, topic, headers, message): #assert 'message' in kwargs
def get_new_keypair(): tf = tempfile.NamedTemporaryFile() ks = KeyStore(tf.name) ks.generate() return ks.public(), ks.secret()
def build_agent(self, address=None, should_spawn=True, identity=None, publickey=None, secretkey=None, serverkey=None, generatekeys=False, **kwargs): """ Build an agent connnected to the passed bus. By default the current instance that this class wraps will be the vip address of the agent. :param address: :param should_spawn: :param identity: :param publickey: :param secretkey: :param serverkey: :return: """ self.logit("Building generic agent.") use_ipc = kwargs.pop('use_ipc', False) if address is None: if use_ipc: self.logit('Using IPC vip-address') address = "ipc://@" + self.volttron_home + "/run/vip.socket" else: self.logit('Using vip-address ' + self.vip_address) address = self.vip_address if generatekeys: self.logit('generating new public secret key pair') tf = tempfile.NamedTemporaryFile() ks = KeyStore(tf.name) ks.generate() publickey = ks.public() secretkey = ks.secret() if publickey and not serverkey: self.logit('using instance serverkey: {}'.format(self.publickey)) serverkey = self.publickey agent = Agent(address=address, identity=identity, publickey=publickey, secretkey=secretkey, serverkey=serverkey, **kwargs) self.logit('platformwrapper.build_agent.address: {}'.format(address)) # Automatically add agent's credentials to auth.json file if publickey: self.logit('Adding publickey to auth.json') gevent.spawn(self._append_allow_curve_key, publickey) gevent.sleep(0.1) if should_spawn: self.logit('platformwrapper.build_agent spawning') event = gevent.event.Event() gevent.spawn(agent.core.run, event) #.join(0) event.wait(timeout=2) hello = agent.vip.hello().get(timeout=.3) self.logit('Got hello response {}'.format(hello)) return agent