create_policy
¶
create_policy(
policy_store_id: PolicyStoreId,
definition: PolicyDefinition,
*,
config_overrides: Optional[VerifiedPermissionsClientConfig] = None,
client_token: Optional[IdempotencyToken] = None,
name: Optional[PolicyName] = None,
) -> CreatePolicyOutput
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
StaticPolicysection of thePolicyDefinition. -
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
templateLinkedsection of thePolicyDefinition. 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.
Parameters:
-
client_token(Optional[IdempotencyToken], default:None) –Specifies a unique, case-sensitive ID that you provide to ensure the idempotency of the request. This lets you safely retry the request without accidentally performing the same operation a second time. Passing the same value to a later call to an operation requires that you also pass the same value for all other parameters. We recommend that you use a UUID type of value..
If you don't provide this value, then Amazon Web Services generates a random one for you.
If you retry the operation with the same
ClientToken, but with different parameters, the retry fails with anConflictExceptionerror.Verified Permissions recognizes a
ClientTokenfor eight hours. After eight hours, the next request with the same parameters performs the operation again regardless of the value ofClientToken. -
policy_store_id(PolicyStoreId) –Specifies the
PolicyStoreIdof the policy store you want to store the policy in.To specify a policy store, use its ID or alias name. When using an alias name, prefix it with
policy-store-alias/. For example:-
ID:
PSEXAMPLEabcdefg111111 -
Alias name:
policy-store-alias/example-policy-store
To view aliases, use ListPolicyStoreAliases.
-
-
definition(PolicyDefinition) –A structure that specifies the policy type and content to use for the new policy. You must include either a static or a templateLinked element. The policy content must be written in the Cedar policy language.
-
name(Optional[PolicyName], default:None) –Specifies a name for the policy that is unique among all policies within the policy store. You can use the name in place of the policy ID in API operations that reference the policy. The name must be prefixed with
name/.If you specify a name that is already associated with another policy in the policy store, you receive a
ConflictExceptionerror.
Raises:
-
AccessDeniedException–You don't have sufficient access to perform this action.
-
InternalServerException–The request failed because of an internal error. Try your request again later
-
ThrottlingException–The request failed because it exceeded a throttling quota.
-
ValidationException–The request failed because one or more input parameters don't satisfy their constraint requirements. The output is provided as a list of fields and a reason for each field that isn't valid.
The possible reasons include the following:
-
UnrecognizedEntityType
The policy includes an entity type that isn't found in the schema.
-
UnrecognizedActionId
The policy includes an action id that isn't found in the schema.
-
InvalidActionApplication
The policy includes an action that, according to the schema, doesn't support the specified principal and resource.
-
UnexpectedType
The policy included an operand that isn't a valid type for the specified operation.
-
IncompatibleTypes
The types of elements included in a
set, or the types of expressions used in anif...then...elseclause aren't compatible in this context. -
MissingAttribute
The policy attempts to access a record or entity attribute that isn't specified in the schema. Test for the existence of the attribute first before attempting to access its value. For more information, see the has (presence of attribute test) operator in the Cedar Policy Language Guide.
-
UnsafeOptionalAttributeAccess
The policy attempts to access a record or entity attribute that is optional and isn't guaranteed to be present. Test for the existence of the attribute first before attempting to access its value. For more information, see the has (presence of attribute test) operator in the Cedar Policy Language Guide.
-
ImpossiblePolicy
Cedar has determined that a policy condition always evaluates to false. If the policy is always false, it can never apply to any query, and so it can never affect an authorization decision.
-
WrongNumberArguments
The policy references an extension type with the wrong number of arguments.
-
FunctionArgumentValidationError
Cedar couldn't parse the argument passed to an extension type. For example, a string that is to be parsed as an IPv4 address can contain only digits and the period character.
-
-
ConflictException–The request failed because another request to modify a resource occurred at the same time.
-
ResourceNotFoundException–The request failed because it references a resource that doesn't exist.
-
ServiceQuotaExceededException–The request failed because it would cause a service quota to be exceeded.
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To create a static policy 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.
>>> client.create_policy(definition={'static': {'description': 'Grant members of janeFriends UserGroup access to the vacationFolder Album', 'statement': 'permit( principal in UserGroup::"janeFriends", action, resource in Album::"vacationFolder" );'}}, policy_store_id='C7v5xMplfFH3i3e4Jrzb1a', client_token='a1b2c3d4-e5f6-a1b2-c3d4-TOKEN1111111', name='name/example-policy')
To create a template-linked policy
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.
>>> client.create_policy(definition={'templateLinked': {'policyTemplateId': 'PTEXAMPLEabcdefg111111', 'principal': {'entityType': 'User', 'entityId': 'alice'}}}, policy_store_id='C7v5xMplfFH3i3e4Jrzb1a', client_token='a1b2c3d4-e5f6-a1b2-c3d4-TOKEN1111111', name='name/example-template-linked-policy')
Source code in docs_src/capo_verifiedpermissions/_services/verified_permissions.py
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 | |