Skip to content

Commit

Permalink
added dynamic cred support during compile (#193)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9bf45c9b02f2e53c0fe3f0792f48b043adc197ee)
  • Loading branch information
utkarshb07 authored and abhijeetkaurav1st committed Mar 6, 2023
1 parent 3e21b5c commit 20a6de1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
45 changes: 33 additions & 12 deletions calm/dsl/decompile/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,48 @@ def render_credential_template(cls):
user_attrs = cls.get_user_attrs()
user_attrs["description"] = cls.__doc__

cred_type = user_attrs.get("cred_class", "")

var_name = "BP_CRED_{}".format(get_valid_identifier(cls.__name__))
file_name = "{}_{}".format(var_name, user_attrs["type"])
user_attrs["var_name"] = var_name
if user_attrs.get("editables", {}):
user_attrs["editables"] = user_attrs["editables"].get_dict()
CRED_VAR_NAME_MAP[user_attrs["name"]] = var_name

if cred_type == "static":

file_name = "{}_{}".format(var_name, user_attrs["type"])
create_file_from_file_name(file_name)
user_attrs["value"] = file_name
text = render_template("basic_credential.py.jinja2", obj=user_attrs)

elif cred_type == "dynamic":

for var_obj in user_attrs.get("variable_list", []):
if var_obj.type == "SECRET":
file_name = "{}_VAR_{}_SECRET".format(
var_name, get_valid_identifier(var_obj.name)
)
create_file_from_file_name(file_name)
var_obj.value = file_name

text = render_template("dynamic_credential.py.jinja2", obj=user_attrs)

else:
raise TypeError("{} is not a supported cred class".format(cred_type))
return text.strip()


def create_file_from_file_name(file_name):
"""create a file on local directory and add to global file stack for given file name"""
file_loc = os.path.join(get_local_dir(), file_name)

# Storing empty value in the file
with open(file_loc, "w+") as fd:
fd.write("")

user_attrs["var_name"] = var_name
user_attrs["value"] = file_name

if user_attrs.get("editables", {}):
user_attrs["editables"] = user_attrs["editables"].get_dict()

# update the map
CRED_VAR_NAME_MAP[user_attrs["name"]] = var_name
CRED_FILES.append(file_name)

text = render_template("credential.py.jinja2", obj=user_attrs)
return text.strip()


def get_cred_var_name(cred_name):
"""Get the var name for credential"""
Expand Down
17 changes: 17 additions & 0 deletions calm/dsl/decompile/schemas/dynamic_credential.py.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{% macro var_dict_obj(var_list) %}
{
{% for var in var_list %}
'{{var.name}}': {% if var.type == "SECRET" %} {{var.value}} {% else %} '{{var.value}}' {% endif %},
{% endfor %}
}
{% endmacro %}

{% macro cred(obj) %}
{% if obj.default %}
{{obj.var_name}} = dynamic_cred('{{obj.username}}', Ref.Account('{{obj.account}}'), {% if obj.resource_type %}resource_type=Ref.Resource_Type('{{obj.resource_type}}'){%- endif %}, variable_dict={{var_dict_obj(obj.variable_list)|indent(2)}}, name='{{obj.name}}', default=True, type='{{obj.type}}', {% if obj.editables %}editables = {{obj.editables}}{%- endif %})
{% else %}
{{obj.var_name}} = dynamic_cred('{{obj.username}}', Ref.Account('{{obj.account}}'), {% if obj.resource_type %}resource_type=Ref.Resource_Type('{{obj.resource_type}}'){%- endif %}, variable_dict={{var_dict_obj(obj.variable_list)|indent(2)}}, name='{{obj.name}}', type='{{obj.type}}', {% if obj.editables %}editables = {{obj.editables}}{%- endif %})
{% endif %}
{% endmacro %}

{{ cred(obj) }}

0 comments on commit 20a6de1

Please sign in to comment.