HOME


Mini Shell 1.0
Redirecting to https://devs.lapieza.net/iniciar-sesion Redirecting to https://devs.lapieza.net/iniciar-sesion.
DIR: /lib/python3/dist-packages/cloudinit/config/__pycache__/
Upload File :
Current File : //lib/python3/dist-packages/cloudinit/config/__pycache__/cc_wireguard.cpython-311.pyc
�

܍~c!%���UdZddlZddlmZddlmZddlmZddlmZm	Z	ddl
mZddlm
Z
dd	lmZmZdd
lmZed��Zddd
edgedged��gd�Zeed<ee��Zeje��Zegd���ZdZdZdZdefd�Zdefd�Z dedefd�Z!de"fd�Z#de"fd�Z$defd�Z%d �Z&d!e'd"e
ded#ed$e"d%dfd&�Z(dS)'�	Wireguard�N)�Logger)�dedent)�log)�subp�util)�Cloud)�Config)�
MetaSchema�get_meta_doc)�PER_INSTANCEaIWireguard module provides a dynamic interface for configuring
Wireguard (as a peer or server) in an easy way.

This module takes care of:
  - writing interface configuration files
  - enabling and starting interfaces
  - installing wireguard-tools package
  - loading wireguard kernel module
  - executing readiness probes

What's a readiness probe?

The idea behind readiness probes is to ensure Wireguard connectivity
before continuing the cloud-init process. This could be useful if you
need access to specific services like an internal APT Repository Server
(e.g Landscape) to install/update packages.

Example:

An edge device can't access the internet but uses cloud-init modules which
will install packages (e.g landscape, packages, ubuntu_advantage). Those
modules will fail due to missing internet connection. The "wireguard" module
fixes that problem as it waits until all readinessprobes (which can be
arbitrary commands - e.g. checking if a proxy server is reachable over
Wireguard network) are finished before continuing the cloud-init
"config" stage.

.. note::
    In order to use DNS with Wireguard you have to install ``resolvconf``
    package or symlink it to systemd's ``resolvectl``, otherwise ``wg-quick``
    commands will throw an error message that executable ``resolvconf`` is
    missing which leads wireguard module to fail.
�cc_wireguardz$Module to configure Wireguard tunnel�ubuntu�	wireguarda�    # Configure one or more WG interfaces and provide optional readinessprobes
    wireguard:
      interfaces:
        - name: wg0
          config_path: /etc/wireguard/wg0.conf
          content: |
            [Interface]
            PrivateKey = <private_key>
            Address = <address>
            [Peer]
            PublicKey = <public_key>
            Endpoint = <endpoint_ip>:<endpoint_ip_port>
            AllowedIPs = <allowedip1>, <allowedip2>, ...
        - name: wg1
          config_path: /etc/wireguard/wg1.conf
          content: |
            [Interface]
            PrivateKey = <private_key>
            Address = <address>
            [Peer]
            PublicKey = <public_key>
            Endpoint = <endpoint_ip>:<endpoint_ip_port>
            AllowedIPs = <allowedip1>
      readinessprobe:
        - 'systemctl restart service'
        - 'curl https://webhook.endpoint/example'
        - 'nc -zv some-service-fqdn 443'
    )�id�name�title�description�distros�	frequency�activate_by_schema_keys�examples�meta)r�config_path�contenti��
)���wg_intc�,�g}t�t|�������}|r:d�t|����}|�d|����t|�����D]G\}}|dks|dks|dkr0t|t��s|�d|�d|�����H|r1tdt�t�|�������d	S)
aRValidate user-provided wg:interfaces option values.

    This function supplements flexible jsonschema validation with specific
    value checks to aid in triage of invalid user-provided configuration.

    @param wg_int: Dict of configuration value under 'wg:interfaces'.

    @raises: ValueError describing invalid values provided.
    z, z%Missing required wg:interfaces keys: rrrz$Expected a string for wg:interfaces:�. Found z*Invalid wireguard interface configuration:N)�REQUIRED_WG_INT_KEYS�
difference�set�keys�join�sorted�append�items�
isinstance�str�
ValueError�NL)r�errors�missingr%�key�values      �?/usr/lib/python3/dist-packages/cloudinit/config/cc_wireguard.py�supplemental_schema_validationr3is#���F�"�-�-�c�&�+�+�-�-�.@�.@�A�A�G��F��y�y�����)�)���
�
�D�d�D�D�E�E�E��V�\�\�^�^�,�,���
��U��&�=�=�C�=�0�0�C�9�4D�4D��e�S�)�)�
��
�
�O�3�O�O��O�O������
��N��N�R�W�W�V�_�_�N�N�
�
�	
�
�
�c
�`�t�d|d��	t�d|d��tj|d|dt���d	S#t
$r5}t
d|d�dt�t|������|�d	}~wwxYw)
z�Writing user-provided configuration into Wireguard
    interface configuration file.

    @param wg_int: Dict of configuration value under 'wg:interfaces'.

    @raises: RuntimeError for issues writing of configuration file.
    z"Configuring Wireguard interface %srz#Writing wireguard config to file %srr)�modez-Failure writing Wireguard configuration file �:N)	�LOG�debugr�
write_file�WG_CONFIG_FILE_MODE�	Exception�RuntimeErrorr-r+)r�es  r2�write_configr?�s����I�I�2�F�6�N�C�C�C�	��	�	�7��
�9N�O�O�O����=�!�6�)�#4�;N�	
�	
�	
�	
�	
�	
�������
5��}�%�
5�
5�(*�
5�,/��F�F�
5�
5�
�
��	��������s�A	A.�.
B-�80B(�(B-�cloudc��	t�d|d��|j�dd|d����t�d|d��|j�dd|d����dS#tj$r,}t
dt�t|������|�d}~wwxYw)	z�Enable and start Wireguard interface

    @param wg_int: Dict of configuration value under 'wg:interfaces'.

    @raises: RuntimeError for issues enabling WG interface.
    zEnabling wg-quick@%s at bootr�enablez	wg-quick@z!Bringing up interface wg-quick@%s�startz0Failed enabling/starting Wireguard interface(s):N)	r8r9�distro�manage_servicer�ProcessExecutionErrorr=r-r+)rr@r>s   r2�	enable_wgrG�s�����	�	�0�&��.�A�A�A�
��#�#�H�.J�&��.�.J�.J�K�K�K��	�	�5�v�f�~�F�F�F�
��#�#�G�-I����-I�-I�J�J�J�J�J���%�����K�r�K�3�q�6�6�K�K�
�
��	��������s�B
B�C	�'C�C	�wg_readinessprobesc���g}d}|D]7}t|t��s |�d|�d|����|dz
}�8|r1tdt�t�|�������dS)z�Basic validation of user-provided probes

    @param wg_readinessprobes: List of readinessprobe probe(s).

    @raises: ValueError of wrong datatype provided for probes.
    rz(Expected a string for readinessprobe at r!�z Invalid readinessProbe commands:N)r*r+r(r,r-r&)rHr.�pos�cs    r2�!readinessprobe_command_validationrM�s����F�
�C�
�����!�S�!�!�	��M�M�K�3�K�K��K�K�
�
�
�
�1�H�C��
�
��D�r�D�2�7�7�6�?�?�D�D�
�
�	
�
�
r4c�f�g}|D]x}	t�dt|����tj|dd����C#tj$r$}|�|�d|����Yd}~�qd}~wwxYw|r1t
dt�t�|�������dS)z�Execute provided readiness probe(s)

    @param wg_readinessprobes: List of readinessprobe probe(s).

    @raises: ProcessExecutionError for issues during execution of probes.
    zRunning readinessprobe: '%s'T��capture�shellz: Nz&Failed running readinessprobe command:)	r8r9r+rrFr(r=r-r&)rHr.rLr>s    r2�readinessproberR�s����F�
�'�'��	'��I�I�4�c�!�f�f�=�=�=��I�a��T�2�2�2�2�2���)�	'�	'�	'��M�M�Q�+�+�!�+�+�&�&�&�&�&�&�&�&�����	'�����
��J�R�J�������J�J�
�
�	
�
�
s�?A�A;�A6�6A;c��dg}tjd��rdStj��tkr|�d��	|j���n)#t$rtj	td���wxYw	|j�|��dS#t$rtj	td���wxYw)z�Install wireguard packages and tools

    @param cloud: Cloud object

    @raises: Exception for issues during package
    installation.
    zwireguard-tools�wgNrzPackage update failedz!Failed to install wireguard-tools)r�whichr�kernel_version�MIN_KERNEL_VERSIONr(rD�update_package_sourcesr<�logexcr8�install_packages)r@�packagess  r2� maybe_install_wireguard_packagesr\�s���"�"�H��z�$����������1�1�1�����$�$�$��
��+�+�-�-�-�-��������C�0�1�1�1�
������
��%�%�h�/�/�/�/�/��������C�<�=�=�=�
����s�A&�&&B�B,�,&Cc	��	tjddd���}tjd|j�����s3t
�d��tjddd���dSdS#tj$r7}tj	t
dt�t|�������d}~wwxYw)	zYLoad wireguard kernel module

    @raises: ProcessExecutionError for issues modprobe
    �lsmodTrOrzLoading wireguard kernel modulezmodprobe wireguardz Could not load wireguard module:N)r�re�search�stdout�stripr8r9rFrrYr-r+)�outr>s  r2�load_wireguard_kernel_modulerd�s���
��i���T�:�:�:���y��c�j�&6�&6�&8�&8�9�9�	F��I�I�7�8�8�8��I�*�D��E�E�E�E�E�E�	F�	F���%������C�H�B�H��A���H�H�I�I�I�
��������s�A4A:�:C�	2B;�;Cr�cfgr�args�returnc���d}d|vr#t�d��|d}nt�d|��dSt|��t��|dD]0}t	|��t|��t
||���1d|vr0|d�(|d}t|��t|��dSt�d��dS)Nrz!Found Wireguard section in configz<Skipping module named %s, no 'wireguard' configuration found�
interfacesrRz+Skipping readinessprobe - no checks defined)	r8r9r\rdr3r?rGrMrR)rrer@rrf�
wg_sectionrrHs        r2�handlerks���J��c����	�	�5�6�6�6���%�
�
��	�	�J��	
�	
�	
�	��%�U�+�+�+� �"�"�"��\�*�!�!��&�v�.�.�.�	�V����	�&�%� � � � �	�J�&�&��'�(�4�'�(8�9��)�*<�=�=�=��)�*�*�*�*�*��	�	�?�@�@�@�@�@r4))�__doc__r_�loggingr�textwrapr�	cloudinitrrr�cloudinit.cloudr	�cloudinit.configr
�cloudinit.config.schemarr�cloudinit.settingsr
�MODULE_DESCRIPTIONr�__annotations__�	getLogger�__name__r8�	frozensetr"r;r-rW�dictr3r?rG�listrMrRr\rdr+rk�r4r2�<module>r|sz��
���	�	�	�	�������������$�$�$�$�$�$� � � � � � � � �!�!�!�!�!�!�#�#�#�#�#�#�<�<�<�<�<�<�<�<�+�+�+�+�+�+��V��!�!��H��
3�%��z�� +�}���
�	
�	
� �)�)��j�)�)�)�V�,�t�
�
���g���!�!�� �y�!C�!C�!C�D�D����	����
�4�
�
�
�
�:������*�d��5�����$
�$�
�
�
�
�,
�t�
�
�
�
�*�E�����<���&A�

�&A��&A�#(�&A�/5�&A�=A�&A�	�&A�&A�&A�&A�&A�&Ar4