Skip to content

Commit

Permalink
Merge pull request #15 from VOLTTRON/feature/env-publickey
Browse files Browse the repository at this point in the history
Feature add environmental key suppport
  • Loading branch information
craig8 authored Dec 12, 2021
2 parents 7b8814b + 9567e94 commit 03ff79b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
5 changes: 5 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGES
=======

* Update command vip\_main for handling if only one env variable set added test
* Closes #6 vip\_main can use environmental variables
* Closes #14 error when file not produced
* Added requirements.txt
* Added generated files to repository
* Standardize setup.cfg to start at python 3.8
* Standardize github actions across core repositories

Expand Down
2 changes: 1 addition & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ flake8 = ">=3.9.2"
pytest = ">=6.2.4"
pre-commit = ">=2.14.1"
twine = "*"
mock = "*"

[requires]
python_version = "3.8.10"
Expand All @@ -25,4 +26,3 @@ pyopenssl = "==19.0.0"
psutil = ">=5.7.0"
pyyaml = ">=5.4.1"
pbr = "*"

11 changes: 9 additions & 2 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions tests/test_environment_vars.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import os

import mock
import pytest

from volttron.utils import vip_main


class AgentMockery:
myargs = None
mykwargs = None

def __init__(self, *args, **kwargs):
AgentMockery.myargs = args
AgentMockery.mykwargs = kwargs

def run(self):
pass


def test_env_args_passed_to_agent():
env = dict(AGENT_PUBLICKEY="uSo_q3vw-DpcOeCOXc1A4o1U11qpTtkkW2EviHM7x24",
AGENT_SECRETKEY="WpnCmf1vM1Z5gw0uIg8tr2C4erNQpSa0KONq9NvjzUE",
VOLTTRON_SERVERKEY="UH4tX5RDNTMjp5VPxVuj-M5QiO82BLUghYeWJ_CgvQc")
with mock.patch.dict(os.environ, env):
vip_main(AgentMockery, identity="foo")
assert env["AGENT_PUBLICKEY"] == AgentMockery.mykwargs['publickey']
assert env["AGENT_SECRETKEY"] == AgentMockery.mykwargs['secretkey']
assert env["VOLTTRON_SERVERKEY"] == AgentMockery.mykwargs['serverkey']


def test_env_only_publickey_passed_to_agent():
env = dict(AGENT_PUBLICKEY="uSo_q3vw-DpcOeCOXc1A4o1U11qpTtkkW2EviHM7x24")
with mock.patch.dict(os.environ, env):
with pytest.raises(ValueError):
vip_main(AgentMockery, identity="foo")
32 changes: 29 additions & 3 deletions volttron/utils/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,34 @@ def vip_main(agent_class, identity=None, version="0.1", **kwargs):

config = os.environ.get("AGENT_CONFIG")
identity = os.environ.get("AGENT_VIP_IDENTITY", identity)
publickey = kwargs.pop("publickey", None)
if not publickey:
publickey = os.environ.get("AGENT_PUBLICKEY")
secretkey = kwargs.pop("secretkey", None)
if not secretkey:
secretkey = os.environ.get("AGENT_SECRETKEY")
serverkey = kwargs.pop("serverkey", None)
if not serverkey:
serverkey = os.environ.get("VOLTTRON_SERVERKEY")

# AGENT_PUBLICKEY and AGENT_SECRETKEY must be specified
# for the agent to execute successfully. aip should set these
# if the agent is run from the platform. If run from the
# run command it should be set automatically from vctl and
# added to the server.
#
# TODO: Make required for all agents. Handle it through vctl and aip.
if not os.environ.get("_LAUNCHED_BY_PLATFORM"):
if not publickey or not secretkey:
raise ValueError("AGENT_PUBLIC and AGENT_SECRET environmental variables must "
"be set to run without the platform.")

message_bus = os.environ.get("MESSAGEBUS", "zmq")
if identity is not None:
if not is_valid_identity(identity):
_log.warning("Deprecation warining")
_log.warning(
"All characters in {identity} are not in the valid set.".format(
idenity=identity
)
f"All characters in {identity} are not in the valid set."
)

address = get_address()
Expand All @@ -161,6 +181,9 @@ def vip_main(agent_class, identity=None, version="0.1", **kwargs):
volttron_home=volttron_home,
version=version,
message_bus=message_bus,
publickey=publickey,
secretkey=secretkey,
serverkey=serverkey,
**kwargs
)
else:
Expand All @@ -172,6 +195,9 @@ def vip_main(agent_class, identity=None, version="0.1", **kwargs):
volttron_home=volttron_home,
version=version,
message_bus=message_bus,
publickey=publickey,
secretkey=secretkey,
serverkey=serverkey,
**kwargs
)

Expand Down
4 changes: 3 additions & 1 deletion volttron/utils/persistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ def _update_file(filename, contents, format, mode):
_log.error("Unable to sync to file {}".format(filename))
finally:
fileobj.close()
shutil.move(tempname, filename) # atomic commit
if os.path.exists(tempname):
shutil.move(tempname, filename) # atomic commit

if mode is not None:
os.chmod(filename, mode)

Expand Down

0 comments on commit 03ff79b

Please sign in to comment.