# Copyright 2024 Google LLC
#
# 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
#
#    https://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.

name: "restricted_destinations"
rule:
  variables:
    - name: matches_origin_ip
      expression: >
        locationCode(origin.ip) == spec.origin
    - name: has_nationality
      expression: >
        has(request.auth.claims.nationality)
    - name: matches_nationality
      expression: >
        variables.has_nationality && request.auth.claims.nationality == spec.origin
    - name: matches_dest_ip
      expression: >
        locationCode(destination.ip) in spec.restricted_destinations
    - name: matches_dest_label
      expression: >
        resource.labels.location in spec.restricted_destinations
    - name: matches_dest
      expression: >
        variables.matches_dest_ip || variables.matches_dest_label
  match:
    - condition: variables.matches_nationality && variables.matches_dest
      output: "true"
    - condition: >
        !variables.has_nationality && variables.matches_origin_ip && variables.matches_dest
      output: "true"
    - output: "false"
