summaryrefslogtreecommitdiff
path: root/qpid/java/systests/src/main/java/org/apache/qpid/systest/rest/acl/ExchangeRestACLTest.java
blob: b0c66cb3af253141283558184ee720581a235ee5 (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
/*
 *
 * 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.
 *
 */
package org.apache.qpid.systest.rest.acl;

import org.apache.qpid.server.management.plugin.HttpManagement;
import org.apache.qpid.server.model.Binding;
import org.apache.qpid.server.model.Exchange;
import org.apache.qpid.server.model.Plugin;
import org.apache.qpid.server.model.Queue;
import org.apache.qpid.server.security.acl.AbstractACLTestCase;
import org.apache.qpid.systest.rest.QpidRestTestCase;
import org.apache.qpid.test.utils.TestBrokerConfiguration;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExchangeRestACLTest extends QpidRestTestCase
{
    private static final String ALLOWED_USER = "user1";
    private static final String DENIED_USER = "user2";
    private String _queueName;
    private String _exchangeName;
    private String _exchangeUrl;

    @Override
    protected void customizeConfiguration() throws IOException
    {
        super.customizeConfiguration();
        getRestTestHelper().configureTemporaryPasswordFile(this, ALLOWED_USER, DENIED_USER);

        AbstractACLTestCase.writeACLFileUtil(this, "ACL ALLOW-LOG ALL ACCESS MANAGEMENT",
                "ACL ALLOW-LOG " + ALLOWED_USER + " CREATE QUEUE",
                "ACL ALLOW-LOG " + ALLOWED_USER + " CREATE EXCHANGE",
                "ACL DENY-LOG " + DENIED_USER + " CREATE EXCHANGE",
                "ACL ALLOW-LOG " + ALLOWED_USER + " UPDATE EXCHANGE",
                "ACL DENY-LOG " + DENIED_USER + " UPDATE EXCHANGE",
                "ACL ALLOW-LOG " + ALLOWED_USER + " DELETE EXCHANGE",
                "ACL DENY-LOG " + DENIED_USER + " DELETE EXCHANGE",
                "ACL ALLOW-LOG " + ALLOWED_USER + " BIND EXCHANGE",
                "ACL DENY-LOG " + DENIED_USER + " BIND EXCHANGE",
                "ACL ALLOW-LOG " + ALLOWED_USER + " UNBIND EXCHANGE",
                "ACL DENY-LOG " + DENIED_USER + " UNBIND EXCHANGE",
                "ACL DENY-LOG ALL ALL");

        getBrokerConfiguration().setObjectAttribute(Plugin.class, TestBrokerConfiguration.ENTRY_NAME_HTTP_MANAGEMENT,
                HttpManagement.HTTP_BASIC_AUTHENTICATION_ENABLED, true);
    }

    @Override
    public void setUp() throws Exception
    {
        super.setUp();
        _queueName = getTestQueueName();
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);
        Map<String, Object> queueData = new HashMap<String, Object>();
        queueData.put(Queue.NAME, _queueName);
        queueData.put(Queue.DURABLE, Boolean.TRUE);
        int status = getRestTestHelper().submitRequest("queue/test/test/" + _queueName, "PUT", queueData);
        assertEquals("Unexpected status", 201, status);

        _exchangeName = getTestName();
        _exchangeUrl = "exchange/test/test/" + _exchangeName;
    }

    public void testCreateExchangeAllowed() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);

        int responseCode = createExchange();
        assertEquals("Exchange creation should be allowed", 201, responseCode);

        assertExchangeExists();
    }

    public void testCreateExchangeDenied() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER);

        int responseCode = createExchange();
        assertEquals("Exchange creation should be denied", 403, responseCode);

        assertExchangeDoesNotExist();
    }

    public void testDeleteExchangeAllowed() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);

        int responseCode = createExchange();
        assertEquals("Exchange creation should be allowed", 201, responseCode);

        assertExchangeExists();


        responseCode = getRestTestHelper().submitRequest(_exchangeUrl, "DELETE");
        assertEquals("Exchange deletion should be allowed", 200, responseCode);

        assertExchangeDoesNotExist();
    }

    public void testDeleteExchangeDenied() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);

        int responseCode = createExchange();
        assertEquals("Exchange creation should be allowed", 201, responseCode);

        assertExchangeExists();

        getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER);
        responseCode = getRestTestHelper().submitRequest(_exchangeUrl, "DELETE");
        assertEquals("Exchange deletion should be denied", 403, responseCode);

        assertExchangeExists();
    }

    public void testSetExchangeAttributesAllowed() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);

        int responseCode = createExchange();

        assertExchangeExists();

        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(Exchange.NAME, _exchangeName);
        attributes.put(Exchange.ALTERNATE_EXCHANGE, "my-alternate-exchange");

        responseCode = getRestTestHelper().submitRequest(_exchangeUrl, "PUT", attributes);
        assertEquals("Setting of exchange attribites should be allowed but it is currently unsupported", 409, responseCode);
    }

    public void testSetExchangeAttributesDenied() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);

        int responseCode = createExchange();
        assertExchangeExists();

        getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER);

        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(Exchange.NAME, _exchangeName);
        attributes.put(Exchange.ALTERNATE_EXCHANGE, "my-alternate-exchange");

        responseCode = getRestTestHelper().submitRequest(_exchangeUrl, "PUT", attributes);
        assertEquals("Setting of exchange attribites should be allowed", 403, responseCode);
    }

    public void testBindToExchangeAllowed() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(ALLOWED_USER, ALLOWED_USER);

        String bindingName = getTestName();
        int responseCode = createBinding(bindingName);
        assertEquals("Binding creation should be allowed", 201, responseCode);

        assertBindingExists(bindingName);
    }

    public void testBindToExchangeDenied() throws Exception
    {
        getRestTestHelper().setUsernameAndPassword(DENIED_USER, DENIED_USER);

        String bindingName = getTestName();
        int responseCode = createBinding(bindingName);
        assertEquals("Binding creation should be denied", 403, responseCode);

        assertBindingDoesNotExist(bindingName);
    }

    private int createExchange() throws Exception
    {
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(Exchange.NAME, _exchangeName);
        attributes.put(Exchange.TYPE, "direct");
        return getRestTestHelper().submitRequest(_exchangeUrl, "PUT", attributes);
    }

    private void assertExchangeDoesNotExist() throws Exception
    {
        assertExchangeExistence(false);
    }

    private void assertExchangeExists() throws Exception
    {
        assertExchangeExistence(true);
    }

    private void assertExchangeExistence(boolean exists) throws Exception
    {
        List<Map<String, Object>> exchanges = getRestTestHelper().getJsonAsList(_exchangeUrl);
        assertEquals("Unexpected result", exists, !exchanges.isEmpty());
    }

    private int createBinding(String bindingName) throws IOException, JsonGenerationException, JsonMappingException
    {
        Map<String, Object> attributes = new HashMap<String, Object>();
        attributes.put(Binding.NAME, bindingName);
        attributes.put(Binding.QUEUE, _queueName);
        attributes.put(Binding.EXCHANGE, "amq.direct");

        int responseCode = getRestTestHelper().submitRequest("binding/test/test/amq.direct/" + _queueName + "/" + bindingName, "PUT", attributes);
        return responseCode;
    }

    private void assertBindingDoesNotExist(String bindingName) throws Exception
    {
        assertBindingExistence(bindingName, false);
    }

    private void assertBindingExists(String bindingName) throws Exception
    {
        assertBindingExistence(bindingName, true);
    }

    private void assertBindingExistence(String bindingName, boolean exists) throws Exception
    {
        List<Map<String, Object>> bindings = getRestTestHelper().getJsonAsList("binding/test/test/amq.direct/" + _queueName + "/" + bindingName);
        assertEquals("Unexpected result", exists, !bindings.isEmpty());
    }
}