Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugin-sqoop/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* 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.ranger.authorization.sqoop.authorizer;

import org.apache.ranger.plugin.policyengine.RangerAccessResourceImpl;
import org.apache.ranger.services.sqoop.client.SqoopResourceMgr;
import org.apache.sqoop.model.MPrincipal;
import org.apache.sqoop.model.MPrivilege;
import org.apache.sqoop.model.MResource;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Collections;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.mockStatic;
import static org.mockito.Mockito.when;

/**
* @generated by Cursor
* @description : Unit Test cases for RangerSqoopAuthorizer
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
class TestRangerSqoopAuthorizer {
@Test
void testCheckPrivilegesPositiveSkipsPolicyEvaluationWhenPluginUnset() throws Exception {
RangerSqoopAuthorizer auth = new RangerSqoopAuthorizer();
Field pluginField = RangerSqoopAuthorizer.class.getDeclaredField("sqoopPlugin");
pluginField.setAccessible(true);
Object previous = pluginField.get(null);
try {
pluginField.set(null, null);
MPrivilege privilege = mock(MPrivilege.class);
MPrincipal principal = mock(MPrincipal.class);
List<MPrivilege> privileges = Collections.singletonList(privilege);
auth.checkPrivileges(principal, privileges);
} finally {
pluginField.set(null, previous);
if (pluginField.get(null) == null) {
new RangerSqoopAuthorizer();
}
}
}

@Test
void testRangerSqoopAccessRequestPositiveHandlesGroupPrincipal() throws Exception {
Class<?> clazz = Class.forName(RangerSqoopAuthorizer.class.getName() + "$RangerSqoopAccessRequest");
Constructor<?> ctor = clazz.getDeclaredConstructor(MPrincipal.class, MPrivilege.class, String.class);
ctor.setAccessible(true);

MPrincipal principal = mock(MPrincipal.class);
MPrivilege privilege = mock(MPrivilege.class);
MResource resource = mock(MResource.class);

when(principal.getType()).thenReturn(MPrincipal.TYPE.GROUP.name());
when(principal.getName()).thenReturn("admins");
when(privilege.getResource()).thenReturn(resource);
when(privilege.getAction()).thenReturn("READ");
when(resource.getType()).thenReturn(MResource.TYPE.JOB.name());
when(resource.getName()).thenReturn("etl-job");

Object request = ctor.newInstance(principal, privilege, "192.168.1.10");

assertNotNull(request);
}

@Test
void testRangerSqoopAccessRequestPositiveHandlesUserPrincipal() throws Exception {
Class<?> clazz = Class.forName(RangerSqoopAuthorizer.class.getName() + "$RangerSqoopAccessRequest");
Constructor<?> ctor = clazz.getDeclaredConstructor(MPrincipal.class, MPrivilege.class, String.class);
ctor.setAccessible(true);

MPrincipal principal = mock(MPrincipal.class);
MPrivilege privilege = mock(MPrivilege.class);
MResource resource = mock(MResource.class);

when(principal.getType()).thenReturn(MPrincipal.TYPE.USER.name());
when(principal.getName()).thenReturn("alice");
when(privilege.getResource()).thenReturn(resource);
when(privilege.getAction()).thenReturn("WRITE");
when(resource.getType()).thenReturn(MResource.TYPE.CONNECTOR.name());
when(resource.getName()).thenReturn("jdbc");

Object request = ctor.newInstance(principal, privilege, "10.0.0.2");

assertNotNull(request);
}

@Test
void testRangerSqoopResourcePositiveRegistersConnectorLinkJobAttributes() throws Exception {
Class<?> clazz = Class.forName(RangerSqoopAuthorizer.class.getName() + "$RangerSqoopResource");
Constructor<?> ctor = clazz.getDeclaredConstructor(MResource.class);
ctor.setAccessible(true);

MResource connector = mock(MResource.class);
when(connector.getType()).thenReturn(MResource.TYPE.CONNECTOR.name());
when(connector.getName()).thenReturn("c1");
RangerAccessResourceImpl resConnector = (RangerAccessResourceImpl) ctor.newInstance(connector);
assertNotNull(resConnector.getValue(SqoopResourceMgr.CONNECTOR));

MResource link = mock(MResource.class);
when(link.getType()).thenReturn(MResource.TYPE.LINK.name());
when(link.getName()).thenReturn("l1");
RangerAccessResourceImpl resLink = (RangerAccessResourceImpl) ctor.newInstance(link);
assertNotNull(resLink.getValue(SqoopResourceMgr.LINK));

MResource job = mock(MResource.class);
when(job.getType()).thenReturn(MResource.TYPE.JOB.name());
when(job.getName()).thenReturn("j1");
RangerAccessResourceImpl resJob = (RangerAccessResourceImpl) ctor.newInstance(job);
assertNotNull(resJob.getValue(SqoopResourceMgr.JOB));
}

@Test
void testInitPositiveSecondCallIsNoOpWhenPluginAlreadyLoaded() {
RangerSqoopAuthorizer auth = new RangerSqoopAuthorizer();
assertDoesNotThrow(auth::init);
}

@Test
void testInitUsesSafeDefaultsWhenInetAddressLookupFails() throws Exception {
Field pluginField = RangerSqoopAuthorizer.class.getDeclaredField("sqoopPlugin");
pluginField.setAccessible(true);
Object previous = pluginField.get(null);
try {
pluginField.set(null, null);
try (MockedStatic<InetAddress> inet = mockStatic(InetAddress.class)) {
inet.when(InetAddress::getLocalHost).thenThrow(new UnknownHostException("missing-host"));
assertDoesNotThrow(() -> new RangerSqoopAuthorizer());
}
} finally {
pluginField.set(null, previous);
if (pluginField.get(null) == null) {
new RangerSqoopAuthorizer();
}
}
}
}
Loading