Skip to content

fix(macros): fix format_kwargs macro #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2020

Conversation

sticky-note
Copy link
Member

@sticky-note sticky-note commented Apr 29, 2020

PR progress checklist (to be filled in by reviewers)

  • Changes to documentation are appropriate (or tick if not required)
  • Changes to tests are appropriate (or tick if not required)
  • Reviews completed

What type of PR is this?

Primary type

  • [build] Changes related to the build system
  • [chore] Changes to the build process or auxiliary tools and libraries such as documentation generation
  • [ci] Changes to the continuous integration configuration
  • [feat] A new feature
  • [fix] A bug fix
  • [perf] A code change that improves performance
  • [refactor] A code change that neither fixes a bug nor adds a feature
  • [revert] A change used to revert a previous commit
  • [style] Changes that do not affect the meaning of the code (white-space, formatting, missing semi-colons, etc.)

Secondary type

  • [docs] Documentation changes
  • [test] Adding missing or correcting existing tests

Does this PR introduce a BREAKING CHANGE?

No.

Related issues and/or pull requests

Quote kwargs values on format_kwargs macro to resolve sls rendering failure on special characters :

    Data failed to compile:
----------
    Rendering SLS 'base:postgres.manage' failed: found character '%' that cannot start any token; line 17
---
[...]
  postgres_user.present:
    - createdb: False
    - createroles: False
    - inherit: True
    - name: username
    - password: %0q`]O/)/d|v(Sj3-JR$HK8;    <======================
    - replication: False
    - user: postgres
    - onchanges:
      - test: postgres-reload-modules

[...]
---
ERROR: Minions returned with non-zero exit code

Describe the changes you're proposing

Pillar / config required to test the proposed changes

Debug log showing how the proposed changes work

Documentation checklist

  • Updated the README (e.g. Available states).
  • Updated pillar.example.

Testing checklist

  • Included in Kitchen (i.e. under state_top).
  • Covered by new/existing tests (e.g. InSpec, Serverspec, etc.).
  • Updated the relevant test pillar.

Additional context

@sticky-note sticky-note requested a review from myii April 29, 2020 03:03
@myii
Copy link
Contributor

myii commented Apr 29, 2020

@sticky-note Thanks for the PR. Just tracking back through the use of format_kwargs:

pkgrepo.managed:
{{- format_kwargs(postgres.pkg_repo) }}

  • This should be fine.

{%- macro format_state(name, state, kwarg) %}
{%- if 'name' not in kwarg %}
{%- do kwarg.update({'name': name}) %}
{%- endif %}
{%- if 'ensure' in kwarg %}
{%- set ensure = kwarg.pop('ensure') %}
{%- endif %}
{%- set user_available = not (state == 'postgres_schema' and grains.saltversioninfo < [2018, 3]) %}
{%- if 'user' not in kwarg and user_available %}
{%- do kwarg.update({'user': postgres.user}) %}
{%- endif -%}
{{ state }}-{{ name }}:
{{ state }}.{{ ensure|default('present') }}:
{{- format_kwargs(kwarg) }}
- onchanges:
- test: postgres-reload-modules
{%- endmacro %}

  • This needs further looking in to, which follows below.

{%- for name, user in postgres.users|dictsort() %}
{{ format_state(name, 'postgres_user', user) }}
{%- endfor %}

  • This is the part you're trying to fix.
  • However, there are potential issues based on the expected data here.

users:
localUser:
ensure: present
password: '98ruj923h4rf'
createdb: false
createroles: false
inherit: true
replication: false
remoteUser:
ensure: present
password: '98ruj923h4rf'
createdb: false
createroles: false
inherit: true
replication: false

  • There are boolean values being supplied here; converting these to string may have consequences when consumed by the Salt state and modules -- almost definitely best to avoid quoting these booleans.

@vutny Can I get your opinion about this?

@sticky-note
Copy link
Member Author

@myii hmmmm, you're right. Non-empty strings are evaluated as true. Is there a way to test the type of a value ?

@sticky-note
Copy link
Member Author

Tested a salt-call state.show_sls postgres on this pillar:

postgres:
  users:
    test_dba:
      ensure: present
      password: '%=Hec$K8;'
      createdb: false
      createroles: false
      inherit: true
      replication: false

It seems that boolean strings are correctly computed in Booleans :

(...)
    postgres_user-test_dba:
        ----------
        __env__:
            base
        __sls__:
            postgres.manage
        postgres_user:
            |_
              ----------
              createdb:
                  False
            |_
              ----------
              createroles:
                  False
            |_
              ----------
              inherit:
                  True
            |_
              ----------
              name:
                  test_dba
            |_
              ----------
              password:
                  %=Hec$K8;
            |_
              ----------
              replication:
                  False
            |_
              ----------
              user:
                  postgres
            |_
              ----------
              onchanges:
                  |_
                    ----------
                    test:
                        postgres-reload-modules
            - present
            |_
              ----------
              order:
                  10011
(...)

I think it's because format_kwargs is never called directly but not sure at all

@pull-assistant
Copy link

pull-assistant bot commented Jul 16, 2020

Score: 1.00

Best reviewed: commit by commit


Optimal code review plan

     fix(macros): fix format_kwargs macro

Powered by Pull Assistant. Last update 5e6511b ... 5e6511b. Read the comment docs.

@sticky-note
Copy link
Member Author

@myii @vutny Is that better like that ?

@sticky-note
Copy link
Member Author

What about this PR @vutny @myii ? :)

@@ -4,7 +4,7 @@

{%- filter indent(4) %}
{%- for k, v in kwarg|dictsort() %}
- {{ k }}: {{ v }}
- {{ k }}: {{ "'" if v is string else '' }}{{ v }}{{ "'" if v is string else '' }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sticky-note Perhaps this can be done more cleanly using https://docs.saltstack.com/en/latest/topics/jinja/#quote. Not tried this yet but how about:

Suggested change
- {{ k }}: {{ "'" if v is string else '' }}{{ v }}{{ "'" if v is string else '' }}
- {{ k }}: {{ v|quote if v is string else v }}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

| quote doesn't work with : in strings for example.
yaml_dquote has better escaping power and encapsulate into " like I wanted

@myii
Copy link
Contributor

myii commented Jul 26, 2020

@sticky-note Did you test this out first?

@sticky-note
Copy link
Member Author

@myii
| quote doesn't work with : in strings for example.
| yaml_dquote has better escaping power and encapsulate into " like I wanted

@myii myii merged commit 21baf28 into saltstack-formulas:master Jul 27, 2020
@myii
Copy link
Contributor

myii commented Jul 27, 2020

Merged, thanks @sticky-note -- apologies for the delay.

@sticky-note
Copy link
Member Author

@myii No problem, I didn't have so much time too ;)

@sticky-note sticky-note deleted the fix/pwd branch July 27, 2020 01:21
@saltstack-formulas-travis

🎉 This PR is included in version 0.41.2 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants