
    Vh=                    x   d dl m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	Z	d dl
mZ d dl
mZ d dlmZ d d	lmZ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ZdZ	 d dlmZ dZ"d Z#d Z$d 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Z e	jB                         ZY Hw xY w)     )annotationsa  
author: 'Ansible Core Team (@ansible)'
short_description: 'Add and remove deb822 formatted repositories'
description:
- 'Add and remove deb822 formatted repositories in Debian based distributions.'
module: deb822_repository
notes:
- This module will not automatically update caches, call the M(ansible.builtin.apt) module based
  on the changed state.
options:
    allow_downgrade_to_insecure:
        description:
        - Allow downgrading a package that was previously authenticated but
          is no longer authenticated.
        type: bool
    allow_insecure:
        description:
        - Allow insecure repositories.
        type: bool
    allow_weak:
        description:
        - Allow repositories signed with a key using a weak digest algorithm.
        type: bool
    architectures:
        description:
        - Architectures to search within repository.
        type: list
        elements: str
    by_hash:
        description:
        - Controls if APT should try to acquire indexes via a URI constructed
          from a hashsum of the expected file instead of using the well-known
          stable filename of the index.
        type: bool
    check_date:
        description:
        - Controls if APT should consider the machine's time correct and hence
          perform time related checks, such as verifying that a Release file
          is not from the future.
        type: bool
    check_valid_until:
        description:
        - Controls if APT should try to detect replay attacks.
        type: bool
    components:
        description:
        - Components specify different sections of one distribution version
          present in a C(Suite).
        type: list
        elements: str
    date_max_future:
        description:
        - Controls how far from the future a repository may be.
        type: int
    enabled:
        description:
        - Tells APT whether the source is enabled or not.
        type: bool
    inrelease_path:
        description:
        - Determines the path to the C(InRelease) file, relative to the normal
          position of an C(InRelease) file.
        type: str
    languages:
        description:
        - Defines which languages information such as translated
          package descriptions should be downloaded.
        type: list
        elements: str
    name:
        description:
        - Name of the repo. Specifically used for C(X-Repolib-Name) and in
          naming the repository and signing key files.
        required: true
        type: str
    pdiffs:
        description:
        - Controls if APT should try to use C(PDiffs) to update old indexes
          instead of downloading the new indexes entirely.
        type: bool
    signed_by:
        description:
        - Either a URL to a GPG key, absolute path to a keyring file, one or
          more fingerprints of keys either in the C(trusted.gpg) keyring or in
          the keyrings in the C(trusted.gpg.d/) directory, or an ASCII armored
          GPG public key block.
        type: str
    suites:
        description:
        - >-
          Suite can specify an exact path in relation to the URI(s) provided,
          in which case the Components: must be omitted and suite must end
          with a slash (C(/)). Alternatively, it may take the form of a
          distribution version (for example a version codename like C(disco) or C(artful)).
          If the suite does not specify a path, at least one component must
          be present.
        type: list
        elements: str
    targets:
        description:
        - Defines which download targets apt will try to acquire from this source.
        type: list
        elements: str
    trusted:
        description:
        - Decides if a source is considered trusted or if warnings should be
          raised before, for example packages are installed from this source.
        type: bool
    types:
        choices:
        - deb
        - deb-src
        default:
        - deb
        type: list
        elements: str
        description:
        - Which types of packages to look for from a given source; either
          binary V(deb) or source code V(deb-src).
    uris:
        description:
        - The URIs must specify the base of the Debian distribution archive,
          from which APT finds the information it needs.
        type: list
        elements: str
    mode:
        description:
        - The octal mode for newly created files in C(sources.list.d).
        type: raw
        default: '0644'
    state:
        description:
        - A source string state.
        type: str
        choices:
        - absent
        - present
        default: present
requirements:
    - python3-debian / python-debian
version_added: '2.15'
ag  
- name: Add debian repo
  deb822_repository:
    name: debian
    types: deb
    uris: http://deb.debian.org/debian
    suites: stretch
    components:
      - main
      - contrib
      - non-free

- name: Add debian repo with key
  deb822_repository:
    name: debian
    types: deb
    uris: https://deb.debian.org
    suites: stable
    components:
      - main
      - contrib
      - non-free
    signed_by: |-
      -----BEGIN PGP PUBLIC KEY BLOCK-----

      mDMEYCQjIxYJKwYBBAHaRw8BAQdAD/P5Nvvnvk66SxBBHDbhRml9ORg1WV5CvzKY
      CuMfoIS0BmFiY2RlZoiQBBMWCgA4FiEErCIG1VhKWMWo2yfAREZd5NfO31cFAmAk
      IyMCGyMFCwkIBwMFFQoJCAsFFgIDAQACHgECF4AACgkQREZd5NfO31fbOwD6ArzS
      dM0Dkd5h2Ujy1b6KcAaVW9FOa5UNfJ9FFBtjLQEBAJ7UyWD3dZzhvlaAwunsk7DG
      3bHcln8DMpIJVXht78sL
      =IE0r
      -----END PGP PUBLIC KEY BLOCK-----

- name: Add repo using key from URL
  deb822_repository:
    name: example
    types: deb
    uris: https://download.example.com/linux/ubuntu
    suites: '{{ ansible_distribution_release }}'
    components: stable
    architectures: amd64
    signed_by: https://download.example.com/linux/ubuntu/gpg
a  
repo:
  description: A source string for the repository
  returned: always
  type: str
  sample: |
    X-Repolib-Name: debian
    Types: deb
    URIs: https://deb.debian.org
    Suites: stable
    Components: main contrib non-free
    Signed-By:
        -----BEGIN PGP PUBLIC KEY BLOCK-----
        .
        mDMEYCQjIxYJKwYBBAHaRw8BAQdAD/P5Nvvnvk66SxBBHDbhRml9ORg1WV5CvzKY
        CuMfoIS0BmFiY2RlZoiQBBMWCgA4FiEErCIG1VhKWMWo2yfAREZd5NfO31cFAmAk
        IyMCGyMFCwkIBwMFFQoJCAsFFgIDAQACHgECF4AACgkQREZd5NfO31fbOwD6ArzS
        dM0Dkd5h2Ujy1b6KcAaVW9FOa5UNfJ9FFBtjLQEBAJ7UyWD3dZzhvlaAwunsk7DG
        3bHcln8DMpIJVXht78sL
        =IE0r
        -----END PGP PUBLIC KEY BLOCK-----

dest:
  description: Path to the repository file
  returned: always
  type: str
  sample: /etc/apt/sources.list.d/focal-archive.sources

key_filename:
  description: Path to the signed_by key file
  returned: always
  type: str
  sample: /etc/apt/keyrings/debian.gpg
N)AnsibleModule)missing_required_lib)is_sequence)S_IRWXU_RXG_RXOS_IRWU_RG_RO)to_bytes)	to_native)
raise_from)generic_urlparse)open_url)get_user_agent)urlparseT)Deb822Fz/etc/apt/keyringsc           
         d}t         j                  j                  t              s/| j                  st        j
                  t        t               |dz  }|| j                  t        g ddddd d|      z  }|S )NFT)NNNroot0755)path	secontextownergroupmode
attributes)osr   isdirKEYRINGS_DIR
check_modemkdirr   set_fs_attributes_if_different)modulechangeds     Q/home/dcms/DCMS/lib/python3.12/site-packages/ansible/modules/deb822_repository.pyensure_keyrings_dirr#     sq    G77==&  HH\?34v44 +	
 	
 
G N    c                T    t         j                  j                  t        | d|      S )N.)r   r   joinr   )slugexts     r"   make_signed_by_filenamer*     s    77<<s&;<<r$   c                H    t         j                  j                  dd| z        S )Nz/etc/apt/sources.list.dz
%s.sources)r   r   r'   )r(   s    r"   make_sources_filenamer,     s"    77<<!t r$   c                    | rdS dS )Nyesno vs    r"   format_boolr3   #  s    54r$   c                $    dj                  |       S )N )r'   r1   s    r"   format_listr6   '  s    88A;r$   c                    dt        j                  dj                  d | j                         j	                         D              d      z   S )N
c              3  D   K   | ]  }|j                         xs d   yw)r&   N)strip).0lines     r"   	<genexpr>z#format_multiline.<locals>.<genexpr>-  s     I$$**,%#%Is    z    )textwrapindentr'   r:   
splitlinesr1   s    r"   format_multilinerA   +  s>    (//		I!'')2F2F2HII  r$   c                Z    | dk(  ry| dk(  ry| j                  dd      j                         S )NnamezX-Repolib-NameurisURIs_-)replacetitler1   s    r"   format_field_namerJ   2  s0    F{	
f99S#$$&&r$   c                
    d| v S )Ns$   -----BEGIN PGP PUBLIC KEY BLOCK-----r0   )b_datas    r"   
is_armoredrM   :  s    2f<<r$   c                D   d}t         j                  j                  |      r||d fS d }t        t	        |            }|j
                  r'	 t        |t                     }|j                         }n|d |fS |s||d fS t        j                  | j                        \  }}	t        j                   |d      5 }
|
j#                  |       d d d        t%        |      rdnd}t'        ||      }| j)                  |	      }| j)                  |      }||k7  r1|t+        |       z  }| j,                  s| j/                  |	|       |dz  }|| j1                  |t2        d      z  }||d fS # t        $ r)}t        t        t        |            |       Y d }~d }~ww xY w# 1 sw Y   xY w)NF)
http_agentdirwbascgpgT)r   r   isfiler   r   schemer   r   read	Exceptionr   RuntimeErrorr
   tempfilemkstemptmpdirfdopenwriterM   r*   sha256r#   r   atomic_moveset_mode_if_differentr   )r    r2   r(   r!   rL   partsrexctmpfdtmpfilefr)   filename
src_chksumdest_chksums                  r"   write_signed_by_keyrk   >  s   G	ww~~a4FXa[)E||	~'78A VVXF a4%%&--8NE7	5$	 1	 f%%5C&tS1Hw'J--)K[ &v..  w14v++HlEJJGHd"";  	:|IcN3S99	: s$   
E! 5F!	F*FFFc                    t        i dddidddidddidddd	d
ddidddidddidddd	dddidddidddidddd	dddddddidddidddd	dddd	ddiddgdddgdddd	ddddd d!gd d"d#d$      } t        s | j                  t        d%      t        &       | j
                  }d'}| j                  j                         }|j                  d(      }|j                  d)      }|d   }t        j                  d*d+t        j                  d,d-|j                                     }t        |      }|d!k(  rt        j                  j                  |      r|st        j                   |       |dz  }d.D ]G  }	t#        ||	      }
t        j                  j                  |
      s/|st        j                   |
       d}I | j%                  d ||
/       t'               }d }
t)        |j+                               D ]  \  }}|	t-        |t.              rt1        |      }nUt-        |t2              rt5        |      }n9t7        |      rt9        |      }n"|dk(  r	 t;        | ||      \  }}
}|
xs |}||z  }|j?                  d2      d3kD  rtA        |      }||tC        |      <    |jE                         }tG        jH                  | jJ                  4      \  }}t        jL                  |d5      5 }|jO                  tQ        |             d d d        t        |      }| jS                  |      }| jS                  |      }||k7  r|s| jU                  ||       |dz  }|| jW                  ||d'      z  }| j%                  ||||
/       y # t<        $ r)}| j                  d0t5        |      z  1       Y d }~?d }~ww xY w# 1 sw Y   xY w)6Nallow_downgrade_to_insecuretypeboolallow_insecure
allow_weakarchitecturesstrlist)elementsrn   by_hash
check_datecheck_valid_until
componentsdate_max_futureintenabledinrelease_path	languagesrC   T)rn   requiredpdiffs	signed_bysuitestargetsdebzdeb-src)choicesru   rn   defaultraw0644)rn   r   presentabsent)rn   r   r   )trustedtypesrD   r   state)argument_specsupports_check_modezpython3-debian)msg	exceptionFr   r   z[^a-z0-9-]+ z[_\s]+rG   )rS   rT   )repor!   destkey_filenamez!Could not fetch signed_by key: %s)r   r8   r   rP   rR   ),r   
HAS_DEBIAN	fail_jsonr   DEBIAN_IMP_ERRr   paramscopypopresublowerr,   r   r   existsunlinkr*   	exit_jsonr   sorteditems
isinstancero   r3   r{   r
   r   r6   rk   rY   countrA   rJ   dumprZ   r[   r\   r]   r^   r	   r_   r`   ra   )r    r   r!   r   r   r   rC   r(   sources_filenamer)   signed_by_filenamedeb822keyvaluekey_changedsigned_by_datard   r   re   rf   rg   ri   rj   s                          r"   mainr   i  s,   Y
),Y
 	Y
 Y
 !Y
 Y
" #Y
(  ")Y
. !/Y
6  7Y
< =Y
B CY
H !IY
P  QY
X YY
^ _Y
d !eY
l !mY
v 
  "
 " !
  %cY
t !w\F| 12BC#1 	 	3 ""JG ]]!F ::fDJJwE&>D66

JJL	
D -T277>>*+		*+tOG! 	C!8s!Cww~~01!II01	 	!+	 	 	
 XFV\\^, /
U=eT"&Es#e$E&EKBUV\^ceiBj?/*<n;& ;;tq $U+E). %&//2 ;;=D%%&--8NE7	5$	  1	  -T2w'J-- 01K[ w(894v++,<dEJJG
'	  7     ;inL !     s$   %N9O	OO  OO__main__).
__future__r   DOCUMENTATIONEXAMPLESRETURNr   r   rZ   r>   	tracebackansible.module_utils.basicr   r   'ansible.module_utils.common.collectionsr    ansible.module_utils.common.filer   r   +ansible.module_utils.common.text.convertersr	   r
   ansible.module_utils.sixr   ansible.module_utils.urlsr   r   r   r   r   r   debian.deb822r   ImportError
format_excr   r#   r*   r,   r3   r6   rA   rJ   rM   rk   r   __name__r0   r$   r"   <module>r      s   
 #M^*X!
F 
 	    4 ; ? J @ A / 6 . 4 .
,$
 #,= '=(#V}@ zF ]	  ,J)Y))+N,s   *B B98B9