Skip to content

Commit e2d8d6b

Browse files
raju-saravananSaravanan Raju
and
Saravanan Raju
authored
MINOR : Refactor service principal configuration (#688)
* Change the serive auth configuration * Rename ranger configuration file * Refactor login code * Log exception Co-authored-by: Saravanan Raju <sraju@cloudera.com>
1 parent f90785e commit e2d8d6b

File tree

8 files changed

+58
-65
lines changed

8 files changed

+58
-65
lines changed

common/src/main/java/com/hortonworks/registries/common/AuthMethodConfiguration.java common/src/main/java/com/hortonworks/registries/common/ServiceAuthenticationConfiguration.java

+10-20
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
**/
1515
package com.hortonworks.registries.common;
1616

17-
public class AuthMethodConfiguration {
17+
import java.util.Map;
18+
19+
public class ServiceAuthenticationConfiguration {
20+
1821
private String type;
19-
private String serverPrinciple;
20-
private String serverPrincipleKeytab;
22+
23+
private Map<String, String> properties;
2124

2225
public String getType() {
2326
return type;
@@ -27,28 +30,15 @@ public void setType(String type) {
2730
this.type = type;
2831
}
2932

30-
public String getServerPrinciple() {
31-
return serverPrinciple;
32-
}
33-
34-
public void setServerPrinciple(String serverPrinciple) {
35-
this.serverPrinciple = serverPrinciple;
36-
}
37-
38-
public String getServerPrincipleKeytab() {
39-
return serverPrincipleKeytab;
40-
}
41-
42-
public void setServerPrincipleKeytab(String serverPrincipleKeytab) {
43-
this.serverPrincipleKeytab = serverPrincipleKeytab;
33+
public Map<String, String> getProperties() {
34+
return properties;
4435
}
4536

4637
@Override
4738
public String toString() {
48-
return "AuthMethodConfiguration{" +
39+
return "ServiceAuthenticationConfiguration{" +
4940
"type='" + type + '\'' +
50-
", serverPrinciple='" + serverPrinciple + '\'' +
51-
", serverPrincipleKeytab='" + serverPrincipleKeytab + '\'' +
41+
", properties='" + properties + '\'' +
5242
'}';
5343
}
5444
}

conf/registry.yaml

+4-3
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ logging:
107107
com.hortonworks.registries: INFO
108108

109109
# Config for schema registry kerberos principle
110-
#authenticationMethod:
110+
#serviceAuthenticationConfiguration:
111111
# type: kerberos
112-
# serverPrinciple: "schema-registry/hostname@GCE.CLOUDERA.COM"
113-
# serverPrincipleKeytab: "/tmp/schema-registry.keytab"
112+
# properties:
113+
# principal: "schema-registry/hostname@GCE.CLOUDERA.COM"
114+
# keytab: "/tmp/schema-registry.keytab"
114115

115116

schema-registry/authorizer/ranger-plugin/ranger-authorizer-shim/src/test/resources/ranger-schema-registry-security.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<name>ranger.plugin.schema-registry.service.name</name>
2222
<value>SR1</value>
2323
<description>
24-
Name of the Ranger service containing policies for this YARN instance
24+
Name of the Ranger service containing policies for this Schema Registry instance
2525
</description>
2626
</property>
2727

schema-registry/authorizer/ranger-plugin/ranger-authorizer/conf/ranger-schema-registry-security.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343

4444
<property>
4545
<name>ranger.plugin.schema-registry.policy.rest.ssl.config.file</name>
46-
<value>/tmp/schema-registry/ranger-plugin/ranger-policymgr-ssl.xml</value>
46+
<value>/tmp/schema-registry/ranger-plugin/ranger-schema-registry-policymgr-ssl.xml</value>
4747
<description>
4848
Path to the file containing SSL details to contact Ranger Admin
4949
</description>

schema-registry/authorizer/ranger-plugin/ranger-authorizer/src/test/resources/ranger-schema-registry-security.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<name>ranger.plugin.schema-registry.service.name</name>
2222
<value>SR1</value>
2323
<description>
24-
Name of the Ranger service containing policies for this YARN instance
24+
Name of the Ranger service containing policies for this Schema Registry instance
2525
</description>
2626
</property>
2727

webservice/src/main/java/com/hortonworks/registries/webservice/RegistryApplication.java

+34-29
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
**/
1515
package com.hortonworks.registries.webservice;
1616

17-
import com.hortonworks.registries.common.AuthMethodConfiguration;
17+
import com.hortonworks.registries.common.ServiceAuthenticationConfiguration;
1818
import com.hortonworks.registries.common.FileStorageConfiguration;
1919
import com.hortonworks.registries.common.GenericExceptionMapper;
2020
import com.hortonworks.registries.common.HAConfiguration;
@@ -44,6 +44,7 @@
4444
import io.dropwizard.setup.Environment;
4545
import io.federecio.dropwizard.swagger.SwaggerBundle;
4646
import io.federecio.dropwizard.swagger.SwaggerBundleConfiguration;
47+
import org.apache.commons.lang.StringUtils;
4748
import org.apache.hadoop.security.UserGroupInformation;
4849
import org.eclipse.jetty.server.Server;
4950
import org.eclipse.jetty.servlets.CrossOriginFilter;
@@ -94,29 +95,33 @@ public void run(RegistryConfiguration registryConfiguration, Environment environ
9495
}
9596

9697
private void initializeUGI(RegistryConfiguration conf) throws IOException {
97-
LOG.debug("Initialization of User Group ininformation...");
98-
if (UserGroupInformation.isSecurityEnabled()) {
99-
LOG.debug("UGI.isSecurityEnabled() = true.");
100-
101-
AuthMethodConfiguration authMethodConf = conf.getAuthenticationMethod();
102-
if(authMethodConf != null) {
103-
String serverPrincipal = authMethodConf.getServerPrinciple();
104-
String keyTab = authMethodConf.getServerPrincipleKeytab();
105-
106-
LOG.debug("UGI is trying to login with principle = " + serverPrincipal
107-
+ ", keyTab = " + keyTab);
108-
109-
//Authenticate using keytab
110-
UserGroupInformation.loginUserFromKeytab(serverPrincipal, keyTab);
111-
112-
LOG.debug("UGI is login successfully with principle = " + serverPrincipal
113-
+ ", keyTab = " + keyTab);
98+
if (conf.getServiceAuthenticationConfiguration() != null) {
99+
String authenticationType = conf.getServiceAuthenticationConfiguration().getType();
100+
if (authenticationType != null && authenticationType.equals("kerberos")) {
101+
Map<String, String> serviceAuthenticationProperties = conf.getServiceAuthenticationConfiguration().getProperties();
102+
if (serviceAuthenticationProperties != null) {
103+
String principal = serviceAuthenticationProperties.get("principal");
104+
String keytab = serviceAuthenticationProperties.get("keytab");
105+
106+
if (StringUtils.isNotEmpty(principal) && StringUtils.isNotEmpty(keytab)) {
107+
LOG.debug("Login with principal = '" + principal + "' and keyTab = '" + keytab + "'");
108+
try {
109+
UserGroupInformation.loginUserFromKeytab(principal, keytab);
110+
LOG.debug("Successfully logged in");
111+
} catch (Exception e) {
112+
LOG.error("Failed to log in", e);
113+
}
114+
} else {
115+
LOG.error("Invalid service authentication configuration for 'kerberos' principal = '" + principal + "' and keytab = '" + keytab + "'");
116+
}
117+
} else {
118+
LOG.error("No service authentication properties were configured for 'kerberos'");
119+
}
114120
} else {
115-
LOG.warn("UGI.isSecurityEnabled() = true, but authenticationMethod section of SR config file is empty. "
116-
+ " Default UGI configuration will be used.");
121+
LOG.error("Invalid service authentication type : " + authenticationType);
117122
}
118123
} else {
119-
LOG.debug("UGI.isSecurityEnabled() = false. Simple authentication method will be used.");
124+
LOG.debug("No service authentication is configured");
120125
}
121126
}
122127

@@ -145,7 +150,7 @@ public void serverStarted(Server server) {
145150

146151
haServerNotificationManager.notifyDebut();
147152

148-
refreshHAServerManagedTask = new RefreshHAServerManagedTask(storageManager,transactionManager, haServerNotificationManager);
153+
refreshHAServerManagedTask = new RefreshHAServerManagedTask(storageManager, transactionManager, haServerNotificationManager);
149154
environment.lifecycle().manage(refreshHAServerManagedTask);
150155
refreshHAServerManagedTask.start();
151156
}
@@ -154,7 +159,7 @@ public void serverStarted(Server server) {
154159
}
155160

156161
private void registerHA(HAConfiguration haConfiguration, Environment environment) throws Exception {
157-
if(haConfiguration != null) {
162+
if (haConfiguration != null) {
158163
environment.lifecycle().addServerLifecycleListener(new ServerLifecycleListener() {
159164
@Override
160165
public void serverStarted(Server server) {
@@ -241,13 +246,13 @@ private void registerResources(Environment environment, RegistryConfiguration re
241246
transactionManagerAware.setTransactionManager(transactionManager);
242247
}
243248

244-
if(moduleRegistration instanceof LeadershipAware) {
249+
if (moduleRegistration instanceof LeadershipAware) {
245250
LOG.info("Module [{}] is registered for LeadershipParticipant registration.", moduleName);
246251
LeadershipAware leadershipAware = (LeadershipAware) moduleRegistration;
247252
leadershipAware.setLeadershipParticipant(leadershipParticipantRef);
248253
}
249254

250-
if(moduleRegistration instanceof HAServersAware) {
255+
if (moduleRegistration instanceof HAServersAware) {
251256
LOG.info("Module [{}] is registered for HAServersAware registration.");
252257
HAServersAware leadershipAware = (HAServersAware) moduleRegistration;
253258
leadershipAware.setHAServerConfigManager(haServerNotificationManager);
@@ -260,7 +265,7 @@ private void registerResources(Environment environment, RegistryConfiguration re
260265
for (Object resource : resourcesToRegister) {
261266
environment.jersey().register(resource);
262267
}
263-
268+
264269
environment.jersey().register(MultiPartFeature.class);
265270
environment.jersey().register(new TransactionEventListener(transactionManager, TransactionIsolation.READ_COMMITTED));
266271

@@ -284,7 +289,7 @@ private FileStorage getJarStorage(FileStorageConfiguration fileStorageConfigurat
284289
if (fileStorageConfiguration.getClassName() != null)
285290
try {
286291
fileStorage = (FileStorage) Class.forName(fileStorageConfiguration.getClassName(), true,
287-
Thread.currentThread().getContextClassLoader()).newInstance();
292+
Thread.currentThread().getContextClassLoader()).newInstance();
288293
fileStorage.init(fileStorageConfiguration.getProperties());
289294
} catch (Exception e) {
290295
throw new RuntimeException(e);
@@ -307,15 +312,15 @@ private StorageManager getStorageManager(StorageProviderConfiguration storagePro
307312
private void addServletFilters(RegistryConfiguration registryConfiguration, Environment environment) {
308313
List<ServletFilterConfiguration> servletFilterConfigurations = registryConfiguration.getServletFilters();
309314
if (servletFilterConfigurations != null && !servletFilterConfigurations.isEmpty()) {
310-
for (ServletFilterConfiguration servletFilterConfig: servletFilterConfigurations) {
315+
for (ServletFilterConfiguration servletFilterConfig : servletFilterConfigurations) {
311316
try {
312317
String className = servletFilterConfig.getClassName();
313318
Map<String, String> params = servletFilterConfig.getParams();
314319
String typeSuffix = params.get("type") != null ? ("-" + params.get("type").toString()) : "";
315320
LOG.info("Registering servlet filter [{}]", servletFilterConfig);
316321
Class<? extends Filter> filterClass = (Class<? extends Filter>) Class.forName(className);
317322
FilterRegistration.Dynamic dynamic = environment.servlets().addFilter(className + typeSuffix, filterClass);
318-
if(params != null) {
323+
if (params != null) {
319324
dynamic.setInitParameters(params);
320325
}
321326
dynamic.addMappingForUrlPatterns(EnumSet.allOf(DispatcherType.class), true, "/*");

webservice/src/main/java/com/hortonworks/registries/webservice/RegistryConfiguration.java

+7-10
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717
package com.hortonworks.registries.webservice;
1818

1919
import com.fasterxml.jackson.annotation.JsonProperty;
20-
import com.hortonworks.registries.common.AuthMethodConfiguration;
21-
import com.hortonworks.registries.common.FileStorageConfiguration;
22-
import com.hortonworks.registries.common.HAConfiguration;
23-
import com.hortonworks.registries.common.ModuleConfiguration;
24-
import com.hortonworks.registries.common.ServletFilterConfiguration;
20+
import com.hortonworks.registries.common.*;
21+
import com.hortonworks.registries.common.ServiceAuthenticationConfiguration;
2522
import com.hortonworks.registries.storage.StorageProviderConfiguration;
2623
import io.dropwizard.Configuration;
2724
import io.federecio.dropwizard.swagger.SwaggerBundleConfiguration;
@@ -55,7 +52,7 @@ public class RegistryConfiguration extends Configuration {
5552
private String httpProxyPassword;
5653

5754
@JsonProperty
58-
private AuthMethodConfiguration authenticationMethod;
55+
private ServiceAuthenticationConfiguration serviceAuthenticationConfiguration;
5956

6057
public String getHttpProxyUrl() {
6158
return httpProxyUrl;
@@ -138,12 +135,12 @@ public void setServletFilters(List<ServletFilterConfiguration> servletFilters) {
138135
this.servletFilters = servletFilters;
139136
}
140137

141-
public AuthMethodConfiguration getAuthenticationMethod() {
142-
return authenticationMethod;
138+
public ServiceAuthenticationConfiguration getServiceAuthenticationConfiguration() {
139+
return serviceAuthenticationConfiguration;
143140
}
144141

145-
public void setAuthenticationMethod(AuthMethodConfiguration authenticationMethod) {
146-
this.authenticationMethod = authenticationMethod;
142+
public void setServiceAuthenticationConfiguration(ServiceAuthenticationConfiguration serviceAuthenticationConfiguration) {
143+
this.serviceAuthenticationConfiguration = serviceAuthenticationConfiguration;
147144
}
148145

149146
}

0 commit comments

Comments
 (0)