Current File : //usr/lib/python3/dist-packages/cloudinit/config/__pycache__/schema.cpython-312.pyc
�

Ӈgn����dZddlZddlZddlZddlZddlZddlZddlZddlm	Z	ddl
mZddlm
Z
ddlmZddlmZddlmZdd	lmZmZmZmZmZmZmZmZddlZdd
lmZm Z m!Z!m"Z"ddl#m$Z$ddl%m&Z&m'Z'dd
l(m)Z)ddl*m+Z+ddl,m-Z-ddl.m/Z/ddl0m1Z1m2Z2	ddl3m4Z4	ddl7m8Z8m9Z9dZ:ejve<�Z=dZ>dZ?dZ@dZAeBe&j��D�cgc]
}|ddk(s�|��c}�ZDerddlZddlEmFZFmGZGGd�deG�ZHneIZHGd�de4�ZJGd �d!e�ZKeeKZLGd"�d#e�ZMeMj�d$e>ieMj�d$e@ieMj�d$e?ieMj�d$e@iiZRGd%�d&e�ZSGd'�d(e�ZTGd)�d*e�ZUdd+d,�d-eLd.eeVd/eVd0eVfd1�ZWGd2�d3eX�ZYGd4�d5eY�ZZd6�Z[	dfd7eId0eVfd8�Z\d0ee4fd9�Z]eJfde^d:eId;eVd<ee6fd=�Z_ee_d�>�Z`ee_d?�>�ZaeJfd<ee6fd@�ZbeJfd<ee6fdA�ZcdB�Zddgd:eIfdC�ZedDeId0eeffdE�Zg			dhdDeIdFe^dGe^dHe^d0e^f
dI�Zhe!j�dJ�deMj�ddddfd7eId:eeIdKeMdFe^dLe^dHe^dMe^d0e^fdN��ZjGdO�dP�ZkdddQ�dReVdSeIdTeeLdUeeLd0eeVelff
dV�ZmdWeVd0eeKfdX�Zn	didYeVdWeVdZeeVd0eUfd[�ZoeMj�ddfdYeVd:eIdKeMdGe^dZeeVd0e^fd\�Zpd0eVfd]�ZqeMj�fdKeMd0eIfd^�Zrdid_�Zsd`�Ztd0eeVeeTffda�Zudb�Zvd0eVfdc�Zwdd�Zxe<dek(rej�ex��yy#e5$re6Z4Y���wxYw#e5$rdZ:Y���wxYwcc}w)jzFschema.py: Set of module functions for processing cloud-config schema.�N)�defaultdict)�suppress)�deepcopy)�Enum)�EACCES)�partial)�
TYPE_CHECKING�DefaultDict�List�
NamedTuple�Optional�Tuple�Type�Union)�features�	lifecycle�performance�safeyaml)�read_cfg_paths)�INCLUSION_TYPES_MAP�type_from_starts_with)�Paths)�error)�DataSourceNotFoundException)�mkdtemp)�load_text_file�
write_file)�ValidationError)�NetplanParserException�ParserTFzschema-cloud-config-v1.jsonzschema-network-config-v1.jsonzschema-network-config-v2.json�
deprecated�#)�NotRequired�	TypedDictc�\�eZdZUeed<ejeed<eed<eeeed<y)�
MetaSchema�id�distros�	frequency�activate_by_schema_keysN)�__name__�
__module__�__qualname__�str�__annotations__�typingrr#���9/usr/lib/python3/dist-packages/cloudinit/config/schema.pyr&r&Qs*�������S�!�!���!,�T�#�Y�!7�7r2r&c�(��eZdZdedef�fd�Z�xZS)�SchemaDeprecationError�message�versionc�4��t�|�|fi|��||_y�N)�super�__init__r7)�selfr6r7�kwargs�	__class__s    �r3r;zSchemaDeprecationError.__init__\s���	����+�F�+�#��r2)r+r,r-r.r;�
__classcell__�r>s@r3r5r5[s���$��$��$�$r2r5c�.�eZdZUeed<eed<defd�Zy)�
SchemaProblem�pathr6�returnc�8�|j�d|j��S)N�: )rCr6�r<s r3�formatzSchemaProblem.formatjs���)�)��B�t�|�|�n�-�-r2N)r+r,r-r.r/rHr1r2r3rBrBfs��

�I�
�L�.��.r2rBc� �eZdZdZdZdZdZdZy)�
SchemaTypeaSupported schema types are either cloud-config or network-config.

    Vendordata and Vendordata2 format adheres to cloud-config schema type.
    Cloud Metadata is unique schema to each cloud platform and likely will not
    be represented in this enum.
    zcloud-config�network-configznetwork-config-v1znetwork-config-v2N)r+r,r-�__doc__�CLOUD_CONFIG�NETWORK_CONFIG�NETWORK_CONFIG_V1�NETWORK_CONFIG_V2r1r2r3rJrJqs���"�L�%�N�+��+�r2rJ�latestc�&�eZdZdZdZdZdZdZd�Zy)�InstanceDataTypez-Types of instance data provided to cloud-initz	user-datarKzvendor-datazvendor2-datac��|jSr9)�valuerGs r3�__str__zInstanceDataType.__str__�s���z�z�r2N)	r+r,r-rL�USERDATArN�
VENDORDATA�VENDOR2DATArVr1r2r3rSrS�s��7��H�%�N��J� �K�r2rSc�,�eZdZUeed<eed<eed<y)�InstanceDataPart�config_type�schema_type�config_pathN)r+r,r-rSr/rJr.r1r2r3r[r[�s��!�!����r2r[c�"�eZdZUeed<eed<y)�UserDataTypeAndDecodedContent�
userdata_type�contentN)r+r,r-r.r/r1r2r3r`r`�s
����
�Lr2r`�, ��prefix�	separator�schema_problemsrerfrDc�N�|jtd�|��}|r|�|��}|S)Nc�"�|j�Sr9)rH)�ps r3�<lambda>z)_format_schema_problems.<locals>.<lambda>�s��Q�X�X�Z�r2)�join�map)rgrerf�	formatteds    r3�_format_schema_problemsro�s1�����s�#7��I�J�I�
��h�y�k�*�	��r2c�J��eZdZdZ		ddeedeef�fd�
Zdefd�Z�xZ	S)�SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.�
schema_errors�schema_deprecationsc�p���d��fd�}||d��|_||d��|_t�|�
��y)z�Init the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        @param schema_deprecations: An n-tuple of the format:
            ((flat.config.key, msg),)
        �c�x��|s|S�r�dz
�ttt|���}�t||��z
�|S)N�

�re)�sorted�list�setro)�problemsrer6s  �r3�handle_problemsz7SchemaValidationError.__init__.<locals>.handle_problems�sB��������6�!���d�3�x�=�1�2�H��.�x��G�G�G��Or2zCloud config schema errors: rx�"Cloud config schema deprecations: N)rrrsr:r;)r<rrrsr}r6r>s    @�r3r;zSchemaValidationError.__init__�sH�����	�-��1�
���$3��7�$
�� �	����!r2rDc�,�t|j�Sr9)�boolrrrGs r3�
has_errorsz SchemaValidationError.has_errors�s���D�&�&�'�'r2)NN)
r+r,r-rLr
�SchemaProblemsr;r�r�r?r@s@r3rqrq�s<���F�37�8<� "���/� "�&�n�5� "�D(�D�(r2rqc��eZdZdZy)�"SchemaValidationInvalidHeaderErrorz>Raised when no valid header is declared in the user-data file.N)r+r,r-rLr1r2r3r�r��s��Hr2r�c��	ddlm}|jj	|d�xst|tf�S#t$rYywxYw)zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r)�Draft4ValidatorF�string)�
jsonschemar��ImportError�TYPE_CHECKER�is_type�
isinstance�bytes)�checker�instancer�s   r3�is_schema_byte_stringr��sO��
�.��'�'�/�/��(��(�	�H�u�h�	'�(������s�7�	A�A�configc����dtf��fd�}|r|ngd�}djt||��}�jdd�}|�|��j	�S)z�combine description with new/changed/deprecated message

    deprecated/changed/new keys require a _version key (this is verified
    in a unittest), a _description key is optional
    �keyc�����j|��sy�j|�d�d�}�j|�d�d|�d��}|j��d|�d|��}�rd|��Sd	|j��d
�S)Nru�_description�_versionz	<missing z'_version key, please file a bug report>z in version �. � z

*�*)�get�
capitalize�strip)r��key_description�v�msg�annotater�s    ��r3�format_messagez:_add_deprecated_changed_or_new_msg.<locals>.format_message�s�����z�z�S�E�#�� �*�*��u�L�%9�2�>���J�J��e�8����u�C�D�
�����!�"�,�q�c��O�3D�E����s�e�9���s�y�y�{�m�1�%�%r2)r!�changed�newru�description)r.rlrmr��rstrip)r�r��
filter_keyr��filter_keys�changed_new_deprecatedr�s``     r3�"_add_deprecated_changed_or_new_msgr��s`���
&�C�
&�"!�
�&F��
 �W�W�S���%E�F���*�*�]�B�/�K��]�1�2�3�:�:�<�<r2c�X�g}d}t|t�xrd|v}|D�]	}|r�|d|jjdi�jdi�jdg�vr|gcSt	|d�r%|j
dddk(s�k|j
|��}|js��|jddk(s��|j
|���t|j�|k(r|j
|���t|j�|kDs��t|j�}|g}��|S)z�Return the best_match errors based on the deepest match in the json_path

    This is useful for anyOf and oneOf subschemas where the most-specific error
    tends to be the most appropriate.
    r�type�
properties�enum�	json_path���N)	r��dict�schemar��hasattrr��appendrC�len)�errorsr��best_matches�
path_depthr��errs      r3�cloud_init_deepest_matchesr�s���L��J���4�(�?�V�x�-?�G��!������3�:�:�>�>�,��#C�#G�#G���$��c�&�"�o���u���s�K�(��=�=���%��/� �'�'��,����c�h�h�q�k�V�3��#�#�C�(�
����]�j�
(�����$�
����]�Z�
'��S�X�X��J��5�L�)!�*�r2r�r��
error_typec#�fK�|r+t|d|g��}|||jdd����yy�w)z�Jsonschema validator for `deprecated` items.

    It yields an instance of `error_type` if deprecated that must be handled,
    otherwise the instance is consider faulty.
    T)r�r��deprecated_version�develN)r�r�)�
_validatorr!�	_instancer�r�r�r�s       r3r�r�4s>�����0��T�z�l�
����f�j�j�)=�w�G�H�H�	�s�/1)r�r�c#��K�ddlm}g}g}d}t|�D]�\}	}
t|j	||
|	���}tt�fd�|��}tt�fd�|��}
|s|j
|
�nrt|t�r1d|vr-d|
jd	d
�vrd|d��|
d	vrd}|Ed
{���|j
|���|s
||���t|�d�|����|Ed
{���y
7�@7��w)a�Jsonschema validator for `anyOf`.

    It treats occurrences of `error_type` as non-errors, but yield them for
    external processing. Useful to process schema annotations, as `deprecated`.

    Cloud-init's network schema under the `config` key has a complexity of
    allowing each list dict item to declare it's type with a `type` key which
    can contain the values: bond, bridge, nameserver, physical, route, vlan.

    This schema 'flexibility' makes it hard for the default
    jsonschema.exceptions.best_match function to find the correct schema
    failure because it typically returns the failing schema error based on
    the schema of greatest match depth. Since each anyOf dict matches the
    same depth into the network schema path, `best_match` just returns the
    first set of schema errors, which is almost always incorrect.

    To find a better schema match when encountering schema validation errors,
    cloud-init network schema introduced schema $defs with the prefix
    `anyOf_type_`. If the object we are validating contains a 'type' key, and
    one of the failing schema objects in an anyOf clause has a name of the
    format anyOf_type_XXX, raise those schema errors instead of calling
    best_match.
    r)�
best_matchF��schema_pathc���t|��Sr9�r���er�s �r3rkz_anyOf.<locals>.<lambda>s�����A�z�)B�%B�r2c���t|��Sr9r�r�s �r3rkz_anyOf.<locals>.<lambda>u����Z��:�6�r2r��
anyOf_typez$refru�anyOf_type_TNz, is not valid under any of the given schemas)�context)�jsonschema.exceptionsr��	enumeraterz�descend�filter�extendr�r�r�r)�	validator�anyOfr��_schemar�r��
all_errors�all_deprecations�skip_best_match�index�	subschema�all_errs�errs�deprecationss    `         r3�_anyOfr�Ls*�����<1��J����O�%�e�,�
���y�����h�	�u��E�
���F�B�H�M�N����6��A�
����#�#�L�1���x��&��(�"��	�
�
�f�b� 9�9��X�f�-�.�/�9�V�3D�D�#'��������$��+
�.��Z�(�(��@H�J��
�	
� ��� �� �s$�B<D�?D�:D�:D�;D�Dc#�D�K�t|�}g}g}|D]z\}}	t|j||	|���}
tt�fd�|
��}tt�fd�|
��}|s|	}
|j	|�n'|j	|��|t||�Ed{���|D��cgc]\}}|j
||�s�|��}}}|r<|j
�djd�|D��}t|�d|�����y|Ed{���y7�tcc}}w7�
�w)z�Jsonschema validator for `oneOf`.

    It treats occurrences of `error_type` as non-errors, but yield them for
    external processing. Useful to process schema annotations, as `deprecated`.
    r�c���t|��Sr9r�r�s �r3rkz_oneOf.<locals>.<lambda>�r�r2c���t|��Sr9r�r�s �r3rkz_oneOf.<locals>.<lambda>�r�r2Nrcc3�2K�|]}t|����y�wr9)�repr)�.0r�s  r3�	<genexpr>z_oneOf.<locals>.<genexpr>�s����@�6�$�v�,�@���z is valid under each of )
r�rzr�r�r�r��is_validr�rlr)r��oneOfr�r�r��
subschemasr�r�r�r�r�r�r��first_valid�i�s�
more_valid�reprss    `             r3�_oneOfr��s0������5�!�J��J���&�D���y�����h�	�u��E�
���F�B�H�M�N����6��A�
���#�K��#�#�L�1�����$��D�.�j�(�C�C�C� *�N���1�i�.@�.@��1�.M�!�N�J�N�����+�&��	�	�@�Z�@�@���.6��>�
�	
�$�#�#�	D��N�	$�s7�BD �!D�"	D �+D�D�
AD �D�D �D c��ddlm}m}ddlm}t|j�}ddi|dd<i}d|d	<|jjdt�}d
|i}t|j�}t|t<t|d<t|d<t |d
<|d||dd�|��}dd�}dd�}	|}
t#|d�r|	}
|
|_||fS)z�Get metaschema validator and format checker

    Older versions of jsonschema require some compatibility changes.

    @returns: Tuple: (jsonschema.Validator, FormatChecker)
    @raises: ImportError when jsonschema is not present
    r)r��
FormatChecker)�creater�r�r��labelF�additionalProperties�type_checkerr�r�r��draft4)�meta_schema�
validatorsr7c�X�td�|j||��}t|d�duS)�gOverride version of `is_valid`.

        It does ignore instances of `SchemaDeprecationError`.
        c�$�t|t�Sr9�r�r5�r�s r3rkzFget_jsonschema_validator.<locals>.is_valid_pre_4_0_0.<locals>.<lambda>����*��)���r2N)r��iter_errors�next�r<r�r��__r�s     r3�is_valid_pre_4_0_0z4get_jsonschema_validator.<locals>.is_valid_pre_4_0_0�s9��
�
�
���X�w�/�	
���F�D�!�T�)�)r2c�v�td�|j|��j|��}t|d�duS)r�c�$�t|t�Sr9r�r�s r3rkz<get_jsonschema_validator.<locals>.is_valid.<locals>.<lambda>�r�r2)r�N)r��evolver�rrs     r3r�z*get_jsonschema_validator.<locals>.is_valid�sB��
�
�
�K�K�w�K�'�3�3�H�=�	
���F�D�!�T�)�)r2rr1r9)r�r�r��jsonschema.validatorsr�r�META_SCHEMAr��redefiner�r��
VALIDATORS�_validator_deprecated�DEPRECATED_KEY�_validator_changedr�r�r�r�)r�r�r�r��validator_kwargsr�r��cloudinitValidatorrr��is_valid_fns           r3�get_jsonschema_validatorr�s��:�,��?�6�6�7�K�+1�(�);�K���g�&���*/�K�&�'�"�/�/�8�8��'��L�	����
�o�0�0�1�J�!6�J�~��.�J�y�� �J�w�� �J�w���������	��*�*�%�K��!�8�,���#.����
�.�.r2c
�8�ddlm}	|j|�y#|$rz}|rXtt	dj|jD�cgc]
}t|���ncc}wc}�|j�g��|�tjd|�Yd}~yd}~wwxYw)a Validate provided schema meets the metaschema definition. Return strict
    Validator and FormatChecker for use in validation
    @param validator: Draft4Validator instance used to validate the schema
    @param schema: schema to validate
    @param throw: Sometimes the validator and checker are required, even if
        the schema is invalid. Toggle for whether to raise
        SchemaValidationError or log warnings.

    @raises: ImportError when jsonschema is not present
    @raises: SchemaValidationError when the schema is invalid
    r)�SchemaError�.�rrzGMeta-schema validation failed, attempting to validate config anyway: %sN)r�r�check_schemarqrBrlrCr.r6�LOG�warning)r�r��throwrr�rjs      r3�validate_cloudconfig_metaschemars���2�
����v�&���
��'�!����#�(�(�!;�Q�#�a�&�!;��!;�<�c�k�k�����

�	���
��	
�	
��
�s ��B�%B�A�9B�B�network_configc�T�d|vr|djd�S|jd�S)z6Return the version of the network schema when present.�networkr7)r�)rs r3�network_schema_versionr2s1���N�"��i�(�,�,�Y�7�7����i�(�(r2�strictr��log_detailsc	���trtjd�ntjd�yt�}tj
j
|d�}t|�}d|vrd|i}tj|�}t||d��t�}g}		|j|�tj
j)|�rt+j,|�|	r�|r<|r/tj.|�\}}t1t3|||	���t5|	��|r*t7|	t8j:j<�d
�d��}
nt8j:j<�d�}
tj?|
�y#t$rX}
|	jtdj!|
j"|
j$�	�d
|
j&����Yd}
~
��/d}
~
wwxYw)ajOn systems with netplan, validate network_config schema for file

    Leverage NetplanParser for error annotation line, column and detailed
    errors.

    @param network_config: Dict of network configuration settings validated
        against
    @param strict: Boolean, when True raise SchemaValidationErrors instead of
       logging warnings.
    @param annotate: Boolean, when True, print original network_config_file
        content with error annotations
    @param log_details: Boolean, when True logs details of validation errors.
       If there are concerns about logging sensitive userdata, this should
       be set to False.

    @return: True when schema validation was performed. False when not on a
        system with netplan and netplan python support.
    @raises: SchemaValidationError when netplan's parser raises
        NetplanParserExceptions.
    z*Validating network-config with netplan APIz<Skipping netplan schema validation. No netplan API availableFzetc/netplan/network-config.yamlri�)�mode�format-l{line}.c{col}��line�colzInvalid netplan schema. Nr� failed schema validation!
�
rd�^ failed schema validation! You may run 'sudo cloud-init schema --system' to check the details.T) �LIBNETPLAN_AVAILABLEr�debugr�osrCrlrr�dumpsrr �load_yaml_hierarchyrr�rBrHr%�columnr6�exists�shutil�rmtree�load_with_marks�print�annotated_cloudconfig_filerqrorJrNrUr)rrr�r �	parse_dir�netplan_file�net_cfg�src_content�parserr�r��_�marksr6s              r3�netplan_validate_network_schemar=9s���4��	�	�>�?��	�	�J�	
��
�	�I��7�7�<�<�	�+L�M�L��~�&�G�����g�&���.�.��)�K��|�[�u�5�
�X�F�
�F�

�	�"�"�9�-�
�w�w�~�~�i� ��
�
�i� �
���#�3�3�K�@���5��.�#��&,���(��/�/��-��!�0�0�6�6�7�8+�+��
�G��,�,�2�2�3�4%�%�
�
	���G����M"�
��
�
��'�.�.�A�F�F����.�I�*�1�9�9�+�6�
�	
�	
��
�s� F�	G%�
A
G � G%zValidating schemar]�strict_metaschema�log_deprecationsc	���ddlm}|tjk(rAt	|�}|dk(rtj
}n|dk(rtj}t|�}|tj
k(rt|||��ry|�ry|�t|�}	t�\}	}
|rt|	|d��|	||
��
�}g}g}
g}t|j|�d���D�];}d
j!|j"D�cgc]
}t%|���c}�}|sE|j&dk(r6|j(|k(r't+j,d|j.�}|r|d}t1|t2�r�|j4dk(s.t7j8|j4t:j<�r&|
j?tA||j.����|j?tA||j.����|j?tA||j.����>|rH|r"tC|d��}tjE|�|
r"tC|
d��}tjG|�|r|s|
s|rtI||
|z��|rB|rtC||jJ�d�d��}n|jJ�d�}tjM|�y#t$rtjd	�YywxYwcc}w)aPValidate provided config meets the schema definition.

    @param config: Dict of cloud configuration settings validated against
        schema. Ignored if strict_metaschema=True
    @param schema: jsonschema dict describing the supported schema definition
       for the cloud config module (config.cc_*). If None, validate against
       global schema.
    @param schema_type: Optional SchemaType.
       One of: SchemaType.CLOUD_CONFIG or SchemaType.NETWORK_CONFIG_V1 or
            SchemaType.NETWORK_CONFIG_V2
       Default: SchemaType.CLOUD_CONFIG
    @param strict: Boolean, when True raise SchemaValidationErrors instead of
       logging warnings.
    @param strict_metaschema: Boolean, when True validates schema using strict
       metaschema definition at runtime (currently unused)
    @param log_details: Boolean, when True logs details of validation errors.
       If there are concerns about logging sensitive userdata, this should
       be set to False.
    @param log_deprecations: Controls whether to log deprecations or not.

    @raises: SchemaValidationError when provided config does not validate
        against the provided schema.
    @raises: RuntimeError when provided config sourced from YAML is not a dict.
    @raises: ValueError on invalid schema_type not in CLOUD_CONFIG or
        NETWORK_CONFIG_V1 or NETWORK_CONFIG_V2
    r��	available��)rrr TF)rz5Ignoring schema validation. jsonschema is not present)�format_checkerc��|jSr9)rCr�s r3rkz-validate_cloudconfig_schema.<locals>.<lambda>�s
��Q�V�V�r2)r�rr�z#.*\('(?P<name>.*)' was unexpected\)�namer�z"Deprecated cloud-config provided: rxr'r(rdr))'�cloudinit.net.netplanrBrJrNrrPrO�
get_schemar=rrr�rr+ryr�rlrCr.r�r��re�matchr6r�r5r7r�should_log_deprecationr�DEPRECATION_INFO_BOUNDARYr�rBro�infor!rqrUr)r�r�r]rr>r r?�netplan_available�network_versionrr�r�r�r��info_deprecations�schema_errorrjrC�
prop_matchr6�detailss                     r3�validate_cloudconfig_schemarU�s���HE��j�/�/�/�0��8���a��$�6�6�K�
��
!�$�6�6�K��K�(���j�2�2�2�*�!�&�k�
��
�
 ��
�~��K�(���.F�.H�+�	�]��+�"�F�%�
�#�6�-�/�J�I��F�#%�L�(*������f�%�+;��E���x�x��):�):�;�A��Q��;�<����&�&�*@�@��#�#�v�-����6��8L�8L��J��!�&�)����0�
��$�$��/��3�3� �(�(�(�*L�*L���#�#�M�$��8L�8L�$M�N�!�(�(�!�$��(<�(<�=��
�M�M�-��l�.B�.B�C�D�=E�@��-�!�;��G�
�H�H�W���-��;��G�
�N�N�7�#�
�6�\�->�#�F�L�;L�,L�M�M�
��-��%�+�+�,�,H�I���G��$�$�%�&%�%�
�
	���G����Q���	�	�I�J����<s�K�?K+
�K(�'K(c���eZdZdedefd�Zededeedefd��Zde	fd	�Z
e	dd
eedeedeed
ededefd��Zdeedededeefd�Z
de	de	defd�Zy)�
_Annotator�original_content�schemamarksc� �||_||_yr9)�_original_content�_schemamarks)r<rXrYs   r3r;z_Annotator.__init__)s��
"2���'��r2�titlerbrDc�6�dj|�}d|�d|�d�S)Nr(�# z: -------------
rw)rl)r]rb�bodys   r3�
_build_footerz_Annotator._build_footer1s&���y�y��!���E�7�+�D�6��6�6r2rgc�>�tt�}|D]�\}}tjd|�}|r1|j	�\}}|t|�j
|�n#d}||j|j
|�|��udj||��}��|S)Nz&format-l(?P<line>\d+)\.c(?P<col>\d+).*zLine {line} column {col}: {msg})r%r&r�)	rrzrJrK�groups�intr�r\rH)r<rg�errors_by_linerCr�rKr%r&s        r3�_build_errors_by_linez _Annotator._build_errors_by_line6s���=H��=N��(�	�I�D�#��H�H�F��M�E��!�L�L�N�	��c��s�4�y�)�0�0��5����t�0�0��6�7�>�>�s�C���7�>�>��3�C�?���	��r2r|�labels�footerr��label_prefixc�z�|D]5}|�|��}|j|�|jd|�d|���|dz
}�7|S)Nr_rFrD)r�)r|rgrhr�ri�problemr�s       r3�
_add_problemsz_Annotator._add_problemsFsV�� �	�G�#�n�U�G�,�E��M�M�%� ��M�M�B�u�g�R��y�1�2��Q�J�E�		�
�r2�linesre�deprecations_by_linec���g}g}g}d}d}t|d�D]y\}	}
||	}||	}|s|rUg}
�j||
||d��}�j||
||d��}|j|
dzdj|
�z��i|j|
��{|j	t�fd�t
d�d	|fd
|ff���|S)NrD�E)ri�Dz		# �,c�"���j|�Sr9)ra)�seqr<s �r3rkz._Annotator._annotate_content.<locals>.<lambda>us���.�D�.�.��4�r2c��t|d�S)NrD)r�)rts r3rkz._Annotator._annotate_content.<locals>.<lambda>ws���S��V��r2�Errors�Deprecations)r�rlr�rlr�rmr�)r<rmrern�annotated_content�error_footer�deprecation_footer�error_index�deprecation_index�line_numberr%r�r�rgs`             r3�_annotate_contentz_Annotator._annotate_contentUs�����"$��(*������!*�5�!�!4�	/��K��#�K�0�F�/��<�L���$&��"�0�0��F�L�+�C�1���%)�$6�$6� ��&�%�!$�%7�%�!�"�(�(����3�8�8�F�;K�)K�L�!�(�(��.�#	/�&	� � ��4��,�!�<�0�'�);�<���	
�	
�!� r2rrrsc���|s|s|jS|jjd�}|j|�}|j|�}|j|||�}dj	|�S)Nr()r[�splitrfr~rl)r<rrrsrmrernrxs       r3r�z_Annotator.annotate�sy��
�%8��)�)�)��&�&�,�,�T�2���3�3�M�B��#�9�9�:M�N�� �2�2��>�#7�
���y�y�*�+�+r2N)ru)r+r,r-r.r�r;�staticmethodrrar�rfrdrlr~r�r1r2r3rWrW(s��(��(��(��7�S�7�4��9�7��7��7��^�� ����s�)���S�	���S�	���	�
��

�
���*!��C�y�*!��*!�#�	*!�

�c��*!�X
,�%�
,�,�
,�

�	
,r2rW�rrrsrXrYrrrsc�J�t||�j|xsg|xsg�S)a�Return contents of the cloud-config file annotated with schema errors.

    @param cloudconfig: YAML-loaded dict from the original_content or empty
        dict if unparsable.
    @param original_content: The contents of a cloud-config file
    @param schemamarks: Dict with schema marks.
    @param schema_errors: Instance of `SchemaProblems`.
    @param schema_deprecations: Instance of `SchemaProblems`.

    @return Annotated schema
    )rWr�)rXrYrrrss    r3r5r5�s/��$�&��4�=�=����0�6�B��r2rbc
���ddlm}||vrgSg}t|j�d�D]F\}}|j	|�s�|jt
d|�d�|j|d����H|S)a�Annotate and return schema validation errors in merged cloud-config.txt

    When merging multiple cloud-config parts cloud-init logs an error and
    ignores any user-data parts which are declared as #cloud-config but
    cannot be processed. the handler.cloud_config module also leaves comments
    in the final merged config for every invalid part file which begin with
    MERGED_CONFIG_SCHEMA_ERROR_PREFIX to aid in triage.
    r)�MERGED_PART_SCHEMA_ERROR_PREFIXrDzformat-lz.c1zIgnored invalid user-data: )�cloudinit.handlers.cloud_configr�r��
splitlines�
startswithr�rB�replace)rbr�r��line_numr%s     r3�)process_merged_cloud_config_part_problemsr��s���P�&�g�5��	�"$�F�#�G�$6�$6�$8�!�<�
���$��?�?�:�;��M�M���x�j��,��L�L�7�5���
�
��Mr2r^�instance_data_pathc
��ddlm}m}m}m}t|�}d}|dk(r	||||�}d
}t|�}|sG|jd�\}
}}tt|d|�d
|
�ddjt����g��|dk7rtd|�d��t||�S#|$r}	t
t|d�g�|	�d}	~	w|$r#}	tdt|	�zd�	�Yd}	~	��d}	~	w|$r }	tt|	�d�	�Yd}	~	��d}	~	wwxYw)a�
    Return tuple of user-data-type and rendered content.

    When encountering jinja user-data, render said content.

    :return: UserDataTypeAndDecodedContent
    :raises: SchemaValidationError when non-jinja content found but
        header declared ## template: jinja.
    :raises JinjaSyntaxParsingException when jinja syntax error found.
    :raises JinjaLoadError when jinja template fails to load.
    r)�JinjaLoadError�JinjaSyntaxParsingException�
NotJinjaError�render_jinja_payload_from_filezformat-l1.c1ztext/jinja2zRDetected type '{user_data_type}' from header. But, content is not a jinja templateNz&Failed to render templated user-data. T��sys_exitzformat-l2.c1r(z!Unrecognized user-data header in z: "z%".
Expected first line to be one of: rc�text/cloud-configzUser-data type 'z.' not currently evaluated by cloud-init schema)�!cloudinit.handlers.jinja_templater�r�r�r�rrqrBrr.�	partitionr�rl�USERDATA_VALID_HEADERSr4r`)r^rbr�r�r�r�r��user_data_type�schema_positionr��header_liner;s            r3�&_get_config_type_and_rendered_userdatar��sb�� ��+�7�3�N�$�O���&�	)�4���&8��G�()��.�w�7���#�-�-�d�3���Q��0��#�7��}�E�$�
�&&�&*�i�i�0F�&G�%H�J��
�	
�		
�
�.�	.�
��~�.�/$�
$�	
�)���A�A��I�		�'�!�'�?�����
��+�	��8�3�q�6�A��
�
���	)��#�a�&�4�(�(��	)�s/�
B � D
�%B<�<D
�C"�"D
�*D�D
c��ddlm}t|�}|std|j�d|�d��y|t
jfvrt|j|�}n
t|||�}|jdvry|j}t|�}		|rtj|�\}
}ntj|�}
i}t9|
t:�s|st=|j�d|�d���|t
jk(r�|
j?d|
�std�ytA|
�}|dk(r2t
jB}tE|
d|��ry|�r,td�y|dk(rt
jF}tI|�}	tK|
||dd��std|j�d��y	y#tj $r�}dx}
}d	}t#|d
�rt%|d
�r
t%|d
�}n$t#|d�rt%|d�rt%|d�}|r|j&dz}
|j(dz}|	j+t-dj/|
|�
�dj/|t1|����t3|	�}|r!tt5|i|j6���||�d	}~wwxYw#t2$r�}|jM�r|	|j6z
}	|r#tt5|||	|jN���n/|jNr#tQ|jNdd��}t|�|	r
t3|	��|�Yd	}~yd	}~wwxYw)a�Validate cloudconfig file adheres to a specific jsonschema.

    @param config_path: Path to the yaml cloud-config file to parse, or None
        to default to system userdata from Paths object.
    @param schema: Dict describing a valid jsonschema to validate against.
    @param schema_type: One of SchemaType.NETWORK_CONFIG or CLOUD_CONFIG
    @param annotate: Boolean set True to print original config file with error
        annotations on the offending lines.
    @param instance_data_path: Path to instance_data JSON, used for text/jinja
        rendering.

    :return: True when validation was performed successfully
    :raises SchemaValidationError containing any of schema_errors encountered.
    :raises RuntimeError when config_path does not exist.
    rrAzEmpty 'z' found at z. Nothing to validate.F)rKr�rDN�context_mark�problem_markr#r$zFile {0} is not valid YAML. {1}rr�z is not a YAML dict.rz:Skipping network-config schema validation on empty config.rCT)rrr�zSSkipping network-config schema validation for version: 2. No netplan API available.)r�r]rr?z	Skipping z2 schema validation. Jsonschema dependency missing.r�r~rcrd))rHrBrr4rUrJrNr`r�rarbr�rr3�yaml�	safe_load�	YAMLErrorr��getattrr%r/r�rBrHr.rqr5rrr�r��RuntimeErrorr�rrPr=rOrIrUr�rsro)r^r�r]r�r�rO�decoded_content�decoded_configrbr��cloudconfigr<r�r%r/�markrRrPr6s                   r3�validate_cloudconfig_filer�sf��,E�$�[�1�O��
�� � �+�
/�	
���z�0�0�2�2�6�����
��@���*<�
���#�#�,����$�$�G�
6�w�
?�F�"��!)�!9�!9�'�!B��K���.�.��1�K��E�2�k�4�(����$�$�%�Q�{�m�3G�H��
��j�/�/�/����y�+�6��N�O��0��=���a��$�6�6�K�.�*�4�(���"�$��1���
��
!�$�6�6�K���,�F�!E�*���#��"�
�
��K�-�-�.�/2�2�
��
�B��k�>�>�"����v����1�n�%�'�!�^�*D��1�n�-�D�
�Q��
'�G�A�~�,F��1�n�-�D���9�9�q�=�D��[�[�1�_�F��
�
��'�.�.�D�f�.�E�1�8�8��c�!�f�M�
�	
�-�V�4����*��R�|�/I�/I��
�
��!��/"��@!�E��<�<�>��a�o�o�%�F���*���"(�()�(=�(=�	�
��
"�
"�-��%�%�;���G�

�'�N��'�f�=�1�D����+E�s2�2F.�)J.�.J+�C%J&�&J+�.	M�7BL>�>Mc��tjjtjjtjj	t
��d�S)N�schemas)r,rCrl�dirname�abspath�__file__r1r2r3�get_schema_dirr��s1��
�7�7�<�<������������(A�B�I�N�Nr2c��tjjt�t|d�}d}	tjt|��}|S#ttf$r&tjd|j|�icYSwxYw)ziReturn jsonschema for a specific type.

    Return empty schema when no specific schema file exists.
    rQNz<Skipping %s schema validation. No JSON schema file found %s.)
r,rCrlr��SCHEMA_FILES_BY_TYPE�json�loadsr�IOError�OSErrorrrrU)r]�schema_file�full_schemas   r3rIrI�s���
�'�'�,�,���.�{�;�H�E��K��K���j�j���!<�=�����
�W������J�����	
�
�	�
�s�A�2B�
Bc	���|stjdd��}|jddd��|jdd	ttj
jtjjgd
tj
����|jdd
tdt�jd�����|jdddd��|jdddd��|S)z0Return a parser for supported cmdline arguments.�cloudconfig-schemaz�Schema validation and documentation of instance-data configuration provided to cloud-init. This includes: user-data, vendor-data and network-config)�progr�z-cz
--config-filez@Path of the cloud-config or network-config YAML file to validate)�helpz-tz
--schema-typezSWhen providing --config-file, the schema type to validate config against. Default: )r��choicesr�z-iz--instance-datazbPath to instance-data.json file for variable expansion of '##template: jinja' user-data. Default: �
instance_data)r�r�z--system�
store_trueFzVValidate the system instance-data provided as vendor-data user-data and network-config)�action�defaultr�z
--annotatez;Annotate existing instance-data files any discovered errors)
�argparse�ArgumentParser�add_argumentr.rJrMrUrNr�get_runpath�r:s r3�
get_parserr��s����(�(�%�=�
�������N�	�������
��#�#�)�)��%�%�+�+�
�

"�",�"9�"9�!:�
<��������
�
:���+�+�O�<�=�
?��	�������
,���������
J�	���Mr2c���|j|jg}t|D�cgc]}|s�|��	c}�dk7r
tdd��|jr|jrtd�yyycc}w)z:Error or warn on invalid exclusive parameter combinations.rDz3Expected one of --config-file or --system argumentsTr�zMWARNING: The --schema-type parameter is inapplicable when --system is presentN)�config_file�systemr�rr]r4)�args�exclusive_args�args   r3�_assert_exclusive_argsr��sh���&�&����4�N�
�>�1�C�S�C�1�2�a�7�
�A��	
��{�{�t�'�'�
�
"�	
�(�{��2s
�A+�A+c
�j�dtdtdtdtfd�}	td��}|jr
|j}n:tj�dk7r|jd�}n|jd
�}g}|jr�|j rt#|j �}nt"j$}|t"j&k(rt(j&}nt(j*}|j-t/|||j���nRtj�dk7r
t1dd��||dd�}|j-t/t(j*t"j$|��t/t(j2t"j$||dd��t/t(j4t"j$||dd��t/t(j&t"j&|j7d�xsd�g}	|	D]J}
|
j8s�tj:j=|
j8�s�:|j-|
��Ltj:j=|dj8�st1d|dj8�d�dd��||fS#ttf$r?}|j
tk(r tjd�t�}n�Yd	}~���d	}~wt$r#t�}tjd
�Y��wxYw)aReturn appropriate instance-data.json and instance data parts

    Based on command line args, and user permissions, determine the
    appropriate instance-data.json to source for jinja templates and
    a list of applicable InstanceDataParts such as user-data, vendor-data
    and network-config for which to validate schema. Avoid returning any
    InstanceDataParts when the expected config_path does not exist.

    :return: A tuple of the instance-data.json path and a list of
        viable InstanceDataParts present on the system.
    �paths�primary_path_key�raw_fallback_path_keyrDc�6�|j|�xsd}tt�5tj|�j
s?|j|�xsd}tj|�j
r|cddd�Sddd�|S#1swY|SxYw)akGet processed data path when non-empty of fallback to raw data path.

        - When primary path and raw path exist and are empty, prefer primary
          path.
        - When primary path is empty but the raw fallback path is non-empty,
          this indicates an invalid and ignored raw user-data was provided and
          cloud-init emitted a warning and did not process unknown raw
          user-data.
          In the case of invalid raw user-data header, prefer
          raw_fallback_path_key so actionable sensible warnings can be
          reported to the user about the raw unparsable user-data.
        ruN)�	get_ipathr�FileNotFoundErrorr,�stat�st_size)r�r�r��primary_datapath�raw_paths     r3�get_processed_or_fallback_pathzBget_config_paths_from_args.<locals>.get_processed_or_fallback_path
s���"!�?�?�+;�<�B���
�'�
(�	$��7�7�+�,�4�4� �?�?�+@�A�G�R���7�7�8�$�,�,�#�		$�	$�	$�
 ��	$�
 ��s�AB�B�trust)�fetch_existing_datasourcez=Using default instance-data/user-data paths for non-root userNzEdatasource not detected, using default instance-data/user-data paths.rr��instance_data_sensitivezNUnable to read system userdata or vendordata as non-root user. Try using sudo.Tr��cloud_config�userdata_raw�vendor_cloud_config�vendordata_raw�vendor2_cloud_config�vendordata2_rawrruzConfig file z does not existz	Error: {}��fmtr�)rr.rr�r��errnorrr+rrr�r,�getuidr�r�r]rJrMrNrSrWr�r[rrXrYr�r^rCr0)r�r�r�r�r��config_filesr]�instancedata_type�
userdata_file�supplemental_config_files�	data_parts           r3�get_config_paths_from_argsr��s��� �� �� � #� �

�	 �2
���A�����!�/�/��	�����	�"�.�.��?��"�.�.�/H�I��+-�L�������$�T�%5�%5�6�K�$�1�1�K��*�3�3�3� 0� ?� ?�� 0� 9� 9������.��T�=M�=M�N�	
��9�9�;�!���)��
�
7��>�>�
�
�	���� �)�)��'�'��
�	
�
� �+�+��'�'�.��0�2B��
�
� �,�,��'�'�.��1�3D��
�
� �/�/��)�)���� 0�1�7�R�
�=
�!�*3�	/�I��$�$������	�8M�8M�)N��#�#�I�.�	/��7�7�>�>�,�q�/�5�5�6�
��<��?�6�6�7��G���	
�
�|�+�+��a
�W����7�7�f���I�I�O�
�#�$�E��
��'�
�� �����
.�	
�
�s�J:�:L2�	4L�+L2�1L2c
��t|�t�}t|�\}}d}tt	|�dkD�}|r&tddj
d�|D��z�d}g}t|d�D]�\}}	d}
|r)td|�d	|	j�d
|	j�d��|	jtjk(rt|	j�}n|}	t|	j||	j|j|�}
|
s��|jr
|	j}nt!|	j�}t|�d|�����|r%t%dj
d�|D��dd��yy#t"$rr}
|js@t|�d
|	j�d|	j���t%t!|
�|dz��|j'|	j�Yd}
~
��wd}
~
wt($rY}
t|�d
|	j���t%t!|
�|dz��|j'|	j�Yd}
~
���d}
~
wwxYw)z@Handle provided schema args and perform the appropriate actions.rurDz!Found cloud-config data types: %srcc3�FK�|]}t|j����y�wr9)r.r\)r��cfg_parts  r3r�z%handle_schema_args.<locals>.<genexpr>�s����O�h��H�0�0�1�O�s�!z  Fr(r�z at �:z
Valid schema zInvalid r�z
Error: {}
)r�Nc3�2K�|]}t|����y�wr9)r.)r�r�s  r3r�z%handle_schema_args.<locals>.<genexpr>�s����D�*�c�*�o�D�r�zError: Invalid schema: {}
Tr�)r�rIr�r�r�r4rlr�r\r^r]rJrNr�r�r�r.rqrr�r�)rGr�r�r�r��nested_output_prefix�multi_config_output�error_types�idxr��performed_schema_validation�
cfg_schema�cfgr�s              r3�handle_schema_argsr�xsT���4� ��,�K�'A�$�'G�$������s�<�0�1�4�5���
�/��i�i�O�,�O�O�
P�	
� $���K�"�<��3�'E�
��X�&+�#����S�E��H�0�0�1��h�6J�6J�5K�1�M�
����:�#<�#<�<�#�H�$8�$8�9�J�$�J�	E�*C��$�$���$�$��
�
�"�+�'�.+��#�#�"�.�.�C��h�2�2�3�C��-�.�m�C�7�C�D�O'E�P�
��I�I�D��D�D�-��	
���-%�
	5��=�=��+�,�-� �,�,�-�Q�x�/C�/C�.D�F�����F�,�}�<��
���x�3�3�4�4���	5��)�*�(�8�3G�3G�2J�K�L��#�a�&�2�]�B�C����x�3�3�4�4��	5�s&�-E4�4	I�=A'G*�*I�6AI
�
Ic�\�tjtdtjdd��y)z�Provide a stub for backwards compatibility.

    This function is no longer used, but earlier versions of modules
    required this function for documentation purposes. This is a stub so
    that custom modules do not break on upgrade.
    z24.4zbThe 'get_meta_doc()' function is deprecated and will be removed in a future version of cloud-init.r1)�loggerr7�requested_levelr�r�ru)r�log_with_downgradable_levelr�logging�WARNING)�_args�_kwargss  r3�get_meta_docr�s.���)�)������
1��	�r2c�L�t�}td|j��y)zDTool to validate schema of a cloud-config file or print schema docs.r�r)r�r��
parse_argsr�s r3�mainr�s ��
�\�F��+�V�->�->�-@�A�r2�__main__)FN)T)FFTr9)zrLr�r�r�r,rJr1�sys�collectionsr�
contextlibr�copyrr�rr�r�	functoolsrr0r	r
rrr
rrrr��	cloudinitrrrr�cloudinit.cmd.develr�cloudinit.handlersrr�cloudinit.helpersr�cloudinit.log.log_utilr�cloudinit.sourcesr�cloudinit.temp_utilsr�cloudinit.utilrrr�rr��	Exception�netplanrr r*�	getLoggerr+r�USERDATA_SCHEMA_FILE�NETWORK_CONFIG_V1_SCHEMA_FILE�NETWORK_CONFIG_V2_SCHEMA_FILErry�keysr��typing_extensionsr#r$r&r�r5rBr�rJrMrNrOrPr�rSr[r`r.ro�
ValueErrorrqr�r�r�r�r�r�rr
r�r�rrrdrr=�timedrUrWr�r5r�r�r�r�rIr�r�r�r�rr�exit)�ts0r3�<module>rs���L����	�	�
�
�#������	�	�	��@�@�.�I�#�(�9�(�5� �*�!�6���
�g����!��5�� ?�� ?���� �(�#�(�(�*�:�1�a��d�c�k�Q�:�����8�8�Y�8��J�$�_�$�.�J�.��m�$��,��,�"����&������/� �� � ��/�#�� � ��/�#�
�� 
�t�
��z���J��!��		�#�	�
�S�M�	��		�
	�	�&(�J�&(�RI�)>�I�(�.2� =�� =�� =�F�D��4I��N#9�
I��I�
�	I�
�I��Y��
I�( �
�|�D���Z�I�>��#9�@ �
�Y��@ �P#9�'$�
�Y��'$�TS/�l 
�t� 
�F)�4�)�H�S�M�)����	\��\��\��\��	\�

�\�~����&�'�"�(�5�5��#��"�L��L��T�N�L��L�
�	L�
�L��
L��L�
�L�(�L�^f,�f,�Z/3�48�������N�+�	�
"�.�1���3��:��
�.�
��	�-���B)-�BB��BB�
�BB�!��
�BB�#�	BB�P)�5�5��(,�J��J��J��J��	J�
!��
�J�
�
J�ZO��O�*4�)@�)@��J��T��*8�v
�z,�
�3��%�&�&�'�z,�z=
�@�s��(
��z���C�H�H�T�V����W-� ��O� ���!� ��!��0;s0�L'�
L5�
M�$M�'L2�1L2�5M�?M