|
| 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 | +} |
0 commit comments