Beispiel #1
0
 def test_acl_authorizer(self):
     ctx = checkers.AuthContext()
     tests = [
         ('no ops, no problem',
          bakery.ACLAuthorizer(allow_public=True, get_acl=lambda x, y: []),
          None,
          [],
          []),
         ('identity that does not implement ACLIdentity; '
          'user should be denied except for everyone group',
          bakery.ACLAuthorizer(
              allow_public=True,
              get_acl=lambda ctx, op: [bakery.EVERYONE] if op.entity == 'a' else ['alice'],
          ),
          SimplestIdentity('bob'),
          [bakery.Op(entity='a', action='a'),
           bakery.Op(entity='b', action='b')],
          [True, False]),
         ('identity that does not implement ACLIdentity with user == Id; '
          'user should be denied except for everyone group',
          bakery.ACLAuthorizer(
              allow_public=True,
              get_acl=lambda ctx, op: [bakery.EVERYONE] if op.entity == 'a' else ['bob'],
          ),
          SimplestIdentity('bob'),
          [bakery.Op(entity='a', action='a'),
           bakery.Op(entity='b', action='b')],
          [True, False]),
         ('permission denied for everyone without AllowPublic',
          bakery.ACLAuthorizer(
              allow_public=False,
              get_acl=lambda x, y: [bakery.EVERYONE],
          ),
          SimplestIdentity('bob'),
          [bakery.Op(entity='a', action='a')],
          [False]),
         ('permission granted to anyone with no identity with AllowPublic',
          bakery.ACLAuthorizer(
              allow_public=True,
              get_acl=lambda x, y: [bakery.EVERYONE],
          ),
          None,
          [bakery.Op(entity='a', action='a')],
          [True])
     ]
     for test in tests:
         allowed, caveats = test[1].authorize(ctx, test[2], test[3])
         self.assertEqual(len(caveats), 0)
         self.assertEqual(allowed, test[4])
    def test_context_wired_properly(self):
        ctx = checkers.AuthContext({'a': 'aval'})

        class Visited:
            in_f = False
            in_allow = False
            in_get_acl = False

        def f(ctx, identity, op):
            self.assertEqual(ctx.get('a'), 'aval')
            Visited.in_f = True
            return False, None

        macaroonbakery.AuthorizerFunc(f).authorize(
            ctx, macaroonbakery.SimpleIdentity('bob'), ['op1'])
        self.assertTrue(Visited.in_f)

        class TestIdentity(SimplestIdentity, macaroonbakery.ACLIdentity):
            def allow(other, ctx, acls):
                self.assertEqual(ctx.get('a'), 'aval')
                Visited.in_allow = True
                return False

        def get_acl(ctx, acl):
            self.assertEqual(ctx.get('a'), 'aval')
            Visited.in_get_acl = True
            return []

        macaroonbakery.ACLAuthorizer(allow_public=False,
                                     get_acl=get_acl).authorize(
                                         ctx, TestIdentity('bob'), ['op1'])
        self.assertTrue(Visited.in_get_acl)
        self.assertTrue(Visited.in_allow)
 def authorize(self, ctx, id, ops):
     return macaroonbakery.ACLAuthorizer(
         allow_public=True,
         get_acl=lambda ctx, op: self._auth.get(op, [])).authorize(
             ctx, id, ops)