Skip to content

Commit

Permalink
Account for jinja templating when validating the cloudwatch agent json
Browse files Browse the repository at this point in the history
  • Loading branch information
hgreebe committed Nov 14, 2023
1 parent a8d01f7 commit f41ddd0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import sys

import jsonschema
from jinja2 import FileSystemLoader
from jinja2.sandbox import SandboxedEnvironment

DEFAULT_SCHEMA_PATH = os.path.realpath(os.path.join(os.path.curdir, "cloudwatch_agent_config_schema.json"))
SCHEMA_PATH = os.environ.get("CW_LOGS_CONFIGS_SCHEMA_PATH", DEFAULT_SCHEMA_PATH)
Expand Down Expand Up @@ -47,6 +49,15 @@ def parse_args():
return parser.parse_args()


def render_jinja_template(template_file_path, **kwargs):
file_loader = FileSystemLoader(str(os.path.dirname(template_file_path)))
env = SandboxedEnvironment(loader=file_loader)
rendered_template = env.get_template(os.path.basename(template_file_path)).render(**kwargs)
with open(template_file_path, "w", encoding="utf-8") as f:
f.write(rendered_template)
return template_file_path


def get_input_json(args):
"""Either load the input JSON data from a file, or returned the JSON parsed on the CLI."""
if args.input_file:
Expand All @@ -59,7 +70,10 @@ def get_input_json(args):
def _read_json_at(path):
"""Read the JSON file at path."""
try:
with open(path, encoding="utf-8") as input_file:
config_args = {
"default_platforms": ["amazon", "centos", "redhat", "rocky", "ubuntu"],
}
with open(render_jinja_template(path, **config_args), encoding="utf-8") as input_file:
return json.load(input_file)
except FileNotFoundError:
_fail(f"No file exists at {path}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

describe file('/usr/local/bin/cloudwatch_agent_config_util.py') do
it { should exist }
its('sha256sum') { should eq 'b816b4891a5e8f1e7ac94616db7927f7955ba72a8f53ec1b320402a2ac9c9b7f' }
its('sha256sum') { should eq '321b1a5e4886588a6f5501e4b1fe00b506ecab54640e529cfb9f407ad03dd289' }
its('owner') { should eq 'root' }
its('group') { should eq 'root' }
its('mode') { should cmp '0644' }
Expand Down

0 comments on commit f41ddd0

Please sign in to comment.