Skip to content

Commit

Permalink
Enabling decoding input config
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPreston committed Sep 27, 2021
1 parent f2e0846 commit 9b12497
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 24 deletions.
8 changes: 7 additions & 1 deletion ecs_files_composer/aws_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def set_session_from_iam_object(iam_config_object, source_session=None):
}
if iam_config_object.external_id:
params["ExternalId"] = iam_config_object.external_id
print("PA", params)
tmp_creds = source_session.client("sts").assume_role(**params)
client_session = create_session_from_creds(
tmp_creds, region=iam_config_object.region_name
Expand Down Expand Up @@ -101,7 +102,12 @@ def __init__(
self.client_session = create_session_from_creds(
tmp_creds, region=region
)
elif iam_config_object:
elif (
iam_config_object
and hasattr(iam_config_object, "role_arn")
and iam_config_object.role_arn
):
print(iam_config_object)
self.client_session = set_session_from_iam_object(
iam_config_object, self.session
)
Expand Down
25 changes: 19 additions & 6 deletions ecs_files_composer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,24 +53,37 @@ def main():
help="The Role ARN to use for the configuration initialization",
required=False,
)
parser.add_argument(
"--decode-base64",
help="Whether the source config is in base64 encoded",
action="store_true",
required=False,
default=False,
)
parser.add_argument("_", nargs="*")
args = parser.parse_args()
print("Arguments: " + str(args._))
if not (
args.env_var or args.ssm_config or args.s3_config or args.file_path
) and environ.get("ECS_CONFIG_CONTENT", None):
LOG.info("Using default env variable ECS_CONFIG_CONTENT")
config = init_config(env_var="ECS_CONFIG_CONTENT")
config = init_config(
env_var="ECS_CONFIG_CONTENT", decode_base64=args.decode_base64
)
elif args.env_var:
config = init_config(env_var=args.env_var)
config = init_config(env_var=args.env_var, decode_base64=args.decode_base64)
elif args.file_path:
config = init_config(file_path=args.file_path)
config = init_config(file_path=args.file_path, decode_base64=args.decode_base64)
elif args.ssm_config:
config = init_config(ssm_parameter=args.ssm_config)
config = init_config(
ssm_parameter=args.ssm_config, decode_base64=args.decode_base64
)
elif args.s3_config:
config = init_config(s3_config=args.s3_config)
config = init_config(s3_config=args.s3_config, decode_base64=args.decode_base64)
elif args.secret_config:
config = init_config(secret_config=args.secret_config)
config = init_config(
secret_config=args.secret_config, decode_base64=args.decode_base64
)
else:
raise parser.error(
"You must specify where the execution configuration comes from or set ECS_CONFIG_CONTENT."
Expand Down
5 changes: 5 additions & 0 deletions ecs_files_composer/ecs_files_composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def init_config(
secret_config=None,
role_arn=None,
external_id=None,
decode_base64=False,
):
"""
Function to initialize the configuration
Expand All @@ -40,6 +41,7 @@ def init_config(
:param str secret_config:
:param str role_arn:
:param str external_id:
:param bool decode_base64:
:return: The ECS Config description
:rtype: dict
"""
Expand Down Expand Up @@ -89,6 +91,9 @@ def init_config(
"files": {config_path: initial_config},
"IamOverride": iam_override,
}
if decode_base64:
initial_config["encoding"] = "base64"
initial_config["context"] = "jinja2"
start_jobs(jobs_input_def)
with open(config_path, "r") as config_fd:
try:
Expand Down
30 changes: 13 additions & 17 deletions ecs_files_composer/files_mgmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"""Main module."""

import base64
import os
import pathlib
import subprocess
import warnings
Expand All @@ -20,7 +21,7 @@
from ecs_files_composer.common import LOG
from ecs_files_composer.envsubst import expandvars
from ecs_files_composer.input import Context, Encoding, FileDef
from ecs_files_composer.jinja2_filters import env
from ecs_files_composer.jinja2_filters import env as env_override


class File(FileDef, object):
Expand Down Expand Up @@ -62,11 +63,13 @@ def handler(self, iam_override=None, session_override=None):
self.handle_sources(
iam_override=iam_override, session_override=session_override
)
self.write_content()
if not self.source and self.content:
self.write_content(decode=True)
if self.content and self.encoding and self.encoding == Encoding["base64"]:
self.content = base64.b64decode(self.content).decode()
if self.templates_dir:
self.write_content(is_template=True)
self.render_jinja()
else:
self.write_content(is_template=False)
self.set_unix_settings()
if self.commands and self.commands.post:
warnings.warn("Commands are not yet implemented", Warning)
Expand Down Expand Up @@ -174,14 +177,15 @@ def render_jinja(self):
a final template.
"""
LOG.info(f"Rendering Jinja for {self.path} - {self.templates_dir.name}")
print(self.content)
jinja_env = Environment(
loader=FileSystemLoader(self.templates_dir.name),
autoescape=True,
auto_reload=False,
)
jinja_env.filters["env_override"] = env
jinja_env.filters["env_override"] = env_override
template = jinja_env.get_template(path.basename(self.path))
self.content = template.render()
self.content = template.render(env=os.environ)
self.write_content(is_template=False)

def set_unix_settings(self):
Expand Down Expand Up @@ -218,14 +222,11 @@ def set_unix_settings(self):
else:
raise

def write_content(
self, is_template=True, decode=False, as_bytes=False, bytes_content=None
):
def write_content(self, is_template=True, as_bytes=False, bytes_content=None):
"""
Function to write the content retrieved to path.
:param bool is_template: Whether the content should be considered to be a template.
:param decode:
:param as_bytes:
:param bytes_content:
:return:
Expand All @@ -237,13 +238,8 @@ def write_content(
)
LOG.info(f"Outputting {self.path} to {file_path}")
if isinstance(self.content, str):
if decode and self.encoding == Encoding["base64"]:
LOG.debug(f"Outputting B64 to {file_path}")
with open(file_path, "w") as file_fd:
file_fd.write(base64.b64decode(self.content).decode())
else:
with open(file_path, "w") as file_fd:
file_fd.write(self.content)
with open(file_path, "w") as file_fd:
file_fd.write(self.content)
elif isinstance(self.content, StreamingBody):
with open(file_path, "wb") as file_fd:
file_fd.write(self.content.read())
Expand Down

0 comments on commit 9b12497

Please sign in to comment.