Skip to content

Commit

Permalink
fix(map): enable import of mapdata from outside the cert-formula
Browse files Browse the repository at this point in the history
`tplroot` can't be set correctly when the import:

- is done from another top directory than the `cert`
- the import is done `with context`

In this case, the `tpldir` is set to the directory of the importer
`.sls` file instead of the `.jinja` imported one.

We force the `without context` which permits to directly use `tpldir`
as the `tplroot` which is the directory of the imported file.

BREAKING CHANGE: `map.jinja` import must use `without context`
  • Loading branch information
baby-gnu committed Mar 17, 2021
1 parent c5dd8c6 commit 235e329
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cert/_mapdata/init.sls
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
---
{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split("/")[0] %}
{%- from tplroot ~ "/map.jinja" import mapdata with context %}
{%- from tplroot ~ "/map.jinja" import mapdata without context %}
{%- set _mapdata = {
"values": mapdata,
Expand Down
2 changes: 1 addition & 1 deletion cert/init.sls
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This is the main state file for deploying certificates

{% from "cert/map.jinja" import mapdata with context %}
{% from "cert/map.jinja" import mapdata without context %}
# Install required packages
cert_packages:
Expand Down
15 changes: 13 additions & 2 deletions cert/libmapstack.jinja
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{#- -*- coding: utf-8 -*- #}
{#- vim: ft=jinja #}

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split("/")[0] %}
{#- Force import `without context` #}
{#- `with context` override the `tplfile` and `tpldir` variables #}
{%- if not tplfile.endswith("/libmapstack.jinja") %}
{{ raise("Import error: map.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }}
{%- endif %}

{#- Force import with absolute path #}
{%- if tplfile.startswith("../") %}
{{ raise("Import error: map.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }}
{%- endif %}

{#- `tplroot` is the directory of the current imported file #}
{%- set tplroot = tpldir %}
{%- from tplroot ~ "/libmatchers.jinja" import parse_matchers, query_map %}

{%- set _default_config_dirs = [
Expand Down
15 changes: 13 additions & 2 deletions cert/libmatchers.jinja
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
{#- -*- coding: utf-8 -*- #}
{#- vim: ft=jinja #}

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split("/")[0] %}
{#- Force import `without context` #}
{#- `with context` override the `tplfile` and `tpldir` variables #}
{%- if not tplfile.endswith("/libmatchers.jinja") %}
{{ raise("Import error: map.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }}
{%- endif %}

{#- Force import with absolute path #}
{%- if tplfile.startswith("../") %}
{{ raise("Import error: map.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }}
{%- endif %}

{#- `tplroot` is the directory of the current imported file #}
{%- set tplroot = tpldir %}
{%- from tplroot ~ "/libsaltcli.jinja" import cli %}

{%- set query_map = {
Expand Down
19 changes: 15 additions & 4 deletions cert/map.jinja
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# -*- coding: utf-8 -*-
# vim: ft=jinja
{#- -*- coding: utf-8 -*- #}
{#- vim: ft=jinja #}

{#- Get the `tplroot` from `tpldir` #}
{%- set tplroot = tpldir.split("/")[0] %}
{#- Force import `without context` #}
{#- `with context` override the `tplfile` and `tpldir` variables #}
{%- if not tplfile.endswith("/map.jinja") %}
{{ raise("Import error: map.jinja must be imported without context. tplfile='" ~ tplfile ~ "'") }}
{%- endif %}

{#- Force import with absolute path #}
{%- if tplfile.startswith("../") %}
{{ raise("Import error: map.jinja must be imported with absolute path. tplfile='" ~ tplfile ~ "'") }}
{%- endif %}

{#- `tplroot` is the directory of the current imported file #}
{%- set tplroot = tpldir %}
{%- from tplroot ~ "/libmapstack.jinja" import mapstack %}

{#- Where to lookup parameters source files #}
Expand Down
2 changes: 1 addition & 1 deletion test/salt/states/setup-certs-to-remove.sls
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# A testing state created to add certificate and key file that will
# be removed to test certificate removal.

{% from "cert/map.jinja" import mapdata with context %}
{% from "cert/map.jinja" import mapdata without context %}
add-test-cert:
file.managed:
Expand Down

0 comments on commit 235e329

Please sign in to comment.