# Copyright 2020 Google LLC. All Rights Reserved.
#
# 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.
workload_identity_pool:
  display_name:
    api_field: workloadIdentityPool.displayName
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPool.displayName
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPool.displayName
    arg_name: display-name
    help_text: |-
      A display name for the pool. Cannot exceed 32 characters.
  description:
    api_field: workloadIdentityPool.description
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPool.description
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPool.description
    arg_name: description
    help_text: |-
      A description of the pool. Cannot exceed 256 characters.
  disabled:
    api_field: workloadIdentityPool.disabled
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPool.disabled
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPool.disabled
    arg_name: disabled
    help_text: |-
      Whether the pool is disabled. You cannot use a disabled pool to exchange tokens, or use
      existing tokens to access resources. If the pool is re-enabled, existing tokens grant access
      again.
  session_duration:
    api_field: workloadIdentityPool.sessionDuration
    arg_name: session-duration
    type: googlecloudsdk.core.util.times:ParseDuration
    processor: googlecloudsdk.core.util.times:FormatDurationForJson
    help_text: |-
      Overrides the lifespan of access tokens issued issued for identities in this pool. If not set,
      the lifespan of issued access tokens is computed based on the type of identity provider:

      - For AWS, the default access token lifespan is equal to 15
        minutes.
      - For OIDC providers, including Microsoft Azure, the default access token lifespan is equal to
        the remaining lifespan of the exchanged OIDC ID token, with a maximum lifespan
        of 1 hour.
      - For SAML providers, the lifetime of the token is the minimum of session_duration
        and the SessionNotOnOrAfter claim in the SAML assertion. If
        session_duration is not set and the SAML assertion does not contain a
        SessionNotOnOrAfter claim, it defaults to 1 hour.

        If set, session duration must be between 2 minutes and 12 hours.
        Organization administrators can further reduce the maximum
        session_duration value using the iam-workloadIdentitySessionDuration
        Resource Setting.

workload_identity_pool_provider:
  display_name:
    api_field: workloadIdentityPoolProvider.displayName
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.displayName
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.displayName
    arg_name: display-name
    help_text: |-
      A display name for the provider. Cannot exceed 32 characters.
  description:
    api_field: workloadIdentityPoolProvider.description
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.description
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.description
    arg_name: description
    required: false
    help_text: |-
      A description for the provider. Cannot exceed 256 characters.
  disabled:
    api_field: workloadIdentityPoolProvider.disabled
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.disabled
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.disabled
    arg_name: disabled
    help_text: |-
      Whether the provider is disabled. You cannot use a disabled provider to exchange tokens.
      However, existing tokens still grant access.
  attribute_mapping:
    api_field: workloadIdentityPoolProvider.attributeMapping
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.attributeMapping
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.attributeMapping
    arg_name: attribute-mapping
    type: "googlecloudsdk.calliope.arg_parsers:ArgDict:"
    help_text: |-
      Maps attributes from authentication credentials issued by an external
      identity provider to Google Cloud attributes, such as `subject` and
      `segment`.

      Each key must be a string specifying the Google Cloud IAM attribute to
      map to.

      The following keys are supported:

      * `google.subject`: The principal IAM is authenticating. You can reference
                          this value in IAM bindings. This is also the
                          subject that appears in Cloud Logging logs.
                          Cannot exceed 127 bytes.

      * `google.groups`: Groups the external identity belongs to. You can grant
                         groups access to resources using an IAM `principalSet`
                         binding; access applies to all members of the group.

      You can also provide custom attributes by specifying
      `attribute.{custom_attribute}`, where `{custom_attribute}` is the name of
      the custom attribute to be mapped. You can define a maximum of 50 custom
      attributes. The maximum length of a mapped attribute key is
      100 characters, and the key may only contain the characters `[a-z_0-9]`.

      You can reference these attributes in IAM policies to define fine-grained
      access for a workload to Google Cloud resources. For example:

      * `google.subject`: `principal://iam.googleapis.com/projects/{project}/locations/{location}/workloadIdentityPools/{pool}/subject/{value}`

      * `google.groups`: `principalSet://iam.googleapis.com/projects/{project}/locations/{location}/workloadIdentityPools/{pool}/group/{value}`

      * `attribute.{custom_attribute}`: `principalSet://iam.googleapis.com/projects/{project}/locations/{location}/workloadIdentityPools/{pool}/attribute.{custom_attribute}/{value}`

      Each value must be a [Common Expression Language]
      (https://opensource.google/projects/cel) function that maps an
      identity provider credential to the normalized attribute specified by the
      corresponding map key.

      You can use the `assertion` keyword in the expression to access a JSON
      representation of the authentication credential issued by the provider.

      The maximum length of an attribute mapping expression is 2048 characters.
      When evaluated, the total size of all mapped attributes must not exceed
      8KB.

      For AWS providers, the following rules apply:

      - If no attribute mapping is defined, the following default mapping
        applies:

        ```
        {
          "google.subject":"assertion.arn",
          "attribute.aws_role":
              "assertion.arn.contains('assumed-role')"
              " ? assertion.arn.extract('{account_arn}assumed-role/')"
              "   + 'assumed-role/'"
              "   + assertion.arn.extract('assumed-role/{role_name}/')"
              " : assertion.arn",
        }
        ```

      - If any custom attribute mappings are defined, they must include a mapping
        to the `google.subject` attribute.


      For OIDC providers, the following rules apply:

      - Custom attribute mappings must be defined, and must include a mapping to
        the `google.subject` attribute. For example, the following maps the
        `sub` claim of the incoming credential to the `subject` attribute on
        a Google token.

        ```
        {"google.subject": "assertion.sub"}
        ```
  attribute_condition:
    api_field: workloadIdentityPoolProvider.attributeCondition
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.attributeCondition
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.attributeCondition
    arg_name: attribute-condition
    help_text: |-
      [A Common Expression Language](https://opensource.google/projects/cel)
      expression, in plain text, to restrict what otherwise valid authentication
      credentials issued by the provider should not be accepted.

      The expression must output a boolean representing whether to allow the
      federation.

      The following keywords may be referenced in the expressions:

      * `assertion`: JSON representing the authentication credential issued by
                     the provider.
      * `google`: The Google attributes mapped from the assertion in the
                  `attribute_mappings`.
      * `attribute`: The custom attributes mapped from the assertion in the
                     `attribute_mappings`.

      The maximum length of the attribute condition expression is 4096
      characters. If unspecified, all valid authentication credential are
      accepted.

      The following example shows how to only allow credentials with a mapped
      `google.groups` value of `admins`:

      ```
      "'admins' in google.groups"
      ```
  aws_account_id:
    api_field: workloadIdentityPoolProvider.aws.accountId
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.aws.accountId
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.aws.accountId
    arg_name: account-id
    help_text: |-
      The AWS account ID.
  oidc_issuer_uri:
    api_field: workloadIdentityPoolProvider.oidc.issuerUri
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.oidc.issuerUri
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.oidc.issuerUri
    arg_name: issuer-uri
    help_text: |-
      The OIDC issuer URL.

  oidc_jwks_json_path:
    api_field: workloadIdentityPoolProvider.oidc.jwksJson
    type: "googlecloudsdk.calliope.arg_parsers:FileContents:"
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.oidc.jwksJson
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.oidc.jwksJson
    arg_name: jwk-json-path
    help_text: |-
      Optional file containing jwk public keys. The file format must follow
      [jwk specifications](https://www.rfc-editor.org/rfc/rfc7517#section-4). Example file format:
      ```
      {
        "keys": [
           {
                "kty": "RSA/EC",
                "alg": "<algorithm>",
                "use": "sig",
                "kid": "<key-id>",
                "n": "",
                "e": ""
                "x": ""
                "y": ""
                "crv": ""
           }
        ]
      }
      ```

  oidc_allowed_audiences:
    api_field: workloadIdentityPoolProvider.oidc.allowedAudiences
    ALPHA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.oidc.allowedAudiences
    BETA:
      api_field: googleIamV1betaWorkloadIdentityPoolProvider.oidc.allowedAudiences
    arg_name: allowed-audiences
    help_text: |-
      Acceptable values for the `aud` field (audience) in the OIDC token. Token
      exchange requests are rejected if the token audience does not match one
      of the configured values. Each audience may be at most 256 characters. A
      maximum of 10 audiences may be configured.

      If this list is empty, the OIDC token audience must be equal to
      the full canonical resource name of the workload identity pool provider,
      with or without the HTTPS prefix. For example:

      ```
      //iam.googleapis.com/projects/<project-number>/locations/<location>/workloadIdentityPools/<pool-id>/providers/<provider-id>
      https://iam.googleapis.com/projects/<project-number>/locations/<location>/workloadIdentityPools/<pool-id>/providers/<provider-id>
      ```
  saml_idp_metadata_path:
    api_field: workloadIdentityPoolProvider.saml.idpMetadataXml
    type: "googlecloudsdk.calliope.arg_parsers:FileContents:"
    arg_name: idp-metadata-path
    help_text: |-
      XML file with configuration metadata for the SAML identity provider. The metadata file must
      follow the
      [SAML 2.0 metadata specification](https://www.oasis-open.org/committees/download.php/35391/sstc-saml-metadata-errata-2.0-wd-04-diff.pdf).

workload_identity_pool_provider_key:
  use:
    api_field: workloadIdentityPoolProviderKey.use
    arg_name: use
    required: true
    help_text: |-
      The purpose of the key.
  spec:
    api_field: workloadIdentityPoolProviderKey.keyData.keySpec
    arg_name: spec
    required: true
    help_text: |-
      The specifications for the key.

workforce_pool:
  parent:
    api_field: workforcePool.parent
    arg_name: organization
    required: true
    help_text: The parent resource name.
  display_name:
    api_field: workforcePool.displayName
    arg_name: display-name
    help_text: A display name for the workforce pool. Cannot exceed 32 characters in length.
  description:
    api_field: workforcePool.description
    arg_name: description
    help_text: A description for the workforce pool. Cannot exceed 256 characters in length.
  session_duration:
    api_field: workforcePool.sessionDuration
    arg_name: session-duration
    help_text: |-
      How long the Google Cloud access tokens, console sign-in sessions, and gcloud sign-in sessions
      from this workforce pool are valid. Must be greater than 15 minutes (900s) and less than 12
      hours (43200s). If not configured, minted credentials will have a default duration of one
      hour (3600s).
  disabled:
    api_field: workforcePool.disabled
    arg_name: disabled
    help_text: |-
      Whether or not the workforce pool is disabled. You cannot use a disabled workforce pool to
      perform new token exchanges or sign-ins using any provider in the workforce pool.

workforce_pool_provider:
  display_name:
    api_field: workforcePoolProvider.displayName
    arg_name: display-name
    help_text: |-
      A display name for the workforce pool provider. Cannot exceed 32 characters in length.
  description:
    api_field: workforcePoolProvider.description
    arg_name: description
    help_text: |-
      A description for the workforce pool provider. Cannot exceed 256 characters in length.
  disabled:
    api_field: workforcePoolProvider.disabled
    arg_name: disabled
    help_text: |-
      Whether the workforce pool provider is disabled. You cannot use a disabled provider to perform
      new token exchanges or sign-ins. However, existing tokens still grant access.
  attribute_mapping:
    api_field: workforcePoolProvider.attributeMapping
    arg_name: attribute-mapping
    type: "googlecloudsdk.calliope.arg_parsers:ArgDict:"
    help_text: |-
      Maps claims from the authentication credentials issued by the Identity Provider into Google
      Cloud IAM attributes, e.g. subject, segment.

      Each key must be a string specifying the Google Cloud IAM attribute to be produced.

      The following predefined keys are currently supported:

      * `google.subject`: required field that indicates the principal that is being authenticated to
                          IAM, and will be logged in all API accesses for which Cloud Audit Logging
                          is configured.

      * `google.groups`: optional field that indicates asserted groups that the user should be
                         considered to belong to. You can create IAM bindings using the groups
                         attribute and access to a resource will be granted if any of the groups
                         asserted here match a group in the respective binding.

      * `google.display_name`: optional field that overrides the name of the user. If not set,
                               `google.subject` will be displayed instead. This attribute cannot be
                               used in IAM policies. The maximum length of this field is 100
                               characters.

      * `google.profile_photo`: optional fields that may be set to a valid URL specifying the user's
                                thumbnail photo. When set, the image will be visible as the user's
                                profile picture. If not set, a generic user icon will be displayed
                                instead. This attribute cannot be used in IAM policies.

      Custom attributes can also be mapped by specifying `attribute.{custom_attribute}`, replacing
      `{custom_attribute}` with the name of the custom attribute to be mapped. A maximum of 50
      custom attribute mappings can be defined. The maximum length of a mapped attribute key is 2048
      characters and may only contain the characters [a-z0-9_].

      These attributes can then be referenced in IAM policies to define fine-grained access for the
      workforce pool to Google Cloud resources by specifying:

      * `google.subject`: `principal://iam.googleapis.com/locations/global/workforcePools/{pool}/subject/{value}`

      * `google.groups`: `principalSet://iam.googleapis.com/locations/global/workforcePools/{pool}/group/{value}`

      * `attribute.{custom_attribute}`: `principalSet://iam.googleapis.com/locations/global/workforcePools/{pool}/attribute.{custom_attribute}/{value}`

      Each value must be a [Common Expression Language](https://opensource.google/projects/cel)
      function that maps an Identity Provider credential to the normalized attribute specified by
      the corresponding map key.

      The following keywords may be referenced in the expressions:

      * `assertion`: JSON representing the authentication credential issued by the Identity
                     Provider.

      The maximum length of an attribute mapping expression is 2048 characters. When evaluated, the
      total size of all mapped attributes must not exceed 8KB.

      Example: Map the `sub` claim of the incoming credential to the
               `subject` Google Cloud IAM attribute.

               ```
               {"google.subject": "assertion.sub"}
               ```
  attribute_condition:
    api_field: workforcePoolProvider.attributeCondition
    arg_name: attribute-condition
    help_text: |-
      A [Common Expression Language](https://opensource.google/projects/cel) expression, in plain
      text, to restrict which otherwise valid authentication credentials issued by the provider
      should be accepted.

      The expression must output a boolean representing whether to allow the federation.

      The following keywords may be referenced in the expressions:

      * `assertion`: JSON representing the authentication credential issued by the Provider.
      * `google`: The Google attributes mapped from the assertion in the `attribute_mappings`.
                  `google.profile_photo` and `google.display_name` are not supported.
      * `attribute`: The custom attributes mapped from the assertion in the `attribute_mappings`.

      The maximum length of the attribute condition expression is 4096 characters. If unspecified,
      all valid authentication credential will be accepted.

      Example: Only allow credentials with a mapped `google.groups` value of `admins`.

               ```
               "'admins' in google.groups"
               ```
  oidc_issuer_uri:
    api_field: workforcePoolProvider.oidc.issuerUri
    arg_name: issuer-uri
    help_text: |-
      The OIDC issuer URI. Must be a valid URI using the 'https' scheme.
  oidc_client_id:
    api_field: workforcePoolProvider.oidc.clientId
    arg_name: client-id
    help_text: |-
      The OIDC client ID. This must match the audience claim of the JWT issued by the identity
      provider.
  saml_idp_metadata_path:
    api_field: workforcePoolProvider.saml.idpMetadataXml
    type: "googlecloudsdk.calliope.arg_parsers:FileContents:"
    arg_name: idp-metadata-path
    help_text: |-
      XML file with configuration metadata for the SAML identity provider. The metadata file must
      follow the
      [SAML 2.0 metadata specification](https://www.oasis-open.org/committees/download.php/35391/sstc-saml-metadata-errata-2.0-wd-04-diff.pdf).

workforce_pool_provider_key:
  use:
    api_field: workforcePoolProviderKey.use
    arg_name: use
    required: true
    help_text: The purpose of the key.
  spec:
    api_field: workforcePoolProviderKey.keyData.keySpec
    arg_name: spec
    required: true
    help_text: The specifications for the key.
