
    Vh                         d dl mZmZmZ eZdZdZdZd dl	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 G d de      Zy)    )absolute_importdivisionprint_functiona
  
name: sops
author: Edoardo Tenani (@endorama) <e.tenani@arduino.cc>
short_description: Read SOPS-encrypted file contents
version_added: '0.1.0'
description:
  - This lookup returns the contents from a file on the Ansible controller's file system.
  - This lookup requires the C(sops) executable to be available in the controller PATH.
options:
  _terms:
    description: Path(s) of files to read.
    required: true
    type: list
    elements: str
  rstrip:
    description: Whether to remove trailing newlines and spaces.
    type: bool
    default: true
  base64:
    description:
      - Base64-encodes the parsed result.
      - Use this if you want to store binary data in Ansible variables.
    type: bool
    default: false
  input_type:
    description:
      - Tell SOPS how to interpret the encrypted file.
      - By default, SOPS will chose the input type from the file extension. If it detects the wrong type for a file, this
        could result in decryption failing.
      - The value V(ini) is available since community.sops 1.9.0.
    type: str
    choices:
      - binary
      - json
      - yaml
      - dotenv
      - ini
  output_type:
    description:
      - Tell SOPS how to interpret the decrypted file.
      - By default, SOPS will chose the output type from the file extension. If it detects the wrong type for a file, this
        could result in decryption failing.
      - The value V(ini) is available since community.sops 1.9.0.
    type: str
    choices:
      - binary
      - json
      - yaml
      - dotenv
      - ini
  empty_on_not_exist:
    description:
      - When set to V(true), will not raise an error when a file cannot be found, but return an empty string instead.
    type: bool
    default: false
  extract:
    description:
      - Tell SOPS to extract a specific key from a JSON or YAML file.
      - Expects a string with the same 'querystring' syntax as SOPS' C(--encrypt) option, for example V(["somekey"][0]).
      - B(Note:) Escape quotes appropriately.
    type: str
    version_added: 1.9.0
extends_documentation_fragment:
  - community.sops.sops
  - community.sops.sops.ansible_variables
  - community.sops.sops.ansible_env
  - community.sops.sops.ansible_ini
notes:
  - This lookup does not understand 'globbing' - use the P(ansible.builtin.fileglob#lookup) lookup instead.
seealso:
  - plugin: community.sops.decrypt
    plugin_type: filter
    description: The decrypt filter can be used to descrypt SOPS-encrypted in-memory data.
  - plugin: community.sops.sops
    plugin_type: vars
    description: The sops vars plugin can be used to load SOPS-encrypted host or group variables.
  - module: community.sops.load_vars
a  
---
- name: Output secrets to screen (BAD IDEA!)
  ansible.builtin.debug:
    msg: "Content: {{ lookup('community.sops.sops', item) }}"
  loop:
    - sops-encrypted-file.enc.yaml

- name: Add SSH private key
  ansible.builtin.copy:
  # Note that rstrip=false is necessary for some SSH versions to be able to use the key
    content: "{{ lookup('community.sops.sops', user + '-id_rsa', rstrip=false) }}"
    dest: /home/{{ user }}/.ssh/id_rsa
    owner: "{{ user }}"
    group: "{{ user }}"
    mode: "0600"
  no_log: true # avoid content to be written to log

- name: The file file.json is a YAML file, which contains the encryption of binary data
  ansible.builtin.debug:
    msg: "Content: {{ lookup('community.sops.sops', 'file.json', input_type='yaml', output_type='binary') }}"
zK
_raw:
  description: Decrypted file content.
  type: list
  elements: str
N)AnsibleLookupError)
LookupBase)	to_native)Sops	SopsError)Displayc                       e Zd ZddZy)LookupModuleNc                      j                  ||        j                  d      } j                  d      } j                  d      } j                  d      } j                  d      } j                  d      }	g }
 fd}|D ]  }t        j                  d	|z          j	                  |d
||      }t        j                  d|z         |s+|r|
j                  d       ^t        dt        |      z        	 t        j                  |t        || ||||	      }|rt        t        j                  |            }|
j                  |        |
S # t        $ r}t        t        |            d }~ww xY w)N)var_optionsdirectrstripbase64
input_typeoutput_typeempty_on_not_existextractc                 &    j                  |       S N)
get_option)argument_nameselfs    f/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/sops/plugins/lookup/sops.pyget_option_valuez*LookupModule.run.<locals>.get_option_value   s    ??=11    zSops lookup term: %sfiles)ignore_missingzSops lookup using %s as file z#could not locate file in lookup: %s)displayr   decode_outputr   r   r   r   )set_optionsr   r"   debugfind_file_in_search_pathvvvvappendr   r   r	   decryptr
   r   	b64encode)r   terms	variableskwargsr   
use_base64r   r   r   r   retr   term
lookupfileoutputes   `               r   runzLookupModule.run   sj   Yv>*__X.
__\2
oom4!__-AB//),	2  	DMM047866y'4`r6sJLL8:EF%JJrN()NQZ[_Q`)`aa7S]~){Ueovx "6#3#3F#;<JJv+	. 
  7(1667s   :"E	E3E..E3r   )__name__
__module____qualname__r4    r   r   r   r      s    %r   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.errorsr   ansible.plugins.lookupr   +ansible.module_utils.common.text.convertersr   <ansible_collections.community.sops.plugins.module_utils.sopsr	   r
   ansible.utils.displayr   r"   r   r8   r   r   <module>rD      sQ    C BM^.
  - - A X )
)': 'r   