mneethiraj commented on code in PR #928: URL: https://github.com/apache/ranger/pull/928#discussion_r3135630081
########## authz-remote/src/conf/ranger-authz-remote.properties: ########## @@ -0,0 +1,24 @@ +# 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. + +ranger.authorizer.impl.class=org.apache.ranger.authz.remote.RangerRemoteAuthorizer +ranger.authz.remote.url=https://localhost:6500 Review Comment: Prefix all PDP server related configurations with `pd`, like: `ranger.authz.remote.url` => `ranger.authz.remote.pdp.url` ########## authz-remote/src/main/java/org/apache/ranger/authz/remote/RangerPdpClient.java: ########## @@ -0,0 +1,234 @@ +/* + * 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.authz.remote; + +import org.apache.http.HttpEntity; +import org.apache.http.client.config.RequestConfig; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.config.Registry; +import org.apache.http.config.RegistryBuilder; +import org.apache.http.conn.socket.ConnectionSocketFactory; +import org.apache.http.conn.socket.PlainConnectionSocketFactory; +import org.apache.http.conn.ssl.DefaultHostnameVerifier; +import org.apache.http.conn.ssl.NoopHostnameVerifier; +import org.apache.http.conn.ssl.SSLConnectionSocketFactory; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClientBuilder; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; +import org.apache.http.ssl.SSLContextBuilder; +import org.apache.http.util.EntityUtils; +import org.apache.ranger.authz.api.RangerAuthzException; +import org.apache.ranger.authz.model.RangerAuthzRequest; +import org.apache.ranger.authz.model.RangerAuthzResult; +import org.apache.ranger.authz.model.RangerMultiAuthzRequest; +import org.apache.ranger.authz.model.RangerMultiAuthzResult; +import org.apache.ranger.authz.model.RangerResourcePermissions; +import org.apache.ranger.authz.model.RangerResourcePermissionsRequest; +import org.apache.ranger.authz.remote.authn.RangerRemoteKerberosContext; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLContext; + +import java.io.Closeable; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.KeyStore; +import java.security.PrivilegedActionException; +import java.security.PrivilegedExceptionAction; +import java.util.Map; + +import static org.apache.ranger.authz.remote.RangerRemoteAuthType.KERBEROS; +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.REMOTE_CALL_UNSUCCESSFUL; +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.REMOTE_REQUEST_FAILED; +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.TLS_CONFIGURATION_FAILED; + +class RangerPdpClient implements Closeable { + private static final String PATH_AUTHORIZE = "/authorize"; + private static final String PATH_AUTHORIZE_MULTI = "/authorizeMulti"; + private static final String PATH_RESOURCE_PERMISSIONS = "/permissions"; + + private final RangerRemoteAuthzConfig config; + private final CloseableHttpClient httpClient; + private final RangerRemoteAuthType authType; + private final RangerRemoteKerberosContext kerberosContext; + + RangerPdpClient(RangerRemoteAuthzConfig config) throws RangerAuthzException { + this.config = config; + this.authType = config.getAuthType(); + this.kerberosContext = authType == KERBEROS ? RangerRemoteKerberosContext.create(config) : null; + this.httpClient = createHttpClient(config, kerberosContext); + } + + RangerAuthzResult authorize(RangerAuthzRequest request) throws RangerAuthzException { + String endpoint = config.getEndpointUrl(PATH_AUTHORIZE); + String payload = RangerRemoteJson.writeAuthzRequest(request); + String response = post(endpoint, payload); Review Comment: Consider having `post()` method perform serialization of the request and deserialization of the response, eliminating calls to `RangerRemoteJson`: ``` ... RangerAuthzResult ret = post(endpoint, request, RangerAuthzResult.class); return ret; } public <T> T post(String path, Object payload, Class<T> responseType) throws RangerAuthzException { String requestBody = objectMapper.writeValueAsString(payload); ... return objectMapper.readValue(responseJson, valueType); } ``` ########## authz-remote/src/conf/ranger-authz-remote.properties: ########## @@ -0,0 +1,24 @@ +# 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. + +ranger.authorizer.impl.class=org.apache.ranger.authz.remote.RangerRemoteAuthorizer Review Comment: `ranger.authorizer.impl.class` configuration doesn't seem to be used. Please review and remove. ########## authz-remote/src/main/java/org/apache/ranger/authz/remote/RangerRemoteAuthzConfig.java: ########## @@ -0,0 +1,234 @@ +/* + * 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.authz.remote; + +import org.apache.commons.lang3.StringUtils; +import org.apache.ranger.authz.api.RangerAuthzException; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Properties; + +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.INVALID_PROPERTY_VALUE; +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.MISSING_AUTH_CONFIG; +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.MISSING_PDP_URL; +import static org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.UNSUPPORTED_AUTH_TYPE; + +public class RangerRemoteAuthzConfig { + public static final String PROP_REMOTE_URL = "ranger.authz.remote.url"; + public static final String PROP_REMOTE_CONNECT_TIMEOUT_MS = "ranger.authz.remote.connect.timeout.ms"; + public static final String PROP_REMOTE_READ_TIMEOUT_MS = "ranger.authz.remote.read.timeout.ms"; + public static final String PROP_REMOTE_HEADER_PREFIX = "ranger.authz.remote.header."; + public static final String PROP_REMOTE_SSL_KEYSTORE_FILE = "ranger.authz.remote.ssl.keystore.file"; + public static final String PROP_REMOTE_SSL_KEYSTORE_PASSWORD = "ranger.authz.remote.ssl.keystore.password"; + public static final String PROP_REMOTE_SSL_KEYSTORE_TYPE = "ranger.authz.remote.ssl.keystore.type"; + public static final String PROP_REMOTE_SSL_TRUSTSTORE_FILE = "ranger.authz.remote.ssl.truststore.file"; + public static final String PROP_REMOTE_SSL_TRUSTSTORE_PASSWORD = "ranger.authz.remote.ssl.truststore.password"; + public static final String PROP_REMOTE_SSL_TRUSTSTORE_TYPE = "ranger.authz.remote.ssl.truststore.type"; + public static final String PROP_REMOTE_SSL_DISABLE_HOSTNAME_VERIFICATION = "ranger.authz.remote.ssl.disable.hostname.verification"; + public static final String PROP_REMOTE_AUTH_TYPE = "ranger.authz.remote.auth.type"; + public static final String PROP_REMOTE_AUTH_KERBEROS_PRINCIPAL = "ranger.authz.remote.auth.kerberos.principal"; + public static final String PROP_REMOTE_AUTH_KERBEROS_KEYTAB = "ranger.authz.remote.auth.kerberos.keytab"; + public static final String PROP_REMOTE_AUTH_KERBEROS_DEBUG = "ranger.authz.remote.auth.kerberos.debug"; + + public static final String PROP_PREFIX_SERVICE = "ranger.authz.service."; + public static final String PROP_PREFIX_SERVICE_TYPE = "ranger.authz.servicetype."; + + private static final String AUTHZ_PATH_PREFIX = "/authz/v1"; + + private static final int DEFAULT_CONNECT_TIMEOUT_MS = 5_000; + private static final int DEFAULT_READ_TIMEOUT_MS = 30_000; + private static final String DEFAULT_STORE_TYPE = "PKCS12"; + + private final Properties properties; + + public RangerRemoteAuthzConfig(Properties properties) { + this.properties = properties != null ? properties : new Properties(); + } + + public String getPdpUrl() throws RangerAuthzException { + String value = normalizeBaseUrl(properties.getProperty(PROP_REMOTE_URL)); + + if (StringUtils.isBlank(value)) { + throw new RangerAuthzException(MISSING_PDP_URL, PROP_REMOTE_URL); + } + + return value; + } + + public String getEndpointUrl(String path) throws RangerAuthzException { + String baseUrl = getPdpUrl(); + + if (baseUrl.endsWith(AUTHZ_PATH_PREFIX)) { + return baseUrl + path; + } + + return baseUrl + AUTHZ_PATH_PREFIX + path; + } + + public int getConnectTimeoutMs() throws RangerAuthzException { + return getIntProperty(PROP_REMOTE_CONNECT_TIMEOUT_MS, DEFAULT_CONNECT_TIMEOUT_MS); + } + + public int getReadTimeoutMs() throws RangerAuthzException { + return getIntProperty(PROP_REMOTE_READ_TIMEOUT_MS, DEFAULT_READ_TIMEOUT_MS); + } + + public RangerRemoteAuthType getAuthType() throws RangerAuthzException { + String value = StringUtils.defaultIfBlank(properties.getProperty(PROP_REMOTE_AUTH_TYPE), RangerRemoteAuthType.NONE.name()); + + try { + return RangerRemoteAuthType.valueOf(value.trim().toUpperCase()); + } catch (IllegalArgumentException e) { + throw new RangerAuthzException(UNSUPPORTED_AUTH_TYPE, e, value); + } + } + + public String getKerberosPrincipal() throws RangerAuthzException { + String ret = trimToNull(properties.getProperty(PROP_REMOTE_AUTH_KERBEROS_PRINCIPAL)); + + if (ret == null) { + throw new RangerAuthzException(MISSING_AUTH_CONFIG, PROP_REMOTE_AUTH_KERBEROS_PRINCIPAL); + } + + return ret; + } + + public String getKerberosKeytab() throws RangerAuthzException { + String ret = trimToNull(properties.getProperty(PROP_REMOTE_AUTH_KERBEROS_KEYTAB)); + + if (ret == null) { + throw new RangerAuthzException(MISSING_AUTH_CONFIG, PROP_REMOTE_AUTH_KERBEROS_KEYTAB); + } + + return ret; + } + + public boolean isKerberosDebugEnabled() throws RangerAuthzException { + return getBooleanProperty(PROP_REMOTE_AUTH_KERBEROS_DEBUG, false); + } + + public String getSslKeyStoreFile() { + return trimToNull(properties.getProperty(PROP_REMOTE_SSL_KEYSTORE_FILE)); + } + + public String getSslKeyStorePassword() { + return properties.getProperty(PROP_REMOTE_SSL_KEYSTORE_PASSWORD); + } + + public String getSslKeyStoreType() { + return StringUtils.defaultIfBlank(properties.getProperty(PROP_REMOTE_SSL_KEYSTORE_TYPE), DEFAULT_STORE_TYPE).trim(); + } + + public String getSslTrustStoreFile() { + return trimToNull(properties.getProperty(PROP_REMOTE_SSL_TRUSTSTORE_FILE)); + } + + public String getSslTrustStorePassword() { + return properties.getProperty(PROP_REMOTE_SSL_TRUSTSTORE_PASSWORD); + } + + public String getSslTrustStoreType() { + return StringUtils.defaultIfBlank(properties.getProperty(PROP_REMOTE_SSL_TRUSTSTORE_TYPE), DEFAULT_STORE_TYPE).trim(); + } + + public boolean isHostnameVerificationDisabled() throws RangerAuthzException { + return getBooleanProperty(PROP_REMOTE_SSL_DISABLE_HOSTNAME_VERIFICATION, false); + } + + public Map<String, String> getHeaders() { Review Comment: Consider caching `headers` in the constructor, given this method is called in every call to `RangerPdpClient.post()` -- 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]
