
    Vh                          d Z ddlmZmZmZ eZdZdZ	 ddl	m
Z
 ddlZddlmZmZ 	 ddlZdZdZd
 Zd Zd Zd Zd Zedk(  r e        yy# e$ r	 ddlm
Z
 Y ?w xY w# e$ r d	Z ej(                         ZY Hw xY w)z,
Ansible Module - community.general.keyring
    )absolute_importdivisionprint_functiona  
module: keyring
version_added: 5.2.0
author:
  - Alexander Hussey (@ahussey-redhat)
short_description: Set or delete a passphrase using the Operating System's native keyring
description: >-
  This module uses the L(keyring Python library, https://pypi.org/project/keyring/) to set or delete passphrases for a given
  service and username from the OS' native keyring.
requirements:
  - keyring (Python library)
  - gnome-keyring (application - required for headless Gnome keyring access)
  - dbus-run-session (application - required for headless Gnome keyring access)
extends_documentation_fragment:
  - community.general.attributes
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  service:
    description: The name of the service.
    required: true
    type: str
  username:
    description: The user belonging to the service.
    required: true
    type: str
  user_password:
    description: The password to set.
    required: false
    type: str
    aliases:
      - password
  keyring_password:
    description: Password to unlock keyring.
    required: true
    type: str
  state:
    description: Whether the password should exist.
    required: false
    default: present
    type: str
    choices:
      - present
      - absent
a  
- name: Set a password for test/test1
  community.general.keyring:
    service: test
    username: test1
    user_password: "{{ user_password }}"
    keyring_password: "{{ keyring_password }}"

- name: Delete the password for test/test1
  community.general.keyring:
    service: test
    username: test1
    user_password: "{{ user_password }}"
    keyring_password: "{{ keyring_password }}"
    state: absent
)quoteN)AnsibleModulemissing_required_libTFc           
         | j                   ry	 t        j                  | j                  d   | j                  d          y# t        j                  j
                  $ r dt        | j                  d         dt        | j                  d         dt        | j                  d         d}| j                  d	d
|d      \  }}}|j                  d      sY y|j                  d      cY S w xY w)zk
    Attempt to delete a passphrase in the keyring using the Python API and fallback to using a shell.
    Nserviceusernameecho "keyring_passwordz." | gnome-keyring-daemon --unlock
keyring del  
dbus-run-session -- /bin/bashTuse_unsafe_shelldataencodingUTF-8)	
check_modekeyringdelete_passwordparamserrorsKeyringLockedr   run_commanddecode)moduledelete_argumentdummystderrs       m/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/keyring.pydel_passphraser#   c   s     &i 8&--
:ST>>'' & fmm$678fmmI./fmmJ/0	 	  &11+! 	  2  
uf }}W%}}W%%%&s   0A   BC+C+*C+c                 *   | j                   ry	 t        j                  | j                  d   | j                  d   | j                  d          y# t        j                  j
                  $ r dt        | j                  d         dt        | j                  d         dt        | j                  d         d	t        | j                  d         d		}| j                  d
d|d      \  }}}|j                  d      sY y|j                  d      cY S w xY w)zf
    Attempt to set passphrase in the keyring using the Python API and fallback to using a shell.
    Nr
   r   user_passwordr   r   z." | gnome-keyring-daemon --unlock
keyring set r   r   r   Tr   r   )	r   r   set_passwordr   r   r   r   r   r   )r   set_argumentr    r!   s       r"   set_passphraser(      s     &MM)$MM*%MM/*	

 >>'' & fmm$678fmmI./fmmJ/0fmmO45 	  &11+!	  2  
uf }}W%}}W%%%&s   >A B.D>DDc           	      0   	 t        j                  | j                  d   | j                  d         }|S # t         j                  j                  $ r Y n,t         j                  j
                  $ r Y nt        $ r Y nw xY wdt        | j                  d         dt        | j                  d         dt        | j                  d         d}| j                  dd	|d
      \  }}}	 |j                  d      j                         d   S # t        $ r Y y
w xY w)zi
    Attempt to retrieve passphrase from keyring using the Python API and fallback to using a shell.
    r
   r   r   r   z." | gnome-keyring-daemon --unlock
keyring get r   r   r   TNr   r      )r   get_passwordr   r   r   	InitErrorAttributeErrorr   r   r   
splitlines
IndexError)r   
passphraseget_argumentr    stdouts        r"   get_passphraser3      s   
))MM)$fmmJ&?

 >>'' >>##   	fmm./0fmmI&'fmmJ'(L
 "--'	 . E65}}W%002155 s-   14 A<A<1A<;A<'!D	 		DDc                  R   t        dd      } t        t        dd      t        dd      t        ddd      t        ddddg	      t        ddd
dd
g            }t        |d      }t        s |j                  t	        d      t
               t        |      }|j                  d   d
k(  r|||j                  d   k(  r%d|j                  d   d|j                  d   | d<   ||j                  d   k7  rKt        |      }|*d| d<   d|j                  d   d|j                  d   | d<   ||j                  |       |Kt        |      }|*d| d<   d|j                  d   d|j                  d   | d<   ||j                  |       |j                  d   dk(  rt|s%d|j                  d   d|j                  d   | d<   |rKt        |      }|*d| d<   d|j                  d   d|j                  d   | d<   ||j                  |        |j                  di |  y) z;
    Attempts to retrieve a passphrase from a keyring.
    F )changedmsgstrT)typerequired)r9   r:   no_logpassword)r9   r:   r;   aliasespresentabsent)r9   r:   defaultchoices)r
   r   r   r%   state)argument_specsupports_check_moder   )r7   	exceptionrB   Nr%   zPassphrase already set for r
   @r   r7   r6   z Passphrase has been updated for )r7   zPassphrase already absent for resultz Passphrase has been removed for  )dictr   HAS_KEYRING	fail_jsonr   KEYRING_IMP_ERRr3   r   r(   r#   	exit_json)rG   module_argsr   r0   
set_result
del_results         r"   
run_modulerQ      sU    F
 %$/54054Etj\
 	HiCX

K $OF1)<X'J}}W*!V]]?;;MM),MM*-!u V]]?;;+F3
%(,F9%i0j1%F5M )$$$4'/J!$(y!MM),MM*-!u %  Z 0}}W)i(j) F8 '/J!$(y!MM),MM*-!u %  Z 0Fv    c                      t                y)z
    main module loop
    N)rQ   rH   rR   r"   mainrT     s	     LrR   __main__)__doc__
__future__r   r   r   r9   __metaclass__DOCUMENTATIONEXAMPLESshlexr   ImportErrorpipes	tracebackansible.module_utils.basicr   r   r   rJ   rL   
format_excr#   r(   r3   rQ   rT   __name__rH   rR   r"   <module>rb      s    A @/b"  J-KO&<&D@HV zF E    -K*i**,O-s"   A A AAA87A8