createPolicy

Creates a Cedar policy and saves it in the specified policy store. You can create either a static policy or a policy linked to a policy template.

  • To create a static policy, provide the Cedar policy text in the StaticPolicy section of the PolicyDefinition.

  • To create a policy that is dynamically linked to a policy template, specify the policy template ID and the principal and resource to associate with this policy in the templateLinked section of the PolicyDefinition. If the policy template is ever updated, any policies linked to the policy template automatically use the updated template.

Creating a policy causes it to be validated against the schema in the policy store. If the policy doesn't pass validation, the operation fails and the policy isn't stored.

Verified Permissions is eventually consistent. It can take a few seconds for a new or changed element to propagate through the service and be visible in the results of other Verified Permissions operations.

Samples

import aws.sdk.kotlin.services.verifiedpermissions.model.EntityIdentifier
import aws.sdk.kotlin.services.verifiedpermissions.model.PolicyDefinition
import aws.sdk.kotlin.services.verifiedpermissions.model.StaticPolicyDefinition
import aws.sdk.kotlin.services.verifiedpermissions.model.TemplateLinkedPolicyDefinition

fun main() { 
   //sampleStart 
   // The following example request creates a static policy with a policy scope that specifies both a
// principal and a resource. The response includes both the Principal and Resource elements because both were
// specified in the request policy scope.
val resp = verifiedPermissionsClient.createPolicy {
    definition = PolicyDefinition.Static(StaticPolicyDefinition {
        description = "Grant members of janeFriends UserGroup access to the vacationFolder Album"
        statement = "permit( principal in UserGroup::\"janeFriends\", action, resource in Album::\"vacationFolder\" );"
    }
    )
    policyStoreId = "C7v5xMplfFH3i3e4Jrzb1a"
    clientToken = "a1b2c3d4-e5f6-a1b2-c3d4-TOKEN1111111"
} 
   //sampleEnd
}
import aws.sdk.kotlin.services.verifiedpermissions.model.EntityIdentifier
import aws.sdk.kotlin.services.verifiedpermissions.model.PolicyDefinition
import aws.sdk.kotlin.services.verifiedpermissions.model.StaticPolicyDefinition
import aws.sdk.kotlin.services.verifiedpermissions.model.TemplateLinkedPolicyDefinition

fun main() { 
   //sampleStart 
   // The following example request creates a static policy with a policy scope that specifies both a
// principal and a resource. The response includes both the Principal and Resource elements because both were
// specified in the request policy scope.
val resp = verifiedPermissionsClient.createPolicy {
    definition = PolicyDefinition.Static(StaticPolicyDefinition {
        description = "Grant members of janeFriends UserGroup access to the vacationFolder Album"
        statement = "permit( principal in UserGroup::\"janeFriends\", action, resource in Album::\"vacationFolder\" );"
    }
    )
    policyStoreId = "C7v5xMplfFH3i3e4Jrzb1a"
    clientToken = "a1b2c3d4-e5f6-a1b2-c3d4-TOKEN1111111"
} 
   //sampleEnd
}
import aws.sdk.kotlin.services.verifiedpermissions.model.EntityIdentifier
import aws.sdk.kotlin.services.verifiedpermissions.model.PolicyDefinition
import aws.sdk.kotlin.services.verifiedpermissions.model.StaticPolicyDefinition
import aws.sdk.kotlin.services.verifiedpermissions.model.TemplateLinkedPolicyDefinition

fun main() { 
   //sampleStart 
   // The following example creates a template linked policy using the specified policy template and
// associates the specified principal to use with the new template linked policy.
val resp = verifiedPermissionsClient.createPolicy {
    definition = PolicyDefinition.TemplateLinked(TemplateLinkedPolicyDefinition {
        policyTemplateId = "PTEXAMPLEabcdefg111111"
        principal = EntityIdentifier {
            entityType = "User"
            entityId = "alice"
        }
    }
    )
    policyStoreId = "C7v5xMplfFH3i3e4Jrzb1a"
    clientToken = "a1b2c3d4-e5f6-a1b2-c3d4-TOKEN1111111"
} 
   //sampleEnd
}