o
    w7e~                     @   s  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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mZmZmZmZmZmZ ddlZdd	lmZmZ dd
lmZ ddl m!Z!m"Z" ddl#m$Z$m%Z%m&Z& z
ddl'm(Z) e)Z(W n e*y   e+Z(Y nw e,e-Z.dZ/dZ0dZ1de0ide1idZ2ddddZ3dZ4dZ5dZ6dZ7dZ8dZ9dZ:dZ;e<dd e!= D Z>erddlZdd l?m@Z@mAZA G d!d" d"eAZBneCZBG d#d$ d$e(ZDG d%d& d&eZEeeE ZFG d'd( d(eZGdd)d*d+eFd,eeH d-eHd.eHfd/d0ZIG d1d2 d2eJZKd3d4 ZL	dd6eCd.eHfd7d8ZMeDfdeNd9eCd:eHd;ee+ fd<d=ZOeeOdd>ZPeeOd?d>ZQeDfd;ee+ fd@dAZReDfd;ee+ fdBdCZSdDdE ZTdd9eCfdGdHZU		I	5	5	F	5dd6eCd9eeC dJeHdKeNdLeNdMeNdNeNfdOdPZVG dQdR dRZWdddSdTeCdUeHdVeCdWeeF dXeeF d.eeHeXf fdYdZZYd[eHd.eeE fd\d]ZZ	dd^eHd[eHd_eHd.eGfd`daZ[	5	dd^eHd9eCdbeNd_eHfdcddZ\dedf Z]dgdh Z^dieCdjeHd.eeH fdkdlZ_dieCdmeCd.eHfdndoZ`d.eHfdpdqZadreCdmeCfdsdtZbdreCfdudvZcdweCd.eHfdxdyZddd9eCdmeCd.eHfd{d|Zed}eBd.eHfd~dZfd}eBd.eHfddZgdd}eBd9eeC d.eHfddZhd.eCfddZidejd.eHfddZkd.eHfddZlddJeHd.eCfddZmdddZndd Zodd Zpe-dkrleqep  dS dS )zFschema.py: Set of module functions for processing cloud-config schema.    N)defaultdict)Iterable)deepcopy)partial)chain)TYPE_CHECKINGList
NamedTupleOptionalTypeUnioncast)importersafeyaml)read_cfg_paths)INCLUSION_TYPES_MAPtype_from_starts_with)errorget_modules_from_dir	load_fileValidationErrorz!versions.schema.cloud-config.jsonzschema-cloud-config-v1.jsonzschema-network-config-v1.jsonlatest)cloud-configznetwork-configtruefalsenull)TFNap  
{name}
{title_underbar}

{title}

.. tab-set::

{prefix3}.. tab-item:: Summary

{description}

{prefix6}**Internal name:** ``{id}``

{prefix6}**Module frequency:** {frequency}

{prefix6}**Supported distros:** {distros}

{prefix6}{activate_by_schema_keys}

{prefix3}.. tab-item:: Config schema

{property_doc}

{prefix3}.. tab-item:: Examples

{prefix6}::

{examples}
 z5{prefix}* **{prop_name}:** ({prop_type}){description}zJ{prefix}* Each object in **{prop_name}** list supports the following keys:z&
   # --- Example{example_count} ---


deprecatedzDEPRECATED: c                 C   s   g | ]
}|d  dkr|qS )r   # ).0tr    r    9/usr/lib/python3/dist-packages/cloudinit/config/schema.py
<listcomp>e   s    r$   )NotRequired	TypedDictc                   @   sb   e Zd ZU eed< eed< eed< eed< eje ed< eje ed< eed< eee  ed< d	S )

MetaSchemanameidtitledescriptiondistrosexamples	frequencyactivate_by_schema_keysN)__name__
__module____qualname__str__annotations__typingr   r%   r    r    r    r#   r'   o   s   
 r'   c                   @   s   e Zd ZdS )SchemaDeprecationErrorN)r0   r1   r2   r    r    r    r#   r6   }   s    r6   c                   @   s,   e Zd ZU eed< eed< defddZdS )SchemaProblempathmessagereturnc                 C   s   | j  d| j S )N: )r8   r9   selfr    r    r#   format   s   zSchemaProblem.formatN)r0   r1   r2   r3   r4   r>   r    r    r    r#   r7      s   
 r7   c                   @   s   e Zd ZU eed< eed< dS )UserDataTypeAndDecodedContentuserdata_typecontentN)r0   r1   r2   r3   r4   r    r    r    r#   r?      s   
 r?   , prefix	separatorschema_problemsrD   rE   r:   c                C   s(   | tdd | }|r| | }|S )Nc                 S   s   |   S N)r>   )pr    r    r#   <lambda>   s    z)_format_schema_problems.<locals>.<lambda>)joinmap)rF   rD   rE   	formattedr    r    r#   _format_schema_problems   s   rM   c                       sF   e Zd ZdZ		d
dee dee f fddZdefdd	Z  Z	S )SchemaValidationErrorz<Raised when validating a cloud-config file against a schema.Nschema_errorsschema_deprecationsc                    s<   d  fdd}||dd| _ ||dd| _t   dS )zInit 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),)
        r   c                    s8   | s| S  r
 d7  t tt| }  t| |d7  | S )N

rD   )sortedlistsetrM   )problemsrD   r9   r    r#   handle_problems   s   z7SchemaValidationError.__init__.<locals>.handle_problemszCloud config schema errors: rR   "Cloud config schema deprecations: N)rO   rP   super__init__)r=   rO   rP   rX   	__class__rW   r#   r[      s   
zSchemaValidationError.__init__r:   c                 C   s
   t | jS rG   )boolrO   r<   r    r    r#   
has_errors   s   
z SchemaValidationError.has_errors)NN)
r0   r1   r2   __doc__r
   SchemaProblemsr[   r^   r_   __classcell__r    r    r\   r#   rN      s    "rN   c                 C   s@   zddl m} W n
 ty   Y dS w |j|dpt|tfS )zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r   )Draft4ValidatorFstring)
jsonschemarc   ImportErrorTYPE_CHECKERis_type
isinstancebytes)checkerinstancerc   r    r    r#   is_schema_byte_string   s   
rm   Fconfigc                    sP   dt f fdd}|r|ng d}dt||}dd}| |  S )zcombine 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                    sr    |  sdS  |  dd} |  dd|  d}|   d| d| } r1d| S d	|  d
S )Nr   _description_versionz	<missing z'_version key, please file a bug report>z in version .  z

**)get
capitalizestrip)ro   key_descriptionvmsgannotatern   r    r#   format_message   s   

z:_add_deprecated_changed_or_new_msg.<locals>.format_message)r   changednewr   r+   )r3   rJ   rK   ru   rstrip)rn   r|   
filter_keyr}   filter_keyschanged_new_deprecatedr+   r    r{   r#   "_add_deprecated_changed_or_new_msg   s   	r   schemar   
error_typec                 c   s(    |rt |d|gd}||V  dS dS )zJsonschema validator for `deprecated` items.

    It raises a instance of `error_type` if deprecated that must be handled,
    otherwise the instance is consider faulty.
    T)r|   r   N)r   )
_validatorr   	_instancer   r   r   rz   r    r    r#   r      s   r   )r   r~   c                 #   s    ddl m} ddlm} g }g }d}	t|D ]P\}
}t| j|||
d}tt fdd|}tt fdd|}|sD||  n5d	|v rbd
|	ddv rbd|d	  |d v rbd}	|E dH  || q|	so||V  |d|f |dV  |E dH  dS )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   r   
best_matchFschema_pathc                       t |   S rG   ri   er   r    r#   rI   7      z_anyOf.<locals>.<lambda>c                    
   t |  S rG   r   r   r   r    r#   rI   9     
 type
anyOf_type$refr   anyOf_type_TN.%r is not valid under any of the given schemascontext)
re   r   jsonschema.exceptionsr   	enumeraterT   descendfilterextendru   )	validatoranyOfrl   _schemar   r   r   
all_errorsall_deprecationsskip_best_matchindex	subschemaall_errserrsdeprecationsr    r   r#   _anyOf  s:   


r   c                 #   s   ddl m} ddlm} t|}g }g }	|D ]4\}
}tj||
d}tt fdd|}tt fdd|}|sF|}|	|  n|| q||V  |df |d	V  fd
d|D }|r|	| d
dd |D }|d|f V  dS |	E dH  dS )zJsonschema 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   r   r   r   c                    r   rG   r   r   r   r    r#   rI   e  r   z_oneOf.<locals>.<lambda>c                    r   rG   r   r   r   r    r#   rI   g  r   r   r   c                    s    g | ]\}}  |r|qS r    )is_valid)r!   is)rl   r   r    r#   r$   u  s     z_oneOf.<locals>.<listcomp>rB   c                 s   s    | ]}t |V  qd S rG   )repr)r!   r   r    r    r#   	<genexpr>x      z_oneOf.<locals>.<genexpr>z%r is valid under each of %sN)re   r   r   r   r   rT   r   r   r   appendrJ   )r   oneOfrl   r   r   r   r   
subschemasr   r   r   r   r   r   r   first_valid
more_validreprsr    )r   rl   r   r#   _oneOfO  s@   




r   c            
      C   s   ddl m} m} ddlm} t| j}ddi|d d< i }t| dr2d	|d
< | j	dt
}d|i}n| j}ttf|d< d|i}t| j}t|t< t|d< t|d< t|d< |d||dd|}ddd}	|	|_||fS )zGet 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   )rc   FormatChecker)creater   rd   
propertieslabelrg   FadditionalPropertiestype_checkerdefault_typesr~   r   r   draft4)meta_schema
validatorsversionNc                 [   s$   t dd | ||}t|ddu S )zgOverride version of `is_valid`.

        It does ignore instances of `SchemaDeprecationError`.
        c                 S   s   t | t S rG   )ri   r6   r   r    r    r#   rI     s    z<get_jsonschema_validator.<locals>.is_valid.<locals>.<lambda>N)r   iter_errorsnext)r=   rl   r   __errorsr    r    r#   r     s
   
z*get_jsonschema_validator.<locals>.is_validr    rG   )re   rc   r   jsonschema.validatorsr   r   META_SCHEMAhasattrrg   redefinerm   DEFAULT_TYPESr3   rj   dict
VALIDATORS_validator_deprecatedDEPRECATED_KEY_validator_changedr   r   r   )
rc   r   r   r   validator_kwargsr   typesr   cloudinitValidatorr   r    r    r#   get_jsonschema_validator  s<   



r   Tc              
   C   s|   ddl m} z| | W dS  |y= } z#|r,ttddd |jD |jgd|t	d| W Y d}~dS d}~ww )	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.c                 S      g | ]}t |qS r    r3   r!   rH   r    r    r#   r$         z3validate_cloudconfig_metaschema.<locals>.<listcomp>rO   zGMeta-schema validation failed, attempting to validate config anyway: %sN)
r   r   check_schemarN   r7   rJ   r8   r9   LOGwarning)r   r   throwr   errr    r    r#   validate_cloudconfig_metaschema  s&   r   r   schema_typestrictstrict_metaschemalog_detailslog_deprecationsc                 C   s&  |du rt |}zt \}}|rt||dd W n ty'   td Y dS w ||| d}	g }
g }t|	| dd dD ]"}d	d
d |j	D }t
||jf}t|tr\||7 }q>|
|7 }
q>|rq|rqt|ddd}t| |r||
sw|r|t|
||
r|rt|
ddd}nd}t| dS dS )a  Validate 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 string. One of: cloud-config, network-config
       Default: 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
    NF)r   z5Ignoring schema validation. jsonschema is not present)format_checkerc                 S   s   | j S rG   )r8   r   r    r    r#   rI   !  s    z-validate_cloudconfig_schema.<locals>.<lambda>ro   r   c                 S   r   r    r   r   r    r    r#   r$   #  r   z/validate_cloudconfig_schema.<locals>.<listcomp>z"Deprecated cloud-config provided:

rC   zInvalid cloud-config provided:
zeInvalid cloud-config provided: Please run 'sudo cloud-init schema --system' to see the schema errors.)
get_schemar   r   rf   r   debugrS   r   rJ   r8   r7   r9   ri   r6   rM   r   rN   r   )rn   r   r   r   r   r   r   r   r   r   r   r   schema_errorr8   problemr9   detailsr    r    r#   validate_cloudconfig_schema  s\   !






r   c                   @   s   e Zd ZdededefddZededee defd	d
Zde	fddZ
e	d dee dee dee dededefddZdee dededee fddZde	de	defddZdS )!
_Annotatorcloudconfigoriginal_contentschemamarksc                 C   s   || _ || _|| _d S rG   )_cloudconfig_original_content_schemamarks)r=   r   r   r   r    r    r#   r[   H  s   
z_Annotator.__init__r*   rA   r:   c                 C   s   d |}d|  d| dS )Nr   # z: -------------
rQ   )rJ   )r*   rA   bodyr    r    r#   _build_footerR  s   
z_Annotator._build_footerrF   c                 C   sz   t t}|D ]4\}}td|}|r"| \}}|t| | nd }|| j|  | |d ur:dj|||d}q|S )Nz&format-l(?P<line>\d+)\.c(?P<col>\d+).*zLine {line} column {col}: {msg})linecolrz   )	r   rT   rematchgroupsintr   r   r>   )r=   rF   errors_by_liner8   rz   r   r   r   r    r    r#   _build_errors_by_lineW  s   z _Annotator._build_errors_by_liner   rV   labelsfooterr   label_prefixc                 C   sB   | D ]}| | }| | | d| d|  |d7 }q|S )Nr   r;      )r   )rV   r  r  r   r  r   r   r    r    r#   _add_problemsg  s   

z_Annotator._add_problemslinesr  deprecations_by_linec              	      s   g }g }g }d}d}t |dD ]8\}	}
||	 }||	 }|s|rBg } j||||dd} j||||dd}||
d d|  q||
 q|t fddtd	d d
|fd|ff |S )Nr  E)r  Dz		# ,c                    s
    j |  S rG   )r   seqr<   r    r#   rI     r   z._Annotator._annotate_content.<locals>.<lambda>c                 S   s   t | d S )Nr  )r^   r  r    r    r#   rI     r   ErrorsDeprecations)r   r  r   rJ   r   rK   r   )r=   r	  r  r
  annotated_contenterror_footerdeprecation_footererror_indexdeprecation_indexline_numberr   r   r   r  r    r<   r#   _annotate_contentv  sD   

z_Annotator._annotate_contentrO   rP   c                 C   sl   |s|s| j S | j d}t| jts d|| ddgg S | |}| |}| |||}d|S )Nr   r  z&# E1: Cloud-config is not a YAML dict.)	r   splitri   r   r   rJ   r   r  r  )r=   rO   rP   r	  r  r
  r  r    r    r#   r|     s$   


z_Annotator.annotateN)r   )r0   r1   r2   r   r3   r[   staticmethodr   r   ra   r  r  r  r  r|   r    r    r    r#   r   G  sT    


,r   rO   rP   r   r   r   rO   rP   c                C   s   t | |||p	g |pg 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 unparseable.
    @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
    )r   r|   )r   r   r   rO   rP   r    r    r#   annotated_cloudconfig_file  s   r  rA   c              	   C   sb   ddl m} || vrg S g }t|  dD ]\}}||r.|td| d||d q|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 hanlder.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_PREFIXr  zformat-lz.c1zIgnored invalid user-data: )cloudinit.handlers.cloud_configr  r   
splitlines
startswithr   r7   replace)rA   r  r   line_numr   r    r    r#   )process_merged_cloud_config_part_problems  s"   

	r#  config_pathinstance_data_pathc                 C   s   ddl m}m}m} t|}d}|dkrQz||| |}W n. |y1 } z	tt|dg|d}~w |yJ } ztt|dd W Y d}~nd}~ww d	}t|}|so|	d
\}	}
}
tt|d|  d|	 dd
t g|dkr{td| d t||S )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.
    r   )JinjaLoadErrorNotJinjaErrorrender_jinja_payload_from_filezformat-l1.c1ztext/jinja2zRDetected type '{user_data_type}' from header. But, content is not a jinja templateNTsys_exitzformat-l2.c1r   z!Unrecognized user-data header in z: "z%".
Expected first line to be one of: rB   text/cloud-configzUser-data type 'z.' not currently evaluated by cloud-init schema)!cloudinit.handlers.jinja_templater&  r'  r(  r   rN   r7   r   r3   	partitionrJ   USERDATA_VALID_HEADERSprintr?   )r$  rA   r%  r&  r'  r(  user_data_typeschema_positionr   header_line_r    r    r#   &_get_config_type_and_rendered_userdata  sV   	


r4  r|   c                 C   s  t | t| dd|}|jdkrdS |j}t|}z|r#t|\}}nt|}i }W ne tj	y }	 zXd }
}d}t
|	drJt|	drJt|	d}nt
|	drYt|	drYt|	d}|re|jd }
|jd }|tdj|
|d	d
| t|	 t|}|rtti |i |jd ||	d}	~	ww t|ts|stdzt||ddd W dS  ty }	 z6|	 r||	j7 }|rtt|||||	jd n|	jrt|	jddd}t| |rt|d|	W Y d}	~	dS d}	~	ww )ay  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 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.

    @raises SchemaValidationError containing any of schema_errors encountered.
    @raises RuntimeError when config_path does not exist.
    T)decoder+  Nr  context_markproblem_markzformat-l{line}.c{col})r   r   zFile {0} is not valid yaml. {1}r   z Cloud-config is not a YAML dict.F)r   r   r  rY   rB   rC   )r4  r   r@   rA   r#  r   load_with_marksloadyaml	YAMLErrorr   getattrr   columnr   r7   r>   r3   rN   r/  r  rO   ri   r   RuntimeErrorr   r_   rP   rM   )r$  r   r|   r%  decoded_userdatarA   r   r   marksr   r   r=  markr   r9   r    r    r#   validate_cloudconfig_file/  s   







	rB  c                 C   s   | dkrdS | dkrdS dS )zProvide a sorting weight for documentation of property types.

    Weight values ensure 'array' sorted after 'object' which is sorted
    after anything else which remains unsorted.
    array   objectr  r   r    )valuer    r    r#   _sort_property_order  s
   rG  c                 c   s>    | D ]}t |trt |ttfst|E d H  q|V  qd S rG   )ri   r   r3   rj   _flatten)xsxr    r    r#   rH    s   rH  property_dict	multi_keyc                 C   sn   g }|  |i D ](}| trq| dr$|dd | dg D  q| dr0||d  qtt|S )Nenumc                 S       g | ]}d t || d qS ``	_YAML_MAPru   )r!   
enum_valuer    r    r#   r$     s    z,_collect_subschema_types.<locals>.<listcomp>r   )ru   r   r   r   rT   rH  )rK  rL  property_typesr   r    r    r#   _collect_subschema_types  s   



rU  defsc           
      C   sn  t | | | dg }t|ts|g}| dr"dd | d D }n| dr0|t| d n| dr=|t| d t|dkrH|d }n|jtd	 d
	|}| di }|dg }t|tsg|g}t
|}t|di |di D ]}t||}|r|dkrqx|| qx|rt|dkr| d|d  S |jtd	 dd
	| d}	| d|	 S |pdS )zNReturn a string representing a property type from a given
    jsonschema.
    r   rM  c                 S   rN  rO  rQ  r!   kr    r    r#   r$     s    z&_get_property_type.<locals>.<listcomp>r   r   r  r   r   /items	UNDEFINEDz of ())_flatten_schema_refsru   ri   rT   r   rU  lensortrG  rJ   r^   r   _get_property_typer   )
rK  rV  rT  property_typerZ  sub_property_typesprune_undefinedsub_itemsub_typesub_property_docr    r    r#   ra    sB   








ra  c                 C   s>   |}t dd| } t dd|| } t dd|| } | S )a  Parse description from the meta in a format that we can better
    display in our docs. This parser does three things:

    - Guarantee that a paragraph will be in a single line
    - Guarantee that each new paragraph will be aligned with
      the first paragraph
    - Proper align lists of items

    @param description: The original description in the meta.
    @param prefix: The number of spaces used to align the current description
    z
(\S)\n(\S)z\1 \2z\n\nz\n\n{}z\n( +)-z\n{}-)r   subr>   )r+   rD   list_paragraphr    r    r#   _parse_description  s   rj  src_cfgc                 C   s   d| v r|  ddd}| ||  d| v rUd| d v r2| d  ddd}| d ||  d| d v rU| d d D ]}d|v rT| ddd}|||  q>t| dg | dg | dg D ]}d|v r}| ddd}|||  qgdS )	zEFlatten schema: replace $refs in src_cfg with definitions from $defs.r   z#/$defs/r   rZ  r   r   allOfN)popr!  updater   ru   )rk  rV  	reference
sub_schemar    r    r#   r^    s.   


r^  c                 C   s,   |  dd}|s
dS |D ]}| | qdS )zFlatten schema: Merge allOf.

    If a schema as allOf, then all of the sub-schemas must hold. Therefore
    it is safe to merge them.
    rl  N)rm  rn  )rk  sub_schemasrp  r    r    r#   _flatten_schema_all_of	  s   rr  prop_configc                 C   s   dt dtdtfdd}| di }| di }g }g }|| || t||D ]}|||| q&dt||}|r>d	| }|S )
aG  Return accumulated property description.

    Account for the following keys:
    - top-level description key
    - any description key present in each subitem under anyOf or allOf

    Order and deprecated property description after active descriptions.
    Add a trailing stop "." to any description not ending with ":".
    rn   descriptionsdeprecated_descriptionsc                 S   sB   t t| jdr|t|  d S | dr|t|  d S d S )N)deprecated_versionchanged_versionnew_versionr+   )anyrK   ru   r   r   )rn   rt  ru  r    r    r#   assign_descriptions!  s   
z6_get_property_description.<locals>.assign_descriptionsr   r   rr   rs   )r   rT   ru   r   rJ   )rs  rz  r   r   rt  ru  re  r+   r    r    r#   _get_property_description  s$   

r{     c                    s  |d }g }  ddu rdS  fdddD } fdd|D }|D ]}| D ]\}}	t|	| t|	 |	 ddu r>q)t|	}
|	 d	|}|tj||t|
|d t	|	|d
 |	 d}|rt|| | dsr| dr|t
j||d |t|||d | dg D ] }| ds| dr|t
j||d |t|||d qd|	v sd|	v r|t|	||d q)q#d|S )zDReturn restructured text describing the supported schema properties.  hiddenTr   c                    s$   g | ]}d  vs| d  vr|qS )r~  r    r!   ro   r   r    r#   r$   F  s
    z%_get_property_doc.<locals>.<listcomp>)r   patternPropertiesc                    s   g | ]}  |i qS r    )ru   r  r  r    r#   r$   K  s    r   )rD   	prop_namer+   	prop_typerZ  r   r  )rD   r  rV  rD   r   rQ   )ru   rZ  r^  rr  r{  r   SCHEMA_PROPERTY_TMPLr>   rj  ra  SCHEMA_LIST_ITEM_TMPL_get_property_docrJ   )r   rV  rD   
new_prefixr   property_keysproperty_schemasprop_schemaprop_keyrs  r+   r   rZ  
alt_schemar    r  r#   r  @  sz   




5r  metac                 C   s^   |  d}|s	dS t}t|dD ]\}}|tj|d7 }t|dd}|d|7 }q|S )zAReturn restructured text describing the meta examples if present.r-   r   r  )example_countr|  r   )	ru   SCHEMA_EXAMPLES_HEADERr   SCHEMA_EXAMPLES_SPACER_TEMPLATEr>   textwrapindentr  rJ   )r  r-   rst_contentcountexampleindented_linesr    r    r#   _get_examples  s   
r  c                 C   s2   |  dsdS ddd | d D }d| dS )Nr/   r   rB   c                 s   s    | ]	}d | d V  qdS )rP  Nr    rW  r    r    r#   r     s    
z3_get_activate_by_schema_keys_doc.<locals>.<genexpr>z**Activate only on keys:** rQ   )ru   rJ   )r  schema_keysr    r    r#    _get_activate_by_schema_keys_doc  s   

r  c           	      C   s  |du r	t dd}| r|stdt|  }h d}dh}d}|| r,d|| }n|| | r9d	|| }|r?t|tt| }d|d
< d|d< d|d< t	t
t|d d|d< |di }|| d rx|| d i }t
t|}zt||dd|d< W n ty   td d|d< Y nw |d sd|d< t	t| d|d< |d sd|d< d| d |d< tdd| d |d< t| |d< tjdi |}|S )zReturn reStructured text rendering the provided metadata.

    @param meta: Dict of metadata to render.
    @param schema: Optional module schema, if absent, read global schema.
    @raise KeyError: If metadata lacks an expected key.
    Nr   r   z"Expected non-empty meta and schema>   r)   r(   r*   r,   r-   r.   r+   r/   r   z(Missing required keys in module meta: {}z3Additional unexpected keys found in module meta: {}property_headerz      prefix6r|  prefix3r+   z$defsr)   r  property_docz3Unable to render property_doc due to invalid schemaz+      No schema definitions for this moduler-   z$         No examples for this modulerB   r,   r   -r(   title_underbarr    )r   
ValueErrorrU   keysr>   KeyErrorr   r   r  r  r   r3   ru   r  AttributeErrorr   r   r  rJ   r   rh  r  SCHEMA_DOC_TMPL)	r  r   r  required_keysoptional_keyserror_message	meta_copyrV  templater    r    r#   get_meta_doc  sj   
	

r  c                  C   s   t jt jt} t| S rG   )osr8   dirnameabspath__file__r   )configs_dirr    r    r#   get_modules  s   r  requested_modulesc                 C   s   d}t t  dg }t| t|}|r&tdt |d|dd |D ]%}d| v s2|| v rMt	|dgdg\}}|rMt
|d	 }||jpKd7 }q(|S )
zaLoad module docstrings

    Docstrings are generated on module load. Reduce, reuse, recycle.
    r   allz+Invalid --docs value {}. Must be one of: {}rB   Tr)  zcloudinit.configr  r   )rT   r  valuesrU   
differencer   r>   rJ   r   find_moduleimport_moduler`   )r  docsall_modulesinvalid_docsmod_namemod_locsr3  modr    r    r#   load_doc  s*   
r  c                   C   s   t jt jt jtdS )Nschemas)r  r8   rJ   r  r  r  r    r    r    r#   get_schema_dir  s   r  c              	   C   sZ   t jt t|  d }d}z
tt|}W |S  tt	fy,   t
d| | i  Y S w )ziReturn jsonschema for a specific type.

    Return empty schema when no specific schema file exists.
    r   Nz;Skipping %s schema valiation. No JSON schema file found %s.)r  r8   rJ   r  SCHEMA_FILES_BY_TYPEjsonloadsr   IOErrorOSErrorr   r   )r   schema_filefull_schemar    r    r#   r     s   r   c                 C   s|   | s	t jddd} | jdddd | jdd	td
t d d | jddddd | jddddd | jddddd | S )z0Return a parser for supported cmdline arguments.cloudconfig-schemaz.Validate cloud-config files or document schema)progr+   z-cz--config-filez.Path of the cloud-config yaml file to validate)helpz-iz--instance-datazbPath to instance-data.json file for variable expansion of '##template: jinja' user-data. Default: instance_data)r   r  z--system
store_trueFz)Validate the system cloud-config userdata)actiondefaultr  z-dz--docs+zCPrint schema module docs. Choices: all or space-delimited cc_names.)nargsr  z
--annotatez/Annotate existing cloud-config file with errors)argparseArgumentParseradd_argumentr3   r   get_runpathparserr    r    r#   
get_parser   sJ   

	r  c                 C   s  |j |j|jg}tdd |D dkrtddd |jr%|jr%tddd tdd	}|jr6tt|j d
S t	dd}|j
rB|j
}nt dkrN|d}n|d}|j r]d|j ff}nDt dkritddd |d}|sxtddd d
S d|ff}d|dfd|dff}|D ]\}	}
|
rtj|
r||	|
ff7 }qtj|d d std|d d  dddd d}tt|dk}|rtdddd  |D   d!}g }t|dD ]\}\}	}|rtd"| d#|	 d$| d% z
t|||j| W nY ty, } z"|js"t| d&|  tt||d' d( ||	 W Y d
}~qd
}~w tyV } zt| d&|	  tt||d' d( ||	 W Y d
}~qd
}~ww |j r]|n|	}t| d)|  q|r}tdd*d  |D d+dd d
S d
S ),z@Handle provided schema args and perform the appropriate actions.c                 S   s   g | ]}|r|qS r    r    )r!   argr    r    r#   r$   Q  r   z&handle_schema_args.<locals>.<listcomp>r  z;Expected one of --config-file, --system or --docs argumentsTr)  z;Invalid flag combination. Cannot use --annotate with --docsr   r  Ntrust)fetch_existing_datasourcer   r  instance_data_sensitivez	user-datazNUnable to read system userdata or vendordata as non-root user. Try using sudo.cloud_configz;Unable to obtain user data file. No instance data availablezvendor-datavendor_cloud_configzvendor2-datavendor2_cloud_configzConfig file z does not existz	Error: {})fmtr*  r   z!Found cloud-config data types: %srB   c                 s   s    | ]\}}|V  qd S rG   r    )r!   cfg_typer3  r    r    r#   r     r   z%handle_schema_args.<locals>.<genexpr>r}  r   rr   z at :zInvalid cloud-config z
Error: {}
)r  zValid cloud-config: c                 s   s    | ]}|V  qd S rG   r    )r!   r   r    r    r#   r     s    z'Error: Invalid cloud-config schema: {}
)config_filer  systemr_  r   r|   r   r/  r  r   r  r  getuidr  	get_ipathr8   existsr^   rJ   r   rB  rN   r3   r   r>  )r(   argsexclusive_argsr  pathsr%  config_filesuserdata_filevendor_config_filesr  vendor_filenested_output_prefixmulti_config_outputerror_typesidxcfg_filer   cfgr    r    r#   handle_schema_argsN  s   







r  c                  C   s   t  } td|   dS )zDTool to validate schema of a cloud-config file or print schema docs.r  r   )r  r  
parse_argsr  r    r    r#   main  s   r  __main__)FN)T)Nr   FFTFrG   )r|  )r   )rr`   r  r  loggingr  r   sysr  collectionsr   collections.abcr   copyr   	functoolsr   	itertoolsr   r5   r   r   r	   r
   r   r   r   r:  	cloudinitr   r   cloudinit.cmd.develr   cloudinit.handlersr   r   cloudinit.utilr   r   r   re   r   _ValidationErrorrf   	Exception	getLoggerr0   r   VERSIONED_USERDATA_SCHEMA_FILEUSERDATA_SCHEMA_FILENETWORK_CONFIG_V1_SCHEMA_FILEr  rR  r  SCHEMA_PROPERTY_HEADERr  r  r  r  r   DEPRECATED_PREFIXrS   r  r.  typing_extensionsr%   r&   r'   r   r6   r7   ra   r?   r3   rM   r  rN   rm   r   r^   r   r   r   r   r   r   r   r   r   rj   r  r#  r4  rB  rG  rH  rU  ra  rj  r^  rr  r{  r  r  r  r  r  rT   r  r  r   r  r  r  exitr    r    r    r#   <module>   sl  $
	
)
)

E
1L%
Xz


!
@
X,*F	I
._
