
    Vh7!                     ~    d dl mZmZmZ eZdZdZd dlZd dl	m
Z
 d dlmZmZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona7  
module: ipa_getkeytab
short_description: Manage keytab file in FreeIPA
version_added: 9.5.0
description:
  - Manage keytab file with C(ipa-getkeytab) utility.
  - See U(https://manpages.ubuntu.com/manpages/jammy/man1/ipa-getkeytab.1.html) for reference.
author: "Alexander Bakanovskii (@abakanovskii)"
attributes:
  check_mode:
    support: full
  diff_mode:
    support: none
options:
  path:
    description:
      - The base path where to put generated keytab file.
    type: path
    aliases: ["keytab"]
    required: true
  principal:
    description:
      - The non-realm part of the full principal name.
    type: str
    required: true
  ipa_host:
    description:
      - The IPA server to retrieve the keytab from (FQDN).
    type: str
  ldap_uri:
    description:
      - LDAP URI. If V(ldap://) is specified, STARTTLS is initiated by default.
      - Can not be used with the O(ipa_host) option.
    type: str
  bind_dn:
    description:
      - The LDAP DN to bind as when retrieving a keytab without Kerberos credentials.
      - Generally used with the O(bind_pw) option.
    type: str
  bind_pw:
    description:
      - The LDAP password to use when not binding with Kerberos.
    type: str
  password:
    description:
      - Use this password for the key instead of one randomly generated.
    type: str
  ca_cert:
    description:
      - The path to the IPA CA certificate used to validate LDAPS/STARTTLS connections.
    type: path
  sasl_mech:
    description:
      - SASL mechanism to use if O(bind_dn) and O(bind_pw) are not specified.
    choices: ["GSSAPI", "EXTERNAL"]
    type: str
  retrieve_mode:
    description:
      - Retrieve an existing key from the server instead of generating a new one.
      - This is incompatible with the O(password), and will work only against a IPA server more recent than version 3.3.
      - The user requesting the keytab must have access to the keys for this operation to succeed.
      - Be aware that if set V(true), a new keytab will be generated.
      - This invalidates all previously retrieved keytabs for this service principal.
    type: bool
  encryption_types:
    description:
      - The list of encryption types to use to generate keys.
      - It will use local client defaults if not provided.
      - Valid values depend on the Kerberos library version and configuration.
    type: str
  state:
    description:
      - The state of the keytab file.
      - V(present) only check for existence of a file, if you want to recreate keytab with other parameters you should set
        O(force=true).
    type: str
    default: present
    choices: ["present", "absent"]
  force:
    description:
      - Force recreation if exists already.
    type: bool
requirements:
  - freeipa-client
  - Managed host is FreeIPA client
extends_documentation_fragment:
  - community.general.attributes
a  
- name: Get Kerberos ticket using default principal
  community.general.krb_ticket:
    password: "{{ aldpro_admin_password }}"

- name: Create keytab
  community.general.ipa_getkeytab:
    path: /etc/ipa/test.keytab
    principal: HTTP/freeipa-dc02.ipa.test
    ipa_host: freeipa-dc01.ipa.test

- name: Retrieve already existing keytab
  community.general.ipa_getkeytab:
    path: /etc/ipa/test.keytab
    principal: HTTP/freeipa-dc02.ipa.test
    ipa_host: freeipa-dc01.ipa.test
    retrieve_mode: true

- name: Force keytab recreation
  community.general.ipa_getkeytab:
    path: /etc/ipa/test.keytab
    principal: HTTP/freeipa-dc02.ipa.test
    ipa_host: freeipa-dc01.ipa.test
    force: true
N)AnsibleModule)	CmdRunnercmd_runner_fmtc                       e Zd Zd ZddZy)	IPAKeytabc                    || _         |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d   | _        |d	   | _	        |d
   | _
        |d   | _        |d   | _        t        |dt        t        j                   d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d      t        j"                  d                  | _        y )Npathstate	principalipa_hostldap_uribind_dnbind_pwpasswordca_cert	sasl_mechretrieve_modeencryption_typeszipa-getkeytabz
--retrievez--keytabz--serverz--principalz	--ldapuriz--binddnz--bindpwz
--passwordz--cacertz--mechz
--enctypes)r   r   r   r   r   r   r   r   r   r   r   )commandarg_formats)moduler   r   r   r   r   r   r   r   r   r   r   r   r   dictr   as_bool
as_opt_valrunner)selfr   kwargss      s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/ipa_getkeytab.py__init__zIPAKeytab.__init__   sN   6N	G_
,z*z*i(i(z*i(,#O4 &'9 :#,44\B#..z:'22:>(33MB'22;?&11*=&11*='22<@&11*=(33H=!/!:!:<!H
    c                     | j                  d|      5 }|j                         \  }}}d d d        |S # 1 sw Y   S xY w)Nzjretrieve_mode path ipa_host principal ldap_uri bind_dn bind_pw password ca_cert sasl_mech encryption_types)check_rc)r   run)r   r%   ctxrcouterrs         r!   _execzIPAKeytab._exec   sM    [[x  
 	% 779LBS		%
 
	%
 
s   3=N)T)__name__
__module____qualname__r"   r+    r#   r!   r
   r
      s    
Br#   r
   c                     t        t        dddg      t        dddg      t        dd	      t        d
      t        d
      t        d
      t        d
      t        dd      t        d
      t        dddg      t        d
      t        d
      t        d
            } t        | ddgd      }|j                  d   }|j                  d   }|j                  d   }t        ||||j                  d   |j                  d   |j                  d   |j                  d   |j                  d   |j                  d   |j                  d   |j                  d   |j                  d   |j                  d          }d!}|dk(  rxt        j
                  j                  |      rG|r4|j                  s(	 t	        j                  |       |j                          d}|r!|j                  rd}nd}|j                          |dk(  rCt        j
                  j                  |      r$d}|j                  s	 t	        j                  |       |j                  |&       y # t        $ r7}|j                  d"|j                  d#|j                  d$%       Y d }~d }~ww xY w# t        $ r7}|j                  d"|j                  d#|j                  d$%       Y d }~d }~ww xY w)'Nr   Tkeytab)typerequiredaliasespresentabsent)defaultchoicesstr)r2   r3   )r2   )r2   no_logGSSAPIEXTERNAL)r2   r8   bool)r   r   r   r   r   r   r   r   r   r   r   r   force)r   r   )r   r   )argument_specmutually_exclusivesupports_check_moder   r>   r   r   r   r   r   r   r   r   r   r   )r   r   r   r   r   r   r   r   r   r   r   r   FzError deleting: z - .)msg)changed)r   r   paramsr
   osr   exists
check_moderemoveOSError	fail_jsonfilenamestrerrorr+   	exit_json)arg_specr   r   r   r>   r1   rD   es           r!   mainrQ      s   vxjA9y(.CDED15!5!% % 5.&!EHj+AB'5)H 46ST F == DMM'"EMM'"Ev "!'{!; &j 9 &j 9%}}Y7%}}Y7 &j 9%}}Y7!'{!;%+]]?%C(.6H(IF G	77>>$V..`IIdO **GLLN77>>$G$$`IIdO W%'  `$$qzzSTS]S])^$__`   `$$qzzSTS]S])^$__`s0   2I /J 	J -JJ	K#-KK__main__)
__future__r   r   r   r2   __metaclass__DOCUMENTATIONEXAMPLESrF   ansible.module_utils.basicr   Eansible_collections.community.general.plugins.module_utils.cmd_runnerr   r   objectr
   rQ   r,   r/   r#   r!   <module>rZ      sV    A @Wr4 
 4 k( (VB&J zF r#   