Skip to content
Open
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
12 changes: 9 additions & 3 deletions hive-agent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,15 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>${slf4j.version}</version>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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 com.xasecure.authorization.hive.authorizer;

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.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
* @generated by Cursor
* @description : Unit Test cases for XaSecureHiveAuthorizerFactory
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestXaSecureHiveAuthorizerFactory {
@Test
public void testConstructor() {
XaSecureHiveAuthorizerFactory factory = new XaSecureHiveAuthorizerFactory();
assertNotNull(factory);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/*
* 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.hive.authorizer;

import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzContext;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzSessionContext;
import org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType;
import org.apache.ranger.authorization.hive.authorizer.RangerHiveAuthorizer.HiveAccessType;
import org.apache.ranger.authorization.hive.authorizer.RangerHiveAuthorizer.HiveObjectType;
import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
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.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import static org.apache.ranger.authorization.hive.authorizer.RangerHiveAuditHandler.ACTION_TYPE_METADATA_OPERATION;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

/**
* @generated by Cursor
* @description : Unit Test cases for RangerHiveAccessRequest
*/

@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerHiveAccessRequest {
@Mock
private HiveAuthzContext mockAuthzContext;

@Mock
private HiveAuthzSessionContext mockSessionContext;

@Test
public void test01_setHiveAccessTypeMapsUseToAnyAndOthersToLowercase() {
RangerHiveResource resource = new RangerHiveResource(HiveObjectType.DATABASE, "db1");

RangerHiveAccessRequest useReq = new RangerHiveAccessRequest(resource, "user1", emptySet(), emptySet(), ACTION_TYPE_METADATA_OPERATION, HiveAccessType.USE, null, null);
assertEquals(HiveAccessType.USE, useReq.getHiveAccessType());
assertEquals(RangerPolicyEngine.ANY_ACCESS, useReq.getAccessType());

RangerHiveAccessRequest createReq = new RangerHiveAccessRequest(resource, "user1", emptySet(), emptySet(), ACTION_TYPE_METADATA_OPERATION, HiveAccessType.CREATE, null, null);
assertEquals(HiveAccessType.CREATE, createReq.getHiveAccessType());
assertEquals("create", createReq.getAccessType());
}

@Test
public void test02_constructorSetsMetadataDefaultsForDatabaseAndTable() {
// Database: expect "show databases"
RangerHiveResource dbResource = new RangerHiveResource(HiveObjectType.DATABASE, "");
when(mockAuthzContext.getCommandString()).thenReturn("");
when(mockAuthzContext.getForwardedAddresses()).thenReturn(Collections.singletonList("10.0.0.1"));
when(mockAuthzContext.getIpAddress()).thenReturn("127.0.0.1");
when(mockSessionContext.getSessionString()).thenReturn("session-1");

RangerHiveAccessRequest dbReq = new RangerHiveAccessRequest(dbResource, "alice", emptySet(), emptySet(), ACTION_TYPE_METADATA_OPERATION, HiveAccessType.USE, mockAuthzContext, mockSessionContext);
assertEquals("show databases", dbReq.getRequestData());
assertEquals("127.0.0.1", dbReq.getRemoteIPAddress());
assertEquals("session-1", dbReq.getSessionId());
assertNotNull(dbReq.getForwardedAddresses());

// Table: expect "show tables / views"
RangerHiveResource tblResource = new RangerHiveResource(HiveObjectType.TABLE, "db1", "tbl1");
when(mockAuthzContext.getCommandString()).thenReturn("");

RangerHiveAccessRequest tblReq = new RangerHiveAccessRequest(tblResource, "bob", emptySet(), emptySet(), ACTION_TYPE_METADATA_OPERATION, HiveAccessType.USE, mockAuthzContext, mockSessionContext);
assertEquals("show tables / views", tblReq.getRequestData());
}

@Test
public void test03_copyClonesAllRelevantFields() {
RangerHiveResource resource = new RangerHiveResource(HiveObjectType.TABLE, "dbA", "t1");
when(mockAuthzContext.getCommandString()).thenReturn("select 1");
when(mockAuthzContext.getForwardedAddresses()).thenReturn(Collections.singletonList("fwd"));
when(mockAuthzContext.getIpAddress()).thenReturn("1.2.3.4");
when(mockSessionContext.getSessionString()).thenReturn("sid");

Set<String> groups = new HashSet<>();
groups.add("g1");
Set<String> roles = new HashSet<>();
roles.add("r1");

RangerHiveAccessRequest req = new RangerHiveAccessRequest(resource, "u1", groups, roles, ACTION_TYPE_METADATA_OPERATION, HiveAccessType.SELECT, mockAuthzContext, mockSessionContext);
req.setClusterName("cl1");
req.setClusterType("test");

RangerHiveAccessRequest copy = req.copy();

assertEquals(req.getResource(), copy.getResource());
assertEquals(req.getAccessType(), copy.getAccessType());
assertEquals(req.getUser(), copy.getUser());
assertEquals(req.getUserGroups(), copy.getUserGroups());
assertEquals(req.getUserRoles(), copy.getUserRoles());
assertEquals(req.getAction(), copy.getAction());
assertEquals(req.getClientIPAddress(), copy.getClientIPAddress());
assertEquals(req.getRemoteIPAddress(), copy.getRemoteIPAddress());
assertEquals(req.getForwardedAddresses(), copy.getForwardedAddresses());
assertEquals(req.getRequestData(), copy.getRequestData());
assertEquals(req.getClientType(), copy.getClientType());
assertEquals(req.getSessionId(), copy.getSessionId());
assertEquals(req.getHiveAccessType(), copy.getHiveAccessType());
assertEquals(req.getClusterName(), copy.getClusterName());
assertEquals(req.getClusterType(), copy.getClusterType());
}

@Test
public void test04_hiveOperationTypeConstructorSetsActionAndAccess() {
RangerHiveResource resource = new RangerHiveResource(HiveObjectType.TABLE, "db2", "t2");

RangerHiveAccessRequest req = new RangerHiveAccessRequest(resource, "user2", emptySet(), emptySet(), HiveOperationType.CREATETABLE, HiveAccessType.CREATE, null, null);
assertEquals(HiveAccessType.CREATE, req.getHiveAccessType());
assertEquals("create", req.getAccessType());
assertEquals(HiveOperationType.CREATETABLE.name(), req.getAction());
}

@Test
public void test05_shortConstructorSetsMetadataUseDefaults() {
RangerHiveResource dbResource = new RangerHiveResource(HiveObjectType.DATABASE, "");
when(mockAuthzContext.getCommandString()).thenReturn("");
when(mockAuthzContext.getForwardedAddresses()).thenReturn(Collections.singletonList("10.0.0.2"));
when(mockAuthzContext.getIpAddress()).thenReturn("127.0.0.2");

RangerHiveAccessRequest req = new RangerHiveAccessRequest(dbResource, "carol", emptySet(), emptySet(), mockAuthzContext, null);
assertEquals(HiveAccessType.USE, req.getHiveAccessType());
assertEquals(RangerPolicyEngine.ANY_ACCESS, req.getAccessType());
assertEquals(ACTION_TYPE_METADATA_OPERATION, req.getAction());
assertEquals("show databases", req.getRequestData());
}

private static Set<String> emptySet() {
return Collections.emptySet();
}
}
Loading
Loading