
    Vh7                     "   d Z dZdZddl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 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 ddlmZ ddlmZ  G d de      ZddZd dZd Zd Zd Zd Zd Zd Zd Z d!dZ
d Z!d Z"e#dk(  r e"        yy)"aN  
---
module: ec2_key
version_added: 1.0.0
short_description: Create or delete an EC2 key pair
description:
  - Create or delete an EC2 key pair.
options:
  name:
    description:
      - Name of the key pair.
    required: true
    type: str
  key_material:
    description:
      - Public key material.
    required: false
    type: str
  force:
    description:
      - Force overwrite of already existing key pair if key has changed.
    required: false
    default: true
    type: bool
  state:
    description:
      - Create or delete keypair.
    required: false
    choices: [ present, absent ]
    default: 'present'
    type: str
  key_type:
    description:
      - The type of key pair to create.
      - Note that ED25519 keys are not supported for Windows instances,
        EC2 Instance Connect, and EC2 Serial Console.
      - By default Amazon will create an RSA key.
      - Mutually exclusive with parameter O(key_material).
    type: str
    choices:
      - rsa
      - ed25519
    version_added: 3.1.0
  file_name:
    description:
      - Name of the file where the generated private key will be saved.
      - When provided, the RV(key.private_key) attribute will be removed from the return value.
      - The file is written out on the 'host' side rather than the 'controller' side.
      - Ignored when O(state=absent) or O(key_material) is provided.
    type: path
    version_added: 6.4.0
notes:
  - Support for O(tags) and O(purge_tags) was added in release 2.1.0.
  - For security reasons, this module should be used with B(no_log=true) and (register) functionalities
    when creating new key pair without providing O(key_material).
extends_documentation_fragment:
  - amazon.aws.common.modules
  - amazon.aws.region.modules
  - amazon.aws.tags
  - amazon.aws.boto3

author:
  - "Vincent Viallet (@zbal)"
  - "Prasad Katti (@prasadkatti)"
a  
# Note: These examples do not set authentication details, see the AWS Guide for details.

- name: create a new EC2 key pair, returns generated private key
  # use no_log to avoid private key being displayed into output
  amazon.aws.ec2_key:
    name: my_keypair
  no_log: true
  register: aws_ec2_key_pair

- name: create key pair using provided key_material
  amazon.aws.ec2_key:
    name: my_keypair
    key_material: 'ssh-rsa AAAAxyz...== me@example.com'

- name: create key pair using key_material obtained using 'file' lookup plugin
  amazon.aws.ec2_key:
    name: my_keypair
    key_material: "{{ lookup('file', '/path/to/public_key/id_rsa.pub') }}"

- name: Create ED25519 key pair and save private key into a file
  amazon.aws.ec2_key:
    name: my_keypair
    key_type: ed25519
    file_name: /tmp/aws_ssh_rsa

# try creating a key pair with the name of an already existing keypair
# but don't overwrite it even if the key is different (force=false)
- name: try creating a key pair with name of an already existing keypair
  amazon.aws.ec2_key:
    name: my_existing_keypair
    key_material: 'ssh-rsa AAAAxyz...== me@example.com'
    force: false

- name: remove key pair from AWS by name
  amazon.aws.ec2_key:
    name: my_keypair
    state: absent
a  
changed:
  description: Whether a keypair was created/deleted.
  returned: always
  type: bool
  sample: true
msg:
  description: Short message describing the action taken.
  returned: always
  type: str
  sample: key pair created
key:
  description: Details of the keypair (this is set to null when state is absent).
  returned: always
  type: complex
  contains:
    fingerprint:
      description: Fingerprint of the key.
      returned: when O(state=present)
      type: str
      sample: 'b0:22:49:61:d9:44:9d:0c:7e:ac:8a:32:93:21:6c:e8:fb:59:62:43'
    name:
      description: Name of the keypair.
      returned: when O(state=present)
      type: str
      sample: my_keypair
    id:
      description: Id of the keypair.
      returned: when O(state=present)
      type: str
      sample: key-123456789abc
    tags:
      description: A dictionary representing the tags attached to the key pair.
      returned: when O(state=present)
      type: dict
      sample: '{"my_key": "my value"}'
    private_key:
      description: Private key of a newly created keypair.
      returned: when a new keypair is created by AWS (O(key_material) is not provided) and O(file_name) is not provided.
      type: str
      sample: '-----BEGIN RSA PRIVATE KEY-----
        MIIEowIBAAKC...
        -----END RSA PRIVATE KEY-----'
    type:
      description: Type of a newly created keypair.
      returned: when a new keypair is created by AWS
      type: str
      sample: rsa
      version_added: 3.1.0
    N)to_bytes)AnsibleEC2Error)create_key_pair)delete_key_pair)describe_key_pairs)ensure_ec2_tags)import_key_pair)AnsibleAWSModule)boto3_tag_list_to_ansible_dict)boto3_tag_specifications)scrub_none_parametersc                         e Zd Zd fd	Z xZS )Ec2KeyFailurec                 @    t         |   |       || _        || _        y N)super__init__
original_emessage)selfr   r   	__class__s      f/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/amazon/aws/plugins/modules/ec2_key.pyr   zEc2KeyFailure.__init__   s    !$    NN)__name__
__module____qualname__r   __classcell__)r   s   @r   r   r      s     r   r   c                     |t        |      |d}t        |      }	 t        | fi |}|S # t        $ r}t	        |d      d }~ww xY w)N)KeyNamePublicKeyMaterialTagSpecificationszerror importing key)r   r   r	   r   r   )
ec2_clientnamekey_materialtag_specparamskeyes          r   _import_key_pairr*      sY    H\4JaijF"6*F6j3F3 J  6A4556s   * 	A?Ac                     | d   | d   | d   t        | j                  d      xs g       | j                  d      | j                  d      xs |d}|r|d   t        ||      }t        |      S )	Nr    KeyFingerprint	KeyPairIdTagsKeyMaterialKeyType)r$   fingerprintidtagsprivate_keytyper4   )r   get_write_private_keyr   )r(   key_type	file_namedatas       r   extract_key_datar;      sy    I+,+.swwv/D"Eww}-	".h	D T-(4!$	2 &&r   c                     d}|r/dt        t        j                               z   }t        ||      }|r/t	        ||      }t        | ||d       |d   S )z
    EC2's fingerprints are non-trivial to generate, so push this key
    to a temporary name and make ec2 calculate the fingerprint for us.
    http://blog.jbrowne.com/?p=23
    https://forums.aws.amazon.com/thread.jspa?messageID=352828
    Tzansible-Ffinish_taskr,   )struuiduuid4find_key_pairr*   r   )
check_moder#   r%   name_in_userandom_nametemp_keys         r   get_key_fingerprintrG      s\     K
 3tzz|#44#J<   
KFHJ
KUK$%%r   c                     	 t        | |g      }|sy 	 |d   S # t        $ r}t        |d      d }~wt        $ r	 d }Y |d   S w xY w)N)KeyNameszerror finding keypairr   )r   r   r   
IndexError)r#   r$   r(   r)   s       r   rB   rB      sa     tf=  q6M  8A677 q6Ms    	A.AAc                 |    |||d}t        |      }	 t        | fi |}|S # t        $ r}t        |d      d }~ww xY w)N)r    r"   r0   zerror creating key)r   r   r   r   )r#   r$   r&   r8   r'   r(   r)   s          r   _create_key_pairrL      sY    %F #6*F5j3F3 J  5A3445s   ! 	;6;c                 \   	 t        j                  |t         j                  t         j                  z  t         j                  z  d      }t        j
                  || d   j                  d             t        j                  |       | d= | S # t        t        f$ r}t        |d      d}~ww xY w)z
    Write the private key data to the specified file, and remove 'private_key'
    from the ouput. This ensures we don't expose the key data in logs or task output.
    i  r4   zutf-8zKCould not save private key to specified path. Private key is irretrievable.N)osopenO_WRONLYO_CREATO_TRUNCwriteencodecloseIOErrorOSErrorr   )key_datar9   filer)   s       r   r7   r7     s    
nwwy"++

":RZZ"GO
x.55g>?
 	O	 W nAlmmns   BB B+B&&B+c                     |rddddS t        |dg      }|rt        | |||      }nt        | |||      }t        |||      }	d|	dd}
|
S )z/
    key does not exist, we create new key
    TNzkey pair createdchangedr(   msgkey-pair)r   r*   rL   r;   )r#   r$   r%   r8   r3   r9   rC   r&   r(   rX   results              r   create_new_key_pairr`     sf     5GHH'zl;Hz4xHz48DXy9Hh7IJFMr   c                     | rdd ddS t        | ||      }d}d}|d   |k7  r!t        | ||d       t        ||||      }d}d}t        |      }	||	|dS )NTkey pair updatedr[   Fkey pair already existsr,   r=   )rG   r   r*   r;   )
rC   r#   r$   r(   r%   r&   new_fingerprintr\   r]   rX   s
             r   update_key_pair_by_key_materialre   *  sx    5GHH)*j,OOG
#C
/
J%Hz4xH $Hx<<r   c                 r    | rdd ddS t        | ||d       t        ||||      }t        |||      }d|ddS )NTrb   r[   Fr=   )r   rL   r;   )rC   r#   r$   r8   r&   r9   r(   rX   s           r   update_key_pair_by_key_typerg   9  sM    5GHH
J%Hz48D#C9=9KLLr   c                     t        ||      }|r
| rdd dd}|S |sd dd}|S 	 t        ||       |sy dd dd}|S # t        $ r}t        |d      d }~ww xY w)NTzkey deletedr[   zkey did not exist)r(   r]   zerror deleting keypair)rB   delete_ec2_key_pairr   r   )rC   r#   r$   r>   r(   r_   r)   s          r   r   r   C  s    

D
)C
z!$}E M &9:	=
D1 !$}EM  	=#;<<	=s   : 	AAAc                 N   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }t        |dg      }	| j                  }
| j                   j                  d      }|r|rt	        |
|||||	      }|S |r||d   k7  rt        |
||||	|      }|S d	}|t        || |d
   ||      z  }t        ||      }t        ||      }||dd}|S )Nr%   forcer8   r3   
purge_tagsr^   r9   r0   Fr-   )r3   rl   )r9   rc   r[   )	r'   r6   r   rC   re   rg   r   rB   r;   )moduler#   r$   r(   r%   rk   r8   r3   rl   r&   rC   r9   r_   r\   rX   s                  r   handle_existing_key_pair_updatern   W  s0   ==$$^4LMMg&E}}  ,H==V$D""<0J'zl;H""J!!+.I0ZsT`bjk M 
h#i.0,ZT8U]_hi M ?:vs;7Gd_ijjJ-#C9=$X>WXMr   c                     t        t        d      t        d      t        dd      t        dddg	      t        d
dg      t        dd      t        dddg      t        dd            } t        | ddggd      }|j                  d      }|j                  d   }|j                  j	                  d      }|j                  j	                  d      }|j                  j	                  d      }|j                  j	                  d      }|j                  j	                  d      }i }		 |dk(  rt        |j                  ||      }	n=|dk(  r8t        ||      }
|
rt        ||||
      }	nt        |||||||j                        }	 |j                  di |	 y # t        $ rX}|j                  r'|j                  |j                  |j                         n|j                  |j                         Y d }~od }~ww xY w)NT)requiredF)no_logbool)r5   defaultpresentabsent)rs   choicesdictresource_tags)r5   aliasesr?   rsaed25519)r5   rv   path)r5   rp   )r$   r%   rk   stater3   rl   r8   r9   r%   r8   )argument_specmutually_exclusivesupports_check_modeec2r$   r}   r3   r9    )rw   r
   clientr'   r6   r   rC   rB   rn   r`   r   r   fail_json_awsr   	fail_json	exit_json)r~   rm   r#   r$   r}   r%   r8   r3   r9   r_   r(   r)   s               r   mainr   m  s   4 '-9y(.CDv'89VT255)*<=FU3	M #+Z89 F u%J== DMMg&E==$$^4L}}  ,H==V$D!!+.IF(H$V%6%6
DIFi
D1C8TSVW,lHdIvO`O` Fv  (<<  qyy9QYY'	(s   *AF 	G8 AG33G8__main__r   r   )T)$DOCUMENTATIONEXAMPLESRETURNrN   r@   ansible.module_utils._textr   7ansible_collections.amazon.aws.plugins.module_utils.ec2r   r   r   ri   r   r   r	   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr
   ;ansible_collections.amazon.aws.plugins.module_utils.taggingr   r   Bansible_collections.amazon.aws.plugins.module_utils.transformationr   	Exceptionr   r*   r;   rG   rB   rL   r7   r`   re   rg   rn   r   r   r   r   r   <module>r      s   @D&P1
f 
  / S S j V S S X f ` dI 	'$&"
  $=M(,1h zF r   