
    :                     
   d 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 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 ddlZdZdZdZdddddddZ G d dej4                        Z G d dej4                        Z G d dej4                        Zd Zd  Zd! Z d" Z!d# Z"d$ Z#d% Z$d& Z%d' Z&d( Z'd) Z(d* Z)d+ Z*d, Z+d- Z,d. Z-d/ Z.d0 Z/d1 Z0d2 Z1d3 Z2d4 Z3d5 Z4d6 Z5d7 Z6d8 Z7d9 Z8d: Z9d; Z:d< Z;d= Z<y)>z.Argument processors for DLP surface arguments.    )absolute_import)division)unicode_literalsN)apis)arg_parsers)base)	arg_utils)
exceptions)log)
properties)	resources)files)timesdlpv2zColors should be specified as a string of `r,g,b` float values in the interval
[0,1] representing the amount of red, green, and blue in the color,
respectively. For example, `black = 0,0,0`, `red = 1.0,0,0`,
`white = 1.0,1.0,1.0`, and so on.
IMAGE	IMAGE_PNG
IMAGE_JPEG	IMAGE_SVG	IMAGE_BMP)n_az.pngz.jpegz.jpgz.svgz.bmpc                       e Zd ZdZy)ImageFileErrorz:Error if an image file is improperly formatted or missing.N__name__
__module____qualname____doc__     +lib/googlecloudsdk/command_lib/dlp/hooks.pyr   r   7   s    Br    r   c                       e Zd ZdZy)RedactColorErrorz;Error if a redact color is improperly formatted or missing.Nr   r   r    r!   r#   r#   ;   s    Cr    r#   c                       e Zd ZdZy)BigQueryTableNameErrorz7Error if a BigQuery table name is improperly formatted.Nr   r   r    r!   r%   r%   ?   s    ?r    r%   c                 V    t        j                  t        t              }t	        ||       S )z3Get API message object for given message type name.)r   GetMessagesModule_DLP_API_DLP_API_VERSIONgetattr)msg_type_namemsgs     r!   _GetMessageClassr-   D   s"    x)9:#	m	$$r    c                      | sy| xr | t         v S )z#Validate image file name extension.T)VALID_IMAGE_EXTENSIONS)	extensions    r!   _ValidateExtensionr1   J   s    			<	%;;<r    c                 F    t        |       }|dkD  s|dk  rt        d      |S )z;Convert color value(color) to a float or raise value error.g      ?g        zInvalid Color.)float
ValueError)colorjs     r!   _ConvertColorValuer7   R   s)    El!WC
%
&&	
(r    c                    | j                  d      }t        |      dk(  r	 |D cg c]  }t        |       c}S t	        dj                  | t                    c c}w # t        $ r  t	        dj                  | t                    w xY w)zFValidates that values has proper format and returns parsed components.,   zInvalid Color Value(s) [{}]. {}z0You must specify exactly 3 color values [{}]. {})splitlenr7   r4   r#   format_COLOR_SPEC_ERROR_SUFFIX)valuevaluesxs      r!   _ValidateAndParseColorsrB   [   s    ;;s&[AK-34V #V44
    &u.F GI I 5 K ""(&0H"IK KKs   A AA A )Bc                 x    | j                  d      }t        |      dk7  rt        dj                  |             |S )CValidate BigQuery table name format and returned parsed components..r:   zInvalid BigQuery table name [{}]. BigQuery tables are uniquely identified by their project_id, dataset_id, and table_id in the format `<project_id>.<dataset_id>.<table_id>`.r;   r<   r%   r=   
table_name
name_partss     r!   _ValidateAndParseInputTableNamerJ   j   sD    $*_
 	99?
9KM M
 
r    c                     | j                  d      }t        |      dk7  r(t        |      dk7  rt        dj                  |             |S )rD   rE   r:      zInvalid BigQuery output table name [{}]. BigQuery tables are uniquely identified by their project_id, dataset_id, and or table_id in the format `<project_id>.<dataset_id>.<table_id>` or `<project_id>.<dataset_id>.rF   rG   s     r!    _ValidateAndParseOutputTableNamerM   v   sQ     $*_c*o2
 	& '-fZ&8	: : 
r    c                 *    t        d      } ||       S )z=Return GooglePrivacyDlpV2InfoType message for a parsed value.GooglePrivacyDlpV2InfoTypenamer-   )r?   infotypes     r!   InfoTyperT      s    :;(	u	r    c                 *    t        d      } ||       S )z-Create a GooglePrivacyDlpV2FieldId for value.GooglePrivacyDlpV2FieldIdrP   rR   )r?   field_ids     r!   PrivacyFieldrX      s    9:(	u	r    c                 x    t        |       \  }}}t        d      }t        d      } ||||      } ||      }|S )a  Convert BigQuery table name into GooglePrivacyDlpV2BigQueryOptions.

  Creates BigQuery input options for a job trigger.

  Args:
    table_name: str, BigQuery table name to create input options from in the
      form `<project_id>.<dataset_id>.<table_id>`.

  Returns:
    GooglePrivacyDlpV2BigQueryOptions, input options for job trigger.

  Raises:
    BigQueryTableNameError if table_name is improperly formatted.
  !GooglePrivacyDlpV2BigQueryOptionsGooglePrivacyDlpV2BigQueryTable	datasetId	projectIdtableIdtableReference)rJ   r-   )rH   
project_iddata_set_idtable_idbig_query_optionsbig_query_tabletableoptionss           r!   BigQueryInputOptionsri      sQ     'F'#*k8&'JK$%FG/
z8E%U3'	.r    c                 N    t        d      }t        d      } | ||             S )z-Return CloudStorageOptions for given GCS url.%GooglePrivacyDlpV2CloudStorageOptionsGooglePrivacyDlpV2FileSet)url)fileSetrR   )rm   cloud_storage_optionsfile_sets      r!   GcsInputOptionsrq      s+    *-/9:(	xC'8	99r    c                 T   t        d      }t        d      }t        d      }t        j                  j                  j                  j                  d      }| j                  d      }t        |      dk(  r|\  }} ||      } |||	      }	n ||       } ||
      }	 |||	      S )am  Convert Datastore arg value into GooglePrivacyDlpV2DatastoreOptions.

  Creates Datastore input options for a job trigger from datastore table name.

  Args:
    table_name: str, Datastore table name to create options from in the form
    `namespace:example-kind` or simply `example-kind`.

  Returns:
    GooglePrivacyDlpV2Action, output action for job trigger.
  "GooglePrivacyDlpV2DatastoreOptions GooglePrivacyDlpV2KindExpressionGooglePrivacyDlpV2PartitionIdTrequired:rL   rP   )namespaceIdr^   )r^   )kindpartitionId)r-   r   VALUEScoreprojectGetr;   r<   )
rH   data_store_optionsrz   partition_idr~   
split_name	namespacerg   kind_exp	partitions
             r!   DatastoreInputOptionsr      s     ((LM	<	=$!"AB,""**...='$*_!IuHgFI$Hw/I	y	AAr    c                 N    t        d      }t        d      } | ||             S )z1Return PubSub DlpV2Action for given PubSub topic.GooglePrivacyDlpV2Action!GooglePrivacyDlpV2PublishToPubSub)topic)pubSubrR   )r   
action_msgpubsub_actions      r!   PubSubTopicActionr      s(     :;*"#FG-	=u5	66r    c                     t        |       }|d   }|d   }d}t        |      dk(  r|d   }t        d      }t        d      }t        d      }t        d	      } ||||
      }	 | | ||	                  S )a  Convert BigQuery formatted table name into GooglePrivacyDlpV2Action.

  Creates a BigQuery output action for a job trigger.

  Args:
    table_name: str, BigQuery table name to create action from in the form
      `<project_id>.<dataset_id>.<table_id>` or `<project_id>.<dataset_id>`.

  Returns:
    GooglePrivacyDlpV2Action, output action for job trigger.

  Raises:
    BigQueryTableNameError if table_name is improperly formatted.
  r       r:   rL   r   GooglePrivacyDlpV2SaveFindings%GooglePrivacyDlpV2OutputStorageConfigr[   r\   )rg   )outputConfig)saveFindings)rM   r<   r-   )
rH   rI   rb   rc   rd   r   save_findings_configoutput_configrf   rg   s
             r!   BigQueryTableActionr      s     0
;*!}*1+(_!}H :;*)*JK"#JK-$%FG/
z8E% 
'$513
4 4r    c                 L    t        j                  | t         j                        S )N)tzinfo)r   FormatDateTimeUTCr?   s    r!   DlpTimeStampr      s    			eEII	66r    c                     ~ |j                   xs4 t        j                  j                  j                   j	                  d      }t
        j                  j                  |d      }|j                         |_	        |S )z%Set parent value for a DlpXXXRequest.Trv   zdlp.projects)
collection)
r~   r   r|   r}   r   r   REGISTRYParseRelativeNameparent)refargsrequestr   project_refs        r!   SetRequestParentr      sc    	<<L:,,1199==t=L&""((N(K+++-'.	.r    c                 8    ~ ~t        d      } |       |_        |S )z6Set cancel message on DlpProjectsDlpJobsCancelRequest.%GooglePrivacyDlpV2CancelDlpJobRequest)r-   %googlePrivacyDlpV2CancelDlpJobRequest)r   r   r   cancel_requests       r!   SetCancelRequestHookr     s#    	
#$KL.2@2B'/	.r    c                     ~ |j                   j                  j                  j                  j                  }|j
                  r|r|j
                  |j                  _        |S )z1Update partitionId.projectId on DatastoreOptions.))googlePrivacyDlpV2CreateJobTriggerRequest
jobTrigger
inspectJobstorageConfigdatastoreOptionsr~   r{   r^   )r   r   r   r   s       r!   UpdateDataStoreOptionsr     sN    	77BBMM}%%  
\\(/3||"",	.r    c                     ~ |j                   j                  j                  j                  }|r=|j                  r1t        d      }|j                  D cg c]  } ||       c}|_        |S c c}w )z<Update bigQueryOptions.identifyingFields with parsed fields.rV   rP   )%googlePrivacyDlpV2CreateDlpJobRequestr   r   bigQueryOptionsidentifying_fieldsr-   identifyingFields)r   r   r   re   rW   fields         r!   UpdateIdentifyingFieldsr     sq    	33>>LL  422 ;<H*.*A*A+*Ae*A+' 
.+s   A-c                     ~ |j                   reg }|j                   D ]>  }|j                  d      r|j                  d      dz   }n|dz  }|j                  |       @ dj	                  |      |_        |S )z<Set orderBy attribute on message from common --sort-by flag.~z descz ascr9   )sort_by
startswithlstripappendjoinorderBy)r   r   r   order_by_fieldsr   s        r!   SetOrderByFromSortByr   )  sn    		\\O			#	S!G+U#  hh/GO	.r    c                     | j                   S )zGExtracts and returns BigQueryTable from parsed BigQueryOptions message.r`   r   s    r!   ExtractBqTableFromInputConfigr   :  s    			r    c                 N    t        d      }t        d      } | ||             S )N$GooglePrivacyDlpV2ReplaceValueConfigGooglePrivacyDlpV2Value)stringValue)newValuerR   )r?   replace_configvalue_holders      r!   GetReplaceTextTransformr   ?  s(    #$JK.!";<,	%!@	AAr    c                 (    ~ t        d      } |       S )N+GooglePrivacyDlpV2ReplaceWithInfoTypeConfigrR   )r?   infotype_configs     r!   GetInfoTypeTransformr   E  s    $35/		r    c                 (    ~ t        d      } |       S )NGooglePrivacyDlpV2RedactConfigrR   )r?   redact_configs     r!   GetRedactTransformr   L  s    "#CD-	r    c                    t         j                  j                  |       d   j                         }|xs d}t	        d      }t         j                  j                  |       rQt        |      rFt        j                  t        |   |j                        } |t        j                  |       |      }|S t        dj                  |             )a  Builds a GooglePrivacyDlpV2ByteContentItem message from a path.

  Will attempt to set message.type from file extension (if present).

  Args:
    path: the path arg given to the command.

  Raises:
    ImageFileError: if the image path does not exist and does not have a valid
    extension.

  Returns:
    GooglePrivacyDlpV2ByteContentItem: an message containing image data for
    the API on the image to analyze.
  r   !GooglePrivacyDlpV2ByteContentItem)datatypezThe image path [{}] does not exist or has an invalid extension. Must be one of [jpg, jpeg, png, bmp or svg]. Please double-check your input and try again.)ospathsplitextlowerr-   isfiler1   r	   ChoiceToEnumr/   TypeValueValuesEnumr   ReadBinaryFileContentsr   r=   )r   r0   
image_itemenum_valimages        r!   GetImageFromFiler   R  s      ggt$R(..0) 5) CD*WW^^D0;%%&<Y&G&0&D&DFHE88>XNE 
,	 	88>tF Fr    c                 L    t        d      }t        |       \  }}} ||||      S )a  Convert color_string into GooglePrivacyDlpV2Color.

  Creates a GooglePrivacyDlpV2Color message from input string to use for image
  redaction.

  Args:
    color_string: str, string representing red, green and blue color saturation
      percentages as float values between 0.0 and 1.0. For example, `black =
      0,0,0`, `red = 1.0,0,0`, `white = 1.0,1.0,1.0` etc.

  Returns:
    GooglePrivacyDlpV2Color, color message.

  Raises:
    RedactColorError if color_string is improperly formatted.
  GooglePrivacyDlpV2Color)redbluegreen)r-   rB   )color_string	color_msgr   r   r   s        r!   GetRedactColorFromStringr   q  s/    " 89),\:#ud	sU	33r    c                 J    dj                  t        j                  |             S )z;Return API required format for duration specified by value.z{}s)r=   six	text_typer   s    r!   GetJobScheduleDurationStringr     s    	cmmE*	++r    c                  ^    d} t        j                  ddt        j                         |       gS )z.Capture identifying fields for BigQuery table.zComma separated list of references to field names uniquely identifying rows within the BigQuery table. Nested fields in the format `person.birthdate.year` are allowed.z--identifying-fieldsIDENTIFYING_FIELDS)metavarr   help)r   Argumentr   ArgList)	help_texts    r!   GetIdentifyingFieldsArgr     s7    A) mm
 &""$	
 r    c                    |j                   syt        j                  |j                         5 }|j                  |        ddd       t        j
                  j                  dj                  |j                                y# 1 sw Y   BxY w)z@Helper function for writing redacted contents to an output file.Nz+The redacted contents can be viewed in [{}])output_filer   BinaryFileWriterwriter   statusPrintr=   )r?   parsed_argsoutfiles      r!   *_PossiblyWriteRedactedResponseToOutputFiler    sg    		 	 
k556'MM% 7**@GG   76s   B  B	c                 F    t        | j                  j                  |       | S )zHWrite the contents of the redacted text file to parsed_args.output_file.)r  itemr?   responser  s     r!   -PossiblyWriteRedactedTextResponseToOutputFiler    s    ,X]]-@-@+N	/r    c                 2    t        | j                  |       | S )z4Write the redacted image to parsed_args.output_file.)r  redactedImager	  s     r!   .PossiblyWriteRedactedImageResponseToOutputFiler    s    ,X-C-C-8:	/r    c                  2    t        j                  dd      gS )z&Add --output-file to a redact command.z--output-filez/Path to the file to write redacted contents to.)r   )r   r   r   r    r!   AddOutputFileFlagr    s!     mm
@B
 r    )=r   
__future__r   r   r   r   googlecloudsdk.api_lib.utilr   googlecloudsdk.callioper   r   $googlecloudsdk.command_lib.util.apisr	   googlecloudsdk.corer
   r   r   r   googlecloudsdk.core.utilr   r   r   r(   r)   r>   r/   Errorr   r#   r%   r-   r1   r7   rB   rJ   rM   rT   rX   ri   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r   r    r!   <module>r     sA   5 &  ' 	 , / ( : * # * ) * * 
    CZ%% CDz'' D@Z-- @
%=I	2:B674D7
	"
B>4,, r    