lmccay commented on code in PR #1459:
URL: https://github.com/apache/knox/pull/1459#discussion_r4321353216


##########
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/SetHostSSLSocketFactory.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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.services.token.impl;
+
+import javax.net.ssl.SSLSocketFactory;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * An {@link SSLSocketFactory} decorator that stamps a fixed host onto every 
socket it creates.
+ * <p>
+ * BouncyCastle's JSSE takes the hostname for HTTPS endpoint identification 
from the socket itself
+ * rather than from the URL. {@link java.net.HttpURLConnection}, which Nimbus 
uses to fetch a JWKS,
+ * creates the socket without a host and connects it by address, so under the 
BouncyCastle FIPS
+ * provider the hostname arrives as {@code null} and endpoint identification 
fails with
+ * "No hostname specified for HTTPS endpoint ID check" before the certificate 
is ever examined.
+ * That happens even when the certificate carries a matching iPAddress SAN.
+ * <p>
+ * Setting the host restores the check: BouncyCastle matches an IP literal 
against the
+ * certificate's iPAddress SANs and a name against its dNSName SANs. 
Verification is preserved,
+ * not bypassed.
+ * <p>
+ * The host is applied through {@code setHost(String)}, declared by
+ * {@code org.bouncycastle.jsse.BCSSLSocket}. That type is reached 
reflectively so Knox does not
+ * take a compile dependency on the BouncyCastle TLS provider, which is 
present only in FIPS
+ * deployments. On sockets that do not expose the method this decorator is a 
no-op, which is
+ * correct for the stock JSSE: it identifies IP endpoints from the URL already.
+ */
+class SetHostSSLSocketFactory extends SSLSocketFactory {
+
+  private static final String SET_HOST = "setHost";
+
+  private final SSLSocketFactory delegate;
+  private final String host;
+
+  SetHostSSLSocketFactory(SSLSocketFactory delegate, String host) {
+    this.delegate = delegate;
+    this.host = host;
+  }
+
+  /**
+   * Wraps {@code base} so sockets created for {@code url} carry that URL's 
host.
+   *
+   * @return the wrapped factory, or {@code base} unchanged when the URL has 
no usable host
+   */
+  static SSLSocketFactory forUrl(URL url, SSLSocketFactory base) {
+    final String host = url == null ? null : url.getHost();
+    if (host == null || host.isEmpty()) {
+      return base;
+    }
+    return new SetHostSSLSocketFactory(base, host);
+  }
+
+  /**
+   * Applies {@code host} to {@code socket} via a public {@code 
setHost(String)} method.
+   *
+   * @return {@code true} when the host was applied, {@code false} when the 
socket does not
+   *         support it or the call failed
+   */
+  static boolean setHost(Socket socket, String host) {
+    if (socket == null || host == null || host.isEmpty()) {
+      return false;
+    }
+    for (Class<?> type : declaringTypes(socket.getClass())) {
+      final Method method;
+      try {
+        method = type.getMethod(SET_HOST, String.class);
+      } catch (NoSuchMethodException e) {
+        continue;
+      }
+      try {
+        method.invoke(socket, host);
+        return true;
+      } catch (IllegalAccessException e) {
+        /* Not reachable through this type; a public interface may still 
expose it. */
+        continue;
+      } catch (InvocationTargetException | RuntimeException e) {
+        return false;

Review Comment:
   Should a single failure to set it even though it appears to have such a 
method fail the entire iteration over the types? Should we not continue to walk 
the hierarchy to see if one will work? Are we assuming that someone overrode 
another implementation in order to purposely not support it and throw a RTE 
rather than just no-op it?



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/SetHostSSLSocketFactory.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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.services.token.impl;
+
+import javax.net.ssl.SSLSocketFactory;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * An {@link SSLSocketFactory} decorator that stamps a fixed host onto every 
socket it creates.
+ * <p>
+ * BouncyCastle's JSSE takes the hostname for HTTPS endpoint identification 
from the socket itself
+ * rather than from the URL. {@link java.net.HttpURLConnection}, which Nimbus 
uses to fetch a JWKS,
+ * creates the socket without a host and connects it by address, so under the 
BouncyCastle FIPS
+ * provider the hostname arrives as {@code null} and endpoint identification 
fails with
+ * "No hostname specified for HTTPS endpoint ID check" before the certificate 
is ever examined.
+ * That happens even when the certificate carries a matching iPAddress SAN.
+ * <p>
+ * Setting the host restores the check: BouncyCastle matches an IP literal 
against the
+ * certificate's iPAddress SANs and a name against its dNSName SANs. 
Verification is preserved,
+ * not bypassed.
+ * <p>
+ * The host is applied through {@code setHost(String)}, declared by
+ * {@code org.bouncycastle.jsse.BCSSLSocket}. That type is reached 
reflectively so Knox does not
+ * take a compile dependency on the BouncyCastle TLS provider, which is 
present only in FIPS
+ * deployments. On sockets that do not expose the method this decorator is a 
no-op, which is
+ * correct for the stock JSSE: it identifies IP endpoints from the URL already.
+ */
+class SetHostSSLSocketFactory extends SSLSocketFactory {
+
+  private static final String SET_HOST = "setHost";
+
+  private final SSLSocketFactory delegate;
+  private final String host;
+
+  SetHostSSLSocketFactory(SSLSocketFactory delegate, String host) {
+    this.delegate = delegate;
+    this.host = host;
+  }
+
+  /**
+   * Wraps {@code base} so sockets created for {@code url} carry that URL's 
host.
+   *
+   * @return the wrapped factory, or {@code base} unchanged when the URL has 
no usable host
+   */
+  static SSLSocketFactory forUrl(URL url, SSLSocketFactory base) {
+    final String host = url == null ? null : url.getHost();
+    if (host == null || host.isEmpty()) {
+      return base;
+    }
+    return new SetHostSSLSocketFactory(base, host);
+  }
+
+  /**
+   * Applies {@code host} to {@code socket} via a public {@code 
setHost(String)} method.
+   *
+   * @return {@code true} when the host was applied, {@code false} when the 
socket does not
+   *         support it or the call failed
+   */
+  static boolean setHost(Socket socket, String host) {

Review Comment:
   It seems that we are iterating over the class hierarchy of the socket 
looking for the first one that accepts or rejects a setHost method call. IIUC, 
then this looks for the most specific implementation of setHost that is public. 
If it fails due to method visibility it continues to walk the hierarchy to try 
and find a public one that will work. If that's accurate then I think we should 
document this a bit here to describe the intent at least.



##########
gateway-server/src/main/java/org/apache/knox/gateway/services/token/impl/SetHostSSLSocketFactory.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
+ * <p>
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * 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.services.token.impl;
+
+import javax.net.ssl.SSLSocketFactory;
+import java.io.IOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.net.InetAddress;
+import java.net.Socket;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * An {@link SSLSocketFactory} decorator that stamps a fixed host onto every 
socket it creates.
+ * <p>
+ * BouncyCastle's JSSE takes the hostname for HTTPS endpoint identification 
from the socket itself
+ * rather than from the URL. {@link java.net.HttpURLConnection}, which Nimbus 
uses to fetch a JWKS,
+ * creates the socket without a host and connects it by address, so under the 
BouncyCastle FIPS
+ * provider the hostname arrives as {@code null} and endpoint identification 
fails with
+ * "No hostname specified for HTTPS endpoint ID check" before the certificate 
is ever examined.
+ * That happens even when the certificate carries a matching iPAddress SAN.
+ * <p>
+ * Setting the host restores the check: BouncyCastle matches an IP literal 
against the
+ * certificate's iPAddress SANs and a name against its dNSName SANs. 
Verification is preserved,
+ * not bypassed.
+ * <p>
+ * The host is applied through {@code setHost(String)}, declared by
+ * {@code org.bouncycastle.jsse.BCSSLSocket}. That type is reached 
reflectively so Knox does not
+ * take a compile dependency on the BouncyCastle TLS provider, which is 
present only in FIPS
+ * deployments. On sockets that do not expose the method this decorator is a 
no-op, which is
+ * correct for the stock JSSE: it identifies IP endpoints from the URL already.
+ */
+class SetHostSSLSocketFactory extends SSLSocketFactory {

Review Comment:
   minor nit - I don't like that this classname reads like a verb rather than a 
noun.
   Not a blocker though. Maybe HostSettingSSLSocketFactory is a better noun 
based name?



-- 
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]

Reply via email to