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_NAMEresourceTypes:-RESOURCE_NAMEmethodTypes:-METHOD1-METHOD2condition:"CONDITION"actionType:ACTIONdisplayName:DISPLAY_NAMEdescription: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:

gcloudorg-policiesset-custom-constraintCONSTRAINT_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:
gcloudorg-policieslist-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_NAMEspec: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:

gcloudorg-policiesset-policyPOLICY_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.customerManagedEncryptionKeysresource_types:storage.googleapis.com/Bucketmethod_types:CREATEUPDATEcondition:"has(resource.encryption.defaultKmsKeyName)"action_type:ALLOWdisplay_name:Enforce Cloud KMS keydescription:When this constraint is enforced, newly created buckets and newly updated buckets must be encrypted with aCloud 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.customerManagedEncryptionKeysspec: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 caseSyntax
Bucket retention policies must have a period that falls within the specified durations
name:organizations/ORGANIZATION_ID/customConstraints/custom.retentionPolicymethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"resource.retentionPolicy.retentionPeriodnotin[3600,2678400]"action_type:DENYdisplay_name:Bucket retention policy is either 3,600 seconds or 2,678,400 secondsdescription:Newly created buckets and newly updated buckets must have aretention policy that's either 3,600 seconds or 2,678,400 seconds.
Buckets must have Object Versioning enabled
name:organizations/ORGANIZATION_ID/customConstraints/custom.enforceBucketVersioningmethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"resource.versioning.enabled==true"action_type:ALLOWdisplay_name:Buckets must have Object Versioning enableddescription: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.bucketNamemethod_types:CREATEresource_types:storage.googleapis.com/Bucketcondition:"resource.name.matches('^[a-zA-Z]+$')"action_type:ALLOWdisplay_name:Bucket names must match the specified regular expressiondescription:Newly created buckets must have a name that matches thespecified regular expression. Only letters are allowed in the bucket name.
Buckets cannot have Bucket Lock enabled
name:organizations/ORGANIZATION_ID/customConstraints/custom.prohibitBucketLockmethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"resource.retentionPolicy.isLocked==true"action_type:DENYdisplay_name:Prohibit the use of Bucket Lockdescription:Newly created buckets and newly updated buckets cannot haveBucket Lock enabled.
Buckets cannot have Object Retention Lock enabled
name:organizations/ORGANIZATION_ID/customConstraints/custom.prohibitObjectRetentionLockmethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"resource.objectRetention.mode=='Enabled'"action_type:DENYdisplay_name:Objects cannot have retention configurationsdescription:Newly created buckets and newly updated buckets cannot haveObject 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.locationRetentionPolicymethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"(resource.location.startsWith('US')||resource.location.startsWith('EU'))&&resource.retentionPolicy.retentionPeriod!=86400"action_type:DENYdisplay_name:All buckets in US and EU must have a retention policy of 86,400 secondsdescription:Newly created buckets and newly updated buckets located inUS and EU regions must have a retention policy of 86,400 seconds.
Buckets must have labels1
name:organizations/ORGANIZATION_ID/customConstraints/custom.labelsmethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"'my_annotations.data.source'inresource.labels&&resource.labels['my_annotations.data.source']in['SOURCE_IMAGES','SOURCE_TEXT','SOURCE_VIDEOS']"action_type:ALLOWdisplay_name:Buckets must have a label classifying the contents of the bucketdescription:Newly created buckets and newly updated buckets must have thelabel my_annotations.data.source with the SOURCE_IMAGES, SOURCE_TEXT, orSOURCE_VIDEOS key.
Buckets must be located in a dual-region
name:organizations/ORGANIZATION_ID/customConstraints/custom.dualRegionUSmethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"'US-EAST1'inresource.customPlacementConfig.dataLocations&&'US-EAST4'inresource.customPlacementConfig.dataLocations"action_type:ALLOWdisplay_name:Buckets must be located in a dual-regiondescription:Newly created buckets and newly updated buckets must be located in a dual-regioncomposed of the us-east1 and us-east4 regions.
Buckets cannot use legacy storage classes
name:organizations/ORGANIZATION_ID/customConstraints/custom.disableLegacyStorageClassmethod_types:CREATEUPDATEresource_types:storage.googleapis.com/Bucketcondition:"resource.storageClassin['STANDARD','NEARLINE','COLDLINE','ARCHIVE']"action_type:ALLOWdisplay_name:Buckets cannot use legacy storage classesdescription:Newly created buckets and newly updated buckets must useStandard storage, Nearline storage, Coldline storage, or Archive storage.
Bucket IP filtering must restrict requests from all the public internet
name:organizations/ORGANIZATION_ID/customConstraints/custom.IpFiltermethod_types:CREATEresource_types:storage.googleapis.com/Bucketcondition:"!has(resource.ipFilter)||(resource.ipFilter.mode=='Disabled'||resource.ipFilter.publicNetworkSource.allowedIpCidrRanges.size()>0)"action_type:DENYdisplay_name:Bucket IP filter rules must restrict all the public networkdescription:Newly created buckets must have IP filtering and IP filtering rules must restrict all public network resources.

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 fieldValue type
billing.requesterPaysbool
corslist
cors.maxAgeSecondsint
cors.methodlist
cors.originlist
cors.responseHeaderlist
customPlacementConfig.dataLocations1list
defaultEventBasedHoldbool
encryption.defaultKmsKeyNamestring
iamConfiguration.publicAccessPreventionstring
iamConfiguration.uniformBucketLevelAccess.enabledbool
labelsmap
lifecycle.rulelist
lifecycle.rule.action.storageClass1string
lifecycle.rule.action.typestring
lifecycle.rule.condition.ageint
lifecycle.rule.condition.createdBeforestring
lifecycle.rule.condition.customTimeBeforestring
lifecycle.rule.condition.daysSinceCustomTimeint
lifecycle.rule.condition.daysSinceNoncurrentTimeint
lifecycle.rule.condition.isLivebool
lifecycle.rule.condition.matchesPrefixlist
lifecycle.rule.condition.matchesStorageClasslist
lifecycle.rule.condition.matchesSuffixlist
lifecycle.rule.condition.noncurrentTimeBeforestring
lifecycle.rule.condition.numNewerVersionsint
location1string
locationTypestring
logging.logBucketstring
logging.logObjectPrefixstring
ipFilter.modestring
ipFilter.publicNetworkSourceobject
ipFilter.publicNetworkSource.allowedIpCidrRangeslist
ipFilter.vpcNetworkSourceslist
ipFilter.vpcNetworkSources.networkstring
ipFilter.vpcNetworkSources.allowedIpCidrRangeslist
objectRetention.modestring
namestring
projectNumber2string
retentionPolicy.isLockedbool
retentionPolicy.retentionPeriodint
rpostring
softDeletePolicy.retentionDurationSecondsint
storageClass1string
versioning.enabledbool
website.mainPageSuffixstring
website.notFoundPagestring

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.