Skip to content

Commit 5f48480

Browse files
authored
RANGER-5307: Test Cases for Security-Admin Module: Package[validation ,solr ,solr.krb ,elasticsearch ,credentialapi ,amazon.cloudwatch] (#657)
1 parent cc0fb14 commit 5f48480

17 files changed

+3125
-7
lines changed
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.ranger.amazon.cloudwatch;
20+
21+
import com.amazonaws.services.logs.AWSLogs;
22+
import com.amazonaws.services.logs.model.FilteredLogEvent;
23+
import org.apache.ranger.audit.model.AuthzAuditEvent;
24+
import org.apache.ranger.audit.provider.MiscUtil;
25+
import org.apache.ranger.common.MessageEnums;
26+
import org.apache.ranger.common.PropertiesUtil;
27+
import org.apache.ranger.common.RESTErrorUtil;
28+
import org.apache.ranger.common.SearchCriteria;
29+
import org.apache.ranger.db.RangerDaoManager;
30+
import org.apache.ranger.db.XXServiceDao;
31+
import org.apache.ranger.db.XXServiceDefDao;
32+
import org.apache.ranger.entity.XXService;
33+
import org.apache.ranger.entity.XXServiceDef;
34+
import org.apache.ranger.view.VXAccessAuditList;
35+
import org.apache.ranger.view.VXLong;
36+
import org.junit.jupiter.api.BeforeEach;
37+
import org.junit.jupiter.api.MethodOrderer;
38+
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.api.TestMethodOrder;
40+
import org.junit.jupiter.api.extension.ExtendWith;
41+
import org.mockito.Mock;
42+
import org.mockito.MockedStatic;
43+
import org.mockito.junit.jupiter.MockitoExtension;
44+
45+
import javax.ws.rs.WebApplicationException;
46+
import javax.ws.rs.core.Response;
47+
48+
import java.util.Arrays;
49+
50+
import static org.junit.jupiter.api.Assertions.assertEquals;
51+
import static org.junit.jupiter.api.Assertions.assertNotNull;
52+
import static org.junit.jupiter.api.Assertions.assertNull;
53+
import static org.junit.jupiter.api.Assertions.assertThrows;
54+
import static org.mockito.ArgumentMatchers.any;
55+
import static org.mockito.ArgumentMatchers.anyBoolean;
56+
import static org.mockito.ArgumentMatchers.anyList;
57+
import static org.mockito.ArgumentMatchers.anyLong;
58+
import static org.mockito.ArgumentMatchers.eq;
59+
import static org.mockito.Mockito.mock;
60+
import static org.mockito.Mockito.mockStatic;
61+
import static org.mockito.Mockito.when;
62+
63+
/**
64+
* @generated by Cursor
65+
* @description : Unit Test cases for CloudWatchAccessAuditsService
66+
*/
67+
68+
@ExtendWith(MockitoExtension.class)
69+
@TestMethodOrder(MethodOrderer.MethodName.class)
70+
public class CloudWatchAccessAuditsServiceTest {
71+
@Mock CloudWatchMgr mgr;
72+
@Mock CloudWatchUtil util;
73+
@Mock RESTErrorUtil restErrorUtil;
74+
@Mock RangerDaoManager daoManager;
75+
TestSvc svc;
76+
77+
@BeforeEach
78+
public void setUp() {
79+
svc = new TestSvc();
80+
svc.setCloudWatchMgrForTest(mgr);
81+
svc.setCloudWatchUtilForTest(util);
82+
svc.setRestErrorUtil(restErrorUtil);
83+
svc.setDaoManagerForTest(daoManager);
84+
}
85+
86+
@Test
87+
public void searchXAccessAudits_throws_whenClientNull() {
88+
when(mgr.getClient()).thenReturn(null);
89+
when(restErrorUtil.createRESTException(any(String.class), any(MessageEnums.class)))
90+
.thenThrow(new WebApplicationException(Response.status(500).build()));
91+
92+
assertThrows(WebApplicationException.class, () -> svc.searchXAccessAudits(new SearchCriteria()));
93+
}
94+
95+
@Test
96+
public void searchXAccessAudits_throws_whenSearchFails() {
97+
AWSLogs client = mock(AWSLogs.class);
98+
when(mgr.getClient()).thenReturn(client);
99+
when(util.searchResources(any(AWSLogs.class), any(SearchCriteria.class), anyList(), anyList()))
100+
.thenThrow(new RuntimeException("err"));
101+
when(restErrorUtil.createRESTException(any(String.class), any(MessageEnums.class)))
102+
.thenThrow(new WebApplicationException(Response.status(500).build()));
103+
104+
assertThrows(WebApplicationException.class, () -> svc.searchXAccessAudits(new SearchCriteria()));
105+
}
106+
107+
@Test
108+
public void searchXAccessAudits_transformsEvents_and_appliesHiveVisibility() {
109+
// properties: hide hive query
110+
String key = "ranger.audit.hive.query.visibility";
111+
String old = PropertiesUtil.getPropertiesMap().get(key);
112+
PropertiesUtil.getPropertiesMap().put(key, "false");
113+
114+
try (MockedStatic<PropertiesUtil> mocked = mockStatic(PropertiesUtil.class)) {
115+
mocked.when(() -> PropertiesUtil.getBooleanProperty(eq(key), anyBoolean())).thenReturn(false);
116+
117+
AWSLogs client = mock(AWSLogs.class);
118+
when(mgr.getClient()).thenReturn(client);
119+
120+
// build two events: one hive grant/revoke with requestData, one non-hive
121+
AuthzAuditEvent hive = new AuthzAuditEvent();
122+
hive.setRepositoryName("svc");
123+
hive.setRepositoryType(1);
124+
hive.setAccessType("grant");
125+
hive.setRequestData("select+*+from+x");
126+
127+
AuthzAuditEvent other = new AuthzAuditEvent();
128+
other.setRepositoryName("svc");
129+
other.setRepositoryType(1);
130+
other.setAccessType("read");
131+
other.setRequestData("abc");
132+
133+
FilteredLogEvent e1 = new FilteredLogEvent().withMessage(MiscUtil.stringify(hive));
134+
FilteredLogEvent e2 = new FilteredLogEvent().withMessage(MiscUtil.stringify(other));
135+
136+
when(util.searchResources(any(AWSLogs.class), any(SearchCriteria.class), anyList(), anyList()))
137+
.thenReturn(Arrays.asList(e1, e2));
138+
139+
// mock dao manager for display names
140+
XXServiceDao svcDao = mock(XXServiceDao.class);
141+
XXServiceDefDao svcDefDao = mock(XXServiceDefDao.class);
142+
when(daoManager.getXXService()).thenReturn(svcDao);
143+
when(daoManager.getXXServiceDef()).thenReturn(svcDefDao);
144+
145+
XXService xxService = mock(XXService.class);
146+
when(xxService.getDisplayName()).thenReturn("ServiceDisplay");
147+
when(svcDao.findByName(any())).thenReturn(xxService);
148+
149+
XXServiceDef def = mock(XXServiceDef.class);
150+
when(def.getName()).thenReturn("hive");
151+
when(def.getDisplayName()).thenReturn("Hive");
152+
when(svcDefDao.getById(anyLong())).thenReturn(def);
153+
154+
SearchCriteria sc = new SearchCriteria();
155+
sc.setStartIndex(0);
156+
sc.setMaxRows(10);
157+
158+
VXAccessAuditList out = svc.searchXAccessAudits(sc);
159+
160+
assertNotNull(out);
161+
assertEquals(2, out.getTotalCount());
162+
assertEquals(2, out.getResultSize());
163+
assertEquals(2, out.getVXAccessAudits().size());
164+
165+
// check hive requestData hidden (null) when visibility is false
166+
assertNull(out.getVXAccessAudits().get(1).getRequestData());
167+
// display names applied
168+
assertEquals("ServiceDisplay", out.getVXAccessAudits().get(0).getRepoDisplayName());
169+
assertEquals("hive", out.getVXAccessAudits().get(1).getServiceType());
170+
assertEquals("Hive", out.getVXAccessAudits().get(1).getServiceTypeDisplayName());
171+
} finally {
172+
if (old == null) {
173+
PropertiesUtil.getPropertiesMap().remove(key);
174+
} else {
175+
PropertiesUtil.getPropertiesMap().put(key, old);
176+
}
177+
}
178+
}
179+
180+
@Test
181+
public void getXAccessAuditSearchCount_returns100() {
182+
VXLong out = svc.getXAccessAuditSearchCount(new SearchCriteria());
183+
assertNotNull(out);
184+
assertEquals(100L, out.getValue());
185+
}
186+
187+
static class TestSvc extends CloudWatchAccessAuditsService {
188+
public void setDaoManagerForTest(RangerDaoManager dm) {
189+
this.daoManager = dm;
190+
}
191+
192+
public void setCloudWatchMgrForTest(CloudWatchMgr m) {
193+
this.cloudWatchMgr = m;
194+
}
195+
196+
public void setCloudWatchUtilForTest(CloudWatchUtil u) {
197+
this.cloudWatchUtil = u;
198+
}
199+
}
200+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.ranger.amazon.cloudwatch;
20+
21+
import com.amazonaws.regions.Regions;
22+
import com.amazonaws.services.logs.AWSLogs;
23+
import com.amazonaws.services.logs.AWSLogsClientBuilder;
24+
import org.apache.ranger.common.PropertiesUtil;
25+
import org.junit.jupiter.api.AfterEach;
26+
import org.junit.jupiter.api.MethodOrderer;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.TestMethodOrder;
29+
import org.junit.jupiter.api.extension.ExtendWith;
30+
import org.mockito.MockedStatic;
31+
import org.mockito.junit.jupiter.MockitoExtension;
32+
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
import static org.mockito.ArgumentMatchers.any;
35+
import static org.mockito.Mockito.mock;
36+
import static org.mockito.Mockito.mockStatic;
37+
import static org.mockito.Mockito.times;
38+
import static org.mockito.Mockito.verify;
39+
import static org.mockito.Mockito.when;
40+
41+
/**
42+
* @generated by Cursor
43+
* @description : Unit Test cases for CloudWatchMgr
44+
*/
45+
46+
@ExtendWith(MockitoExtension.class)
47+
@TestMethodOrder(MethodOrderer.MethodName.class)
48+
public class CloudWatchMgrTest {
49+
@AfterEach
50+
public void tearDown() {
51+
PropertiesUtil.getPropertiesMap().remove("ranger.audit.amazon_cloudwatch.region");
52+
}
53+
54+
@Test
55+
public void getClient_buildsDefault_whenRegionBlank() {
56+
PropertiesUtil.getPropertiesMap().put("ranger.audit.amazon_cloudwatch.region", " ");
57+
CloudWatchMgr mgr = new CloudWatchMgr();
58+
59+
AWSLogsClientBuilder builder = mock(AWSLogsClientBuilder.class);
60+
AWSLogs client = mock(AWSLogs.class);
61+
62+
try (MockedStatic<AWSLogsClientBuilder> mocked = mockStatic(AWSLogsClientBuilder.class)) {
63+
mocked.when(AWSLogsClientBuilder::standard).thenReturn(builder);
64+
// For default path, build() is called directly without withRegion
65+
when(builder.build()).thenReturn(client);
66+
67+
AWSLogs out = mgr.getClient();
68+
assertNotNull(out);
69+
verify(builder, times(1)).build();
70+
}
71+
}
72+
73+
@Test
74+
public void getClient_buildsWithRegion_whenRegionProvided() {
75+
PropertiesUtil.getPropertiesMap().put("ranger.audit.amazon_cloudwatch.region", Regions.US_EAST_1.getName());
76+
CloudWatchMgr mgr = new CloudWatchMgr();
77+
78+
AWSLogsClientBuilder builder = mock(AWSLogsClientBuilder.class);
79+
AWSLogs client = mock(AWSLogs.class);
80+
81+
try (MockedStatic<AWSLogsClientBuilder> mocked = mockStatic(AWSLogsClientBuilder.class)) {
82+
mocked.when(AWSLogsClientBuilder::standard).thenReturn(builder);
83+
when(builder.withRegion(any(String.class))).thenReturn(builder);
84+
when(builder.build()).thenReturn(client);
85+
86+
AWSLogs out = mgr.getClient();
87+
assertNotNull(out);
88+
verify(builder, times(1)).withRegion(Regions.US_EAST_1.getName());
89+
verify(builder, times(1)).build();
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)