Anons79 Mini Shell

Directory : /lib/python2.7/site-packages/ansible/modules/crypto/
Upload File :
Current File : //lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyo

�
�Udac@`s$ddlmZmZmZeZidd6dgd6dd6ZdZd	Zd
Z	ddl
Z
ddlZddlZddl
mZdZd
ZdZejd�Zejd�Zdefd��YZdefd��YZdefd��YZd�Zd�Zedkr e�ndS(i(tabsolute_importtdivisiontprint_functions1.1tmetadata_versiontpreviewtstatust	communitytsupported_bys
---
module: luks_device

short_description: Manage encrypted (LUKS) devices

version_added: "2.8"

description:
    - "Module manages L(LUKS,https://en.wikipedia.org/wiki/Linux_Unified_Key_Setup)
      on given device. Supports creating, destroying, opening and closing of
      LUKS container and adding or removing new keys."

options:
    device:
        description:
            - "Device to work with (e.g. C(/dev/sda1)). Needed in most cases.
              Can be omitted only when I(state=closed) together with I(name)
              is provided."
        type: str
    state:
        description:
            - "Desired state of the LUKS container. Based on its value creates,
              destroys, opens or closes the LUKS container on a given device."
            - "I(present) will create LUKS container unless already present.
              Requires I(device) and I(keyfile) options to be provided."
            - "I(absent) will remove existing LUKS container if it exists.
              Requires I(device) or I(name) to be specified."
            - "I(opened) will unlock the LUKS container. If it does not exist
              it will be created first.
              Requires I(device) and I(keyfile) to be specified. Use
              the I(name) option to set the name of the opened container.
              Otherwise the name will be generated automatically and returned
              as a part of the result."
            - "I(closed) will lock the LUKS container. However if the container
              does not exist it will be created.
              Requires I(device) and I(keyfile) options to be provided. If
              container does already exist I(device) or I(name) will suffice."
        type: str
        default: present
        choices: [present, absent, opened, closed]
    name:
        description:
            - "Sets container name when I(state=opened). Can be used
              instead of I(device) when closing the existing container
              (i.e. when I(state=closed))."
        type: str
    keyfile:
        description:
            - "Used to unlock the container and needed for most
              of the operations. Parameter value is the path
              to the keyfile with the passphrase."
            - "BEWARE that working with keyfiles in plaintext is dangerous.
              Make sure that they are protected."
        type: path
    new_keyfile:
        description:
            - "Adds additional key to given container on I(device).
              Needs I(keyfile) option for authorization. LUKS container
              supports up to 8 keys. Parameter value is the path
              to the keyfile with the passphrase."
            - "NOTE that adding additional keys is I(not idempotent).
              A new keyslot will be used even if another keyslot already
              exists for this keyfile."
            - "BEWARE that working with keyfiles in plaintext is dangerous.
              Make sure that they are protected."
        type: path
    remove_keyfile:
        description:
            - "Removes given key from the container on I(device). Does not
              remove the keyfile from filesystem.
              Parameter value is the path to the keyfile with the passphrase."
            - "NOTE that removing keys is I(not idempotent). Trying to remove
              a key which no longer exists results in an error."
            - "NOTE that to remove the last key from a LUKS container, the
              I(force_remove_last_key) option must be set to C(yes)."
            - "BEWARE that working with keyfiles in plaintext is dangerous.
              Make sure that they are protected."
        type: path
    force_remove_last_key:
        description:
            - "If set to C(yes), allows removing the last key from a container."
            - "BEWARE that when the last key has been removed from a container,
              the container can no longer be opened!"
        type: bool
        default: no

requirements:
    - "cryptsetup"
    - "wipefs"
    - "lsblk"

author:
    "Jan Pokorny (@japokorn)"
s�

- name: create LUKS container (remains unchanged if it already exists)
  luks_device:
    device: "/dev/loop0"
    state: "present"
    keyfile: "/vault/keyfile"

- name: (create and) open the LUKS container; name it "mycrypt"
  luks_device:
    device: "/dev/loop0"
    state: "opened"
    name: "mycrypt"
    keyfile: "/vault/keyfile"

- name: close the existing LUKS container "mycrypt"
  luks_device:
    state: "closed"
    name: "mycrypt"

- name: make sure LUKS container exists and is closed
  luks_device:
    device: "/dev/loop0"
    state: "closed"
    keyfile: "/vault/keyfile"

- name: create container if it does not exist and add new key to it
  luks_device:
    device: "/dev/loop0"
    state: "present"
    keyfile: "/vault/keyfile"
    new_keyfile: "/vault/keyfile2"

- name: add new key to the LUKS container (container has to exist)
  luks_device:
    device: "/dev/loop0"
    keyfile: "/vault/keyfile"
    new_keyfile: "/vault/keyfile2"

- name: remove existing key from the LUKS container
  luks_device:
    device: "/dev/loop0"
    remove_keyfile: "/vault/keyfile2"

- name: completely remove the LUKS container and its contents
  luks_device:
    device: "/dev/loop0"
    state: "absent"
s�
name:
    description:
        When I(state=opened) returns (generated or given) name
        of LUKS container. Returns None if no name is supplied.
    returned: success
    type: str
    sample: "luks-c1da9a58-2fde-4256-9d9f-6ab008b4dd1b"
N(t
AnsibleModuleiis\s*crypt\s+([^\s]*)\s*s\s*device:\s+([^\s]*)\s*tHandlercB`s#eZd�Zd�Zd�ZRS(cC`s%||_|jjdt�|_dS(Ntlsblk(t_moduletget_bin_pathtTruet
_lsblk_bin(tselftmodule((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt__init__�s	cC`s|jj|�S(N(Rtrun_command(Rtcommand((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt_run_command�scC`sf|j|jd|ddg�}|tdkrNtd||tf��n|tj�}d|S(s� Generate name for luks based on device UUID ('luks-<UUID>').
            Raises ValueError when obtaining of UUID fails.
        s-ns-otUUIDis+Error while generating LUKS name for %s: %ssluks-%s(RRtRETURN_CODEt
ValueErrortSTDERRtSTDOUTtstrip(Rtdevicetresulttdev_uuid((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytgenerate_luks_name�s!(t__name__t
__module__RRR(((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyR	�s		tCryptHandlercB`seeZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z	d�Z
ed	�ZRS(
cC`s2tt|�j|�|jjdt�|_dS(Nt
cryptsetup(tsuperR!RRRR
t_cryptsetup_bin(RR((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyR�scC`s�|j|j|ddg�}|tdkrKtd||tf��ntj|t�}y|jd�}Wnt	k
r�d}nX|S(s� obtain LUKS container name based on the device where it is located
            return None if not found
            raise ValueError if lsblk command fails
        s-nlos	type,nameis*Error while obtaining LUKS name for %s: %siN(RRRRRtLUKS_NAME_REGEXtsearchRtgrouptAttributeErrortNone(RRRtmtname((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytget_container_name_by_device�s

cC`sU|j|jd|g�}|tdkr/dStj|t�}|jd�}|S(s� obtain device name based on the LUKS container name
            return None if not found
            raise ValueError if lsblk command fails
        RiiN(RR$RR)tLUKS_DEVICE_REGEXR&RR'(RR+RR*R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytget_container_device_by_name�scC`s)|j|jd|g�}|tdkS(s0 check if the LUKS container does exist
        tisLuksi(RR$R(RRR((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytis_luks�scC`sR|j|jdd||g�}|tdkrNtd||tf��ndS(Nt
luksFormats-qis#Error while creating LUKS on %s: %s(RR$RRR(RRtkeyfileR((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytrun_luks_create�s
c	C`s[|j|jd|ddd||g�}|tdkrWtd||tf��ndS(Ns
--key-filetopens--typetluksis,Error while opening LUKS container on %s: %s(RR$RRR(RRR2R+R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt
run_luks_opens
cC`sB|j|jd|g�}|tdkr>td|��ndS(Ntcloseis%Error while closing LUKS container %s(RR$RR(RR+R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytrun_luks_closescC`s�|jjdt�}|j|�}|dk	r@|j|�n|j|d|g�}|tdkr�td||t	f��ndS(Ntwipefss--allis(Error while wiping luks container %s: %s(
RRR
R,R)R8RRRR(RRt
wipefs_binR+R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytrun_luks_removescC`sU|j|jd||d|g�}|tdkrQtd||tf��ndS(s} Add new key to given 'device'; authentication done using 'keyfile'
            Raises ValueError when command fails
        t
luksAddKeys
--key-fileis)Error while adding new LUKS key to %s: %sN(RR$RRR(RRR2tnew_keyfileR((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytrun_luks_add_keys
c	C`s||s'|j|jd|g�}|tdkrGtd|f��nd}t}tjd�}x�|tj�D]�}|j	d�r�t
}qs|j	d�r�|r�|ddkr�|d	7}q�qs|j	d
�r�qs|j|�r�|d	7}qst}qsW|dkr'|jj
dd|�q'n|j|jd
|dd|g�}|tdkrxtd||tf��ndS(sW Remove key from given device
            Raises ValueError when command fails
        tluksDumpis'Error while dumping LUKS header from %ss^Key Slot [0-9]+: ENABLEDs	Keyslots:s  it
0123456789is	tmsgszLUKS device %s has less than two active keyslots. To be able to remove a key, please set `force_remove_last_key` to `yes`.t
luksRemoveKeys-qs
--key-files)Error while removing LUKS key from %s: %sN(RR$RRtFalsetretcompileRt
splitlinest
startswithR
tmatchRt	fail_jsonR(	RRR2tforce_remove_last_keyRt
keyslot_counttkeyslot_areat
keyslot_retline((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytrun_luks_remove_key%s6	

(
RR RR,R.R0R3R6R8R;R>RCRO(((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyR!�s									
tConditionsHandlercB`sPeZd�Zd�Zd�Zd�Zd�Zd�Zd�Zd�Z	RS(cC`s#tt|�j|�||_dS(N(R#RPRt
_crypthandler(RRtcrypthandler((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyRPscC`s]|jjddk	o\|jjddk	o\|jjddko\|jj|jjd�S(NRR2tstatetpresenttopenedtclosed(RTRUsclosed(RtparamsR)RQR0(R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytluks_createTs
	cC`s�|jjddkrdS|jj|jjd�}|dkrFdS|jjddkr`|S||jjdkr�|jjdd|�n|S(s� If luks is already opened, return its name.
            If 'name' parameter is specified and differs
            from obtained value, fail.
            Return None otherwise
        RSRURR+RAs;LUKS container is already opened under different name '%s'.N(RRWR)RQR,RI(RR+((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytopened_luks_name\s	cC`sf|jjddksB|jjddksB|jjddkrFtS|j�}|dkrbtStS(NRR2RSRU(RRWR)RCRYR
(RR+((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt	luks_openzscC`s�|jjddkr,|jjddksB|jjddkrFtS|jjddk	r�|jj|jjd�}|dk	}n|jjddk	r�|jj|jjd�}|dk	}n|S(NR+RRSRV(RRWR)RCRQR,R.(RR+tluks_is_openR((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt
luks_close�s		cC`sv|jjddksB|jjddksB|jjddkrFtS|jjddkrr|jjdd�ntS(NRR2R=RStabsentRAs;Contradiction in setup: Asking to add a key to absent LUKS.(RRWR)RCRIR
(R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytluks_add_key�scC`s`|jjddks,|jjddkr0tS|jjddkr\|jjdd�ntS(NRtremove_keyfileRSR]RAs@Contradiction in setup: Asking to remove a key from absent LUKS.(RRWR)RCRIR
(R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytluks_remove_key�scC`sF|jjddk	oE|jjddkoE|jj|jjd�S(NRRSR](RRWR)RQR0(R((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytluks_remove�s(
RR RRXRYRZR\R^R`Ra(((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyRPNs				
		
	c	C`s�tdtddddddddd	g�d
tdd�dtdd�dtdd
�dtdd
�dtdd
�dtdddt��}tdtdd�}td|dt�}|jd
dk	r^ybtj|jd
�}|j}tj	|�r.tj
|�r.tdj|jd
���nWq^tk
rZ}|j
dt|��q^Xnt|�}t||�}|j�r|js�y"|j|jd
|jd�Wq�tk
r�}|j
dd|�q�Xnt|d<|jr|j|�qn|j�}|dk	r(||d<n|j�r|jd}|dkr�y|j|jd
�}Wq�tk
r�}|j
dd|�q�Xn|js�y%|j|jd
|jd|�Wq�tk
r�}|j
dd|�q�Xn||d<t|d<|jr|j|�qn|j�r�|jd
dk	r�y|j|jd
�}Wq�tk
r�}|j
dd|�q�Xn
|jd}|js�y|j|�Wq�tk
r�}|j
dd|�q�Xnt|d<|jr�|j|�q�n|j�r�|jsmy,|j|jd
|jd|jd�Wqmtk
ri}|j
dd|�qmXnt|d<|jr�|j|�q�n|j�r*|jsy/|j |jd
|jdd|jd�Wqtk
r}|j
dd|�qXnt|d<|jr*|j|�q*n|j!�r�|js�y|j"|jd
�Wq�tk
r�}|j
dd|�q�Xnt|d<|jr�|j|�q�n|j|�dS(NRSttypetstrtdefaultRTtchoicesR]RURVRR+R2tpathR=R_RJtbooltchangedt
argument_spectsupports_check_modes{0} is not a deviceRAsluks_device error: %s(#tdictRCR)RR
RWtoststattst_modetS_ISBLKtS_ISCHRt	ExceptiontformatRIRcR!RPRXt
check_modeR3Rt	exit_jsonRYRZRR6R\R,R8R^R>R`RORaR;(	tmodule_argsRRtstatinfotmodetetcryptt
conditionsR+((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt
run_module�s�'			 #	
	

	


	
	
		

		

		
	cC`st�dS(N(R{(((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pytmain@st__main__(t
__future__RRRRbt
__metaclass__tANSIBLE_METADATAt
DOCUMENTATIONtEXAMPLEStRETURNRlRDRmtansible.module_utils.basicRRRRRER%R-tobjectR	R!RPR{R|R(((sF/usr/lib/python2.7/site-packages/ansible/modules/crypto/luks_device.pyt<module>s.


`2
~m	�	

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]