~evarlast/ubuntu/utopic/mongodb/upstart-workaround-debian-bug-718702

« back to all changes in this revision

Viewing changes to src/mongo/db/auth/privilege_set.h

  • Committer: Package Import Robot
  • Author(s): James Page, James Page, Robie Basak
  • Date: 2013-05-29 17:44:42 UTC
  • mfrom: (44.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130529174442-z0a4qmoww4y0t458
Tags: 1:2.4.3-1ubuntu1
[ James Page ]
* Merge from Debian unstable, remaining changes:
  - Enable SSL support:
    + d/control: Add libssl-dev to BD's.
    + d/rules: Enabled --ssl option.
    + d/mongodb.conf: Add example SSL configuration options.
  - d/mongodb-server.mongodb.upstart: Add upstart configuration.
  - d/rules: Don't strip binaries during scons build for Ubuntu.
  - d/control: Add armhf to target archs.
  - d/p/SConscript.client.patch: fixup install of client libraries.
  - d/p/0010-install-libs-to-usr-lib-not-usr-lib64-Closes-588557.patch:
    Install libraries to lib not lib64.
* Dropped changes:
  - d/p/arm-support.patch: Included in Debian.
  - d/p/double-alignment.patch: Included in Debian.
  - d/rules,control: Debian also builds with avaliable system libraries
    now.
* Fix FTBFS due to gcc and boost upgrades in saucy:
  - d/p/0008-ignore-unused-local-typedefs.patch: Add -Wno-unused-typedefs
    to unbreak building with g++-4.8.
  - d/p/0009-boost-1.53.patch: Fixup signed/unsigned casting issue.

[ Robie Basak ]
* d/p/0011-Use-a-signed-char-to-store-BSONType-enumerations.patch: Fixup
  build failure on ARM due to missing signed'ness of char cast.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*    Copyright 2012 10gen Inc.
 
2
 *
 
3
 *    Licensed under the Apache License, Version 2.0 (the "License");
 
4
 *    you may not use this file except in compliance with the License.
 
5
 *    You may obtain a copy of the License at
 
6
 *
 
7
 *    http://www.apache.org/licenses/LICENSE-2.0
 
8
 *
 
9
 *    Unless required by applicable law or agreed to in writing, software
 
10
 *    distributed under the License is distributed on an "AS IS" BASIS,
 
11
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
12
 *    See the License for the specific language governing permissions and
 
13
 *    limitations under the License.
 
14
 */
 
15
#pragma once
 
16
 
 
17
#include <map>
 
18
#include <string>
 
19
 
 
20
#include "mongo/base/disallow_copying.h"
 
21
#include "mongo/db/auth/action_set.h"
 
22
#include "mongo/db/auth/action_type.h"
 
23
#include "mongo/db/auth/privilege.h"
 
24
#include "mongo/db/auth/principal_name.h"
 
25
#include "mongo/util/string_map.h"
 
26
 
 
27
namespace mongo {
 
28
 
 
29
    /**
 
30
     * A collection of privileges describing which authenticated principals bestow the client the
 
31
     * ability to perform various actions on specific resources.  Since every privilege comes from
 
32
     * an authenticated principal, removing that principal removes all privileges granted by that
 
33
     * principal.
 
34
     *
 
35
     * Resources are arranged hierarchically, with a wildcard resource,
 
36
     * PrivilegeSet::WILDCARD_RESOURCE, matching any resource.  In the current implementation, the
 
37
     * only two levels of the hierarchy are the wildcard and one level below, which is analagous to
 
38
     * the name of a database.  It is future work to support collection or other sub-database
 
39
     * resources.
 
40
     *
 
41
     * This class does not do any locking/synchronization, the consumer will be responsible for
 
42
     * synchronizing access.
 
43
     */
 
44
    class PrivilegeSet {
 
45
        MONGO_DISALLOW_COPYING(PrivilegeSet);
 
46
    public:
 
47
        static const std::string WILDCARD_RESOURCE;
 
48
 
 
49
        PrivilegeSet();
 
50
        ~PrivilegeSet();
 
51
 
 
52
        /**
 
53
         * Adds the specified privilege to the set, associating it with the named principal.
 
54
         *
 
55
         * The privilege should be on a specific resource, or on the WILDCARD_RESOURCE.
 
56
         */
 
57
        void grantPrivilege(const Privilege& privilege, const PrincipalName& authorizingPrincipal);
 
58
 
 
59
        /**
 
60
         * Adds the specified privileges to the set, associating them with the named principal.
 
61
         */
 
62
        void grantPrivileges(const std::vector<Privilege>& privileges,
 
63
                             const PrincipalName& authorizingPrincipal);
 
64
 
 
65
        /**
 
66
         * Removes from the set all privileges associated with the given principal.
 
67
         *
 
68
         * If multiple princpals enable the same privilege, the set will continue to
 
69
         * contain those privileges until all authorizing principals have had their
 
70
         * privileges revoked from the set.
 
71
         */
 
72
        void revokePrivilegesFromPrincipal(const PrincipalName& principal);
 
73
 
 
74
        /**
 
75
         * Returns true if the set authorizes "desiredPrivilege".
 
76
         *
 
77
         * The set is considered to authorize "desiredPrivilege" if each action in
 
78
         * "desiredPrivilege" is satisfied either on the database component of
 
79
         * "desiredPrivilege.getResource()" or on WILDCARD_RESOURCE.
 
80
         *
 
81
         * TODO: Support checking for the privilege on the full resource name as well as the
 
82
         * database component, to support sub-database granularity privilege assignment.
 
83
         */
 
84
        bool hasPrivilege(const Privilege& desiredPrivilege);
 
85
 
 
86
        /**
 
87
         * Same as hasPrivilege, except checks all the privileges in a vector.
 
88
         */
 
89
        bool hasPrivileges(const std::vector<Privilege>& desiredPrivileges);
 
90
 
 
91
    private:
 
92
 
 
93
        /**
 
94
         * Information about privileges held on a resource.
 
95
         *
 
96
         * Instances are stored in the _byResource map, and accelerate the fast path of
 
97
         * hasPrivilege().  Privilege revocations via revokePrivilegesFromPrincipal() can make these
 
98
         * entries invalid, at which point they are marked "dirty".  Dirty entries are rebuilt via
 
99
         * _rebuildEntry(), below, during execution of hasPrivilege().
 
100
         */
 
101
        class ResourcePrivilegeCacheEntry {
 
102
        public:
 
103
            ResourcePrivilegeCacheEntry() : actions(), dirty(false) {}
 
104
 
 
105
            // All actions enabled on the associated resource, provided that "dirty" is false.
 
106
            ActionSet actions;
 
107
 
 
108
            // False if this data is consistent with the full privilege information, stored in the
 
109
            // _byPrincipal map.
 
110
            bool dirty;
 
111
        };
 
112
 
 
113
        /**
 
114
         * Type of map from resource names to authorized actions.
 
115
         */
 
116
        typedef StringMap<ResourcePrivilegeCacheEntry> ResourcePrivilegeCache;
 
117
 
 
118
        /**
 
119
         * Type of map from principal identity to information about the principal's privileges.  The
 
120
         * values in the map are themselves maps from resource names to associated actions.
 
121
         */
 
122
        typedef std::map<PrincipalName, StringMap<ActionSet> > PrincipalPrivilegeMap;
 
123
 
 
124
        void _rebuildEntry(const StringData& resource, ResourcePrivilegeCacheEntry* summary);
 
125
 
 
126
        ResourcePrivilegeCacheEntry* _lookupEntry(const StringData& resource);
 
127
        ResourcePrivilegeCacheEntry* _lookupOrInsertEntry(const StringData& resource);
 
128
 
 
129
        // Information about privileges available on all resources.
 
130
        ResourcePrivilegeCacheEntry _globalPrivilegeEntry;
 
131
 
 
132
        // Cache of privilege information, by resource.
 
133
        ResourcePrivilegeCache _byResource;
 
134
 
 
135
        // Directory of privilege information, by principal.
 
136
        PrincipalPrivilegeMap _byPrincipal;
 
137
    };
 
138
 
 
139
} // namespace mongo