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
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,52 @@
import org.apache.commons.lang3.StringUtils;
import org.apache.ranger.authorization.utils.JsonUtils;
import org.apache.ranger.plugin.util.RangerPluginCapability;
import org.junit.Test;
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 java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

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

/**
* @generated by Cursor
* @description <Unit Test for RangerPluginCapability class>
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerPluginCapability {
private static final Gson gsonBuilder = new GsonBuilder().setDateFormat("yyyyMMdd-HH:mm:ss.SSS-Z").setPrettyPrinting().create();

@Test
public void testRangerPluginCapabilities() {
public void test01_RangerPluginCapabilitiesFromResource() {
String[] tests = {"/policyengine/plugin/test_plugin_capability.json"};

runTestsFromResourceFiles(tests);
}

@Test
public void test02_toStringAndUnknownBit() {
// Construct a capability with a bit beyond defined enum to trigger "unknown"
long beyond = 1L << 62; // well beyond current enum size
RangerPluginCapability me = new RangerPluginCapability(beyond);
RangerPluginCapability other = new RangerPluginCapability();

List<String> diff = me.compare(other);
assertTrue(diff.contains("unknown"));

String json = me.toString();
assertNotNull(json);
// toString returns JSON array
assertTrue(json.startsWith("["));
}

private void runTestsFromResourceFiles(String[] resourceNames) {
for (String resourceName : resourceNames) {
InputStream inStream = this.getClass().getResourceAsStream(resourceName);
Expand All @@ -69,7 +96,7 @@ private void runTests(InputStreamReader reader, String fileName) {

List<String> difference = me.compare(other);

assertTrue(fileName + "-" + testCase.name + "-" + Arrays.toString(difference.toArray()), StringUtils.equals(JsonUtils.listToJson(difference), JsonUtils.listToJson(testCase.difference)));
assertTrue(StringUtils.equals(JsonUtils.listToJson(difference), JsonUtils.listToJson(testCase.difference)), fileName + "-" + testCase.name + "-" + Arrays.toString(difference.toArray()));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* 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.plugin.service;

import org.apache.ranger.plugin.model.RangerRole;
import org.apache.ranger.plugin.policyengine.RangerPolicyEngine;
import org.apache.ranger.plugin.util.RangerCommonConstants;
import org.apache.ranger.plugin.util.RangerRoles;
import org.apache.ranger.plugin.util.RangerUserStore;
import org.apache.ranger.ugsyncutil.transform.Mapper;
import org.apache.ranger.ugsyncutil.util.UgsyncCommonConstants;
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 java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

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

/**
* @generated by Cursor
* @description <Unit Test for RangerAuthContext class>
*/
@ExtendWith(MockitoExtension.class)
@TestMethodOrder(MethodOrderer.MethodName.class)
public class TestRangerAuthContext {
public static class NoopMapper implements Mapper {
@Override
public void init(String baseProperty, List<String> regexPatterns, String regexSeparator) {
}

@Override
public String transform(String attrValue) {
return attrValue;
}
}

@Test
public void test1_RoleAggregationForUserAndGroups() {
RangerRole roleA = new RangerRole();
roleA.setName("roleA");
roleA.setUsers(Collections.singletonList(new RangerRole.RoleMember("u1", false)));

RangerRole roleB = new RangerRole();
roleB.setName("roleB");
roleB.setGroups(Collections.singletonList(new RangerRole.RoleMember("g1", false)));

RangerRole roleC = new RangerRole();
roleC.setName("roleC");
roleC.setGroups(Collections.singletonList(new RangerRole.RoleMember(RangerPolicyEngine.GROUP_PUBLIC, false)));

Set<RangerRole> rset = new HashSet<>();
rset.add(roleA);
rset.add(roleB);
rset.add(roleC);

RangerRoles roles = new RangerRoles();
roles.setRoleVersion(5L);
roles.setRangerRoles(rset);

RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, roles, new RangerUserStore());

Set<String> rolesOut = ctx.getRolesForUserAndGroups("u1", Collections.singleton("g1"));
assertTrue(rolesOut.contains("roleA"));
assertTrue(rolesOut.contains("roleB"));
assertTrue(rolesOut.contains("roleC"));
assertEquals(5L, ctx.getRoleVersion());
}

@Test
public void test2_OnServiceConfigsUpdateSetsTransformers() {
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, new RangerRoles(), new RangerUserStore());
Map<String, String> cfg = new HashMap<>();
cfg.put(RangerCommonConstants.PLUGINS_CONF_USERNAME_CASE_CONVERSION_PARAM, "lower");
cfg.put(RangerCommonConstants.PLUGINS_CONF_GROUPNAME_CASE_CONVERSION_PARAM, "upper");
ctx.onServiceConfigsUpdate(cfg);
assertNotNull(ctx.getUserNameCaseConversion());
assertNotNull(ctx.getGroupNameCaseConversion());
assertEquals(UgsyncCommonConstants.CaseConversion.TO_LOWER, ctx.getUserNameCaseConversion());
assertEquals(UgsyncCommonConstants.CaseConversion.TO_UPPER, ctx.getGroupNameCaseConversion());
}

@Test
public void test3_OnServiceConfigsUpdateRegexPatternsAggregation() {
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, new RangerRoles(), new RangerUserStore());
Map<String, String> cfg = new HashMap<>();
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_USERNAME_HANDLER, this.getClass().getName() + "$NoopMapper");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_USERNAME, "^(.+)@example\\.com$");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_USERNAME + ".1", "^user_(.+)$");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_SEPARATOR, ":");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_GROUPNAME_HANDLER, this.getClass().getName() + "$NoopMapper");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_GROUPNAME, "^grp_(.+)$");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_GROUPNAME + ".1", "^team_(.+)$");
cfg.put(RangerCommonConstants.PLUGINS_CONF_MAPPING_SEPARATOR, ":");

ctx.onServiceConfigsUpdate(cfg);

// handlers should be set
assertNotNull(ctx.getUserNameTransformer());
assertNotNull(ctx.getGroupNameTransformer());
}

@Test
public void test4_UserStoreVersionPassThrough() {
RangerUserStore us = new RangerUserStore();
us.setUserStoreVersion(42L);
RangerAuthContext ctx = new RangerAuthContext(new HashMap<>(), null, new RangerRoles(), us);
assertEquals(42L, ctx.getUserStoreVersion());
}
}
Loading
Loading