
    Vh*                         d dl mZmZmZ eZdZd dlZd dlm	Z	 d dl
mZmZmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ  e       Zi Zi Z G d de      Zy)    )absolute_importdivisionprint_functiona_  
name: sops
author: Edoardo Tenani (@endorama) <e.tenani@arduino.cc>
short_description: Loading SOPS-encrypted vars files
version_added: '0.1.0'
description:
  - Load encrypted YAML files into corresponding groups/hosts in C(group_vars/) and C(host_vars/) directories.
  - Files are encrypted prior to reading, making this plugin an effective companion to P(ansible.builtin.host_group_vars#vars)
    plugin.
  - Files are restricted to V(.sops.yaml), V(.sops.yml), V(.sops.json) extensions, unless configured otherwise with O(valid_extensions).
  - Hidden files are ignored.
options:
  valid_extensions:
    default: [".sops.yml", ".sops.yaml", ".sops.json"]
    description:
      - Check all of these extensions when looking for 'variable' files.
      - These files must be SOPS encrypted YAML or JSON files.
      - By default the plugin will produce errors when encountering files matching these extensions that are not SOPS encrypted.
        This behavior can be controlled with the O(handle_unencrypted_files) option.
    type: list
    elements: string
    ini:
      - key: valid_extensions
        section: community.sops
        version_added: 1.7.0
    env:
      - name: ANSIBLE_VARS_SOPS_PLUGIN_VALID_EXTENSIONS
        version_added: 1.7.0
  stage:
    version_added: 0.2.0
    ini:
      - key: vars_stage
        section: community.sops
    env:
      - name: ANSIBLE_VARS_SOPS_PLUGIN_STAGE
  cache:
    description:
      - Whether to cache decrypted files or not.
      - If the cache is disabled, the files will be decrypted for almost every task. This is very slow!
      - Only disable caching if you modify the variable files during a playbook run and want the updated result to be available
        from the next task on.
      - 'Note that setting O(stage=inventory) has the same effect as setting O(cache=true): the variables will be loaded only
        once (during inventory loading) and the vars plugin will not be called for every task.'
    type: bool
    default: true
    version_added: 0.2.0
    ini:
      - key: vars_cache
        section: community.sops
    env:
      - name: ANSIBLE_VARS_SOPS_PLUGIN_CACHE
  disable_vars_plugin_temporarily:
    description:
      - Temporarily disable this plugin.
      - Useful if ansible-inventory is supposed to be run without decrypting secrets (in AWX for instance).
    type: bool
    default: false
    version_added: 1.3.0
    env:
      - name: SOPS_ANSIBLE_AWX_DISABLE_VARS_PLUGIN_TEMPORARILY
  handle_unencrypted_files:
    description:
      - How to handle files that match the extensions in O(valid_extensions) that are not SOPS encrypted.
      - The default value V(error) will produce an error.
      - The value V(skip) will simply skip these files. This requires SOPS 3.9.0 or later.
      - The value V(warn) will skip these files and emit a warning. This requires SOPS 3.9.0 or later.
      - B(Note) that this will not help if the store SOPS uses cannot parse the file, for example because it is no valid JSON/YAML/...
        file despite its file extension. For extensions other than the default ones SOPS uses the binary store, which tries
        to parse the file as JSON.
    type: string
    choices:
      - skip
      - warn
      - error
    default: error
    version_added: 1.8.0
    ini:
      - key: handle_unencrypted_files
        section: community.sops
    env:
      - name: ANSIBLE_VARS_SOPS_PLUGIN_HANDLE_UNENCRYPTED_FILES
extends_documentation_fragment:
  - ansible.builtin.vars_plugin_staging
  - community.sops.sops
  - community.sops.sops.ansible_env
  - community.sops.sops.ansible_ini
seealso:
  - plugin: community.sops.sops
    plugin_type: lookup
    description: The sops lookup can be used decrypt SOPS-encrypted files.
  - plugin: community.sops.decrypt
    plugin_type: filter
    description: The decrypt filter can be used to decrypt SOPS-encrypted in-memory data.
  - module: community.sops.load_vars
N)AnsibleParserError)to_bytes	to_nativeto_text)BaseVarsPlugin)Host)Group)combine_vars)Sops	SopsError)Displayc                         e Zd Zd fd	Z xZS )
VarsModulec           
          t        |t              s|g}t        t           |||        fd}| j                  d      } j                  d      ri S  j                  d      } j                  d      }i }|D ]  }	t        |	t              rd}
n*t        |	t              rd}
nt        d	t        |	      z        |	j                  j                  t        j                  j                        ru	 g }t        j                  j                  t!        t        j                  j#                   j$                  |
                  }t'        |      }|	j                  d
|} j(                  j+                  d|z         |r|t,        v r
t,        |   }nt        j                  j/                  |      rt        j                  j1                  |      r j(                  j3                  d|z         |j5                  ||	j                  |d      }|j7                  |j5                  ||	j                        D cg c]  t9        fd|D              r c}       |t,        |<   n! j(                  j;                  d|
d|       |D ]  }|r|t<        v r
t<        |   }nht?        j@                  |tB              }|dk7  r+|jE                         st        d|d|jF                        	 |jI                  ||      }|t<        |<   |jQ                  |      }|stS        ||      }  |S c c}w # tJ        $ r}d}|jE                         r	 |jM                  |      }|jN                  s_|dk(  r! j(                  j+                  d|z         d}n9|dk(  r! j(                  j;                  d|z         d}n|dk(  rt        d|z        n7# tJ        $ r+} j(                  j;                  d|d|       Y d}~nd}~ww xY w|rY d}~ d}~ww xY w# t        $ r  tJ        $ r}t        tU        |            d}~wtV        $ r}t        dtU        |      z        d}~ww xY w)z parses the inventory file c                 &    j                  |       S N)
get_option)argument_nameselfs    d/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/sops/plugins/vars/sops.pyget_option_valuez-VarsModule.get_vars.<locals>.get_option_value   s    ??=11    Ncachedisable_vars_plugin_temporarilyvalid_extensionshandle_unencrypted_files	host_vars
group_varsz5Supplied entity must be Host or Group, got %s instead.zkey: %sz	processing dir %sF)
extensions	allow_dirc              3   R   K   | ]  }t              j                  |         y wr   )r	   endswith).0	extension	file_paths     r   	<genexpr>z&VarsModule.get_vars.<locals>.<genexpr>   s)       ;Efo79;M;V;VW`;a  ;Es   $'zFound z$ that is not a directory, skipping: )displayerrorz$Cannot use handle_unencrypted_files=z with SOPS )r   skipz.SOPS vars plugin: skipping unencrypted file %sTwarnz*SOPS vars plugin: file %s is not encryptedz/SOPS vars plugin: cannot obtain file status of z: z,Unexpected error in the SOPS vars plugin: %s),
isinstancelistsuperr   get_varsr   r   r   r   typename
startswithospathseprealpathr   join_basedirr	   _displayvvvvFOUNDexistsisdirdebugfind_vars_filesextendanywarning	DECRYPTEDr   get_sops_runner_from_optionsr+   has_filestatusversion_stringdecryptr   get_filestatus	encryptedloadr   r   	Exception)r   loaderr7   entitiesr   r   r   r   dataentitysubdirfound_filesb_opathopathkeyr)   foundfile_contentsops_runnerexcr-   file_status
status_excnew_datae	__class__s   `              `         r   r2   zVarsModule.get_vars}   s>    (D) zHj$(x@	2 =OOG,E??<=I??+=>#'??3M#N  P	lF&$'$FE*%()`dhiodp)qrr ;;))"''++6Fl"$K gg..xT]]TZ8[/\]G#G,E%+[[%8CMM&&yC'89&+Cj 77>>'2!ww}}W5 $ 3 34IE4Q R /5.D.DUFKKdt  AF.D  /G + 2 2vOeOefkmsmxmxOy 4F)7:  ;E  tD  ;E  8E 5> 4F !G-8c
 $ 5 5jprw6x y!, %@ Ui%7+4U+;L*.*K*KL\fm*nK77B;KeKeKg&8]u  xC  xR  xR  %S'" !"&/:/B/B5[k/B/l0 0<Ie,#);;|#<##/h#?DK%@IP	ld i4F" $- &',#.#=#=#?%M6A6P6PQV6W/:/D/D/G6/Q040B0BCsv{C{0|7;1IV1S040E0EFvy~F~07;1IW1T6HIux}I}6~0~+4 %M )-(=(=y~  AK  ?L  )M  )M%M $($, %-&8 *   ;,Yq\::  l,-[^ghi^j-jkkls   3EP8L/
BP,L4?PP/P4	P=PA<OP	P	!O<	7P<P	PPPPPQ#'P;;Q#QQ#r   )__name__
__module____qualname__r2   __classcell__)r`   s   @r   r   r   {   s    g gr   r   )
__future__r   r   r   r3   __metaclass__DOCUMENTATIONr6   ansible.errorsr   +ansible.module_utils.common.text.convertersr   r   r	   ansible.plugins.varsr
   ansible.inventory.hostr   ansible.inventory.groupr   ansible.utils.varsr   <ansible_collections.community.sops.plugins.module_utils.sopsr   r   ansible.utils.displayr   r+   r>   rF   r    r   r   <module>rq      s]    C B^@ 
 - T T / ' ) + X )
) 		i ir   