Custom organization policy constraints

This page shows you how to use Google Cloud Organization Policy Service custom constraints to restrict specific operations on the following Cloud Storage for Firebase resources:

  • firebasestorage.googleapis.com/DefaultBucket

To learn more about Organization Policy, see Custom organization policies.

About organization policies and constraints

The Google Cloud Organization Policy Service gives you centralized, programmatic control over your organization's resources. As the organization policy administrator, you can define an organization policy, which is a set of restrictions called constraints that apply to Google Cloud resources and descendants of those resources in the Google Cloud resource hierarchy. You can enforce organization policies at the organization, folder, or project level.

Organization Policy provides built-in managed constraints for various Google Cloud services. However, if you want more granular, customizable control over the specific fields that are restricted in your organization policies, you can also create custom constraints and use those custom constraints in an organization policy.

Policy inheritance

By default, organization policies are inherited by the descendants of the resources on which you enforce the policy. For example, if you enforce a policy on a folder, Google Cloud enforces the policy on all projects in the folder. To learn more about this behavior and how to change it, refer to Hierarchy evaluation rules.

In addition, because Cloud Storage for Firebase stores files in underlying Cloud Storage buckets, any organization policies or custom constraints enforced on Cloud Storage resources also apply to your Cloud Storage for Firebase buckets.

Benefits

  • Security and governance: Enforce centralized organizational control over Cloud Storage bucket provisioning to ensure compliance with data residency and cost management guardrails.
  • Fine-grained attribute control: Restrict default bucket creation based on specific resource attributes such as storageClass or location.

Limitations

The following limitations apply when using custom organization policies with Cloud Storage for Firebase:

  • No retroactive enforcement: Newly enforced custom constraints don't apply to existing resources. Existing default buckets aren't evaluated unless they are updated. To identify existing resources that might violate a constraint, you can test policies using dry-run mode.
  • Access control restrictions: Custom constraints can't restrict access control lists (ACLs) or IAM policies on buckets.

Before you begin

  1. In the Firebase console, select or create a Firebase project.

    Ensure that your project is part of an organization and that you know your organization ID.

  2. To set up and enforce custom constraints using the command line, install the Google Cloud CLI (gcloud). Custom organization policies cannot be managed using the Firebase CLI.

Required roles

To get the permissions that you need to manage custom organization policies, ask your administrator to grant you the Organization Policy Administrator (roles/orgpolicy.policyAdmin) IAM role on the organization.

Cloud Storage for Firebase supported resources

The following table lists the Cloud Storage for Firebase resources that you can reference in custom constraints. You can also reference Cloud Storage supported resources, since custom constraints on those resources also apply to Cloud Storage for Firebase buckets.

Resource Supported fields
firebasestorage.googleapis.com/DefaultBucket
  • resource.location
  • resource.storageClass

Set up a custom constraint

A custom constraint is defined in a YAML file by the resources, methods, conditions, and actions that are supported by the service on which you are enforcing the organization policy. Conditions for your custom constraints are defined using Common Expression Language (CEL) . For more information about how to build conditions in custom constraints using CEL, see the CEL section of Creating and managing custom constraints.

Console

To create a custom constraint, do the following:

  1. In the Google Cloud console, go to the Organization policies page.

    Go to Organization policies

  2. From the project picker, select the project that you want to set the organization policy for.
  3. Click Custom constraint.
  4. In the Display name box, enter a human-readable name for the constraint. This name is used in error messages and can be used for identification and debugging. Don't use personally identifiable information (PII) or sensitive data in display names because this name could be exposed in error messages. This field can contain up to 200 characters.
  5. In the Constraint ID box, enter the ID that you want for your new custom constraint. A custom constraint can only contain letters (including upper and lowercase) or numbers, for example custom.restrictStorageClass. This field can contain up to 70 characters, not counting the prefix (custom.), for example, organizations/123456789/customConstraints/custom.restrictStorageClass. Don't include PII or sensitive data in your constraint ID, because it could be exposed in error messages.
  6. In the Description box, enter a human-readable description of the constraint. This description is used as an error message when the policy is violated. Include details about why the policy violation occurred and how to resolve the policy violation. Don't include PII or sensitive data in your description, because it could be exposed in error messages. This field can contain up to 2000 characters.
  7. In the Resource type box, select the name of the REST resource containing the object and field that you want to restrict—for example, firebasestorage.googleapis.com/DefaultBucket. Most resource types support up to 20 custom constraints. If you attempt to create more custom constraints, the operation fails.
  8. Under Enforcement method, select whether to enforce the constraint on a REST CREATE method or both CREATE and UPDATE methods. (Note: DefaultBucket only supports CREATE).
  9. To define a condition, click Edit condition.
    1. In the Add condition panel, create a CEL condition that refers to a supported service resource, for example, resource.storageClass == 'STANDARD'. This field can contain up to 1000 characters. For details about CEL usage, see Common Expression Language.
    2. Click Save.
  10. Under Action, select whether to allow or deny the evaluated method if the condition is met.

    The deny action means that the operation to create or update the resource is blocked if the condition evaluates to true.

    The allow action means that the operation to create or update the resource is permitted only if the condition evaluates to true. Every other case except those explicitly listed in the condition is blocked.

  11. Click Create constraint.

gcloud

  1. To create a custom constraint, create a YAML file using the following format:
    name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictStorageClass
    resourceTypes: firebasestorage.googleapis.com/DefaultBucket
    methodTypes:
      -   CREATE
    condition: "resource.storageClass == 'STANDARD'"
    actionType: DENY
    displayName: Restrict Standard Storage Class
    description: Deny creating buckets with STANDARD storage class.
  2. After you have created the YAML file for a new custom constraint, you must set it up to make it available for organization policies in your organization. To set up a custom constraint, use the gcloud org-policies set-custom-constraint command:
    gcloud org-policies set-custom-constraint CONSTRAINT_PATH

    Replace CONSTRAINT_PATH with the full path to your custom constraint file. For example, /home/user/customconstraint.yaml.

  3. To verify that the custom constraint exists, use the gcloud org-policies list-custom-constraints command:
    gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID

Enforce a custom organization policy

You can enforce a constraint by creating an organization policy that references it, and then applying that organization policy to a Google Cloud resource.

Console

  1. In the Google Cloud console, go to the Organization policies page.

    Go to Organization policies

  2. From the project picker, select the project that you want to set the organization policy for.
  3. From the list on the Organization policies page, select your constraint to view the Policy details page for that constraint.
  4. To configure the organization policy for this resource, click Manage policy.
  5. On the Edit policy page, select Override parent's policy.
  6. Click Add a rule.
  7. In the Enforcement section, select whether this organization policy is enforced or not.
  8. To enforce the organization policy in dry-run mode, click Set dry run policy. For more information, see Test organization policies.
  9. After you verify that the organization policy in dry-run mode works as intended, set the live policy by clicking Set policy.

gcloud

  1. To create an organization policy with boolean rules, create a policy YAML file that references the constraint:
    name: projects/PROJECT_ID/policies/custom.restrictStorageClass
    spec:
      rules:
      -   enforce: true
    dryRunSpec:
      rules:
      -   enforce: true
  2. To enforce the organization policy in dry-run mode, run the following command with the dryRunSpec flag:
    gcloud org-policies set-policy POLICY_PATH --update-mask=dryRunSpec
  3. After you verify that the organization policy in dry-run mode works as intended, set the live policy with the org-policies set-policy command and the spec flag:
    gcloud org-policies set-policy POLICY_PATH --update-mask=spec

Test the custom organization policy

The following example creates a custom constraint and policy that require all newly created default buckets to avoid using the STANDARD storage class. You can test the policy by trying to take an action that the policy should prevent.

Create the constraint

  1. Save the following file as constraint-restrict-storage-class.yaml:

    name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictStorageClass
    resourceTypes:
    -   firebasestorage.googleapis.com/DefaultBucket
    methodTypes:
    -   CREATE
    condition: "resource.storageClass == 'STANDARD'"
    actionType: DENY
    displayName: Restrict Standard Storage Class
    description: Deny creating buckets with STANDARD storage class.
    

    Replace ORGANIZATION_ID with your organization ID.

  2. Apply the constraint:

    gcloud org-policies set-custom-constraint ~/constraint-restrict-storage-class.yaml
    
  3. Verify that the constraint exists:

    gcloud org-policies list-custom-constraints --organization=ORGANIZATION_ID
    

    The output is similar to the following:

    CUSTOM_CONSTRAINT            ACTION_TYPE   METHOD_TYPES   RESOURCE_TYPES                                 DISPLAY_NAME
    custom.restrictStorageClass  DENY          CREATE         firebasestorage.googleapis.com/DefaultBucket   Restrict Standard Storage Class
    ...
    

Create the policy

  1. Save the following file as policy-restrict-storage-class.yaml:

    name: projects/PROJECT_ID/policies/custom.restrictStorageClass
    spec:
      rules:
      -   enforce: true
    

    Replace PROJECT_ID with your project ID.

  2. Apply the policy:

    gcloud org-policies set-policy ~/policy-restrict-storage-class.yaml
    
  3. Verify that the policy exists:

    gcloud org-policies list --project=PROJECT_ID
    

    The output is similar to the following:

    CONSTRAINT                   LIST_POLICY    BOOLEAN_POLICY    ETAG
    custom.restrictStorageClass  -              SET               CIqktscGELiZn6cC-
    

After you apply the policy, wait for about two minutes for Google Cloud to start enforcing the policy.

Test the policy

Try to create a default bucket using the Cloud Storage for Firebase REST API without specifying a storage class (which defaults to STANDARD):

PROJECT_ID="PROJECT_ID"
TOKEN=$(gcloud auth print-access-token)

curl -X POST "https://firebasestorage.googleapis.com/v1beta/projects/${PROJECT_ID}/defaultBucket" \
  -H "Authorization: Bearer ${TOKEN}" \
  -H "Content-Type: application/json" \
  -d '{"location": "US-EAST1"}'

The request fails with an error similar to the following:

FAILED_PRECONDITION: Operation denied by org policy: ["customConstraints/custom.restrictStorageClass": "Deny creating buckets with STANDARD storage class."].
-   '@type': type.googleapis.com/google.rpc.ErrorInfo
  domain: iam.googleapis.com
  metadata:
    customConstraints: customConstraints/custom.restrictStorageClass
  reason: CUSTOM_ORG_POLICY_VIOLATION

Example custom organization policies for common use cases

This table provides syntax examples for some common custom constraints.

Description Constraint syntax
Restrict creating default buckets with STANDARD storage class
name: organizations/ORGANIZATION_ID/customConstraints/custom.restrictStorageClass
resourceTypes:
-   firebasestorage.googleapis.com/DefaultBucket
methodTypes:
-   CREATE
condition: "resource.storageClass == 'STANDARD'"
actionType: DENY
displayName: Restrict Standard Storage Class
description: Deny creating buckets with STANDARD storage class.
Require default buckets to be created in a specific location
name: organizations/ORGANIZATION_ID/customConstraints/custom.requireDefaultBucketLocation
resourceTypes:
-   firebasestorage.googleapis.com/DefaultBucket
methodTypes:
-   CREATE
condition: "resource.location != 'US-EAST1'"
actionType: DENY
displayName: Require US-EAST1 Location
description: Deny creating default buckets outside the US-EAST1 location.

What's next