Manage Cloud Storage resources using custom constraints

Organization Policy provides predefined constraints for Cloud Storage. 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.

This page describes how to set custom constraints to enforce policies on Cloud Storage resources.

To test a new constraint before it is enforced on your production environment, use the Policy Simulator.

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 an organization, Google Cloud enforces the policy on all projects in the organization. To learn more about this behavior and how to change it, refer to Hierarchy evaluation rules.

Pricing

The Organization Policy Service, including predefined and custom constraints, is offered at no charge.

Limitations

  • Custom constraints for Cloud Storage resources can only be set up by using the Google Cloud console or Google Cloud CLI.

  • Custom constraints can only be enforced on the CREATE or UPDATE methods for Cloud Storage resources.

  • Newly enforced custom constraints don't apply automatically to existing resources. Existing resources must be updated for the constraint to apply.

    To find existing resources that will need to be updated, you can enforce a dry-run organization policy.

  • Custom constraints cannot be used to constrain ACLs or IAM policies on objects or buckets.

Cloud Storage supported resources

For Cloud Storage, you can set custom constraints on the following resource:

  • Buckets: storage.googleapis.com/Bucket

Required roles

For information about the roles required to manage organization policies with custom constraints, see Required roles.

In addition to managing organization policies, you might want to test the custom constraints you create. To test custom constraints, it's recommended that you use the least permissive predefined or custom role that contains the permissions required to test the specific constraint. To see which permissions and roles are required, refer to roles and permissions for Cloud Storage.

Set up a custom constraint

Console

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

    Go to Organization policies

  2. Select the Project picker at the top of the page.

  3. From the Project picker, select the resource for which you want to set the organization policy.

  4. Click Custom constraint.

  5. In the Display name field, enter a human-friendly name for the constraint. This field has a maximum length of 200 characters. Don't use PII or sensitive data in constraint names, because they could be exposed in error messages.

  6. In the Constraint ID box, enter the name you want for your new custom constraint. A custom constraint must start with custom., and can only include uppercase letters, lowercase letters, or numbers, for example, custom.enforceBucketVersioning. The maximum length of this field is 70 characters, not counting the prefix, for example, organizations/123456789/customConstraints/custom..

  7. In the Description field, enter a human-friendly description of the constraint to display as an error message when the policy is violated. This field has a maximum length of 2000 characters.

  8. In the Resource type field, select the name of the Google Cloud REST resource containing the object and field you want to restrict. For example, storage.googleapis.com/Bucket.

  9. Under Enforcement method, select whether to enforce the constraint on the REST CREATE or UPDATE method.

  10. 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.versioning.enabled == true. This field has a maximum length of 1000 characters.

    2. Click Save.

  11. Under Action, select whether to allow or deny the evaluated method if the condition is met.

  12. Click Create constraint.

When you have entered a value into each field, the equivalent YAML configuration for this custom constraint appears on the right.

gcloud

To create a custom constraint using the Google Cloud CLI, create a YAML file for the custom constraint:

name: organizations/ORGANIZATION_ID/customConstraints/CONSTRAINT_NAME
resourceTypes:
- RESOURCE_NAME
methodTypes:
- METHOD1
- METHOD2
condition: "CONDITION"
actionType: ACTION
displayName: DISPLAY_NAME
description: DESCRIPTION

Replace the following:

  • ORGANIZATION_ID: your organization ID, such as 123456789.

  • CONSTRAINT_NAME: the name you want for your new custom constraint. A custom constraint must start with custom., and can only include uppercase letters, lowercase letters, or numbers, for example, custom.enforceBucketVersioning. The maximum length of this field is 70 characters, not counting the prefix, for example, organizations/123456789/customConstraints/custom..

  • RESOURCE_NAME: the fully qualified name of the Google Cloud REST resource containing the object and field you want to restrict. For example, storage.googleapis.com/Bucket.

  • METHOD1,METHOD2: a list of RESTful methods for which to enforce the constraint. Can be CREATE or CREATE and UPDATE.

  • CONDITION: a CEL condition that refers to a supported service resource, for example "resource.versioning.enabled == true". This field has a maximum length of 1000 characters. For details about CEL usage, see Common Expression Language.

  • ACTION: the action to take if the condition is met. This can be either ALLOW or DENY.

  • DISPLAY_NAME: a human-friendly name for the constraint. This field has a maximum length of 200 characters.

  • DESCRIPTION: a human-friendly description of the constraint to display as an error message when the policy is violated. This field has a maximum length of 2000 characters.

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. Once completed, your custom constraints are available as organization policies in your list of Google Cloud organization policies. 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
Replace ORGANIZATION_ID with the ID of your organization resource. For more information, see Viewing organization policies.

If the request is successful, the output is similar to the following:

CUSTOM_CONSTRAINT             ACTION_TYPE  METHOD_TYPES   RESOURCE_TYPES                     DISPLAY_NAME
custom.uniformBucketLevelAccess  DENY         CREATE,UPDATE  storage.googleapis.com/Bucket  Enable object versioning

For more information on setting up and managing custom constraints, see Create and manage custom constraints.

Enforce a constraint

You can enforce a boolean 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 for which you want to set the organization policy.
  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 enforcement of this organization policy is on or off.
  8. Optional: To make the organization policy conditional on a tag, click Add condition. Note that if you add a conditional rule to an organization policy, you must add at least one unconditional rule or the policy cannot be saved. For more information, see Setting an organization policy with tags.
  9. If this is a custom constraint, you can click Test changes to simulate the effect of this organization policy. For more information, see Test organization policy changes with Policy Simulator.
  10. To finish and apply the organization policy, click Set policy. The policy requires up to 15 minutes to take effect.

gcloud

To create an organization policy that enforces a boolean constraint, create a policy YAML file that references the constraint:

      name: projects/PROJECT_ID/policies/CONSTRAINT_NAME
      spec:
        rules:
        - enforce: true
    

Replace the following:

  • PROJECT_ID: the project on which you want to enforce your constraint.
  • CONSTRAINT_NAME: the name you defined for your custom constraint. For example, custom.uniformBucketLevelAccess.

To enforce the organization policy containing the constraint, run the following command:

    gcloud org-policies set-policy POLICY_PATH
    

Replace POLICY_PATH with the full path to your organization policy YAML file. The policy requires up to 15 minutes to take effect.

Requests that get denied for violating a custom constraint fail with a 412 error: CUSTOM_ORGPOLICY_CONSTRAINT_FAILED.

Example: Create a constraint that enforces customer-managed encryption keys on buckets

gcloud

  1. Create an enforceCMEK.yaml constraint file with the following information:

    name: organizations/ORGANIZATION_ID/customConstraints/custom.customerManagedEncryptionKeys
    resource_types: storage.googleapis.com/Bucket
    method_types:
    – CREATE
    – UPDATE
    condition: "has(resource.encryption.defaultKmsKeyName)"
    action_type: ALLOW
    display_name: Enforce Cloud KMS key
    description: When this constraint is enforced, newly created buckets and newly updated buckets must be encrypted with a
    Cloud KMS key. The Cloud KMS key on existing buckets can be updated but not deleted.
  2. Set the custom constraint.

    gcloud org-policies set-custom-constraint enforceCMEK.yaml
    
  3. Create a enforceCMEK-policy.yaml policy file with the following information.

    name: projects/PROJECT_ID/policies/custom.customerManagedEncryptionKeys
    spec:
      rules:
      – enforce: true
    

    Replace PROJECT_ID with your project ID.

    In this example, we enforce this constraint at the project level, but you might also set this at the organization or folder level.

  4. Enforce the policy.

    gcloud org-policies set-policy enforceCMEK-policy.yaml
    

Example custom constraints for common use cases

The following sections provide the syntax of some custom constraints that you might find useful:

Use case Syntax
Bucket retention policies must have a period that falls within the specified durations
      name: organizations/ORGANIZATION_ID/customConstraints/custom.retentionPolicy
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.retentionPolicy.retentionPeriod not in [3600, 2678400]"
      action_type: DENY
      display_name: Bucket retention policy is either 3,600 seconds or 2,678,400 seconds
      description: Newly created buckets and newly updated buckets must have a
      retention policy that's either 3,600 seconds or 2,678,400 seconds.
Buckets must have Object Versioning enabled
      name: organizations/ORGANIZATION_ID/customConstraints/custom.enforceBucketVersioning
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.versioning.enabled == true"
      action_type: ALLOW
      display_name: Buckets must have Object Versioning enabled
      description: Newly created buckets and newly updated buckets must have Object Versioning enabled.
Buckets must be named using a specific regular expression
      name: organizations/ORGANIZATION_ID/customConstraints/custom.bucketName
      method_types:
      – CREATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.name.matches('^[a-zA-Z]+$')"
      action_type: ALLOW
      display_name: Bucket names must match the specified regular expression
      description: Newly created buckets must have a name that matches the
      specified regular expression. Only letters are allowed in the bucket name.
Buckets cannot have Bucket Lock enabled
      name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitBucketLock
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.retentionPolicy.isLocked == true"
      action_type: DENY
      display_name: Prohibit the use of Bucket Lock
      description: Newly created buckets and newly updated buckets cannot have
      Bucket Lock enabled.
Buckets cannot have Object Retention Lock enabled
      name: organizations/ORGANIZATION_ID/customConstraints/custom.prohibitObjectRetentionLock
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.objectRetention.mode == 'Enabled'"
      action_type: DENY
      display_name: Objects cannot have retention configurations
      description: Newly created buckets and newly updated buckets cannot have
      Object Retention Lock enabled.
Buckets located in the US or EU multi-regions must have a retention period of 86,400 seconds
      name: organizations/ORGANIZATION_ID/customConstraints/custom.locationRetentionPolicy
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "(resource.location.startsWith('US') || resource.location.startsWith('EU')) && resource.retentionPolicy.retentionPeriod != 86400"
      action_type: DENY
      display_name: All buckets in US and EU must have a retention policy of 86,400 seconds
      description: Newly created buckets and newly updated buckets located in
      US and EU regions must have a retention policy of 86,400 seconds.
Buckets must have labels1
      name: organizations/ORGANIZATION_ID/customConstraints/custom.labels
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "'my_annotations.data.source' in resource.labels && resource.labels['my_annotations.data.source'] in ['SOURCE_IMAGES','SOURCE_TEXT','SOURCE_VIDEOS']"
      action_type: ALLOW
      display_name: Buckets must have a label classifying the contents of the bucket
      description: Newly created buckets and newly updated buckets must have the
      label my_annotations.data.source with the SOURCE_IMAGES, SOURCE_TEXT, or
      SOURCE_VIDEOS key.
Buckets must be located in a dual-region
      name: organizations/ORGANIZATION_ID/customConstraints/custom.dualRegionUS
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "'US-EAST1' in resource.customPlacementConfig.dataLocations && 'US-EAST4' in resource.customPlacementConfig.dataLocations"
      action_type: ALLOW
      display_name: Buckets must be located in a dual-region
      description: Newly created buckets and newly updated buckets must be located in a dual-region
      composed of the us-east1 and us-east4 regions.
Buckets cannot use legacy storage classes
      name: organizations/ORGANIZATION_ID/customConstraints/custom.disableLegacyStorageClass
      method_types:
      – CREATE
      – UPDATE
      resource_types: storage.googleapis.com/Bucket
      condition: "resource.storageClass in ['STANDARD', 'NEARLINE', 'COLDLINE', 'ARCHIVE']"
      action_type: ALLOW
      display_name: Buckets cannot use legacy storage classes
      description: Newly created buckets and newly updated buckets must use
      Standard storage, Nearline storage, Coldline storage, or Archive storage.

1 Specifying a bucket label key that does not exist returns a BAD_CONDITION error. This error blocks the CREATE and UPDATE methods from being run on the bucket. To avoid this error, always check that the label key exists first using "my_annotations.data.source" in resource.labels.

Expression fields for conditions

The following table contains the expression fields you can use to create conditions. Conditions are written in Common Expression Language (CEL). Note that the value of these expression fields are case sensitive.

For descriptions of the following expression fields and what values you can specify, refer to the Buckets resource representation for the JSON API.

Expression field Value type
billing.requesterPays bool
cors list
cors.maxAgeSeconds int
cors.method list
cors.origin list
cors.responseHeader list
customPlacementConfig.dataLocations1 list
defaultEventBasedHold bool
encryption.defaultKmsKeyName string
iamConfiguration.publicAccessPrevention string
iamConfiguration.uniformBucketLevelAccess.enabled bool
labels map
lifecycle.rule list
lifecycle.rule.action.storageClass1 string
lifecycle.rule.action.type string
lifecycle.rule.condition.age int
lifecycle.rule.condition.createdBefore string
lifecycle.rule.condition.customTimeBefore string
lifecycle.rule.condition.daysSinceCustomTime int
lifecycle.rule.condition.daysSinceNoncurrentTime int
lifecycle.rule.condition.isLive bool
lifecycle.rule.condition.matchesPrefix list
lifecycle.rule.condition.matchesStorageClass list
lifecycle.rule.condition.matchesSuffix list
lifecycle.rule.condition.noncurrentTimeBefore string
lifecycle.rule.condition.numNewerVersions int
location1 string
locationType string
logging.logBucket string
logging.logObjectPrefix string
objectRetention.mode string
name string
projectNumber2 string
retentionPolicy.isLocked bool
retentionPolicy.retentionPeriod int
rpo string
softDeletePolicy.retentionDurationSeconds int
storageClass1 string
versioning.enabled bool
website.mainPageSuffix string
website.notFoundPage string

1 The value to this field must be written in uppercase.

2 This field is deprecated.

Considerations

Bucket labels aren't recommended for usage in custom constraint conditions. Instead, use tags, which can only be set by individuals with required IAM roles and are more tightly controlled than labels.