> Documentation index: https://firebase.google.com/docs/storage/llms.txt

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](https://cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints).

## 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](https://cloud.google.com/iam/docs/roles-permissions/orgpolicy#orgpolicy.policyAdmin),
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](https://cloud.google.com/resource-manager/docs/cloud-platform-resource-hierarchy).
You can enforce organization policies at the organization, folder, or project
level.

Organization Policy provides built-in [managed
constraints](https://cloud.google.com/resource-manager/docs/organization-policy/org-policy-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](https://cloud.google.com/resource-manager/docs/organization-policy/hierarchy-evaluation#disallow_inheritance).

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](https://cloud.google.com/storage/docs/custom-constraints)
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](https://cloud.google.com/resource-manager/docs/organization-policy/test-policies).
- **Access control restrictions**: Custom constraints can't restrict access control lists (ACLs) or IAM policies on buckets.

## Before you begin

> [!NOTE]
> **Note:** To use organization policies and custom constraints, your Firebase project must be part of a Google Cloud organization resource. Because many Firebase projects aren't associated with an organization by default, verify that your project belongs to an organization before attempting the steps on this page. To learn more, see [Creating and managing
> organizations](https://cloud.google.com/resource-manager/docs/creating-managing-organization).

1. In the [Firebase console](https://console.firebase.google.com/), select
   or create a Firebase project.

   Ensure that your project is part of an organization and that you know your
   [organization
   ID](https://cloud.google.com/resource-manager/docs/creating-managing-organization#retrieving_your_organization_id).
2. To set up and enforce custom constraints using the command line,
   [install the Google Cloud CLI](https://cloud.google.com/sdk/docs/install)
   (`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](https://cloud.google.com/iam/docs/roles-permissions/orgpolicy#orgpolicy.policyAdmin)
(`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](https://cloud.google.com/storage/docs/custom-constraints#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)](https://github.com/google/cel-spec/blob/master/doc/intro.md)
. For more information about how to build conditions in custom constraints using
CEL, see the CEL section of [Creating and managing custom
constraints](https://cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints#common_expression_language).

### 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](https://console.cloud.google.com/iam-admin/orgpolicies)
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](https://cloud.google.com/resource-manager/docs/organization-policy/creating-managing-custom-constraints#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:

   ```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.
   ```
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`](https://cloud.google.com/sdk/gcloud/reference/org-policies/set-custom-constraint) command:

   ```bash
   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`](https://cloud.google.com/sdk/gcloud/reference/org-policies/list-custom-constraints) command:

   ```bash
   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](https://console.cloud.google.com/iam-admin/orgpolicies)
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](https://cloud.google.com/resource-manager/docs/organization-policy/test-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:

   ```yaml
   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](https://cloud.google.com/resource-manager/docs/organization-policy/test-policies), run the following command with the `dryRunSpec` flag:

   ```bash
   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:

   ```bash
   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.

> [!NOTE]
> **Note:** **Default Values** : When creating a default bucket in Cloud Storage for Firebase, if the `storage_class` attribute is omitted in your request, the physical bucket defaults to `STANDARD`. The `firebasestorage` API automatically populates `storage_class: "STANDARD"` during admission control. Therefore, an empty `storage_class` request evaluates against and is successfully blocked by this custom constraint.

### Create the constraint

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

   ```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:

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

   ```bash
   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`:

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

   Replace `PROJECT_ID` with your project ID.
2. Apply the policy:

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

   ```bash
   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`):

```bash
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:

```bash
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 | ```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. ``` |
| Require default buckets to be created in a specific location | ```yaml 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

- Learn more about [Organization Policy Service](https://cloud.google.com/resource-manager/docs/organization-policy/overview).
- Learn more about how to [create and manage organization policies](https://cloud.google.com/resource-manager/docs/organization-policy/create-organization-policies).
- See the full list of managed [organization policy constraints](https://cloud.google.com/resource-manager/docs/organization-policy/org-policy-constraints).