mneethiraj commented on code in PR #986: URL: https://github.com/apache/ranger/pull/986#discussion_r3471589257
########## security-admin/src/main/java/org/apache/ranger/opensearch/OpenSearchMgr.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.opensearch; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthSchemeProvider; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.AuthSchemes; +import org.apache.http.config.Lookup; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.impl.auth.SPNegoSchemeFactory; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.ranger.audit.destination.OpenSearchAuditDestination; +import org.apache.ranger.authorization.credutils.CredentialsProviderUtil; +import org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider; +import org.apache.ranger.common.PropertiesUtil; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.security.auth.Subject; +import javax.security.auth.kerberos.KerberosTicket; + +import java.io.File; +import java.security.PrivilegedActionException; +import java.util.Arrays; +import java.util.Date; +import java.util.Locale; + +@Component +public class OpenSearchMgr { + private static final Logger LOG = LoggerFactory.getLogger(OpenSearchMgr.class); + + private static final String CONFIG_PREFIX = OpenSearchAuditDestination.CONFIG_PREFIX; + private static final String CONFIG_URLS = OpenSearchAuditDestination.CONFIG_URLS; + private static final String CONFIG_PORT = OpenSearchAuditDestination.CONFIG_PORT; + private static final String CONFIG_PROTOCOL = OpenSearchAuditDestination.CONFIG_PROTOCOL; + private static final String CONFIG_USER = OpenSearchAuditDestination.CONFIG_USER; + private static final String CONFIG_PASSWORD = OpenSearchAuditDestination.CONFIG_PASSWORD; + private static final String CONFIG_INDEX = OpenSearchAuditDestination.CONFIG_INDEX; + + public String index; Review Comment: Avoid `public` mutable fields. It looks like `index` is directly read only by tests. Consider using package private methods to set this filed, and update tests to use this method. ``` void setIndex(String index) { this.index = index; } ``` ########## security-admin/src/main/java/org/apache/ranger/opensearch/OpenSearchMgr.java: ########## @@ -0,0 +1,175 @@ +/* + * 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.opensearch; + +import org.apache.commons.lang3.StringUtils; +import org.apache.http.HttpHost; +import org.apache.http.auth.AuthSchemeProvider; +import org.apache.http.auth.AuthScope; +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.CredentialsProvider; +import org.apache.http.client.config.AuthSchemes; +import org.apache.http.config.Lookup; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.impl.auth.SPNegoSchemeFactory; +import org.apache.http.impl.client.BasicCredentialsProvider; +import org.apache.ranger.audit.destination.OpenSearchAuditDestination; +import org.apache.ranger.authorization.credutils.CredentialsProviderUtil; +import org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider; +import org.apache.ranger.common.PropertiesUtil; +import org.elasticsearch.client.RestClient; +import org.elasticsearch.client.RestClientBuilder; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +import javax.security.auth.Subject; +import javax.security.auth.kerberos.KerberosTicket; + +import java.io.File; +import java.security.PrivilegedActionException; +import java.util.Arrays; +import java.util.Date; +import java.util.Locale; + +@Component +public class OpenSearchMgr { + private static final Logger LOG = LoggerFactory.getLogger(OpenSearchMgr.class); + + private static final String CONFIG_PREFIX = OpenSearchAuditDestination.CONFIG_PREFIX; + private static final String CONFIG_URLS = OpenSearchAuditDestination.CONFIG_URLS; + private static final String CONFIG_PORT = OpenSearchAuditDestination.CONFIG_PORT; + private static final String CONFIG_PROTOCOL = OpenSearchAuditDestination.CONFIG_PROTOCOL; + private static final String CONFIG_USER = OpenSearchAuditDestination.CONFIG_USER; + private static final String CONFIG_PASSWORD = OpenSearchAuditDestination.CONFIG_PASSWORD; + private static final String CONFIG_INDEX = OpenSearchAuditDestination.CONFIG_INDEX; + + public String index; + + private volatile RestClient client; + private Subject subject; + private String user; + private String password; + + public RestClient getClient() { + RestClient me = client; + + if (me != null && subject != null) { + KerberosTicket ticket = CredentialsProviderUtil.getTGT(subject); + + try { + if (new Date().getTime() > ticket.getEndTime().getTime()) { + client = null; + CredentialsProviderUtil.ticketExpireTime80 = 0; + + me = connect(); + } else if (CredentialsProviderUtil.ticketWillExpire(ticket)) { + subject = CredentialsProviderUtil.login(user, password); + } + } catch (PrivilegedActionException e) { + LOG.error("PrivilegedActionException:", e); + + throw new RuntimeException(e); + } + + return me; + } else { + me = connect(); + } + + return me; + } + + synchronized RestClient connect() { + RestClient me = client; + + if (me == null) { + synchronized (OpenSearchMgr.class) { + me = client; + + if (me == null) { + String urls = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_URLS); + String protocol = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PROTOCOL, "http"); + + user = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_USER, ""); + password = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PASSWORD, ""); + + int port = Integer.parseInt(PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PORT, "9200")); + + this.index = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_INDEX, "ranger_audits"); + + String parameterString = String.format(Locale.ROOT, "User:%s, %s://%s:%s/%s", user, protocol, urls, port, index); + + LOG.info("Initializing OpenSearch connection: {}", parameterString); + + if (urls != null) { + urls = urls.trim(); + } + + if (StringUtils.isBlank(urls) || "NONE".equalsIgnoreCase(urls)) { + LOG.warn("OpenSearch URLs not configured or set to NONE"); + + return null; + } + + try { + if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && password.contains("keytab") && new File(password).exists()) { + subject = CredentialsProviderUtil.login(user, password); + } + + RestClientBuilder builder = buildRestClientBuilder(urls, protocol, user, password, port); + + client = builder.build(); + me = client; + } catch (Throwable t) { + LOG.error("Cannot connect to OpenSearch: {}", parameterString, t); + } + } + } + } + + return me; + } + + public static RestClientBuilder buildRestClientBuilder(String urls, String protocol, String user, String password, int port) { + HttpHost[] hosts = Arrays.stream(urls.split(",")).map(String::trim).filter(h -> !h.isEmpty()).map(h -> new HttpHost(h, port, protocol)).toArray(HttpHost[]::new); + + RestClientBuilder builder = RestClient.builder(hosts); + + if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && !"NONE".equalsIgnoreCase(user) && !"NONE".equalsIgnoreCase(password)) { + if (password.contains("keytab") && new File(password).exists()) { Review Comment: Configuring keytab file in password property is not intuitive. I suggest either - using a separate configuration to explicitly capture the authentication type: "authentication.type", values: basic-auth, kerberos, (jwt - in future) - pick authentication type to use based on configured properties: - use kerberos if related configurations are set (`kerberos.keytab`, `kerberos.principal`) - use basic-auth if related configurations are set (`username`, `password` -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
