authorization check for more Kafka operations
[dmaap/kafka11aaf.git] / src / test / java / org / onap / dmaap / kafkaAuthorize / KafkaCustomAuthorizerTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Licensed under the Apache License, Version 2.0 (the "License");
8  *  you may not use this file except in compliance with the License.
9  *  You may obtain a copy of the License at
10  *        http://www.apache.org/licenses/LICENSE-2.0
11 *  
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *  ============LICENSE_END=========================================================
18  *  
19  *  
20  *******************************************************************************/
21 package org.onap.dmaap.kafkaAuthorize;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.junit.Assert.assertTrue;
25
26 import org.apache.kafka.common.acl.AclOperation;
27 import org.apache.kafka.common.security.auth.KafkaPrincipal;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.mockito.Mock;
32 import org.mockito.MockitoAnnotations;
33 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProvider;
34 import org.onap.dmaap.commonauth.kafka.base.authorization.AuthorizationProviderFactory;
35 import org.powermock.api.mockito.PowerMockito;
36 import org.powermock.core.classloader.annotations.PowerMockIgnore;
37 import org.powermock.core.classloader.annotations.PrepareForTest;
38 import org.powermock.modules.junit4.PowerMockRunner;
39
40 import kafka.network.RequestChannel.Session;
41 import kafka.security.auth.Operation;
42 import kafka.security.auth.Resource;
43 import kafka.security.auth.ResourceType;
44
45 @RunWith(PowerMockRunner.class)
46 @PowerMockIgnore({"javax.net.ssl.*", "javax.security.auth.*"})
47 @PrepareForTest({ AuthorizationProviderFactory.class })
48 public class KafkaCustomAuthorizerTest {
49         @Mock
50         Session arg0;
51         @Mock
52         Operation arg1;
53         @Mock
54         Resource arg2;
55         @Mock
56         KafkaPrincipal principal;
57         @Mock
58         ResourceType resourceType;
59         @Mock
60         AuthorizationProviderFactory factory;
61         @Mock
62         AuthorizationProvider provider;
63
64         KafkaCustomAuthorizer authorizer;
65         
66         static {
67                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
68         }
69
70         @Before
71         public void setUp() throws Exception {
72
73                 MockitoAnnotations.initMocks(this);
74                 PowerMockito.when(principal.getName()).thenReturn("fullName");
75                 PowerMockito.when(arg0.principal()).thenReturn(principal);
76                 PowerMockito.when(arg1.name()).thenReturn("Write");
77                 PowerMockito.when(resourceType.name()).thenReturn("Topic");
78                 PowerMockito.when(arg2.resourceType()).thenReturn(resourceType);
79                 PowerMockito.when(arg2.name()).thenReturn("namespace.Topic");
80                 PowerMockito.mockStatic(AuthorizationProviderFactory.class);
81                 PowerMockito.when(AuthorizationProviderFactory.getProviderFactory()).thenReturn(factory);
82                 PowerMockito.when(factory.getProvider()).thenReturn(provider);
83
84         }
85
86         @Test
87         public void testAuthorizerSuccess() {
88
89                 
90                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
91                                 .thenReturn(true);
92                 authorizer = new KafkaCustomAuthorizer();
93                 assertTrue(authorizer.authorize(arg0, arg1, arg2));
94
95         }
96
97         @Test
98         public void testAuthorizerFailure() {
99                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
100                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
101                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.CREATE);
102                 System.setProperty("msgRtr.topicfactory.aaf", "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:");
103                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
104                                 .thenReturn(false);
105                 authorizer = new KafkaCustomAuthorizer();
106                 try {
107                         authorizer.authorize(arg0, arg1, arg2);
108                 } catch (Exception e) {
109                         assertTrue(true);
110                 }
111
112         }
113         
114         @Test
115         public void testAuthorizerFailure1() {
116                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
117                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
118                 PowerMockito.when(resourceType.name()).thenReturn("Cluster");
119                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.CREATE);
120                 System.setProperty("msgRtr.topicfactory.aaf", "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:");
121                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
122                                 .thenReturn(false);
123                 authorizer = new KafkaCustomAuthorizer();
124                 try {
125                         authorizer.authorize(arg0, arg1, arg2);
126                 } catch (Exception e) {
127                         assertTrue(true);
128                 }
129
130         }
131         
132         @Test
133         public void testAuthorizerFailure2() {
134                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
135                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
136                 PowerMockito.when(resourceType.name()).thenReturn("Topic");
137                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.WRITE);
138                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
139                                 .thenReturn(false);
140                 authorizer = new KafkaCustomAuthorizer();
141                 try {
142                         authorizer.authorize(arg0, arg1, arg2);
143                 } catch (Exception e) {
144                         assertTrue(true);
145                 }
146
147         }
148         
149         @Test
150         public void testAuthorizerFailure3() {
151                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
152                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
153                 PowerMockito.when(resourceType.name()).thenReturn("Topic");
154                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.DESCRIBE);
155                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
156                                 .thenReturn(false);
157                 authorizer = new KafkaCustomAuthorizer();
158                 try {
159                         authorizer.authorize(arg0, arg1, arg2);
160                 } catch (Exception e) {
161                         assertTrue(true);
162                 }
163
164         }
165         @Test
166         public void testAuthorizerFailure4() {
167                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
168                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
169                 PowerMockito.when(resourceType.name()).thenReturn("Topic");
170                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.READ);
171                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
172                                 .thenReturn(false);
173                 authorizer = new KafkaCustomAuthorizer();
174                 try {
175                         authorizer.authorize(arg0, arg1, arg2);
176                 } catch (Exception e) {
177                         assertTrue(true);
178                 }
179
180         }
181         
182         @Test
183         public void testAuthorizerFailure5() {
184                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
185                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
186                 PowerMockito.when(resourceType.name()).thenReturn("Cluster");
187                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.IDEMPOTENT_WRITE);
188                 System.setProperty("msgRtr.topicfactory.aaf", "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:");
189                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
190                                 .thenReturn(false);
191                 authorizer = new KafkaCustomAuthorizer();
192                 try {
193                         authorizer.authorize(arg0, arg1, arg2);
194                 } catch (Exception e) {
195                         assertTrue(true);
196                 }
197
198         }
199         
200         @Test
201         public void testAuthorizerFailure6() {
202                 System.setProperty("CADI_PROPERTIES", "src/test/resources/cadi.properties");
203                 PowerMockito.when(arg2.name()).thenReturn("org.onap.dmaap.mr.testtopic");
204                 PowerMockito.when(arg1.toJava()).thenReturn(AclOperation.DELETE);
205                 System.setProperty("msgRtr.topicfactory.aaf", "org.onap.dmaap.mr.topicFactory|:org.onap.dmaap.mr.topic:");
206                 PowerMockito.when(provider.hasPermission("fullName", "namespace.topic", ":topic.namespace.Topic", "pub"))
207                                 .thenReturn(false);
208                 authorizer = new KafkaCustomAuthorizer();
209                 try {
210                         authorizer.authorize(arg0, arg1, arg2);
211                 } catch (Exception e) {
212                         assertTrue(true);
213                 }
214
215         }
216         
217
218 }