
                             S r SSKJr  SSKJr  SSKJr  SSKrSSKJr  SSKJ	r	  SSK
Jr  SSKrS	rS
rSrS rS r " S S\5      r " S S\5      rg)a  Directly processes text of dos.xml.

DosXmlParser is called with an XML string to produce a list of BlackListEntry
objects containing the data from the XML.

DosXmlParser: converts XML to list of BlackListEntrys.
BlacklistEntry: describes a blacklisted IP.
    )absolute_import)division)unicode_literalsN)ElementTree)xml_parser_utils)AppEngineConfigExceptionz-<blacklist> node must have a subnet specifiedz'"%s" is not a valid IPv4 or IPv6 subnetzUPrefix length of subnet "%s" must be an integer (quad-dotted masks are not supported)c                 F    [        [        5       R                  U5      5      $ )N)_MakeDosListIntoYamlDosXmlParser
ProcessXml)unused_applicationdos_xml_strs     4lib/googlecloudsdk/appengine/tools/dos_xml_parser.py
GetDosYamlr   )   s    	ln77D	EE    c                 f    S/nU  H  nXR                  5       -  nM     SR                  U5      S-   $ )z?Converts yaml statement list of blacklisted IP's into a string.z
blacklist:
)ToYamljoin)dos_list
statementsentrys      r   r
   r
   -   s5    ~*e,,. J 	:		%%r   c                   *    \ rS rSrSrS rS rS rSrg)r   5   z:Provides logic for walking down XML tree and pulling data.c                     / U l         / U l        [        R                  " U5      nUR                  S:w  a  [        S5      e[        UR                  5       5       H  nU R                  U5        M     U R                  (       a$  [        SR                  U R                  5      5      eU R                   $ ! [        R                   a    [        S5      ef = f)a*  Parses XML string and returns object representation of relevant info.

Args:
  xml_str: The XML string.
Returns:
  A list of BlacklistEntry objects containing information about blacklisted
  IP's specified in the XML.
Raises:
  AppEngineConfigException: In case of malformed XML or illegal inputs.
blacklistentriesz#Root tag must be <blacklistentries>r   zBad input -- not valid XML)blacklist_entrieserrorsr   
fromstringtagr   listgetchildrenProcessBlacklistNoder   
ParseError)selfxml_strxml_rootchilds       r   r   DosXmlParser.ProcessXml8   s    C!ddk''0h	+	+&'LMM,,./%!!%( 0 
&tyy'=>>###!! C$%ABBCs   B0B3 3 Cc                    [         R                  " U5      nUS:w  a  U R                  R                  SU-  5        g[	        5       n[         R
                  " US5      Ul        [         R
                  " US5      Ul        U R                  U5      nU(       a  U R                  R                  U5        gU R                  R                  U5        g)a  Processes XML <blacklist> nodes into BlacklistEntry objects.

The following information is parsed out:
  subnet: The IP, in CIDR notation.
  description: (optional)
If there are no errors, the data is loaded into a BlackListEntry object
and added to a list. Upon error, a description of the error is added to
a list and the method terminates.

Args:
  node: <blacklist> XML node in dos.xml.
	blacklistzUnrecognized node: <%s>Nsubnetdescription)
r   GetTagr   appendBlacklistEntryGetChildNodeTextr,   r-   _ValidateEntryr   )r%   noder    r   
validations        r   r#   !DosXmlParser.ProcessBlacklistNodeU   s     
!
!$
'C
k
kk2S89E#44T8DEL(99$NE$$U+J
kk$!!%(r   c                 t   UR                   (       d  [        $  [        R                  " UR                   5        UR                   R                  S5      n[        U5      S:X  a3  [        R                  " SUS   5      (       d  [        UR                   -  $ g g ! [         a    [
        UR                   -  s $ f = f)N/   z^[0-9]+$   )r,   MISSING_SUBNETipaddr	IPNetwork
ValueErrorBAD_IPV_SUBNETsplitlenrematchBAD_PREFIX_LENGTH)r%   r   partss      r   r2   DosXmlParser._ValidateEntryq   s    <<+u||$ LLs#E
5zQrxx
E!H==--  >  +ell**+s    B B76B7)r   r   N)	__name__
__module____qualname____firstlineno____doc__r   r#   r2   __static_attributes__ r   r   r   r   5   s    BC:)8	.r   r   c                   $    \ rS rSrSrS rS rSrg)r0   }   zAInstances contain information about individual blacklist entries.c                     SU R                   -  /nU R                  (       a-  UR                  SU R                  U R                  5      -  5        U$ )Nz- subnet: %sz  description: %s)r,   r-   r/   _SanitizeForYaml)r%   r   s     r   r   BlacklistEntry.ToYaml   sJ     4;;./J
 5 5d6F6F G
GIr   c                 ,    SUR                  SS5      -  $ )Nz'%s'r    )replace)r%   	dirty_strs     r   rP   BlacklistEntry._SanitizeForYaml   s    I%%dC000r   rL   N)rF   rG   rH   rI   rJ   r   rP   rK   rL   r   r   r0   r0   }   s    I1r   r0   )rJ   
__future__r   r   r   rA   	xml.etreer   googlecloudsdk.appengine.toolsr   :googlecloudsdk.appengine.tools.app_engine_config_exceptionr   r;   r:   r>   rC   r   r
   objectr   r0   rL   r   r   <module>r\      s\    '  ' 	 ! ; _ @:= F&E.6 E.P1V 1r   