
    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
Z
d dlZd dlZd dlmZmZ d dlmZ d dlmZ d d	lmZmZmZ 	 d dlZdZd
Zd Zd Zd Zd Z e!dk(  r e         yy# e$ r  ej8                         ZdZdZY 6w xY w)    )absolute_importdivisionprint_functiona_  
author: Felix Fontein (@felixfontein)
module: sops_encrypt
short_description: Encrypt data with SOPS
version_added: '0.1.0'
description:
  - Allows to encrypt binary data (Base64 encoded), text data, JSON or YAML data with SOPS.
options:
  path:
    description:
      - The SOPS encrypt file.
    type: path
    required: true
  force:
    description:
      - Force rewriting the encrypted file.
    type: bool
    default: false
  content_text:
    description:
      - The data to encrypt. Must be a Unicode text.
      - Please note that the module might not be idempotent if the text can be parsed as JSON or YAML.
      - Exactly one of O(content_text), O(content_binary), O(content_json), and O(content_yaml) must be specified.
    type: str
  content_binary:
    description:
      - The data to encrypt. Must be L(Base64 encoded,https://en.wikipedia.org/wiki/Base64) binary data.
      - Please note that the module might not be idempotent if the data can be parsed as JSON or YAML.
      - Exactly one of O(content_text), O(content_binary), O(content_json), and O(content_yaml) must be specified.
    type: str
  content_json:
    description:
      - The data to encrypt. Must be a JSON dictionary.
      - Exactly one of O(content_text), O(content_binary), O(content_json), and O(content_yaml) must be specified.
    type: dict
  content_yaml:
    description:
      - The data to encrypt. Must be a YAML dictionary.
      - Please note that Ansible only allows to pass data that can be represented as a JSON dictionary.
      - Exactly one of O(content_text), O(content_binary), O(content_json), and O(content_yaml) must be specified.
    type: dict
extends_documentation_fragment:
  - ansible.builtin.files
  - community.sops.sops
  - community.sops.sops.encrypt_specific
  - community.sops.attributes
  - community.sops.attributes.files
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
  safe_file_operations:
    support: full
  idempotent:
    support: full
seealso:
  - plugin: community.sops.sops
    plugin_type: lookup
    description: The sops lookup can be used decrypt SOPS-encrypted files.
a  
---
- name: Encrypt a secret text
  community.sops.sops_encrypt:
    path: text-data.sops
    content_text: This is a secret text.

- name: Encrypt the contents of a file
  community.sops.sops_encrypt:
    path: binary-data.sops
    content_binary: "{{ lookup('ansible.builtin.file', '/path/to/file', rstrip=false) | b64encode }}"

- name: Encrypt some datastructure as YAML
  community.sops.sops_encrypt:
    path: stuff.sops.yaml
    content_yaml: "{{ result }}"
#N)AnsibleModulemissing_required_lib)to_text)
write_file)Sops	SopsErrorget_sops_argument_specTFc                     | j                   d   y| j                   d   y| j                   d   y| j                   d   y| j                  d	       y )
Ncontent_textbinarycontent_binarycontent_jsonjsoncontent_yamlyaml$Internal error: unknown content typemsg)params	fail_json)modules    o/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/sops/plugins/modules/sops_encrypt.pyget_data_typer   t   s]    }}^$0}}%&2}}^$0}}^$0
?@    c                    | j                   d   !|| j                   d   j                  d      k(  S | j                   d   ||k(  S | j                   d   &	 t        j                  |      | j                   d   k(  S | j                   d   &	 t        j                  |      | j                   d   k(  S | j                  d       y # t        $ r Y yw xY w# t        $ r Y yw xY w)	Nr   utf-8r   r   Fr   r   r   )r   encoder   loads	Exceptionr   	safe_loadr   )r   binary_datacontents      r   compare_encoded_contentr'      s    }}^$0&--7>>wGGG}}%&2+%%}}^$0	::g&&--*GGG }}^$0	>>'*fmmN.KKK ?@  		  		s$   $C 
$C 	CC	CCc                    | j                   d    d| j                   d   j                  d      fS | j                   d   d|fS | j                   d   3dt        j                  | j                   d         j                  d      fS | j                   d   3dt	        j
                  | j                   d         j                  d      fS | j                  d	
       y )Nr   r   r    r   r   r   r   r   r   r   )r   r!   r   dumpsr   	safe_dumpr   )r   r%   s     r   get_encoded_type_contentr+      s    }}^$0~6==gFFF}}%&2$$}}^$0tzz&--"?@GGPPP}}^$0t~~fmmN&CDKKGTTT
?@r   c                     t        t        dd      t        dd      t        dd      t        dd      t        d	d      t        d	d      
      } | j                  t        d             t        | dgdgdd      j                  d   &t
        s j                  t        d      t               d }j                  d   #	 t        j                  j                  d         }j                  d   }t        j                  j                  |      xs d }d}fd}	 j                  d   st        j                  j!                  |      sd}n3t#        j$                  |dt'              d|      }t)        ||      sd}|rj*                  st-        |      \  }}	d }
|j/                  d      rd}
n|j/                  d      rd}
t#        j0                  |	|||
| t        j                  j3                  ||      n||      }t5        |       j;                  j                        }j=                  ||      }j?                  |       y # t        $ r,}j                  dj                  |             Y d }~d }~ww xY w# t6        $ r%}j                  t9        |             Y d }~d }~ww xY w)NpathT)typerequiredboolF)r.   defaultstr)r.   no_logdict)r-   forcer   r   r   r   )add_encrypt_specific)r   r   r   r   )argument_specmutually_exclusiverequired_one_ofsupports_check_modeadd_file_common_argsr   pyyaml)r   	exceptionr   z&Cannot decode Base64 encoded data: {0}r   c                 :    j                   j                  |       S )N)r   get)argument_namer   s    r   get_option_valuezmain.<locals>.get_option_value   s    }}  //r   r5   )decode_outputoutput_typerstriprA   r   z.jsonr   )z.ymlz.yamlr   )datacwd
input_typerC   filenamerA   r   )changed) r4   updater   r   r   HAS_YAMLr   r   YAML_IMP_ERRbase64	b64decoder#   formatosr-   dirnameexistsr   decryptr   r'   
check_moder+   endswithencryptrelpathr
   r   r	   load_file_common_argumentsset_fs_attributes_if_different	exit_json)r7   r%   er-   	directoryrI   rA   decrypted_contentrG   
input_datarC   rE   	file_argsr   s                @r   mainr`      s   v-.uT2t4vd3vd3M /TJK#N
 O
 !!
F }}^$01(;|T K}}%&2	U **6==9I+JKK == D%-IG0)==!)=G !%E}V7LUZ!1&! +6;@QR6,,%=fk%R"J
K}}W%$01$<<Y:S^=F=Ry9X\!1&D
 vt$ 11&--@I33IwGG
W%U  	U!I!P!PQR!STT	UH  )WQZ(()s1   	"I %C4J 	J"!J		J	J?J::J?__main__)"
__future__r   r   r   r.   __metaclass__DOCUMENTATIONEXAMPLESRETURNrM   r   rP   	tracebackansible.module_utils.basicr   r   +ansible.module_utils.common.text.convertersr	   :ansible_collections.community.sops.plugins.module_utils.ior
   <ansible_collections.community.sops.plugins.module_utils.sopsr   r   r   r   rL   rK   ImportError
format_excr   r'   r+   r`   __name__ r   r   <module>rp      s    A @<|$ 
   	  J ? Q p pLH	AA,	AI&X zF A  '9'')LHDs   A$ $B B