Skip to content

[BUG] Custom renderer synchronized from file_root, but not loaded from there #62801

Closed

Description

@heini

Description
While writing a custom renderer, placed into _renderers (which is next to _grains, _modules, etc.) inside a git repository, I found that, after synchronizing the repository to the Salt master, a saltutil.sync_all synchronizes the renderer to the minion (Why? It's only needed on the master anyway.), but doesn't load it from there (as opposed to what the documentation says). Also tried putting it into a local file_root on the master, but got the same result.

The master log always shows a message like this:

2022-10-03 15:14:38,655 [salt.template    :240 ][ERROR   ][78661] The renderer "yaml|custom" is not available

It is found and executed only if I put it into /usr/lib/python3/dist-packages/salt/renderers and restart salt-master.service.

Setup
Please be as specific as possible and give set-up details.

  • on-prem machine
  • VM (Virtualbox, KVM, etc. please specify)
  • VM running on a cloud service, please be explicit and add details
  • container (LXC)
  • or a combination, please be explicit
  • jails if it is FreeBSD
  • classic packaging
  • onedir packaging
  • used bootstrap to install

Steps to Reproduce the behavior

  • Add custom renderer into _renderers directory in either a git repository or local file_root, to have it accessible via salt://_renderers
  • Use it to render pillar data

Expected behavior
Custom renderer should be loaded from said location

Versions Report

salt --versions-report (Provided by running salt --versions-report. Please also mention any differences in master/minion versions.)
Salt Version:
          Salt: 3004.2
 
Dependency Versions:
          cffi: Not Installed
      cherrypy: Not Installed
      dateutil: 2.8.1
     docker-py: Not Installed
         gitdb: 4.0.5
     gitpython: 3.1.14
        Jinja2: 2.11.3
       libgit2: Not Installed
      M2Crypto: Not Installed
          Mako: Not Installed
       msgpack: 1.0.0
  msgpack-pure: Not Installed
  mysql-python: Not Installed
     pycparser: Not Installed
      pycrypto: Not Installed
  pycryptodome: 3.9.7
        pygit2: Not Installed
        Python: 3.9.2 (default, Feb 28 2021, 17:03:44)
  python-gnupg: 0.4.6
        PyYAML: 5.3.1
         PyZMQ: 20.0.0
         smmap: 4.0.0
       timelib: Not Installed
       Tornado: 4.5.3
           ZMQ: 4.3.4
 
System Versions:
          dist: debian 11 bullseye
        locale: utf-8
       machine: x86_64
       release: 5.10.0-17-amd64
        system: Linux
       version: Debian GNU/Linux 11 bullseye

Activity

  1. added
    bug
    on Oct 3, 2022
  2. OrangeDog commented on Oct 3, 2022

    @OrangeDog
    Contributor

    Why? It's only needed on the master anyway.

    Most rendering happens on the minion. Only pillars are the usual exception.

  3. heini commented on Oct 3, 2022

    @heini
    ContributorAuthor

    Ah, OK.

  4. OrangeDog commented on Oct 3, 2022

    @OrangeDog
    Contributor

    What is the code for the renderer and how did you attempt to use it?
    Is there any additional debug logging?

  5. added
    needs-more-info
    on Oct 3, 2022
  6. heini commented on Oct 3, 2022

    @heini
    ContributorAuthor

    There's no additional debug logging. It's only the line shown above.

    The code is the beginning of a renderer to decrypt pillar strings as produced by hiera-eyaml:

    #!/usr/bin/python3
    
    import base64
    import logging
    import re
    import yaml
    
    log = logging.getLogger(__name__)
    
    def __virtual__():
      log.debug('Loading renderer: decrypt...')
      return True
    
    
    class Decrypter(object):
    
      def __init__(self):
        log.debug('Inside init.')
    
      def _decrypt(self, cipher, method):
        log.debug('Decrypting with method: %s...', method)
        if method == 'PLAINTEXT':
          # PLAINTEXT is a dummy encryption method used for testing the module (more to follow)
          return cipher.decode('utf-8')
        else:
          log.debug('Unsupported encryption method: %s', method) 
    
    
      def _decode(self, item):
        match = re.search('^ENC\[(?P<method>\w+),(?P<cipher>.*)\]$', item)
        if match:
          return {
            'cipher': base64.b64decode(match['cipher']),
            'method': match['method'],
          }
        else:
          return False
    
    
      def iterdict(self, data):
        newdict = {}
        for k,v in data.items():
          if isinstance(v, dict):
            newdict[k] = self.iterdict(data=v)
          elif isinstance(v, list):
            newlist = []
            for item in v:
              decoded = self._decode(item=item)
              if decoded:
                newlist.append(self._decrypt(cipher=decoded['cipher'], method=decoded['method']))
              else:
                newlist.append(item)
                newdict[k] = newlist
          else:
            decoded = self._decode(item=v)
            if decoded:
              newdict[k] = self._decrypt(cipher=decoded['cipher'], method=decoded['method'])
            else:
              newdict[k] = v
        return newdict
    
    
    def render(data, saltenv="base", sls="", argline="", **kwargs):
      d = Decrypter()
      return(d.iterdict(data))
    
    
    if __name__ == '__main__':
      d = Decrypter()
      with open('test.sls', 'r') as file:
        data = yaml.safe_load(file)
        print(d.iterdict(data))

    with test.sls looking like so:

    #!yaml|custom
    plain:
      text: ENC[PLAINTEXT,c29tZXRleHQ=]

    which resolves to

    # salt-call pillar.get plain
    local:
        ----------
        text:
            sometext
  7. OrangeDog commented on Oct 3, 2022

    @OrangeDog
    Contributor

    You set the minion log level to DEBUG and "Loading renderer: decrypt..." was never logged?

    This code is in a file at salt://_renderers/custom.py?

  8. heini commented on Oct 3, 2022

    @heini
    ContributorAuthor

    Yes, that's correct. It's only executed if stored in grains['saltpath']/renderers on the master. Then, and only then, it is working as expected and I see the above message (in the master log).

  9. OrangeDog commented on Oct 3, 2022

    @OrangeDog
    Contributor

    So you are trying to run it on the master (i.e. for pillar), not on minions?

    Did you do salt-run saltutil.sync_all? That syncs custom modules to the master, not the minions.
    A restart may also be needed after that, I am unsure.

  10. heini commented on Oct 3, 2022

    @heini
    ContributorAuthor

    No, I also tried to run it on minions, of course. But the outcome is the same. As written above, it gets synchronized, but it is never executed. Also in case of salt myminion saltutil.sync_all && salt myminion pillar.get plain it's only executed after copying it to grains['saltpath']/renderers on the master and restarting it.

  11. OrangeDog commented on Oct 3, 2022

    @OrangeDog
    Contributor

    If you're trying to use it in pillar then it runs on the master, not the minion.
    Therefore you need to sync it to the master, not the minion.
    That's why it works when you manually copy it to the master's source tree, not the minion's.

  12. heini commented on Oct 3, 2022

    @heini
    ContributorAuthor

    OK, understood. Indeed, I removed it from the grains['saltpath']/renderersdirectory again, restarted the master, did a salt-run saltutil.sync_all and it's working now.

    Maybe worth mentioning in the documentation?

    Anyway, thanks a lot for your quick help!

  13. added
    expected-behavior
    and removed
    needs-more-info
    on Oct 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Loaderbugexpected-behavior

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions