openstack-manuals/doc/config-reference/app_policy_json.xml
Bernd Bausch 683999e929 Amendments to the introduction of policy.json
This is an amendment of https://review.openstack.org/#/c/166853/,
after additional information became available.
Specifically, information about the is_admin flag and a note on
tweaking policy.json are added, and some points reworded for
clarity.

While there is still room for improvement, the bug, previously
declared as partially fixed, can be closed now.

Change-Id: Ifd01bc4e367f65d6ce45c54a005bafb660535260
Closes-Bug: #1311067
2015-05-04 13:28:17 +09:00

267 lines
11 KiB
XML

<?xml version="1.0" encoding="UTF-8"?>
<appendix xmlns="http://docbook.org/ns/docbook"
xmlns:xi="http://www.w3.org/2001/XInclude"
xmlns:xlink="http://www.w3.org/1999/xlink"
version="5.0"
xml:id="policy-json-file">
<?dbhtml stop-chunking?>
<title>The policy.json file</title>
<para>Each OpenStack service, Identity, Compute, Networking and so on, has its own
role-based access policies. They determine which user can access which objects in
which way, and are defined in the service's <filename>policy.json</filename>
file.</para>
<para>Whenever an API call to an OpenStack service is made, the service's policy
engine uses the appropriate policy definitions to determine if the call can be
accepted. Any changes to <filename>policy.json</filename> are effective immediately,
which allows
new policies to be implemented while the service is running.</para>
<para>A <filename>policy.json</filename> file is a text file in JSON (Javascript
Object Notation) format. Each policy is defined by a one-line statement in the form
<literal>"&lt;target&gt;" : "&lt;rule&gt;"</literal>.</para>
<para>
The policy target, also named "action", represents an API call like "start an instance" or
"attach a volume".
</para>
<para>Action names are usually qualified.
Example: OpenStack Compute features API calls to list instances,
volumes and networks. In <filename>/etc/nova/policy.json</filename>, these APIs are
represented by <literal>compute:get_all</literal>, <literal>volume:get_all</literal>
and <literal>network:get_all</literal>, respectively.</para>
<para>The mapping between API calls and actions is not generally documented.</para>
<para>The policy rule determines under which circumstances the API call
is permitted. Usually this involves the user who makes the
call (hereafter named the "API user") and often the object on which the
API call operates. A typical rule checks if the API user is the object's owner.</para>
<caution><title>Modifying the policy</title>
<para>
While recipes for editing <filename>policy.json</filename> files
are found on blogs, modifying the policy can have unexpected side effects and is
not encouraged.
</para>
</caution>
<section xml:id="Examples">
<title>Examples</title>
<para>A simple rule might look like this:
<programlisting language="json">"compute:get_all" : ""</programlisting>
The target is <literal>"compute:get_all"</literal>, the "list all instances" API
of the Compute service. The rule is an empty string meaning "always". This policy
allows anybody to list instances.</para>
<para>You can also decline permission to use an API:
<programlisting language="json">"compute:shelve": "!"</programlisting>
The exclamation mark stands for "never" or "nobody", which effectively disables
the Compute API "shelve an instance".</para>
<para>Many APIs can only be called by admin users. This can be expressed by the rule
<literal>"role:admin"</literal>.
The following policy ensures that only administrators can create new users in the
Identity database:
<programlisting language="json">"identity:create_user" : "role:admin"</programlisting>
</para>
<para>You can limit APIs to any role. For example, the Orchestration service defines
a role named <literal>heat_stack_user</literal>. Whoever has this role isn't allowed to create stacks:
<programlisting language="json">"stacks:create": "not role:heat_stack_user"</programlisting>
This rule makes use of the boolean operator <literal>not</literal>.
More complex rules can be built using operators <literal>and</literal>, <literal>or</literal>
and parentheses.</para>
<para>You can define aliases for rules:
<programlisting language="json">"deny_stack_user": "not role:heat_stack_user"</programlisting>
The policy engine
understands that <literal>"deny_stack_user"</literal> is not an API and consequently
interprets it as an alias. The stack creation policy above can then be written as:
<programlisting language="json">"stacks:create": "rule:deny_stack_user"</programlisting>
This is taken verbatim from <filename>/etc/heat/policy.json</filename>.</para>
<para>Rules can compare API attributes to object attributes. For example:
<programlisting language="json">"compute:start" : "user_id:%(user_id)s"</programlisting>
states that only the owner of an instance can start it up. The <literal>user_id</literal>
string
before the colon is an API attribute, namely the user ID of the API user.
It is compared with the user ID of the object (in this case, an instance); more
precisely, it is compared with the <literal>user_id</literal> field of that object
in the database. If the two values are equal, permission is
granted.</para>
<para>An admin user always has permission to call APIs.
This is how <filename>/etc/keystone/policy.json</filename> makes this policy explicit:
<programlisting language="json">"admin_required": "role:admin or is_admin:1",
"owner" : "user_id:%(user_id)s",
"admin_or_owner": "rule:admin_required or rule:owner",
"identity:change_password": "rule:admin_or_owner"</programlisting>
The first line defines an alias for "user is an admin user".
The <literal>is_admin</literal> flag is only used when setting
up the Identity service for the first time. It indicates that the user
has admin privileges granted by the service token
(<parameter>--os-token</parameter>
parameter of the <command>keystone</command> command line client).
</para>
<para>The second line creates an alias for "user owns the object" by comparing
the API's user ID with the object's user ID.
</para>
<para>Line 3 defines a third alias <literal>admin_or_owner</literal>, combining
the two first aliases with the Boolean operator <literal>or</literal>.
</para>
<para>
Line 4 sets up the policy that
a password can only be modified by its owner or an admin user.
</para>
<para>
As a final example, let's examine a more complex rule:
<programlisting language="json">"identity:ec2_delete_credential": "rule:admin_required or
(rule:owner and user_id:%(target.credential.user_id)s)"
</programlisting>
This rule determines who can use the Identity API "delete EC2 credential".
Here, boolean operators and parentheses combine three simpler rules.
<literal>admin_required</literal> and <literal>owner</literal> are the same aliases as in the
previous example. <literal>user_id:%(target.credential.user_id)s</literal> compares the
API user with the user ID of the credential object associated with the target.
</para>
</section>
<section xml:id="syntax">
<title>Syntax</title>
<para>A <filename>policy.json</filename> file consists of policies and aliases of the
form <literal>target:rule</literal> or <literal>alias:definition</literal>, separated by commas
and enclosed in curly braces:
<programlisting language="json"> {
"alias 1" : "definition 1",
"alias 2" : "definition 2",
...
"target 1" : "rule 1",
"target 2" : "rule 2",
....
}</programlisting>
</para>
<para>Targets are APIs and are written <literal>"service:API"</literal> or simply
<literal>"API"</literal>. For example, <literal>"compute:create"</literal> or
<literal>"add_image"</literal>.</para>
<para>Rules determine whether the API call is allowed.</para>
<para>Rules can be:
<itemizedlist>
<listitem><para>always true. The action is always permitted. This can be written as
<literal>""</literal> (empty string), <literal>[]</literal>, or <literal>"@"</literal>.</para></listitem>
<listitem><para>always false. The action is never permitted. Written as <literal>"!"</literal>.</para></listitem>
<listitem><para>a special check</para></listitem>
<listitem><para>a comparison of two values</para></listitem>
<listitem><para>boolean expressions based on simpler rules</para></listitem>
</itemizedlist>
</para>
<para>Special checks are
<itemizedlist>
<listitem><para><literal>&lt;role&gt;:&lt;role name&gt;</literal>, a test whether the API credentials
contain this role.</para></listitem>
<listitem><para><literal>&lt;rule&gt;:&lt;rule name&gt;</literal>,
the definition of an alias.</para></listitem>
<listitem><para><literal>http:&lt;target URL&gt;</literal>,
which delegates the check to a remote server.
The API is authorized when the server returns True.</para></listitem>
</itemizedlist>
</para>
<para>Developers can define additional special checks.</para>
<para>Two values are compared in the following way:
<programlisting language="json">"value1 : value2"</programlisting>
</para>
<para>Possible values are
<itemizedlist>
<listitem><para>constants: Strings, numbers, <literal>true</literal>, <literal>false</literal></para></listitem>
<listitem><para>API attributes</para></listitem>
<listitem><para>target object attributes</para></listitem>
<listitem><para>the flag <literal>is_admin</literal></para></listitem>
</itemizedlist>
</para>
<para>API attributes can be <literal>project_id</literal>, <literal>user_id</literal> or
<literal>domain_id</literal>.</para>
<para>Target object attributes are fields from the object description in the database.
For example in the case of the <literal>"compute:start"</literal> API, the object is the
instance to be started. The policy for starting instances could use the
<literal>%(project_id)s</literal>attribute, that is the project that owns the instance.
The trailing s indicates this is a string.</para>
<para><literal>is_admin</literal> indicates that administrative privileges
are granted via the admin token mechanism (the <parameter>--os-token</parameter>
option of the <command>keystone</command> command).
The admin token allows initialisation of the identity database before the admin
role exists.</para>
<para>The alias construct exists for convenience. An alias is short name for a
complex or hard to understand rule.
It is defined in the same way as a policy:
<programlisting language="json">alias name : alias definition</programlisting>
Once an alias is defined, use the <literal>rule</literal> keyword to use it in a policy rule.</para>
</section>
<section xml:id="older-syntax">
<title>Older syntax</title>
<para>You may encounter older <filename>policy.json</filename> files that feature a different syntax,
where JavaScript arrays are used instead of boolean operators.
For example, the EC2 credentials rule above would have been written as
follows:
<programlisting language="json">"identity:ec2_delete_credential": [ [ "rule:admin_required ],
[ "rule:owner", "user_id:%(target.credential.user_id)s)" ] ]
</programlisting>
The rule is an array of arrays. The innermost arrays are or'ed together,
whereas elements inside the innermost arrays are and'ed.</para>
<para>While the old syntax is still supported, we recommend using the newer, more
intuitive syntax.</para>
</section>
</appendix>