Skip to content

Commit

Permalink
add blacked files
Browse files Browse the repository at this point in the history
  • Loading branch information
krysopath committed Apr 20, 2019
1 parent d308833 commit 5b499c1
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 261 deletions.
7 changes: 2 additions & 5 deletions vaultify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
CFG = configure()
logging.config.dictConfig(CFG)
logger = logging.getLogger(__name__)
logger.debug('vaultify module initializing...')
logger.debug("vaultify module initializing...")

from .vaultify import main

__all__ = (
"main"
"CFG"
)
__all__ = "main" "CFG"
16 changes: 6 additions & 10 deletions vaultify/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,19 @@

class Provider:
def __str__(self):
return '{}'.format(self.__class__)
return "{}".format(self.__class__)

@abc.abstractmethod
def get_secrets(self) -> dict:
pass



class Consumer(metaclass=abc.ABCMeta):
def __str__(self):
return '{}'.format(self.__class__)
return "{}".format(self.__class__)

@abc.abstractmethod
def consume_secrets(self,
data: dict) -> bool:
def consume_secrets(self, data: dict) -> bool:
pass


Expand All @@ -36,9 +34,8 @@ class API(metaclass=abc.ABCMeta):
ABC meta class for later extensibility
and cheap NotImplementedErrors
"""
def __init__(self,
provider: Provider,
consumer: Consumer):

def __init__(self, provider: Provider, consumer: Consumer):

self._provider = provider
self._consumer = consumer
Expand All @@ -52,8 +49,7 @@ def get_secrets(self) -> dict:
pass

@abc.abstractmethod
def consume_secrets(self,
data: dict) -> bool:
def consume_secrets(self, data: dict) -> bool:
pass

@abc.abstractmethod
Expand Down
85 changes: 28 additions & 57 deletions vaultify/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,21 @@
from pprint import pprint
from .util import yaml_dict_merge, load_yaml_cfg_sources

MODULE_BASE_DIR = os.path.dirname(
os.path.realpath(__file__))
ETC_DEFAULT_CONFIG = '/etc/default/vaultify.yml'
USER_CONFIG = '{}/.vaultify.yml'.format(os.environ.get("HOME"))
LOCAL_CONFIG = '{}/.vaultify.yml'.format(os.environ.get("PWD", '.'))
MODULE_BASE_DIR = os.path.dirname(os.path.realpath(__file__))
ETC_DEFAULT_CONFIG = "/etc/default/vaultify.yml"
USER_CONFIG = "{}/.vaultify.yml".format(os.environ.get("HOME"))
LOCAL_CONFIG = "{}/.vaultify.yml".format(os.environ.get("PWD", "."))

CFG_DEFAULT_FILES = [
ETC_DEFAULT_CONFIG,
USER_CONFIG,
LOCAL_CONFIG]
CFG_DEFAULT_FILES = [ETC_DEFAULT_CONFIG, USER_CONFIG, LOCAL_CONFIG]


BASE_CFG = {
"vaultify": {
},
"vaultify": {},
"handlers": {
"console": {
"level": "WARN",
},
"file": {
"level": "WARN",
"filename": "./vaultify.log"
}
"console": {"level": "WARN"},
"file": {"level": "WARN", "filename": "./vaultify.log"},
},
"loggers": {
}
"loggers": {},
}


Expand All @@ -38,73 +27,57 @@
"formatters": {
"simple": {
"class": "logging.Formatter",
"format": '[%(levelname)-5.5s] [%(name)-20.20s] - %(message)s'
"format": "[%(levelname)-5.5s] [%(name)-20.20s] - %(message)s",
},
"extended": {
"class": "logging.Formatter",
"format": "[%(asctime)s] [%(name)-20.20s] [%(levelname)-5.5s] %(message)s"
}
"format": "[%(asctime)s] [%(name)-20.20s] [%(levelname)-5.5s] %(message)s",
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"level": "DEBUG",
"stream": 'ext://sys.stdout',
"formatter": "simple"
"stream": "ext://sys.stdout",
"formatter": "simple",
},
"file": {
"class": "logging.FileHandler",
"level": "DEBUG",
"filename": "./debug.log",
"mode": "w",
"formatter": "extended"
}
"formatter": "extended",
},
},
"loggers": {
"": {
"level": "DEBUG",
"handlers": ["console", "file"],
"propagate": True
},

}
"": {"level": "DEBUG", "handlers": ["console", "file"], "propagate": True}
},
}


ENV_CFG = {
"vaultify": {
"provider": {
"class": os.environ.get("VAULTIFY_PROVIDER"),
"args": {
"secret": os.environ.get("VAULTIFY_SECRET")
}
"args": {"secret": os.environ.get("VAULTIFY_SECRET")},
},
"consumer": {
"class": os.environ.get("VAULTIFY_CONSUMER"),
"args": {
"path": os.environ.get("VAULTIFY_DESTINATION")
}
}
"args": {"path": os.environ.get("VAULTIFY_DESTINATION")},
},
},
"handlers": {
"console": {
"level": os.environ.get("VAULTIFY_LOG_LEVEL"),
},
"console": {"level": os.environ.get("VAULTIFY_LOG_LEVEL")},
"file": {
"level": os.environ.get("VAULTIFY_LOG_LEVEL"),
"filename": os.environ.get("VAULTIFY_LOG_FILE")
}
"filename": os.environ.get("VAULTIFY_LOG_FILE"),
},
},
"loggers": {
"": {
"level": os.environ.get("VAULTIFY_LOG_LEVEL")
}
}
"loggers": {"": {"level": os.environ.get("VAULTIFY_LOG_LEVEL")}},
}


def configure(
yaml_files: list = CFG_DEFAULT_FILES) -> dict:
def configure(yaml_files: list = CFG_DEFAULT_FILES) -> dict:

"""
This populates the global config dictionary with merged values
Expand All @@ -124,9 +97,7 @@ def configure(

for src in cfg_sources:
if src:
config_data = yaml_dict_merge(
config_data, src
)
config_data = yaml_dict_merge(config_data, src)

config_data = yaml_dict_merge(config_data, ENV_CFG)

Expand All @@ -141,5 +112,5 @@ def configure(
"CFG_DEFAULT_FILES",
"BASE_CFG",
"LOG_CFG",
"configure"
"configure",
)
99 changes: 34 additions & 65 deletions vaultify/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@

from .base import Consumer

__all__ = (
'DotEnvWriter',
'JsonWriter',
'EnvRunner'
)
__all__ = ("DotEnvWriter", "JsonWriter", "EnvRunner")

logger = logging.getLogger(__name__)

Expand All @@ -31,26 +27,21 @@ class FileWriter:
>>> isinstance(fw, FileWriter)
True
"""
def __init__(self,
path: str,
mode: oct = 0o600,
overwrite: bool = False,
*args, **kwargs):

def __init__(
self, path: str, mode: oct = 0o600, overwrite: bool = False, *args, **kwargs
):
self.path = path
self.mode = mode
self.overwrite = overwrite

def _write_data_to_fd(self, data: str):
with open(os.open(self.path,
os.O_CREAT | os.O_WRONLY,
0o200), 'w') as file_out:
logger.info(
"writing to {}, mode {}".format(
self.path, oct(self.mode)))

with open(os.open(self.path, os.O_CREAT | os.O_WRONLY, 0o200), "w") as file_out:
logger.info("writing to {}, mode {}".format(self.path, oct(self.mode)))

file_out.write(data)
file_out.write('\n')
file_out.write("\n")

def write(self, data: str):
"""
>>> fw = FileWriter('tests/new.filewriter', overwrite=False)
Expand All @@ -70,16 +61,12 @@ def write(self, data: str):
self._write_data_to_fd(data)
else:
if self.overwrite:
logger.warning(
'overwriting {}'.format(self.path))
logger.warning("overwriting {}".format(self.path))
self._write_data_to_fd(data)
else:
logger.warning(
'{} already exists: skip'.format(
self.path))

os.chmod(
self.path, self.mode)
logger.warning("{} already exists: skip".format(self.path))

os.chmod(self.path, self.mode)


class DotEnvWriter(Consumer, FileWriter):
Expand All @@ -91,11 +78,10 @@ class DotEnvWriter(Consumer, FileWriter):
>>> open('tests/new.env').read()
"export K1='V1'\\nexport K2='V2'\\n"
"""

def consume_secrets(self, data: dict):
self.write(
"\n".join(
util.dict2env(data)))

self.write("\n".join(util.dict2env(data)))


class JsonWriter(Consumer, FileWriter):
"""
Expand All @@ -105,13 +91,11 @@ class JsonWriter(Consumer, FileWriter):
>>> open('tests/new.json').read()
'{\\n "K1": "V1",\\n "K2": "V2"\\n}\\n'
"""

def consume_secrets(self, data: dict):
self.write(json.dumps(
data,
sort_keys=True,
indent=2)
)

self.write(json.dumps(data, sort_keys=True, indent=2))


class YamlWriter(Consumer, FileWriter):
"""
This Consumer writes secrets as a YAML dictionary
Expand All @@ -120,12 +104,12 @@ class YamlWriter(Consumer, FileWriter):
>>> open('tests/new.yaml').read()
'K1: V1\\nK2: V2\\n\\n'
"""

def consume_secrets(self, data: dict):
self.write(yaml.dump(
data,
default_flow_style=False,
allow_unicode=True,
encoding='utf-8').decode()
self.write(
yaml.dump(
data, default_flow_style=False, allow_unicode=True, encoding="utf-8"
).decode()
)


Expand All @@ -147,39 +131,24 @@ class EnvRunner(Consumer):
...
FileNotFoundError: [Errno 2] No such file or directory: 'nowhere.sh': 'nowhere.sh'
"""

def __init__(self, path: str):
self.path = os.environ.get(
"VAULTIFY_TARGET", path
).split()
self.path = os.environ.get("VAULTIFY_TARGET", path).split()

def consume_secrets(self, data: dict):
prepared_env = dict(os.environ)

for key, value in data.items():
prepared_env.update(
{key: value}
)
logger.info(
'{} enriched the environment'.format(self))
prepared_env.update({key: value})
logger.info("{} enriched the environment".format(self))

try:
# TODO Overhaul this
proc = run(
self.path,
stdout=PIPE,
stderr=PIPE,
env=prepared_env
)
logger.info(
'running the process "{}"'.format(self.path))
proc = run(self.path, stdout=PIPE, stderr=PIPE, env=prepared_env)
logger.info('running the process "{}"'.format(self.path))

except FileNotFoundError as error:
logger.critical(
'error in {} executing "{}"'.format(self, self.path)
)
logger.critical('error in {} executing "{}"'.format(self, self.path))
raise error

print(
proc.stdout.decode()
)
print(proc.stdout.decode())
Loading

0 comments on commit 5b499c1

Please sign in to comment.