Skip to content

KNOX-3094 - Update CM API swagger to 7.13.1 #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions gateway-discovery-cm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,9 @@
<dependency>
<groupId>com.cloudera.api.swagger</groupId>
<artifactId>cloudera-manager-api-swagger</artifactId>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.squareup.okhttp</groupId>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,21 @@
import java.util.Set;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLContext;
import javax.net.ssl.X509TrustManager;
import javax.security.auth.Subject;

import com.cloudera.api.swagger.client.ApiClient;
import com.cloudera.api.swagger.client.Pair;
import com.cloudera.api.swagger.client.auth.Authentication;
import com.cloudera.api.swagger.client.auth.HttpBasicAuth;
import com.squareup.okhttp.ConnectionSpec;
import com.squareup.okhttp.OkHttpClient;
import okhttp3.ConnectionSpec;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import org.apache.knox.gateway.config.ConfigurationException;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.i18n.messages.MessagesFactory;
import org.apache.knox.gateway.services.security.AliasService;
import org.apache.knox.gateway.services.security.AliasServiceException;
import org.apache.knox.gateway.topology.discovery.ServiceDiscoveryConfig;
import org.apache.knox.gateway.topology.discovery.cm.auth.AuthUtils;
import org.apache.knox.gateway.topology.discovery.cm.auth.DoAsQueryParameterInterceptor;
import org.apache.knox.gateway.topology.discovery.cm.auth.SpnegoAuthInterceptor;
import org.apache.knox.gateway.util.TruststoreSSLContextUtils;

Expand Down Expand Up @@ -126,55 +126,39 @@ private void configure(GatewayConfig gatewayConfig, AliasService aliasService, K
setUsername(username);
setPassword(password);

if (isKerberos) {
if (isKerberos()) {
// If there is a Kerberos subject, then add the SPNEGO auth interceptor
Subject subject = AuthUtils.getKerberosSubject();
if (subject != null) {
SpnegoAuthInterceptor spnegoInterceptor = new SpnegoAuthInterceptor(subject);
getHttpClient().interceptors().add(spnegoInterceptor);
addInterceptor(new SpnegoAuthInterceptor(subject));
}
addInterceptor(new DoAsQueryParameterInterceptor(username));
}
configureTimeouts(gatewayConfig);

configureSsl(gatewayConfig, trustStore);
}

private void configureTimeouts(GatewayConfig config) {
OkHttpClient client = getHttpClient();
client.setConnectTimeout(config.getServiceDiscoveryConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
client.setReadTimeout(config.getServiceDiscoveryReadTimeoutMillis(), TimeUnit.MILLISECONDS);
client.setWriteTimeout(config.getServiceDiscoveryWriteTimeoutMillis(), TimeUnit.MILLISECONDS);
log.discoveryClientTimeout(client.getConnectTimeout(), client.getReadTimeout(), client.getWriteTimeout());
private void addInterceptor(Interceptor interceptor) {
OkHttpClient newClient = getHttpClient().newBuilder().addInterceptor(interceptor).build();
setHttpClient(newClient);
}

@Override
public String buildUrl(String path, List<Pair> queryParams) {
// If kerberos is enabled, then for every request, we're going to include a doAs query param
if (isKerberos()) {
String user = getUsername();
if (user != null) {
queryParams.add(new Pair("doAs", user));
}
}
return super.buildUrl(path, queryParams);
}

/**
* @return The username set from the discovery configuration when this instance was initialized.
*/
private String getUsername() {
String username = null;
Authentication basicAuth = getAuthentication("basic");
if (basicAuth instanceof HttpBasicAuth) {
username = ((HttpBasicAuth) basicAuth).getUsername();
}
return username;
private void configureTimeouts(GatewayConfig config) {
OkHttpClient.Builder builder = getHttpClient().newBuilder();
builder.connectTimeout(config.getServiceDiscoveryConnectTimeoutMillis(), TimeUnit.MILLISECONDS);
builder.readTimeout(config.getServiceDiscoveryReadTimeoutMillis(), TimeUnit.MILLISECONDS);
builder.writeTimeout(config.getServiceDiscoveryWriteTimeoutMillis(), TimeUnit.MILLISECONDS);
OkHttpClient client = builder.build();
setHttpClient(client);
log.discoveryClientTimeout(client.connectTimeoutMillis(), client.readTimeoutMillis(), client.writeTimeoutMillis());
}

private void configureSsl(GatewayConfig gatewayConfig, KeyStore trustStore) {
final SSLContext truststoreSSLContext = TruststoreSSLContextUtils.getTruststoreSSLContext(trustStore);
final X509TrustManager trustManager = TruststoreSSLContextUtils.getTrustManager(trustStore);

if (truststoreSSLContext != null) {
if (truststoreSSLContext != null && trustManager != null) {
final ConnectionSpec.Builder connectionSpecBuilder = new ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS);
final List<String> includedSslCiphers = gatewayConfig.getIncludedSSLCiphers();
if (includedSslCiphers == null || includedSslCiphers.isEmpty()) {
Expand All @@ -188,8 +172,10 @@ private void configureSsl(GatewayConfig gatewayConfig, KeyStore trustStore) {
} else {
connectionSpecBuilder.tlsVersions(includedSslProtocols.toArray(new String[0]));
}
getHttpClient().setConnectionSpecs(Arrays.asList(connectionSpecBuilder.build()));
getHttpClient().setSslSocketFactory(truststoreSSLContext.getSocketFactory());
OkHttpClient.Builder builder = getHttpClient().newBuilder();
builder.connectionSpecs(Arrays.asList(connectionSpecBuilder.build()));
builder.sslSocketFactory(truststoreSSLContext.getSocketFactory(), trustManager);
setHttpClient(builder.build());
} else {
log.failedToConfigureTruststore();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.knox.gateway.topology.discovery.cm.auth;

import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;

public class DoAsQueryParameterInterceptor implements Interceptor {

private final String userName;
private static final String DO_AS_PRINCIPAL_PARAM = "doAs";

public DoAsQueryParameterInterceptor(String userName) {
this.userName = userName;
}

@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
HttpUrl url = chain.request().url().newBuilder()
.addQueryParameter(DO_AS_PRINCIPAL_PARAM, userName)
.build();
Request request = chain.request().newBuilder()
.url(url)
.build();
return chain.proceed(request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
*/
package org.apache.knox.gateway.topology.discovery.cm.auth;

import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.Interceptor;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import okhttp3.Authenticator;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.Route;

import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSCredential;
Expand All @@ -32,7 +33,6 @@

import java.io.IOException;
import java.net.InetAddress;
import java.net.Proxy;
import java.net.UnknownHostException;
import java.security.Principal;
import java.security.PrivilegedActionException;
Expand Down Expand Up @@ -102,7 +102,7 @@ private static boolean isNegotiate(String value) {
}

@Override
public Request authenticate(Proxy proxy, Response response) throws IOException {
public Request authenticate(Route route, Response response) throws IOException {
// If already attempted or not challenged for Kerberos, then skip this attempt
if (response.request().headers(AUTHORIZATION).stream().anyMatch(SpnegoAuthInterceptor::isNegotiate) ||
response.headers(WWW_AUTHENTICATE).stream().noneMatch(SpnegoAuthInterceptor::isNegotiate)) {
Expand All @@ -112,13 +112,8 @@ public Request authenticate(Proxy proxy, Response response) throws IOException {
return authenticate(response.request());
}

@Override
public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
return null; // Not needed
}

private Request authenticate(Request request) {
String principal = defineServicePrincipal(remoteServiceName, request.url().getHost(), useCanonicalHostname);
String principal = defineServicePrincipal(remoteServiceName, request.url().host(), useCanonicalHostname);
byte[] token = generateToken(principal);

String credential = format(Locale.getDefault(), "%s %s", NEGOTIATE, Base64.getEncoder().encodeToString(token));
Expand Down Expand Up @@ -245,4 +240,4 @@ public boolean needsRefresh() throws GSSException {
}
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.cloudera.api.swagger.model.ApiService;
import com.cloudera.api.swagger.model.ApiServiceConfig;
import com.cloudera.api.swagger.model.ApiServiceList;
import com.squareup.okhttp.Call;
import okhttp3.Call;
import org.apache.knox.gateway.config.GatewayConfig;
import org.apache.knox.gateway.services.security.AliasService;
import org.apache.knox.gateway.topology.discovery.ServiceDiscovery;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactory;
import javax.net.ssl.X509TrustManager;

import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.SSLContexts;
Expand All @@ -48,4 +52,25 @@ public static SSLContext getTruststoreSSLContext(KeyStore truststore) {
return sslContext;
}

public static X509TrustManager getTrustManager(KeyStore truststore) {
try {
if (truststore != null) {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(truststore);
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
if (trustManagers != null) {
for (TrustManager tm : trustManagers) {
if (tm instanceof X509TrustManager) {
return (X509TrustManager) tm;
}
}
}
throw new IllegalStateException("Unexpected default trust managers:" + Arrays.toString(trustManagers));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the consequence of throwing this exception? The DiscoveryApiClient#configureSsl() method does not catch it, so it will bubble up. Do you know where it will eventually be handled and how?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took this part of the migration from here:

https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/-builder/ssl-socket-factory.html
I would expect this to work in a standard setup (and it's the case with JDK 8-17).
I have not tested it on a FIPS-enabled cluster though.

I throw and catch IllegalStateException and return null if the trust manager for the default algorithm and trust store is not an instance of X509TrustManager. The getTruststoreSSLContext() method also uses the same methods by calling SSLContextBuilder.loadTrustMaterial(): uses the default TrustManagerFactory algorithm, initializes and gets the trust managers; it does not check the instance type. But the okhttp API expects an implementation of javax.net.ssl.X509TrustManager to validate the server's certificates.

The deprecated method variant does not require it, okhttp would use reflection to get one from sun.security.ssl.SSLContextImpl:

https://github.com/square/okhttp/blob/4984568367caaf359b82c452bd28b5e192824d1c/okhttp/src/main/kotlin/okhttp3/internal/platform/Platform.kt#L88
But this was removed in Okhttp 5.

}
} catch (KeyStoreException | NoSuchAlgorithmException | IllegalStateException e) {
LOGGER.failedToLoadTruststore(e.getMessage(), e);
}
return null;
}

}
38 changes: 33 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
<buildnumber-maven-plugin.version>1.4</buildnumber-maven-plugin.version>
<cglib.version>3.3.0</cglib.version>
<checkstyle.version>8.38</checkstyle.version>
<cloudera-manager.version>7.8.1</cloudera-manager.version>
<cloudera-manager.version>7.13.1</cloudera-manager.version>
<cloudera-manager-api-swagger.version>${cloudera-manager.version}</cloudera-manager-api-swagger.version>
<commons-beanutils.version>1.9.4</commons-beanutils.version>
<commons-cli.version>1.4</commons-cli.version>
Expand Down Expand Up @@ -208,7 +208,8 @@
<frontend-maven-plugin.version>1.11.0</frontend-maven-plugin.version>
<gethostname4j.version>1.0.0</gethostname4j.version> <!-- See here: https://github.com/mattsheppard/gethostname4j -->
<glassfish-jaxb.version>2.3.3</glassfish-jaxb.version>
<gson.version>2.8.9</gson.version>
<gson.version>2.10.1</gson.version>
<gsonfire.version>1.9.0</gsonfire.version>
<groovy.version>3.0.7</groovy.version>
<guava.version>28.2-jre</guava.version>
<hadoop.version>3.2.4</hadoop.version>
Expand Down Expand Up @@ -242,6 +243,7 @@
<json-path.version>2.9.0</json-path.version>
<json-smart.version>2.4.9</json-smart.version>
<junit.version>4.13.2</junit.version>
<kotlin-stdlib.version>1.9.10</kotlin-stdlib.version>
<lang-tag.version>1.5</lang-tag.version>
<libpam4j.version>1.11</libpam4j.version>
<log4j2.version>2.17.1</log4j2.version>
Expand All @@ -256,12 +258,13 @@
<netty.version>4.1.77.Final</netty.version>
<nimbus-jose-jwt.version>9.37.3</nimbus-jose-jwt.version>
<nodejs.version>v14.15.0</nodejs.version>
<okhttp.version>2.7.5</okhttp.version>
<okhttp.version>4.12.0</okhttp.version>
<opensaml.version>3.4.5</opensaml.version>
<pac4j.version>4.5.6</pac4j.version>
<postgresql.version>42.4.4</postgresql.version>
<mysql.version>8.0.28</mysql.version>
<mariadb.connector.version>3.3.0</mariadb.connector.version>
<okio.version>3.6.0</okio.version>
<protobuf.version>3.16.3</protobuf.version>
<powermock.version>2.0.9</powermock.version>
<purejavacomm.version>0.0.11.1</purejavacomm.version>
Expand Down Expand Up @@ -1570,11 +1573,36 @@
</dependency>

<dependency>
<groupId>com.squareup.okhttp</groupId>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>${okhttp.version}</version>
</dependency>

<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>logging-interceptor</artifactId>
<version>${okhttp.version}</version>
</dependency>
<dependency>
<groupId>com.squareup.okio</groupId>
<artifactId>okio</artifactId>
<version>${okio.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin-stdlib.version}</version>
<exclusions>
<exclusion>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.gsonfire</groupId>
<artifactId>gson-fire</artifactId>
<version>${gsonfire.version}</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
Expand Down
Loading