Skip to content

Commit d21032d

Browse files
authored
RANGER-5397: IllegalArgumentException in RangerRESTClient when no ranger.plugin.<service>.policy.rest.url is configured (#779)
* RANGER-5397: IllegalArgumentException in RangerRESTClient when no ranger.plugin.<service>.policy.rest.url is configured * fixed checkstyle issues. * change the code to throw IAE if url is not set. * migrated test to junit 5
1 parent bb84319 commit d21032d

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

agents-common/src/main/java/org/apache/ranger/plugin/util/RangerRESTClient.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ public RangerRESTClient(String url, String sslConfigFileName, Configuration conf
110110
mUrl = url;
111111
mSslConfigFileName = sslConfigFileName;
112112
configuredURLs = StringUtil.getURLs(mUrl);
113-
114-
setLastKnownActiveUrlIndex((new Random()).nextInt(getConfiguredURLs().size()));
115-
113+
if (StringUtil.isEmpty(url)) {
114+
throw new IllegalArgumentException("Ranger URL is null or empty. Likely caused by incorrect configuration");
115+
} else {
116+
setLastKnownActiveUrlIndex((new Random()).nextInt(getConfiguredURLs().size()));
117+
}
116118
init(config);
117119
}
118120

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
20+
package org.apache.ranger.plugin.util;
21+
22+
import org.apache.ranger.authorization.hadoop.config.RangerPluginConfig;
23+
import org.apache.ranger.plugin.policyengine.RangerPolicyEngineOptions;
24+
import org.apache.ranger.plugin.service.RangerBasePlugin;
25+
import org.junit.jupiter.api.Test;
26+
27+
import static org.junit.jupiter.api.Assertions.assertNotNull;
28+
import static org.junit.jupiter.api.Assertions.assertThrows;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
public class TestRangerRESTClient {
32+
private static final String SERVICE_TYPE = "hive";
33+
private static final String SERVICE_NAME = "test-service";
34+
private static final String APP_ID = "test-app";
35+
private static final String ERR_MESSAGE = "Ranger URL is null or empty.";
36+
37+
@Test
38+
public void testPluginInit_WithNoUrl_ThrowsException() {
39+
RangerBasePlugin plugin = new RangerBasePlugin(SERVICE_TYPE, SERVICE_NAME, APP_ID);
40+
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, plugin::init);
41+
assertTrue(exception.getMessage().contains(ERR_MESSAGE));
42+
}
43+
44+
@Test
45+
public void testPluginInit_WithValidUrl_Succeeds() {
46+
RangerPolicyEngineOptions peOptions = new RangerPolicyEngineOptions();
47+
RangerPluginConfig pluginConfig = new RangerPluginConfig(SERVICE_TYPE, SERVICE_NAME, APP_ID, "cl1", "on-perm", peOptions);
48+
pluginConfig.set("ranger.plugin.hive.policy.rest.url", "http://dummy:1234");
49+
RangerBasePlugin plugin = new RangerBasePlugin(pluginConfig);
50+
plugin.init();
51+
assertNotNull(plugin, "RangerBasePlugin should be initialized successfully");
52+
}
53+
}

0 commit comments

Comments
 (0)