summaryrefslogtreecommitdiff
path: root/oslo_policy/policy.py
blob: 187669b405a687c7a3c001ad621be576a3b8c191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012 OpenStack Foundation.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

"""
Common Policy Engine Implementation

Policies are expressed as a target and an associated rule::

    "<target>": <rule>

The `target` is specific to the service that is conducting policy
enforcement.  Typically, the target refers to an API call.

For the `<rule>` part, see `Policy Rule Expressions`.

Policy Rule Expressions
~~~~~~~~~~~~~~~~~~~~~~~

Policy rules can be expressed in one of two forms: a string written in the new
policy language or a list of lists. The string format is preferred since it's
easier for most people to understand.

In the policy language, each check is specified as a simple "a:b" pair that is
matched to the correct class to perform that check:

 +--------------------------------+------------------------------------------+
 |            TYPE                |                SYNTAX                    |
 +================================+==========================================+
 |User's Role                     |              role:admin                  |
 +--------------------------------+------------------------------------------+
 |Rules already defined on policy |          rule:admin_required             |
 +--------------------------------+------------------------------------------+
 |Against URLs¹                   |         http://my-url.org/check          |
 +--------------------------------+------------------------------------------+
 |User attributes²                |    project_id:%(target.project.id)s      |
 +--------------------------------+------------------------------------------+
 |Strings                         |        - <variable>:'xpto2035abc'        |
 |                                |        - 'myproject':<variable>          |
 +--------------------------------+------------------------------------------+
 |                                |         - project_id:xpto2035abc         |
 |Literals                        |         - domain_id:20                   |
 |                                |         - True:%(user.enabled)s          |
 +--------------------------------+------------------------------------------+

¹URL checking must return ``True`` to be valid

²User attributes (obtained through the token): user_id, domain_id or project_id

Conjunction operators ``and`` and ``or`` are available, allowing for more
expressiveness in crafting policies. For example::

    "role:admin or (project_id:%(project_id)s and role:projectadmin)"

The policy language also has the ``not`` operator, allowing a richer
policy rule::

    "project_id:%(project_id)s and not role:dunce"

Operator precedence is below:

 +------------+-------------+-------------+
 | PRECEDENCE |     TYPE    | EXPRESSION  |
 +============+=============+=============+
 |      4     |  Grouping   |    (...)    |
 +------------+-------------+-------------+
 |      3     | Logical NOT |   not ...   |
 +------------+-------------+-------------+
 |      2     | Logical AND | ... and ... |
 +------------+-------------+-------------+
 |      1     | Logical OR  | ... or ...  |
 +------------+-------------+-------------+

Operator with larger precedence number precedes others with smaller numbers.

In the list-of-lists representation, each check inside the innermost
list is combined as with an "and" conjunction -- for that check to pass,
all the specified checks must pass.  These innermost lists are then
combined as with an "or" conjunction. As an example, take the following
rule, expressed in the list-of-lists representation::

    [["role:admin"], ["project_id:%(project_id)s", "role:projectadmin"]]

Finally, two special policy checks should be mentioned; the policy
check "@" will always accept an access, and the policy check "!" will
always reject an access.  (Note that if a rule is either the empty
list (``[]``) or the empty string (``""``), this is equivalent to the "@"
policy check.)  Of these, the "!" policy check is probably the most useful,
as it allows particular rules to be explicitly disabled.

Generic Checks
~~~~~~~~~~~~~~

A `generic` check is used to perform matching against attributes that are sent
along with the API calls.  These attributes can be used by the policy engine
(on the right side of the expression), by using the following syntax::

    <some_attribute>:%(user.id)s

The value on the right-hand side is either a string or resolves to a
string using regular Python string substitution.  The available attributes
and values are dependent on the program that is using the common policy
engine.

All of these attributes (related to users, API calls, and context) can be
checked against each other or against constants.  It is important to note
that these attributes are specific to the service that is conducting
policy enforcement.

Generic checks can be used to perform policy checks on the following user
attributes obtained through a token:

    - user_id
    - domain_id or project_id (depending on the token scope)
    - list of roles held for the given token scope

.. note::
   Some resources which are exposed by the API do not support policy
   enforcement by user_id, and only support policy enforcement by project_id.
   Some global resources do not support policy enforcement by combination of
   user_id and project_id.

For example, a check on the user_id would be defined as::

    user_id:<some_value>

Together with the previously shown example, a complete generic check
would be::

    user_id:%(user.id)s

It is also possible to perform checks against other attributes that
represent the credentials.  This is done by adding additional values to
the ``creds`` dict that is passed to the
:meth:`~oslo_policy.policy.Enforcer.enforce` method.

Special Checks
~~~~~~~~~~~~~~

Special checks allow for more flexibility than is possible using generic
checks.  The built-in special check types are ``role``, ``rule``, and ``http``
checks.

Role Check
^^^^^^^^^^

A ``role`` check is used to check if a specific role is present in the supplied
credentials.  A role check is expressed as::

    "role:<role_name>"

Rule Check
^^^^^^^^^^

A :class:`rule check <oslo_policy.policy.RuleCheck>` is used to
reference another defined rule by its name.  This allows for common
checks to be defined once as a reusable rule, which is then referenced
within other rules.  It also allows one to define a set of checks as a
more descriptive name to aid in readability of policy.  A rule check is
expressed as::

    "rule:<rule_name>"

The following example shows a role check that is defined as a rule,
which is then used via a rule check::

    "admin_required": "role:admin"
    "<target>": "rule:admin_required"

HTTP Check
^^^^^^^^^^

An ``http`` check is used to make an HTTP request to a remote server to
determine the results of the check.  The target and credentials are passed to
the remote server for evaluation.  The action is authorized if the remote
server returns a response of ``True``. An http check is expressed as::

    "http:<target URI>"

It is expected that the target URI contains a string formatting keyword,
where the keyword is a key from the target dictionary.  An example of an
http check where the `name` key from the target is used to construct the
URL is would be defined as::

    "http://server.test/%(name)s"

Registering New Special Checks
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It is also possible for additional special check types to be registered
using the :func:`~oslo_policy.policy.register` function.

The following classes can be used as parents for custom special check types:

* :class:`~oslo_policy.policy.AndCheck`
* :class:`~oslo_policy.policy.NotCheck`
* :class:`~oslo_policy.policy.OrCheck`
* :class:`~oslo_policy.policy.RuleCheck`

Default Rule
~~~~~~~~~~~~

A default rule can be defined, which will be enforced when a rule does
not exist for the target that is being checked.  By default, the rule
associated with the rule name of ``default`` will be used as the default
rule.  It is possible to use a different rule name as the default rule
by setting the ``policy_default_rule`` configuration setting to the
desired rule name.
"""

import copy
import logging
import os
import warnings

from oslo_config import cfg
from oslo_serialization import jsonutils
import six
import yaml

from oslo_policy import _cache_handler
from oslo_policy import _checks
from oslo_policy._i18n import _
from oslo_policy import _parser
from oslo_policy import opts


LOG = logging.getLogger(__name__)


register = _checks.register
"""Register a function or :class:`.Check` class as a policy check.

:param name: Gives the name of the check type, e.g., "rule",
             "role", etc.  If name is ``None``, a default check type
             will be registered.
:param func: If given, provides the function or class to register.
             If not given, returns a function taking one argument
             to specify the function or class to register,
             allowing use as a decorator.
"""

Check = _checks.Check
"""A base class to allow for user-defined policy checks.

:param kind: The kind of the check, i.e., the field before the ``:``.
:param match: The match of the check, i.e., the field after the ``:``.

"""

AndCheck = _checks.AndCheck
"""Implements the "and" logical operator.

A policy check that requires that a list of other checks all return True.

:param list rules: rules that will be tested.

"""

NotCheck = _checks.NotCheck
"""Implements the "not" logical operator.

A policy check that inverts the result of another policy check.

:param rule: The rule to negate.

"""

OrCheck = _checks.OrCheck
"""Implements the "or" operator.

A policy check that requires that at least one of a list of other
checks returns ``True``.

:param rules: A list of rules that will be tested.

"""

RuleCheck = _checks.RuleCheck
"""Recursively checks credentials based on the defined rules."""


class PolicyNotAuthorized(Exception):
    """Default exception raised for policy enforcement failure."""

    def __init__(self, rule, target, creds):
        msg = _("%(rule)s is disallowed by policy") % {'rule': rule}
        super(PolicyNotAuthorized, self).__init__(msg)


class InvalidScope(Exception):
    """Raised when the scope of the request mismatches the policy scope."""

    def __init__(self, rule, operation_scopes, token_scope):
        msg = (
            "%(rule)s requires a scope of %(operation_scopes)s, request "
            "was made with %(token_scope)s scope." % {
                'rule': rule,
                'operation_scopes': operation_scopes,
                'token_scope': token_scope
            }
        )
        super(InvalidScope, self).__init__(msg)


class DuplicatePolicyError(Exception):
    def __init__(self, name):
        msg = _('Policy %(name)s is already registered') % {'name': name}
        super(DuplicatePolicyError, self).__init__(msg)


class PolicyNotRegistered(Exception):
    def __init__(self, name):
        msg = _('Policy %(name)s has not been registered') % {'name': name}
        super(PolicyNotRegistered, self).__init__(msg)


class InvalidDefinitionError(Exception):
    def __init__(self, names):
        msg = _('Policies %(names)s are not well defined. Check logs for '
                'more details.') % {'names': names}
        super(InvalidDefinitionError, self).__init__(msg)


class InvalidRuleDefault(Exception):
    def __init__(self, error):
        msg = (_('Invalid policy rule default: '
                 '%(error)s.') % {'error': error})
        super(InvalidRuleDefault, self).__init__(msg)


def parse_file_contents(data):
    """Parse the raw contents of a policy file.

    Parses the contents of a policy file which currently can be in either
    yaml or json format. Both can be parsed as yaml.

    :param data: A string containing the contents of a policy file.
    :returns: A dict of the form {'policy_name1': 'policy1',
                                  'policy_name2': 'policy2,...}
    """
    try:
        # NOTE(snikitin): jsonutils.loads() is much faster than
        # yaml.safe_load(). However jsonutils.loads() parses only JSON while
        # yaml.safe_load() parses JSON and YAML. So here we try to parse data
        # by jsonutils.loads() first. In case of failure yaml.safe_load()
        # will be used instead.
        parsed = jsonutils.loads(data)
    except ValueError:
        try:
            parsed = yaml.safe_load(data)
        except yaml.YAMLError as e:
            # For backwards-compatibility, convert yaml error to ValueError,
            # which is what JSON loader raised.
            raise ValueError(six.text_type(e))
    return parsed or {}


class Rules(dict):
    """A store for rules. Handles the default_rule setting directly."""

    @classmethod
    def load(cls, data, default_rule=None):
        """Allow loading of YAML/JSON rule data.

        .. versionadded:: 1.5.0

        """
        parsed_file = parse_file_contents(data)

        # Parse the rules
        rules = {k: _parser.parse_rule(v) for k, v in parsed_file.items()}

        return cls(rules, default_rule)

    @classmethod
    def load_json(cls, data, default_rule=None):
        """Allow loading of YAML/JSON rule data.

        .. warning::
            This method is deprecated as of the 1.5.0 release in favor of
            :meth:`load` and may be removed in the 2.0 release.

        """
        warnings.warn(
            'The load_json() method is deprecated as of the 1.5.0 release in '
            'favor of load() and may be removed in the 2.0 release.',
            DeprecationWarning)
        return cls.load(data, default_rule)

    @classmethod
    def from_dict(cls, rules_dict, default_rule=None):
        """Allow loading of rule data from a dictionary."""

        # Parse the rules stored in the dictionary
        rules = {k: _parser.parse_rule(v) for k, v in rules_dict.items()}

        return cls(rules, default_rule)

    def __init__(self, rules=None, default_rule=None):
        """Initialize the Rules store."""

        super(Rules, self).__init__(rules or {})
        self.default_rule = default_rule

    def __missing__(self, key):
        """Implements the default rule handling."""

        if isinstance(self.default_rule, dict):
            raise KeyError(key)

        # If the default rule isn't actually defined, do something
        # reasonably intelligent
        if not self.default_rule:
            raise KeyError(key)

        if isinstance(self.default_rule, _checks.BaseCheck):
            return self.default_rule

        # We need to check this or we can get infinite recursion
        if self.default_rule not in self:
            raise KeyError(key)

        elif isinstance(self.default_rule, six.string_types):
            return self[self.default_rule]

    def __str__(self):
        """Dumps a string representation of the rules."""

        # Start by building the canonical strings for the rules
        out_rules = {}
        for key, value in self.items():
            # Use empty string for singleton TrueCheck instances
            if isinstance(value, _checks.TrueCheck):
                out_rules[key] = ''
            else:
                out_rules[key] = str(value)

        # Dump a pretty-printed JSON representation
        return jsonutils.dumps(out_rules, indent=4)


class Enforcer(object):
    """Responsible for loading and enforcing rules.

    :param conf: A configuration object.
    :param policy_file: Custom policy file to use, if none is
                        specified, ``conf.oslo_policy.policy_file`` will be
                        used.
    :param rules: Default dictionary / Rules to use. It will be
                  considered just in the first instantiation. If
                  :meth:`load_rules` with ``force_reload=True``,
                  :meth:`clear` or :meth:`set_rules` with ``overwrite=True``
                  is called this will be overwritten.
    :param default_rule: Default rule to use, conf.default_rule will
                         be used if none is specified.
    :param use_conf: Whether to load rules from cache or config file.
    :param overwrite: Whether to overwrite existing rules when reload rules
                      from config file.
    """

    def __init__(self, conf, policy_file=None, rules=None,
                 default_rule=None, use_conf=True, overwrite=True):
        self.conf = conf
        opts._register(conf)

        self.default_rule = (default_rule or
                             self.conf.oslo_policy.policy_default_rule)
        self.rules = Rules(rules, self.default_rule)
        self.registered_rules = {}
        self.file_rules = {}

        self.policy_path = None

        self.policy_file = policy_file or self.conf.oslo_policy.policy_file
        self.use_conf = use_conf
        self._need_check_rule = True
        self.overwrite = overwrite
        self._loaded_files = []
        self._policy_dir_mtimes = {}
        self._file_cache = {}
        self._informed_no_policy_file = False

    def set_rules(self, rules, overwrite=True, use_conf=False):
        """Create a new :class:`Rules` based on the provided dict of rules.

        :param dict rules: New rules to use.
        :param overwrite: Whether to overwrite current rules or update them
                          with the new rules.
        :param use_conf: Whether to reload rules from cache or config file.
        """

        if not isinstance(rules, dict):
            raise TypeError(_('Rules must be an instance of dict or Rules, '
                            'got %s instead') % type(rules))
        self.use_conf = use_conf
        self._need_check_rule = True
        if overwrite:
            self.rules = Rules(rules, self.default_rule)
        else:
            self.rules.update(rules)

    def clear(self):
        """Clears :class:`Enforcer` contents.

        This will clear this instances rules, policy's cache, file cache
        and policy's path.
        """
        self.set_rules({})
        self.default_rule = None
        self.policy_path = None
        self._loaded_files = []
        self._policy_dir_mtimes = {}
        self._file_cache.clear()
        self.registered_rules = {}
        self.file_rules = {}
        self._informed_no_policy_file = False

    def load_rules(self, force_reload=False):
        """Loads policy_path's rules.

        Policy file is cached and will be reloaded if modified.

        :param force_reload: Whether to reload rules from config file.
        """

        if force_reload:
            self.use_conf = force_reload

        policy_file_rules_changed = False

        if self.use_conf:
            if not self.policy_path:
                try:
                    self.policy_path = self._get_policy_path(self.policy_file)
                except cfg.ConfigFilesNotFoundError:
                    if not self._informed_no_policy_file:
                        LOG.debug('The policy file %s could not be found.',
                                  self.policy_file)
                        self._informed_no_policy_file = True

            if self.policy_path:
                # If the policy file rules have changed any policy.d rules
                # also need to be reapplied on top of that change.
                policy_file_rules_changed = self._load_policy_file(
                    self.policy_path,
                    force_reload,
                    overwrite=self.overwrite
                )

            force_reload_policy_dir = force_reload
            if policy_file_rules_changed:
                force_reload_policy_dir = True

            for path in self.conf.oslo_policy.policy_dirs:
                try:
                    path = self._get_policy_path(path)
                except cfg.ConfigFilesNotFoundError:
                    continue
                if (self._is_directory_updated(self._policy_dir_mtimes, path)
                        or force_reload_policy_dir):
                    self._walk_through_policy_directory(
                        path,
                        self._load_policy_file,
                        force_reload_policy_dir, False
                    )

            for default in self.registered_rules.values():
                if default.deprecated_rule:
                    deprecated_msg = (
                        'Policy "%(old_name)s":"%(old_check_str)s" was '
                        'deprecated in %(release)s in favor of "%(name)s":'
                        '"%(check_str)s". Reason: %(reason)s. Either ensure '
                        'your deployment is ready for the new default or '
                        'copy/paste the deprecated policy into your policy '
                        'file and maintain it manually.' % {
                            'old_name': default.deprecated_rule.name,
                            'old_check_str': default.deprecated_rule.check_str,
                            'release': default.deprecated_since,
                            'name': default.name,
                            'check_str': default.check_str,
                            'reason': default.deprecated_reason
                        }
                    )
                    if default.deprecated_rule.name != default.name and (
                            default.deprecated_rule.name in self.rules):
                        # Print a warning because the actual policy name is
                        # changing. If deployers are relying on an override for
                        # foo:bar and it's getting renamed to foo:create_bar
                        # then they need to be able to see that before they
                        # roll out the next release.
                        warnings.warn(deprecated_msg)
                    if (default.deprecated_rule.check_str !=
                            default.check_str and default.name not in
                            self.rules):
                        # In this case, the default check_str is changing. We
                        # need to let operators know that this is going to
                        # change. If they don't want to override it, they are
                        # going to have to make sure the right infrastructure
                        # exists before they upgrade. This overrides the new
                        # check with an OrCheck that combines the new and old
                        # check_str attributes from the new and deprecated
                        # policies. This will make it so that deployments don't
                        # break on upgrade, but they receive log messages
                        # telling them stuff is going to change if they don't
                        # maintain the policy manually or add infrastructure to
                        # their deployment to support the new policy.
                        default.check = _parser.parse_rule(
                            default.check_str + ' or ' +
                            default.deprecated_rule.check_str
                        )
                        warnings.warn(deprecated_msg)
                if default.deprecated_for_removal and (
                        default.name in self.file_rules):
                    # If a policy is going to be removed altogether, then we
                    # need to make sure we let operators know so they can clean
                    # up their policy files, if they are overriding it.
                    warnings.warn(
                        'Policy "%(policy)s":"%(check_str)s" was '
                        'deprecated for removal in %(release)s. Reason: '
                        '%(reason)s. Its value may be silently ignored in '
                        'the future.' % {
                            'policy': default.name,
                            'check_str': default.check_str,
                            'release': default.deprecated_since,
                            'reason': default.deprecated_reason
                        }
                    )
                if default.name not in self.rules:
                    self.rules[default.name] = default.check

            # Detect and log obvious incorrect rule definitions
            if self._need_check_rule:
                self.check_rules()
                self._need_check_rule = False

    def check_rules(self, raise_on_violation=False):
        """Look for rule definitions that are obviously incorrect."""
        undefined_checks = []
        cyclic_checks = []
        violation = False
        for name, check in self.rules.items():
            if self._undefined_check(check):
                undefined_checks.append(name)
                violation = True
            if self._cycle_check(check):
                cyclic_checks.append(name)
                violation = True

        if undefined_checks:
            LOG.warning('Policies %(names)s reference a rule that is not '
                        'defined.', {'names': undefined_checks})
        if cyclic_checks:
            LOG.warning('Policies %(names)s are part of a cyclical '
                        'reference.', {'names': cyclic_checks})

        if raise_on_violation and violation:
            raise InvalidDefinitionError(undefined_checks + cyclic_checks)

        return not violation

    def _undefined_check(self, check):
        '''Check if a RuleCheck references an undefined rule.'''
        if isinstance(check, RuleCheck):
            if check.match not in self.rules:
                # Undefined rule
                return True

        # An AndCheck or OrCheck is composed of multiple rules so check
        # each of those.
        rules = getattr(check, 'rules', None)
        if rules:
            for rule in rules:
                if self._undefined_check(rule):
                    return True
        return False

    def _cycle_check(self, check, seen=None):
        '''Check if RuleChecks cycle.

        Looking for something like:
        "foo": "rule:bar"
        "bar": "rule:foo"
        '''
        if seen is None:
            seen = set()

        if isinstance(check, RuleCheck):
            if check.match in seen:
                # Cycle found
                return True
            seen.add(check.match)
            if check.match in self.rules:
                # There can only be a cycle if the referenced rule is defined.
                if self._cycle_check(self.rules[check.match], seen):
                    return True

        # An AndCheck or OrCheck is composed of multiple rules so check
        # each of those.
        rules = getattr(check, 'rules', None)
        if rules:
            for rule in rules:
                # As there being an OrCheck or AndCheck, a copy of the father's
                # seen should be called here. In order that the checks in
                # different branchs are seperated.
                if self._cycle_check(rule, seen.copy()):
                    return True
        return False

    @staticmethod
    def _is_directory_updated(cache, path):
        # Get the current modified time and compare it to what is in
        # the cache and check if the new mtime is greater than what
        # is in the cache
        mtime = 0
        if os.path.exists(path):
            if not os.path.isdir(path):
                raise ValueError('{} is not a directory'.format(path))
            # Make a list of all the files
            files = [path] + [os.path.join(path, file) for file in
                              os.listdir(path)]
            # Pick the newest one, let's use its time.
            mtime = os.path.getmtime(max(files, key=os.path.getmtime))
        cache_info = cache.setdefault(path, {})
        if mtime > cache_info.get('mtime', 0):
            cache_info['mtime'] = mtime
            return True
        return False

    @staticmethod
    def _walk_through_policy_directory(path, func, *args):
        if not os.path.isdir(path):
            raise ValueError('%s is not a directory' % path)
        # We do not iterate over sub-directories.
        policy_files = next(os.walk(path))[2]
        policy_files.sort()
        for policy_file in [p for p in policy_files if not p.startswith('.')]:
            func(os.path.join(path, policy_file), *args)

    def _record_file_rules(self, data, overwrite=False):
        """Store a copy of rules loaded from a file.

        It is useful to be able to distinguish between rules loaded from a file
        and those registered by a consuming service. In order to do so we keep
        a record of rules loaded from a file.

        :param data: The raw contents of a policy file.
        :param overwrite: If True clear out previously loaded rules.
        """
        if overwrite:
            self.file_rules = {}
        parsed_file = parse_file_contents(data)
        for name, check_str in parsed_file.items():
            self.file_rules[name] = RuleDefault(name, check_str)

    def _load_policy_file(self, path, force_reload, overwrite=True):
        """Load policy rules from the specified policy file.

        :param str path: A path of the policy file to load rules from.
        :param bool force_reload: Forcefully reload the policy file content.
        :param bool overwrite: Replace policy rules instead of updating them.
        :return: A bool indicating whether rules have been changed or not.
        :rtype: bool
        """
        rules_changed = False
        reloaded, data = _cache_handler.read_cached_file(
            self._file_cache, path, force_reload=force_reload)
        if reloaded or not self.rules:
            rules = Rules.load(data, self.default_rule)
            self.set_rules(rules, overwrite=overwrite, use_conf=True)
            rules_changed = True
            self._record_file_rules(data, overwrite)
            self._loaded_files.append(path)
            LOG.debug('Reloaded policy file: %(path)s', {'path': path})
        return rules_changed

    def _get_policy_path(self, path):
        """Locate the policy YAML/JSON data file/path.

        :param path: It's value can be a full path or related path. When
                     full path specified, this function just returns the full
                     path. When related path specified, this function will
                     search configuration directories to find one that exists.

        :returns: The policy path

        :raises: ConfigFilesNotFoundError if the file/path couldn't
                 be located.
        """
        policy_path = self.conf.find_file(path)

        if policy_path:
            return policy_path

        raise cfg.ConfigFilesNotFoundError((path,))

    def enforce(self, rule, target, creds, do_raise=False, exc=None,
                *args, **kwargs):
        """Checks authorization of a rule against the target and credentials.

        :param rule: The rule to evaluate.
        :type rule: string or :class:`BaseCheck`
        :param dict target: As much information about the object being
                            operated on as possible. The target
                            argument should be a dict instance or an
                            instance of a class that fully supports
                            the Mapping abstract base class and deep
                            copying.
        :param dict creds: As much information about the user performing the
                           action as possible.
        :param do_raise: Whether to raise an exception or not if check
                        fails.
        :param exc: Class of the exception to raise if the check fails.
                    Any remaining arguments passed to :meth:`enforce` (both
                    positional and keyword arguments) will be passed to
                    the exception class. If not specified,
                    :class:`PolicyNotAuthorized` will be used.

        :return: ``False`` if the policy does not allow the action and `exc` is
                 not provided; otherwise, returns a value that evaluates to
                 ``True``.  Note: for rules using the "case" expression, this
                 ``True`` value will be the specified string from the
                 expression.
        """

        self.load_rules()

        # Allow the rule to be a Check tree
        if isinstance(rule, _checks.BaseCheck):
            # If the thing we're given is a Check, we don't know the
            # name of the rule, so pass None for current_rule.
            result = _checks._check(
                rule=rule,
                target=target,
                creds=creds,
                enforcer=self,
                current_rule=None,
            )
        elif not self.rules:
            # No rules to reference means we're going to fail closed
            result = False
        else:
            try:
                to_check = self.rules[rule]
            except KeyError:
                LOG.debug('Rule [%s] does not exist', rule)
                # If the rule doesn't exist, fail closed
                result = False
            else:
                # Check the scope of the operation against the possible scope
                # attributes provided in `creds`.
                if creds.get('system'):
                    token_scope = 'system'  # nosec
                else:
                    # If the token isn't system-scoped then we're dealing with
                    # either a domain-scoped token or a project-scoped token.
                    # From a policy perspective, both are "project" operations.
                    # Whether or not the project is a domain depends on where
                    # it sits in the hierarchy.
                    token_scope = 'project'  # nosec

                registered_rule = self.registered_rules.get(rule)
                if registered_rule and registered_rule.scope_types:
                    if token_scope not in registered_rule.scope_types:
                        if self.conf.oslo_policy.enforce_scope:
                            raise InvalidScope(
                                rule, registered_rule.scope_types, token_scope
                            )
                        # If we don't raise an exception we should at least
                        # inform operators about policies that are being used
                        # with improper scopes.
                        msg = (
                            'Policy %(rule)s failed scope check. The token '
                            'used to make the request was %(token_scope)s '
                            'scoped but the policy requires %(policy_scope)s '
                            'scope. This behavior may change in the future '
                            'where using the intended scope is required' % {
                                'rule': rule,
                                'token_scope': token_scope,
                                'policy_scope': registered_rule.scope_types
                            }
                        )
                        warnings.warn(msg)

                result = _checks._check(
                    rule=to_check,
                    target=target,
                    creds=creds,
                    enforcer=self,
                    current_rule=rule,
                )

        # If it is False, raise the exception if requested
        if do_raise and not result:
            if exc:
                raise exc(*args, **kwargs)

            raise PolicyNotAuthorized(rule, target, creds)

        return result

    def register_default(self, default):
        """Registers a RuleDefault.

        Adds a RuleDefault to the list of registered rules. Rules must be
        registered before using the Enforcer.authorize method.

        :param default: A RuleDefault object to register.
        """
        if default.name in self.registered_rules:
            raise DuplicatePolicyError(default.name)

        self.registered_rules[default.name] = default

    def register_defaults(self, defaults):
        """Registers a list of RuleDefaults.

        Adds each RuleDefault to the list of registered rules. Rules must be
        registered before using the Enforcer.authorize method.

        :param default: A list of RuleDefault objects to register.
        """
        for default in defaults:
            self.register_default(default)

    def authorize(self, rule, target, creds, do_raise=False,
                  exc=None, *args, **kwargs):
        """A wrapper around 'enforce' that checks for policy registration.

        To ensure that a policy being checked has been registered this method
        should be used rather than enforce. By doing so a project can be sure
        that all of it's used policies are registered and therefore available
        for sample file generation.

        The parameters match the enforce method and a description of them can
        be found there.
        """
        if rule not in self.registered_rules:
            raise PolicyNotRegistered(rule)
        return self.enforce(rule, target, creds, do_raise, exc,
                            *args, **kwargs)


class RuleDefault(object):
    """A class for holding policy definitions.

    It is required to supply a name and value at creation time. It is
    encouraged to also supply a description to assist operators.

    :param name: The name of the policy. This is used when referencing it
                 from another rule or during policy enforcement.
    :param check_str: The policy. This is a string  defining a policy that
                      conforms to the policy language outlined at the top of
                      the file.
    :param description: A plain text description of the policy. This will be
                        used to comment sample policy files for use by
                        deployers.
    :param deprecated_rule: :class:`.DeprecatedRule`
    :param deprecated_for_removal: indicates whether the policy is planned for
                                   removal in a future release.
    :param deprecated_reason: indicates why this policy is planned for removal
                              in a future release. Silently ignored if
                              deprecated_for_removal is False.
    :param deprecated_since: indicates which release this policy was deprecated
                             in. Accepts any string, though valid version
                             strings are encouraged. Silently ignored if
                             deprecated_for_removal is False.
    :param scope_types: A list containing the intended scopes of the operation
                        being done.

    .. versionchanged 1.29
       Added *deprecated_rule* parameter.

    .. versionchanged 1.29
       Added *deprecated_for_removal* parameter.

    .. versionchanged 1.29
       Added *deprecated_reason* parameter.

    .. versionchanged 1.29
       Added *deprecated_since* parameter.

    .. versionchanged 1.31
       Added *scope_types* parameter.

    """
    def __init__(self, name, check_str, description=None,
                 deprecated_rule=None, deprecated_for_removal=False,
                 deprecated_reason=None, deprecated_since=None,
                 scope_types=None):
        self.name = name
        self.check_str = check_str
        self.check = _parser.parse_rule(check_str)
        self.description = description
        self.deprecated_rule = copy.deepcopy(deprecated_rule) or []
        self.deprecated_for_removal = deprecated_for_removal
        self.deprecated_reason = deprecated_reason
        self.deprecated_since = deprecated_since

        if self.deprecated_rule:
            if not isinstance(self.deprecated_rule, DeprecatedRule):
                raise ValueError(
                    'deprecated_rule must be a DeprecatedRule object.'
                )

        if (deprecated_for_removal or deprecated_rule) and (
                deprecated_reason is None or deprecated_since is None):
            raise ValueError(
                '%(name)s deprecated without deprecated_reason or '
                'deprecated_since. Both must be supplied if deprecating a '
                'policy' % {'name': self.name}
            )

        if scope_types:
            msg = 'scope_types must be a list of strings.'
            if not isinstance(scope_types, list):
                raise ValueError(msg)
            for scope_type in scope_types:
                if not isinstance(scope_type, six.string_types):
                    raise ValueError(msg)
                if scope_types.count(scope_type) > 1:
                    raise ValueError(
                        'scope_types must be a list of unique strings.'
                    )
        self.scope_types = scope_types

    def __str__(self):
        return '"%(name)s": "%(check_str)s"' % {'name': self.name,
                                                'check_str': self.check_str}

    def __eq__(self, other):
        """Equality operator.

        All check objects have a stable string representation. It is used for
        comparison rather than check_str because multiple check_str's may parse
        to the same check object. For instance '' and '@' are equivalent and
        the parsed rule string representation for both is '@'.

        The description does not play a role in the meaning of the check so it
        is not considered for equality.
        """
        # Name should match, check should match, and class should be equivalent
        # or one should be a subclass of the other.
        if (self.name == other.name and
                str(self.check) == str(other.check) and
                (isinstance(self, other.__class__) or
                 isinstance(other, self.__class__))):
            return True
        return False


class DocumentedRuleDefault(RuleDefault):
    """A class for holding policy-in-code policy objects definitions

    This class provides the same functionality as the RuleDefault class, but it
    also requires additional data about the policy rule being registered. This
    is necessary so that proper documentation can be rendered based on the
    attributes of this class. Eventually, all usage of RuleDefault should be
    converted to use DocumentedRuleDefault.

    :param operations: List of dicts containing each api url and
        corresponding http request method.

        Example::

            operations=[{'path': '/foo', 'method': 'GET'},
                        {'path': '/some', 'method': 'POST'}]
    """
    def __init__(self, name, check_str, description, operations,
                 deprecated_rule=None, deprecated_for_removal=False,
                 deprecated_reason=None, deprecated_since=None,
                 scope_types=None):
        super(DocumentedRuleDefault, self).__init__(
            name, check_str, description,
            deprecated_rule=deprecated_rule,
            deprecated_for_removal=deprecated_for_removal,
            deprecated_reason=deprecated_reason,
            deprecated_since=deprecated_since,
            scope_types=scope_types
        )
        self.operations = operations

    @property
    def description(self):
        return self._description

    @description.setter
    def description(self, value):
        # Validates description isn't empty.
        if not value:
            raise InvalidRuleDefault('Description is required')
        self._description = value

    @property
    def operations(self):
        return self._operations

    @operations.setter
    def operations(self, ops):
        if not isinstance(ops, list):
            raise InvalidRuleDefault('Operations must be a list')
        if not ops:
            raise InvalidRuleDefault('Operations list must not be empty')

        for op in ops:
            if 'path' not in op:
                raise InvalidRuleDefault('Operation must contain a path')
            if 'method' not in op:
                raise InvalidRuleDefault('Operation must contain a method')
            if len(op.keys()) > 2:
                raise InvalidRuleDefault('Operation contains > 2 keys')
        self._operations = ops


class DeprecatedRule(object):

    """Represents a Deprecated policy or rule.

    Here's how you can use it to change a policy's default role or rule. Assume
    the following policy exists in code::

        from oslo_policy import policy

        policy.DocumentedRuleDefault(
            name='foo:create_bar',
            check_str='role:fizz',
            description='Create a bar.',
            operations=[{'path': '/v1/bars', 'method': 'POST'}]
        )

    The next snippet will maintain the deprecated option, but allow
    ``foo:create_bar`` to default to ``role:bang`` instead of ``role:fizz``::

        deprecated_rule = policy.DeprecatedRule(
            name='foo:create_bar',
            check_str='role:fizz'
        )

        policy.DocumentedRuleDefault(
            name='foo:create_bar',
            check_str='role:bang',
            description='Create a bar.',
            operations=[{'path': '/v1/bars', 'method': 'POST'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='role:bang is a better default',
            deprecated_since='N'
        )

    DeprecatedRule can be used to change the policy name itself. Assume the
    following policy exists in code::

        from oslo_policy import policy

        policy.DocumentedRuleDefault(
            name='foo:post_bar',
            check_str='role:fizz',
            description='Create a bar.',
            operations=[{'path': '/v1/bars', 'method': 'POST'}]
        )

    For the sake of consistency, let's say we want to replace ``foo:post_bar``
    with ``foo:create_bar``, but keep the same ``check_str`` as the default. We
    can accomplish this by doing::

        deprecated_rule = policy.DeprecatedRule(
            name='foo:post_bar',
            check_str='role:fizz'
        )

        policy.DocumentedRuleDefault(
            name='foo:create_bar',
            check_str='role:fizz',
            description='Create a bar.',
            operations=[{'path': '/v1/bars', 'method': 'POST'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='foo:create_bar is more consistent',
            deprecated_since='N'
        )

    Finally, let's use DeprecatedRule to break a policy into more granular
    policies. Let's assume the following policy exists in code::

        policy.DocumentedRuleDefault(
            name='foo:bar',
            check_str='role:bazz',
            description='Create, read, update, or delete a bar.',
            operations=[
                {
                    'path': '/v1/bars',
                    'method': 'POST'
                },
                {
                    'path': '/v1/bars',
                    'method': 'GET'
                },
                {
                    'path': '/v1/bars/{bar_id}',
                    'method': 'GET'
                },
                {
                    'path': '/v1/bars/{bar_id}',
                    'method': 'PATCH'
                },
                {
                    'path': '/v1/bars/{bar_id}',
                    'method': 'DELETE'
                }
            ]
        )

    Here we can see the same policy is used to protect multiple operations on
    bars. This prevents operators from being able to assign different roles to
    different actions that can be taken on bar. For example, what if an
    operator wanted to require a less restrictive role or rule to list bars but
    a more restrictive rule to delete them? The following will introduce a
    policy that helps achieve that and deprecate the original, overly-broad
    policy::

        deprecated_rule = policy.DeprecatedRule(
            name='foo:bar',
            check_str='role:bazz'
        )

        policy.DocumentedRuleDefault(
            name='foo:create_bar',
            check_str='role:bang',
            description='Create a bar.',
            operations=[{'path': '/v1/bars', 'method': 'POST'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='foo:create_bar is more granular than foo:bar',
            deprecated_since='N'
        )
        policy.DocumentedRuleDefault(
            name='foo:list_bars',
            check_str='role:bazz',
            description='List bars.',
            operations=[{'path': '/v1/bars', 'method': 'GET'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='foo:list_bars is more granular than foo:bar',
            deprecated_since='N'
        )
        policy.DocumentedRuleDefault(
            name='foo:get_bar',
            check_str='role:bazz',
            description='Get a bar.',
            operations=[{'path': '/v1/bars/{bar_id}', 'method': 'GET'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='foo:get_bar is more granular than foo:bar',
            deprecated_since='N'
        )
        policy.DocumentedRuleDefault(
            name='foo:update_bar',
            check_str='role:bang',
            description='Update a bar.',
            operations=[{'path': '/v1/bars/{bar_id}', 'method': 'PATCH'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='foo:update_bar is more granular than foo:bar',
            deprecated_since='N'
        )
        policy.DocumentedRuleDefault(
            name='foo:delete_bar',
            check_str='role:bang',
            description='Delete a bar.',
            operations=[{'path': '/v1/bars/{bar_id}', 'method': 'DELETE'}],
            deprecated_rule=deprecated_rule,
            deprecated_reason='foo:delete_bar is more granular than foo:bar',
            deprecated_since='N'
        )

    .. versionchanged 1.29
       Added *DeprecatedRule* object.
    """

    def __init__(self, name, check_str):
        """Construct a DeprecatedRule object.

        :param name: the policy name
        :param check_str: the value of the policy's check string
        """
        self.name = name
        self.check_str = check_str