summaryrefslogtreecommitdiff
path: root/cpp/src/qpid/acl/AclResourceCounter.cpp
blob: 2527af63759621e3e4794a3390b044ddc80c17db (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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 *
 */

#include "AclResourceCounter.h"
#include "Acl.h"
#include "qpid/log/Statement.h"
#include "qpid/sys/Mutex.h"
#include <assert.h>
#include <sstream>

using namespace qpid::sys;

namespace qpid {
namespace acl {

//
// This module approves various resource creation requests:
//   Queues
//


//
//
//
ResourceCounter::ResourceCounter(Acl& a, uint16_t ql) :
    acl(a), queueLimit(ql) {}

ResourceCounter::~ResourceCounter() {}


//
// limitApproveLH
//
// Resource creation approver.
// If user is under limit increment count and return true.
// Called with lock held.
//
bool ResourceCounter::limitApproveLH(
    countsMap_t& theMap,
    const std::string& theName,
    uint16_t theLimit,
    bool emitLog,
    bool enforceLimit) {

    bool result(true);
    uint16_t count;
    countsMap_t::iterator eRef = theMap.find(theName);
    if (eRef != theMap.end()) {
        count = (uint16_t)(*eRef).second;
        result = (enforceLimit ? count < theLimit : true);
        if (result) {
            count += 1;
            (*eRef).second = count;
        }
    } else {
        // user not found in map
        if (enforceLimit) {
            if (theLimit > 0) {
                theMap[theName] = count = 1;
            } else {
                count = 0;
                result = false;
            }
        }
        else {
            // not enforcing the limit
            theMap[theName] = count = 1;
        }
    }
    if (emitLog) {
        QPID_LOG(trace, "ACL QueueApprover user=" << theName
            << " limit=" << theLimit
            << " curValue=" << count
            << " result=" << (result ? "allow" : "deny"));
    }
    return result;
}


//
// releaseLH
//
// Decrement the name's count in map.
// called with dataLock already taken
//
void ResourceCounter::releaseLH(countsMap_t& theMap, const std::string& theName) {

    countsMap_t::iterator eRef = theMap.find(theName);
    if (eRef != theMap.end()) {
        uint16_t count = (uint16_t) (*eRef).second;
        assert (count > 0);
        if (1 == count) {
            theMap.erase (eRef);
        } else {
            (*eRef).second = count - 1;
        }
    } else {
        // User had no connections.
        QPID_LOG(notice, "ACL resource counter: Queue owner for queue '" << theName
            << "' not found in resource count pool");
    }
}


//
// approveCreateQueue
//  Count an attempted queue creation by this user.
//  Disapprove if over limit.
//
bool ResourceCounter::approveCreateQueue(const std::string& userId,
        const std::string& queueName,
        bool enforcingQueueQuotas,
        uint16_t queueUserQuota )
{
    Mutex::ScopedLock locker(dataLock);

    bool okByQ = limitApproveLH(queuePerUserMap, userId, queueUserQuota, true, enforcingQueueQuotas);

    if (okByQ) {
        // Queue is owned by this userId
        queueOwnerMap[queueName] = userId;

        QPID_LOG(trace, "ACL create queue approved for user '" << userId
            << "' queue '" << queueName << "'");
    } else {

        QPID_LOG(error, "Client max queue count limit of " << queueUserQuota
            << " exceeded by '" << userId << "' creating queue '"
            << queueName << "'. Queue creation denied.");

        acl.reportQueueLimit(userId, queueName);
    }
    return okByQ;
}


//
// recordDestroyQueue
//  Return a destroyed queue to a user's quota
//
void ResourceCounter::recordDestroyQueue(const std::string& queueName)
{
    Mutex::ScopedLock locker(dataLock);

    queueOwnerMap_t::iterator eRef = queueOwnerMap.find(queueName);
    if (eRef != queueOwnerMap.end()) {
        releaseLH(queuePerUserMap, (*eRef).second);

        queueOwnerMap.erase(eRef);
    } else {
        QPID_LOG(notice, "ACL resource counter: Queue '" << queueName
            << "' not found in queue owner map");
    }
}

}} // namespace qpid::acl