// Copyright 2018 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";

package google.privacy.dlp.v2;

import "google/api/annotations.proto";
import "google/protobuf/timestamp.proto";

option csharp_namespace = "Google.Cloud.Dlp.V2";
option go_package = "google.golang.org/genproto/googleapis/privacy/dlp/v2;dlp";
option java_multiple_files = true;
option java_outer_classname = "DlpStorage";
option java_package = "com.google.privacy.dlp.v2";
option php_namespace = "Google\\Cloud\\Dlp\\V2";


// Type of information detected by the API.
message InfoType {
  // Name of the information type.
  string name = 1;
}

// Custom information type provided by the user. Used to find domain-specific
// sensitive information configurable to the data in question.
message CustomInfoType {
  // Custom information type based on a dictionary of words or phrases. This can
  // be used to match sensitive information specific to the data, such as a list
  // of employee IDs or job titles.
  //
  // Dictionary words are case-insensitive and all characters other than letters
  // and digits in the unicode [Basic Multilingual
  // Plane](https://en.wikipedia.org/wiki/Plane_%28Unicode%29#Basic_Multilingual_Plane)
  // will be replaced with whitespace when scanning for matches, so the
  // dictionary phrase "Sam Johnson" will match all three phrases "sam johnson",
  // "Sam, Johnson", and "Sam (Johnson)". Additionally, the characters
  // surrounding any match must be of a different type than the adjacent
  // characters within the word, so letters must be next to non-letters and
  // digits next to non-digits. For example, the dictionary word "jen" will
  // match the first three letters of the text "jen123" but will return no
  // matches for "jennifer".
  //
  // Dictionary words containing a large number of characters that are not
  // letters or digits may result in unexpected findings because such characters
  // are treated as whitespace.
  message Dictionary {
    // Message defining a list of words or phrases to search for in the data.
    message WordList {
      // Words or phrases defining the dictionary. The dictionary must contain
      // at least one phrase and every phrase must contain at least 2 characters
      // that are letters or digits. [required]
      repeated string words = 1;
    }

    oneof source {
      // List of words or phrases to search for.
      WordList word_list = 1;

      // Newline-delimited file of words in Cloud Storage. Only a single file
      // is accepted.
      CloudStoragePath cloud_storage_path = 3;
    }
  }

  // Message defining a custom regular expression.
  message Regex {
    // Pattern defining the regular expression.
    string pattern = 1;
  }

  // Message for detecting output from deidentification transformations
  // such as
  // [`CryptoReplaceFfxFpeConfig`](/dlp/docs/reference/rest/v2/organizations.deidentifyTemplates#cryptoreplaceffxfpeconfig).
  // These types of transformations are
  // those that perform pseudonymization, thereby producing a "surrogate" as
  // output. This should be used in conjunction with a field on the
  // transformation such as `surrogate_info_type`. This custom info type does
  // not support the use of `detection_rules`.
  message SurrogateType {

  }

  // Rule for modifying a custom info type to alter behavior under certain
  // circumstances, depending on the specific details of the rule. Not supported
  // for the `surrogate_type` custom info type.
  message DetectionRule {
    // Message for specifying a window around a finding to apply a detection
    // rule.
    message Proximity {
      // Number of characters before the finding to consider.
      int32 window_before = 1;

      // Number of characters after the finding to consider.
      int32 window_after = 2;
    }

    // Message for specifying an adjustment to the likelihood of a finding as
    // part of a detection rule.
    message LikelihoodAdjustment {
      oneof adjustment {
        // Set the likelihood of a finding to a fixed value.
        Likelihood fixed_likelihood = 1;

        // Increase or decrease the likelihood by the specified number of
        // levels. For example, if a finding would be `POSSIBLE` without the
        // detection rule and `relative_likelihood` is 1, then it is upgraded to
        // `LIKELY`, while a value of -1 would downgrade it to `UNLIKELY`.
        // Likelihood may never drop below `VERY_UNLIKELY` or exceed
        // `VERY_LIKELY`, so applying an adjustment of 1 followed by an
        // adjustment of -1 when base likelihood is `VERY_LIKELY` will result in
        // a final likelihood of `LIKELY`.
        int32 relative_likelihood = 2;
      }
    }

    // Detection rule that adjusts the likelihood of findings within a certain
    // proximity of hotwords.
    message HotwordRule {
      // Regex pattern defining what qualifies as a hotword.
      Regex hotword_regex = 1;

      // Proximity of the finding within which the entire hotword must reside.
      // The total length of the window cannot exceed 1000 characters. Note that
      // the finding itself will be included in the window, so that hotwords may
      // be used to match substrings of the finding itself. For example, the
      // certainty of a phone number regex "\(\d{3}\) \d{3}-\d{4}" could be
      // adjusted upwards if the area code is known to be the local area code of
      // a company office using the hotword regex "\(xxx\)", where "xxx"
      // is the area code in question.
      Proximity proximity = 2;

      // Likelihood adjustment to apply to all matching findings.
      LikelihoodAdjustment likelihood_adjustment = 3;
    }

    oneof type {
      // Hotword-based detection rule.
      HotwordRule hotword_rule = 1;
    }
  }

  // Info type configuration. All custom info types must have configurations
  // that do not conflict with built-in info types or other custom info types.
  InfoType info_type = 1;

  // Likelihood to return for this custom info type. This base value can be
  // altered by a detection rule if the finding meets the criteria specified by
  // the rule. Defaults to `VERY_LIKELY` if not specified.
  Likelihood likelihood = 6;

  oneof type {
    // Dictionary-based custom info type.
    Dictionary dictionary = 2;

    // Regex-based custom info type.
    Regex regex = 3;

    // Surrogate info type.
    SurrogateType surrogate_type = 4;
  }

  // Set of detection rules to apply to all findings of this custom info type.
  // Rules are applied in order that they are specified. Not supported for the
  // `surrogate_type` custom info type.
  repeated DetectionRule detection_rules = 7;
}

// General identifier of a data field in a storage service.
message FieldId {
  // Name describing the field.
  string name = 1;
}

// Datastore partition ID.
// A partition ID identifies a grouping of entities. The grouping is always
// by project and namespace, however the namespace ID may be empty.
//
// A partition ID contains several dimensions:
// project ID and namespace ID.
message PartitionId {
  // The ID of the project to which the entities belong.
  string project_id = 2;

  // If not empty, the ID of the namespace to which the entities belong.
  string namespace_id = 4;
}

// A representation of a Datastore kind.
message KindExpression {
  // The name of the kind.
  string name = 1;
}

// Options defining a data set within Google Cloud Datastore.
message DatastoreOptions {
  // A partition ID identifies a grouping of entities. The grouping is always
  // by project and namespace, however the namespace ID may be empty.
  PartitionId partition_id = 1;

  // The kind to process.
  KindExpression kind = 2;
}

// Options defining a file or a set of files (path ending with *) within
// a Google Cloud Storage bucket.
message CloudStorageOptions {
  // Set of files to scan.
  message FileSet {
    // The url, in the format `gs://<bucket>/<path>`. Trailing wildcard in the
    // path is allowed.
    string url = 1;
  }

  // How to sample bytes if not all bytes are scanned. Meaningful only when used
  // in conjunction with bytes_limit_per_file. If not specified, scanning would
  // start from the top.
  enum SampleMethod {
    SAMPLE_METHOD_UNSPECIFIED = 0;

    // Scan from the top (default).
    TOP = 1;

    // For each file larger than bytes_limit_per_file, randomly pick the offset
    // to start scanning. The scanned bytes are contiguous.
    RANDOM_START = 2;
  }

  FileSet file_set = 1;

  // Max number of bytes to scan from a file. If a scanned file's size is bigger
  // than this value then the rest of the bytes are omitted.
  int64 bytes_limit_per_file = 4;

  // List of file type groups to include in the scan.
  // If empty, all files are scanned and available data format processors
  // are applied.
  repeated FileType file_types = 5;

  SampleMethod sample_method = 6;

  // Limits the number of files to scan to this percentage of the input FileSet.
  // Number of files scanned is rounded down. Must be between 0 and 100,
  // inclusively. Both 0 and 100 means no limit. Defaults to 0.
  int32 files_limit_percent = 7;
}

// Message representing a single file or path in Cloud Storage.
message CloudStoragePath {
  // A url representing a file or path (no wildcards) in Cloud Storage.
  // Example: gs://[BUCKET_NAME]/dictionary.txt
  string path = 1;
}

// Options defining BigQuery table and row identifiers.
message BigQueryOptions {
  // How to sample rows if not all rows are scanned. Meaningful only when used
  // in conjunction with rows_limit. If not specified, scanning would start
  // from the top.
  enum SampleMethod {
    SAMPLE_METHOD_UNSPECIFIED = 0;

    // Scan from the top (default).
    TOP = 1;

    // Randomly pick the row to start scanning. The scanned rows are contiguous.
    RANDOM_START = 2;
  }

  // Complete BigQuery table reference.
  BigQueryTable table_reference = 1;

  // References to fields uniquely identifying rows within the table.
  // Nested fields in the format, like `person.birthdate.year`, are allowed.
  repeated FieldId identifying_fields = 2;

  // Max number of rows to scan. If the table has more rows than this value, the
  // rest of the rows are omitted. If not set, or if set to 0, all rows will be
  // scanned. Cannot be used in conjunction with TimespanConfig.
  int64 rows_limit = 3;

  SampleMethod sample_method = 4;
}

// Shared message indicating Cloud storage type.
message StorageConfig {
  // Configuration of the timespan of the items to include in scanning.
  // Currently only supported when inspecting Google Cloud Storage and BigQuery.
  message TimespanConfig {
    // Exclude files older than this value.
    google.protobuf.Timestamp start_time = 1;

    // Exclude files newer than this value.
    // If set to zero, no upper time limit is applied.
    google.protobuf.Timestamp end_time = 2;

    // Specification of the field containing the timestamp of scanned items.
    // Required for data sources like Datastore or BigQuery.
    // The valid data types of the timestamp field are:
    // for BigQuery - timestamp, date, datetime;
    // for Datastore - timestamp.
    // Datastore entity will be scanned if the timestamp property does not exist
    // or its value is empty or invalid.
    FieldId timestamp_field = 3;

    // When the job is started by a JobTrigger we will automatically figure out
    // a valid start_time to avoid scanning files that have not been modified
    // since the last time the JobTrigger executed. This will be based on the
    // time of the execution of the last run of the JobTrigger.
    bool enable_auto_population_of_timespan_config = 4;
  }

  oneof type {
    // Google Cloud Datastore options specification.
    DatastoreOptions datastore_options = 2;

    // Google Cloud Storage options specification.
    CloudStorageOptions cloud_storage_options = 3;

    // BigQuery options specification.
    BigQueryOptions big_query_options = 4;
  }

  TimespanConfig timespan_config = 6;
}

// Row key for identifying a record in BigQuery table.
message BigQueryKey {
  // Complete BigQuery table reference.
  BigQueryTable table_reference = 1;

  // Absolute number of the row from the beginning of the table at the time
  // of scanning.
  int64 row_number = 2;
}

// Record key for a finding in Cloud Datastore.
message DatastoreKey {
  // Datastore entity key.
  Key entity_key = 1;
}

// A unique identifier for a Datastore entity.
// If a key's partition ID or any of its path kinds or names are
// reserved/read-only, the key is reserved/read-only.
// A reserved/read-only key is forbidden in certain documented contexts.
message Key {
  // A (kind, ID/name) pair used to construct a key path.
  //
  // If either name or ID is set, the element is complete.
  // If neither is set, the element is incomplete.
  message PathElement {
    // The kind of the entity.
    // A kind matching regex `__.*__` is reserved/read-only.
    // A kind must not contain more than 1500 bytes when UTF-8 encoded.
    // Cannot be `""`.
    string kind = 1;

    // The type of ID.
    oneof id_type {
      // The auto-allocated ID of the entity.
      // Never equal to zero. Values less than zero are discouraged and may not
      // be supported in the future.
      int64 id = 2;

      // The name of the entity.
      // A name matching regex `__.*__` is reserved/read-only.
      // A name must not be more than 1500 bytes when UTF-8 encoded.
      // Cannot be `""`.
      string name = 3;
    }
  }

  // Entities are partitioned into subsets, currently identified by a project
  // ID and namespace ID.
  // Queries are scoped to a single partition.
  PartitionId partition_id = 1;

  // The entity path.
  // An entity path consists of one or more elements composed of a kind and a
  // string or numerical identifier, which identify entities. The first
  // element identifies a _root entity_, the second element identifies
  // a _child_ of the root entity, the third element identifies a child of the
  // second entity, and so forth. The entities identified by all prefixes of
  // the path are called the element's _ancestors_.
  //
  // A path can never be empty, and a path can have at most 100 elements.
  repeated PathElement path = 2;
}

// Message for a unique key indicating a record that contains a finding.
message RecordKey {
  oneof type {
    DatastoreKey datastore_key = 2;

    BigQueryKey big_query_key = 3;
  }
}

// Message defining the location of a BigQuery table. A table is uniquely
// identified  by its project_id, dataset_id, and table_name. Within a query
// a table is often referenced with a string in the format of:
// `<project_id>:<dataset_id>.<table_id>` or
// `<project_id>.<dataset_id>.<table_id>`.
message BigQueryTable {
  // The Google Cloud Platform project ID of the project containing the table.
  // If omitted, project ID is inferred from the API call.
  string project_id = 1;

  // Dataset ID of the table.
  string dataset_id = 2;

  // Name of the table.
  string table_id = 3;
}

// An entity in a dataset is a field or set of fields that correspond to a
// single person. For example, in medical records the `EntityId` might be a
// patient identifier, or for financial records it might be an account
// identifier. This message is used when generalizations or analysis must take
// into account that multiple rows correspond to the same entity.
message EntityId {
  // Composite key indicating which field contains the entity identifier.
  FieldId field = 1;
}

// Categorization of results based on how likely they are to represent a match,
// based on the number of elements they contain which imply a match.
enum Likelihood {
  // Default value; information with all likelihoods is included.
  LIKELIHOOD_UNSPECIFIED = 0;

  // Few matching elements.
  VERY_UNLIKELY = 1;

  UNLIKELY = 2;

  // Some matching elements.
  POSSIBLE = 3;

  LIKELY = 4;

  // Many matching elements.
  VERY_LIKELY = 5;
}

// Definitions of file type groups to scan.
enum FileType {
  // Includes all files.
  FILE_TYPE_UNSPECIFIED = 0;

  // Includes all file extensions not covered by text file types.
  BINARY_FILE = 1;

  // Included file extensions:
  //   asc, brf, c, cc, cpp, csv, cxx, c++, cs, css, dart, eml, go, h, hh, hpp,
  //   hxx, h++, hs, html, htm, shtml, shtm, xhtml, lhs, ini, java, js, json,
  //   ocaml, md, mkd, markdown, m, ml, mli, pl, pm, php, phtml, pht, py, pyw,
  //   rb, rbw, rs, rc, scala, sh, sql, tex, txt, text, tsv, vcard, vcs, wml,
  //   xml, xsl, xsd, yml, yaml.
  TEXT_FILE = 2;
}
