U
    bÈk  ã                   @   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mZ ddl	m
Z
 ddlm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mZ eed
d�Ze e¡ZddddœZdZdZdZdZdZ dZ!G dd„ de"ƒZ#dd„ Z$dd„ Z%dIe&dœdd„Z'dJe&e&e(e(d œd!d"„Z)d#d$„ Z*dKd%d&„Z+d'd(„ Z,e&e-d)œd*d+„Z.e-d,œd-d.„Z/dLe&e&e-d0œd1d2„Z0ee-d3œd4d5„Z1dMee&e-d6œd7d8„Z2e&d,œd9d:„Z3e4e-d;œd<d=„Z5e&d,œd>d?„Z6e&d,œd@dA„Z7dNdBdC„Z8dDdE„ Z9dFdG„ Z:edHk�ròe ;e:ƒ ¡ dS )OzFschema.py: Set of module functions for processing cloud-config schema.é    N)Údefaultdict)Údeepcopy)Úpartial)Úimporter)Úread_cfg_paths)Ú
MetaSchema)ÚerrorÚfind_modulesÚ	load_fileT)Zsys_exitÚtrueZfalseZnull)TFNs   #cloud-configzÍ
{name}
{title_underbar}
**Summary:** {title}

{description}

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

**Module frequency:** {frequency}

**Supported distros:** {distros}

**Config schema**:
{property_doc}
{examples}
z4{prefix}**{prop_name}:** ({prop_type}) {description}zF{prefix}Each item in **{prop_name}** list supports the following keys:z
**Examples**::

z
    # --- Example{0} ---c                       s"   e Zd ZdZd‡ fdd„	Z‡  ZS )ÚSchemaValidationErrorz<Raised when validating a cloud-config file against a schema.© c                    s8   || _ dd„ |D ƒ}d d |¡¡}tt| ƒ |¡ dS )z—Init the exception an n-tuple of schema errors.

        @param schema_errors: An n-tuple of the format:
            ((flat.config.key, msg),)
        c                 S   s   g | ]\}}d   ||¡‘qS ©z{0}: {1}©Úformat)Ú.0Z
config_keyÚmessager   r   ú9/usr/lib/python3/dist-packages/cloudinit/config/schema.pyÚ
<listcomp>=   s   ÿz2SchemaValidationError.__init__.<locals>.<listcomp>zCloud config schema errors: {0}ú, N)Úschema_errorsr   ÚjoinÚsuperr   Ú__init__)Úselfr   Zerror_messagesr   ©Ú	__class__r   r   r   6   s    þÿzSchemaValidationError.__init__)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Ú__classcell__r   r   r   r   r   3   s   r   c                 C   sB   zddl m} W n tk
r&   Y dS X |j |d¡p@t|tfƒS )zWTYPE_CHECKER override allowing bytes for string type

    For jsonschema v. 3.0.0+
    r   )ÚDraft4ValidatorFÚstring)Ú
jsonschemar"   ÚImportErrorÚTYPE_CHECKERZis_typeÚ
isinstanceÚbytes)ZcheckerÚinstancer"   r   r   r   Úis_schema_byte_stringG   s     ÿ
þr*   c                  C   s–   ddl m} m} ddlm} t| jƒ}d|d< ddi|d d	< t| d
ƒrj| j 	dt
¡}||| jd|d�}n$| j}ttf|d< ||| jd|d�}||fS )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)ÚcreateFZadditionalPropertiesÚtyper#   Ú
propertiesÚlabelr&   Zdraft4)Úmeta_schemaÚ
validatorsÚversionÚtype_checker)r0   r1   r2   Zdefault_types)r$   r"   r+   Zjsonschema.validatorsr,   r   ZMETA_SCHEMAÚhasattrr&   Zredefiner*   Z
VALIDATORSZDEFAULT_TYPESÚstrr(   )r"   r+   r,   Ústrict_metaschemar3   ÚcloudinitValidatorÚtypesr   r   r   Úget_jsonschema_validatorU   s2    

 ÿüür9   )Úschemac              
   C   sx   ddl m} z|  |¡ W nX |k
rr } z:|rVtd dd„ |jD ƒ¡|jffd�|‚t d|¡ W 5 d}~X Y nX dS )	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   s   g | ]}t |ƒ‘qS r   ©r5   ©r   Úpr   r   r   r   Ÿ   s     z3validate_cloudconfig_metaschema.<locals>.<listcomp>)r   zGMeta-schema validation failed, attempting to validate config anyway: %sN)	Zjsonschema.exceptionsr;   Zcheck_schemar   r   Úpathr   ÚLOGÚwarning)Ú	validatorr:   Úthrowr;   Úerrr   r   r   Úvalidate_cloudconfig_metaschemaˆ   s    ÿÿüýrF   F)Úconfigr:   Ústrictr6   c                 C   sÚ   |dkrt ƒ }z tƒ \}}|r,t||dd� W n  tk
rN   t d¡ Y dS X |||ƒ d�}d}t| | ¡dd„ d	�D ]*}d
 dd„ |j	D ƒ¡}	||	|j
ff7 }qx|rÖ|r¶t|ƒ‚n dd„ |D ƒ}
t dd |
¡¡ 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 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)

    @raises: SchemaValidationError when provided config does not validate
        against the provided schema.
    @raises: RuntimeError when provided config sourced from YAML is not a dict.
    NF)rD   z5Ignoring schema validation. jsonschema is not present)Zformat_checkerr   c                 S   s   | j S ©N)r@   )Úer   r   r   Ú<lambda>Í   ó    z-validate_cloudconfig_schema.<locals>.<lambda>)Úkeyr<   c                 S   s   g | ]}t |ƒ‘qS r   r=   r>   r   r   r   r   Î   s     z/validate_cloudconfig_schema.<locals>.<listcomp>c                 S   s   g | ]\}}d   ||¡‘qS r   r   )r   ÚkÚmsgr   r   r   r   Ô   s     z!Invalid cloud-config provided:
%sÚ
)Ú
get_schemar9   rF   r%   rA   ÚdebugÚsortedZiter_errorsr   r@   r   r   rB   )rG   r:   rH   r6   r7   r+   rC   Úerrorsr   r@   Zmessagesr   r   r   Úvalidate_cloudconfig_schema©   s4    
  ÿ


 ÿrU   c                 C   sj  |s|S i }t tƒ}g }d}g }| ¡  d¡}t| tƒsNd || d¡g ¡S | r\t| |ƒ}|D ]f\}	}
t	 
d|	¡}|r˜| ¡ \}}|t|ƒ  |
¡ nd}|||	   |
¡ |dk	r`dj|||
d�}
q`d}t|dƒD ]r\}}|| }|�r>g }|D ].}| d	 |¡¡ | d
 ||¡¡ |d7 }qô| |d d |¡ ¡ qÖ| |¡ qÖ| | d |¡¡¡ d |¡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 schema_errors: List of tuples from a JSONSchemaValidationError. The
        tuples consist of (schemapath, error_message).
    z# Errors: -------------
{0}

rP   z&# E1: Cloud-config is not a YAML dict.z&format-l(?P<line>\d+)\.c(?P<col>\d+).*NzLine {line} column {col}: {msg})ÚlineÚcolrO   é   zE{0}z# E{0}: {1}z		# ú,)r   ÚlistÚdecodeÚsplitr'   Údictr   r   Ú_schemapath_for_cloudconfigÚreÚmatchÚgroupsÚintÚappendÚ	enumerate)ÚcloudconfigÚoriginal_contentr   ZschemapathsZerrors_by_lineZerror_footerZerror_headerZannotated_contentÚlinesr@   rO   r`   rV   rW   Zerror_indexÚline_numberrT   Zerror_labelr   r   r   r   Úannotated_cloudconfig_fileÚ   sZ    	

ÿÿ ÿ  ÿ
ri   c              
   C   sö  | dkr:t  ¡ dkrtdƒ‚tƒ }| d¡}t|dd�}n&t j | ¡sTtd | ¡ƒ‚t| dd�}| 	t
¡s¢dd	 | t
 ¡ ¡ff}t|ƒ}|ržtti ||jƒƒ |‚zt |¡}W nÔ tjk
�r„ }	 z²d
 }
}d}t|	dƒròt|	dƒròt|	dƒ}n"t|	dƒ�rt|	dƒ�rt|	dƒ}|�r.|jd
 }
|jd
 }dj|
|d�d | t|	ƒ¡ff}t|ƒ}|�rntti ||jƒƒ ||	‚W 5 d}	~	X Y nX t|tƒ�s |�s tdƒ‚zt||dd� W n> tk
�rð }	 z|�rÞtt|||	jƒƒ ‚ W 5 d}	~	X Y nX dS )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 annotate: Boolean set True to print original config file with error
        annotations on the offending lines.

    @raises SchemaValidationError containing any of schema_errors encountered.
    @raises RuntimeError when config_path does not exist.
    Nr   z?Unable to read system userdata as non-root user. Try using sudoZuserdata_rawF)r[   zConfigfile {0} does not existzformat-l1.c1z"File {0} needs to begin with "{1}"rX   Zcontext_markZproblem_markzformat-l{line}.c{col})rV   rW   zFile {0} is not valid yaml. {1}z Cloud-config is not a YAML dict.T)rH   )ÚosÚgetuidÚRuntimeErrorr   Zget_ipath_curr
   r@   Úexistsr   Ú
startswithÚCLOUD_CONFIG_HEADERr[   r   Úprintri   r   ÚyamlÚ	safe_loadZ	YAMLErrorr4   ÚgetattrrV   Úcolumnr5   r'   r]   rU   )Zconfig_pathr:   ÚannotateÚpathsZuser_data_fileZcontentrT   r   re   rJ   rV   rt   Zmarkr   r   r   Úvalidate_cloudconfig_file  sx    ÿ
ÿ
 ÿþÿ


þÿ  ÿÿrw   c                 C   s  |  ¡  d¡}i }d}d}g }| s&i S t|dƒD �]à\}}tt ||¡ ¡ d ƒ}	| ¡ }|r0| d¡rjq0|r||d \}
}nd}
d}| d¡�rd	|d  }|r¼| 	|¡r¼|d
t|ƒ … }t
|ƒ}tt ||dd
… ¡ ¡ d ƒ}|d7 }|	}
|	|7 }	||d
… }|d7 }nd}| dd¡\}}|�r>|	|
k�r>|d | }|	|
k�r˜|�rŒ| ¡ \}
}|dk�r”|	|
k�r”d | d¡d
d… ¡}�q˜nd}
d}�q>| |	|f¡ |�r
| ¡ }| d¡�r
| |	d |d f¡ tdtt |¡ƒƒD ]}|d t
|ƒ }|||< �qê|||< q0|S )zèReturn a dictionary mapping schemapath to original_content line number.

    @param config: The yaml.loaded config dictionary of a cloud-config file.
    @param original_content: The simple file content of the cloud-config file
    rP   r   z^(\s*)rX   ú#éÿÿÿÿÚ z- z.%dNú:r<   ú[é   z.0)r[   r\   rd   Úlenr_   r`   ra   Ústriprn   Úendswithr5   Úpopr   rc   Úrangerq   rr   )rG   rf   Zcontent_linesZschema_line_numbersZ
list_indexZRE_YAML_INDENTZscopesrh   rV   Zindent_depthZprevious_depthZpath_prefixZprevious_list_idxrM   Zitem_indentÚvalueZinner_list_indexZlist_keyr   r   r   r^   a  sb     


r^   )Úproperty_dictÚreturnc                 C   sÄ   |   d¡}|dkrN|   d¡r0dd„ | d D ƒ}n|   d¡rNdd„ |   d¡D ƒ}t|tƒrbd |¡}|   d	i ¡}|  dd
¡}|  di ¡D ]$}|r–|d7 }|dt|ƒ d 7 }q†|r¼d ||¡S |pÂdS )zNReturn a string representing a property type from a given
    jsonschema.
    r-   NÚenumc                 S   s   g | ]}t t ||¡ƒ‘qS r   )r5   Ú	_YAML_MAPÚget)r   rN   r   r   r   r   ¨  s    z&_get_property_type.<locals>.<listcomp>ZoneOfc                 S   s   g | ]}|  d ¡r|d  ‘qS )r-   )rˆ   )r   Z	subschemar   r   r   r   ¬  s   
þú/Úitemsrz   ú(ú)z
{0} of {1}Z	UNDEFINED)rˆ   r'   rZ   r   Ú_get_property_typer   )r„   Zproperty_typerŠ   Zsub_property_typeZsub_itemr   r   r   r�   ¡  s*    

ÿ
þ

r�   )r…   c                 C   sB   |d }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   )ÚdescriptionÚprefixZlist_paragraphr   r   r   Ú_parse_description¿  s      ÿr’   ú    )r:   Údefsr…   c              
   C   s`  |d }g }|   di ¡|   di ¡g}|D �]*}| ¡ D �]\}}d|krf||d  dd¡ }	| |	¡ |  dd¡}
|  d|¡}| tj||t|
|ƒt|ƒd	�¡ |  d
¡}|�r,t	|t
ƒrÚ|D ]}| t|||d�¡ q¾nRt	|tƒ�r,|  d¡sü|  d¡�r,| tj||d�¡ |d7 }| t|||d�¡ d|k�s>d|kr6| t|||d�¡ q6q(d |¡S )zDReturn restructured text describing the supported schema properties.r“   r.   ZpatternPropertiesz$refz#/$defs/rz   r�   r/   )r‘   Ú	prop_namer�   Z	prop_typerŠ   )r”   r‘   )r‘   r•   z

)rˆ   rŠ   ÚreplaceÚupdaterc   ÚSCHEMA_PROPERTY_TMPLr   r’   r�   r'   rZ   Ú_get_property_docr]   ÚSCHEMA_LIST_ITEM_TMPLr   )r:   r”   r‘   Z
new_prefixr.   Zproperty_keysZpropsZprop_keyZprop_configÚrefr�   r/   rŠ   Úitemr   r   r   r™   Õ  st    

þ

üÿ

  ÿÿÿÿ ÿÿÿÿþ  ÿÿr™   )Úmetar…   c                 C   sp   |   d¡}|sdS t}t|ƒD ]L\}}| d¡}dd„ |D ƒ}|tkr\| dt |d ¡¡ |d |¡7 }q|S )zAReturn restructured text describing the meta examples if present.Úexamplesrz   rP   c                 S   s   g | ]}d   |¡‘qS )z    {0}r   )r   rV   r   r   r   r     s     z!_get_examples.<locals>.<listcomp>r   rX   )rˆ   ÚSCHEMA_EXAMPLES_HEADERrd   r\   ÚinsertÚSCHEMA_EXAMPLES_SPACER_TEMPLATEr   r   )r�   rž   Zrst_contentÚcountZexamplerg   Zindented_linesr   r   r   Ú_get_examples  s    

 ÿr£   )r�   r:   r…   c                 C   s2  |dkrt ƒ }| r|stdƒ‚t|  ¡ ƒ}tddddddd	hƒ}d
}|| r\d || ¡}n|| rrd || ¡}|r~t|ƒ‚tt| ƒƒ}| di ¡}| | d ¡r²| | d ¡}zt	||d�|d< W n& t
k
rì   t d¡ d
|d< Y nX t| ƒ|d< d | d ¡|d< t dd| d	 ¡|d< tjf |Ž}|S )zîReturn 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.
    Nz"Expected non-empty meta and schemaÚidÚtitlerž   Z	frequencyZdistrosr�   Únamerz   z(Missing expected keys in module meta: {}z3Additional unexpected keys found in module meta: {}ú$defs)r”   Zproperty_docz3Unable to render property_doc due to invalid schemar   r<   ú-Ztitle_underbar)rQ   Ú
ValueErrorÚsetÚkeysr   ÚKeyErrorr]   r   rˆ   r™   ÚAttributeErrorrA   rB   r£   r   r_   r�   ÚSCHEMA_DOC_TMPL)r�   r:   r«   ZexpectedZerror_messageZ	meta_copyr”   Útemplater   r   r   Úget_meta_doc$  sR    ùÿÿÿÿ
r°   c                  C   s   t j t j t¡¡} t| ƒS rI   )rj   r@   ÚdirnameÚabspathÚ__file__r	   )Zconfigs_dirr   r   r   Úget_modules\  s    r´   )Úrequested_modulesr…   c                 C   sœ   d}t tƒ  ¡ ƒdg }t| ƒ t|ƒ¡}|rHtd t |ƒd |¡¡ƒ |D ]J}d| ks`|| krLt 	|dgdg¡\}}|rLt 
|d ¡}||jp’d7 }qL|S )zaLoad module docstrings

    Docstrings are generated on module load. Reduce, reuse, recycle.
    rz   Úallz+Invalid --docs value {}. Must be one of: {}r   úcloudinit.configr�   r   )rZ   r´   Úvaluesrª   Ú
differencer   r   r   r   Úfind_moduleÚimport_moduler    )rµ   ÚdocsZall_modulesZinvalid_docsÚmod_nameÚmod_locsÚ_Úmodr   r   r   Úload_doca  s*    þÿ  ÿrÁ   c               
   C   sÒ   t j t j t j t¡¡d¡} d}zt t| ƒ¡}W n0 t	k
rd } zt
 d| |¡ W 5 d}~X Y nX |s‚t
 d| ¡ i dg dœ}tƒ  ¡ D ]@\}}t |dgdg¡\}}|rŒt |d	 ¡}|d
  |j¡ qŒ|S )z?Return jsonschema coalesced from all cc_* cloud-config modules.zcloud-init-schema.jsonNz$Cannot parse JSON schema file %s. %szCNo base JSON schema files found at %s. Setting default empty schemaz'http://json-schema.org/draft-04/schema#)r§   z$schemaÚallOfr·   r:   r   rÂ   )rj   r@   r   r±   r²   r³   ÚjsonÚloadsr
   Ú	ExceptionrA   rB   r´   rŠ   r   rº   r»   rc   r:   )Zschema_fileÚfull_schemarJ   r¿   r½   r¾   rÀ   r   r   r   rQ   {  s8     ÿ ýý  ÿrQ   c                  C   sV   t ƒ } tƒ  ¡ D ]@\}}t |dgdg¡\}}|rt |d ¡}|j| |jd < q| S )z<Return metadata coalesced from all cc_* cloud-config module.r·   r�   r   r¤   )r]   r´   rŠ   r   rº   r»   r�   )Z	full_metar¿   r½   r¾   rÀ   r   r   r   Úget_meta¡  s      ÿrÇ   c                 C   s\   | st jddd�} | jdd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--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_argument©Úparserr   r   r   Ú
get_parser®  s:    þýüû	ürÕ   c              
   C   s  |j |j|jg}tdd„ |D ƒƒdkr.tdƒ |jrB|jrBtdƒ tƒ }|j sT|jrìzt|j ||jƒ W n` tk
rœ } z|jsŒtt	|ƒƒ W 5 d}~X Y nN t
k
rÈ } ztt	|ƒƒ W 5 d}~X Y n"X |j dkrÚd}n|j }td|ƒ n|j�rtt|jƒƒ 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   Õ  s      z&handle_schema_args.<locals>.<listcomp>rX   z;Expected one of --config-file, --system or --docs argumentsz;Invalid flag combination. Cannot use --annotate with --docsNzsystem userdatazValid cloud-config:)Zconfig_filer¼   Úsystemr~   r   ru   rQ   rw   r   r5   rl   rp   rÁ   )r¦   ÚargsZexclusive_argsrÆ   rJ   Zcfg_namer   r   r   Úhandle_schema_argsÒ  s0      ÿ
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__)T)NFF)F)r“   )N)N)<r    rÐ   rÃ   Zloggingrj   r_   ÚsysÚcollectionsr   Úcopyr   Ú	functoolsr   rq   Z	cloudinitr   Zcloudinit.cmd.develr   Zcloudinit.importerr   Zcloudinit.utilr   r	   r
   Z	getLoggerr   rA   r‡   ro   r®   r˜   rš   rŸ   r¡   r©   r   r*   r9   r]   rF   ÚboolrU   ri   rw   r^   r5   r�   r’   r™   r£   r°   r´   rZ   rÁ   rQ   rÇ   rÕ   rÙ   rÛ   Úexitr   r   r   r   Ú<module>   sl   
ÿ3#   üü18
O@=8&
$
