vyommani commented on code in PR #568: URL: https://github.com/apache/ranger/pull/568#discussion_r2094171255
########## agents-common/src/test/java/org/apache/ranger/plugin/policyengine/RangerBasePluginTest.java: ########## @@ -0,0 +1,257 @@ +/* + * 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.policyengine; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonDeserializer; +import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import org.apache.commons.lang.StringUtils; +import org.apache.ranger.authorization.hadoop.config.RangerPluginConfig; +import org.apache.ranger.plugin.model.RangerServiceDef; +import org.apache.ranger.plugin.service.RangerBasePlugin; +import org.apache.ranger.plugin.store.EmbeddedServiceDefsUtil; +import org.apache.ranger.plugin.util.RangerAccessRequestUtil; +import org.apache.ranger.plugin.util.RangerRoles; +import org.apache.ranger.plugin.util.RangerUserStore; +import org.apache.ranger.plugin.util.ServiceDefUtil; +import org.apache.ranger.plugin.util.ServiceGdsInfo; +import org.apache.ranger.plugin.util.ServicePolicies; +import org.apache.ranger.plugin.util.ServiceTags; +import org.junit.BeforeClass; +import org.junit.Test; + +import java.io.File; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.lang.reflect.Type; +import java.nio.file.FileSystems; +import java.util.Collection; +import java.util.Date; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; + +public class RangerBasePluginTest { + private static final String RANGER_SERVICE_TYPE = "hbase"; + private static final String RANGER_APP_ID = "hbase"; + private static final String RANGER_DEFAULT_SERVICE_NAME = "cm_hbase"; + private static final String TEST_JSON = "/policyengine/test_base_plugin_hbase.json"; + private static final String RANGER_DEFAULT_SECURITY_CONF = "/target/test-classes/policyengine/ranger-hbase-security.xml"; + private static final String RANGER_DEFAULT_AUDIT_CONF = "/target/test-classes/policyengine/ranger-trino-audit.xml"; + private static final String RANGER_DEFAULT_POLICY_MGR_SSL_CONF = "/target/test-classes/policyengine/ranger-policymgr-ssl.xml"; + private static final String MESSAGE = "The failed count being zero suggests one of two possibilities: " + + "1. The PolicyRefresher might not be starting correctly. 2. There might be a race condition in our code, preventing the policy engine modifications from being reflected in RangerBasePlugin."; + + private static Gson gsonBuilder; + private static RangerBasePlugin rangerBasePlugin; + + @BeforeClass + public static void setUpBeforeClass() + throws Exception { + gsonBuilder = new GsonBuilder().setDateFormat("yyyyMMdd-HH:mm:ss.SSS-Z") + .setPrettyPrinting() + .registerTypeAdapter(RangerAccessRequest.class, new RangerAccessRequestDeserializer()) + .registerTypeAdapter(RangerAccessResource.class, new RangerResourceDeserializer()) + .create(); + + RangerPolicyEngineOptions peOptions = new RangerPolicyEngineOptions(); + RangerPluginConfig pluginConfig = new RangerPluginConfig(RANGER_SERVICE_TYPE, RANGER_DEFAULT_SERVICE_NAME, RANGER_APP_ID, "cl1", "on-perm", peOptions); + String basedir = new File(".").getCanonicalPath(); + pluginConfig.addResourceIfReadable(FileSystems.getDefault().getPath(basedir, RANGER_DEFAULT_AUDIT_CONF).toString()); + pluginConfig.addResourceIfReadable(FileSystems.getDefault().getPath(basedir, RANGER_DEFAULT_SECURITY_CONF).toString()); + pluginConfig.addResourceIfReadable(FileSystems.getDefault().getPath(basedir, RANGER_DEFAULT_POLICY_MGR_SSL_CONF).toString()); + pluginConfig.getProperties().put("ranger.plugin.hbase.supports.in.place.policy.updates", "true"); + pluginConfig.getProperties().put("ranger.plugin.hbase.supports.policy.deltas", "true"); + + rangerBasePlugin = new RangerBasePlugin(pluginConfig); + rangerBasePlugin.init(); + } + + @Test + @SuppressWarnings("PMD") + public void testCanSetUserOperations() throws Exception { + runTestsFromResourceFile(); + } + + private void runTestsFromResourceFile() throws Exception { + InputStream inStream = this.getClass().getResourceAsStream(TEST_JSON); + InputStreamReader reader = new InputStreamReader(inStream); + runTests(reader); + } + + private void runTests(Reader reader) throws Exception { + RangerBasePluginTestCase testCase = readTestCase(reader); + + assertNotNull("invalid input: " + TEST_JSON, testCase); + assertNotNull("invalid input: " + TEST_JSON, testCase.policies); + assertNotNull("invalid input: " + TEST_JSON, testCase.tags); + assertNotNull("invalid input: " + TEST_JSON, testCase.roles); + assertNotNull("invalid input: " + TEST_JSON, testCase.userStore); + assertNotNull("invalid input: " + TEST_JSON, testCase.gdsInfo); + assertNotNull("invalid input: " + TEST_JSON, testCase.tests); + + int count = 0; + int failedCount = 0; + while (count < 10000) { + RangerAccessResult result; + for (TestData test : testCase.tests) { + RangerAccessRequest request = test.request; + try { + if (test.result != null) { + result = rangerBasePlugin.isAccessAllowed(request); + assertNotNull("result was null! - " + test.name, result); + assertEquals("isAllowed mismatched! - " + test.name, test.result.getIsAllowed(), result.getIsAllowed()); + assertEquals("isAccessDetermined mismatched! - " + test.name, test.result.getIsAccessDetermined(), result.getIsAccessDetermined()); + assertEquals("isAllowed mismatched! - " + test.name, test.result.getPolicyId(), result.getPolicyId()); + } + } catch (Error e) { + // The PolicyRefresher modifies the policy, so it's expected that tests for the modified policy would fail. + if (test.result.getPolicyId() == 821) { Review Comment: The test is designed to ensure policy-delta triggering works correctly. Here's how it works: PolicyRefresher runs every 2 seconds. The test iterates 10,000 times, waiting 20ms each time. This wait mechanism allows sufficient time for policy-delta to trigger during the test. The test also checks for failed counts, throwing an error if it's zero. This setup ensures the test validates policy-delta triggering and handles potential failures -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@ranger.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org