knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject [knox] branch master updated: KNOX-2122 - Misc code cleanup (#198)
Date Sun, 17 Nov 2019 14:52:33 GMT
This is an automated email from the ASF dual-hosted git repository.

krisden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new d2ee1ef  KNOX-2122 - Misc code cleanup (#198)
d2ee1ef is described below

commit d2ee1efedfa093f704e476bc6ac65bf22bae7dcc
Author: Kevin Risden <risdenk@users.noreply.github.com>
AuthorDate: Sun Nov 17 09:52:24 2019 -0500

    KNOX-2122 - Misc code cleanup (#198)
    
    Signed-off-by: Kevin Risden <krisden@apache.org>
---
 .../securequery/SecureQueryEncryptProcessor.java   | 11 +++-----
 .../apache/knox/gateway/dispatch/NiFiDispatch.java | 32 ----------------------
 .../knox/gateway/dispatch/NiFiHaDispatch.java      | 32 ----------------------
 .../java/org/apache/knox/gateway/shell/KnoxSh.java |  2 +-
 .../shell/table/KnoxShellTableHistoryAspect.java   |  5 ++--
 .../gateway/dispatch/AbstractGatewayDispatch.java  | 12 ++++----
 .../knox/gateway/dispatch/DefaultDispatch.java     | 28 ++++++++-----------
 .../apache/knox/gateway/security/SubjectUtils.java |  8 +++---
 8 files changed, 30 insertions(+), 100 deletions(-)

diff --git a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/knox/gateway/securequery/SecureQueryEncryptProcessor.java
b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/knox/gateway/securequery/SecureQueryEncryptProcessor.java
index 7dd976f..469b468 100644
--- a/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/knox/gateway/securequery/SecureQueryEncryptProcessor.java
+++ b/gateway-provider-rewrite-step-secure-query/src/main/java/org/apache/knox/gateway/securequery/SecureQueryEncryptProcessor.java
@@ -29,8 +29,6 @@ import org.apache.knox.gateway.services.security.impl.ConfigurableEncryptor;
 import org.apache.knox.gateway.util.urltemplate.Parser;
 import org.apache.knox.gateway.util.urltemplate.Template;
 
-import java.io.UnsupportedEncodingException;
-
 public class SecureQueryEncryptProcessor
     implements UrlRewriteStepProcessor<SecureQueryEncryptDescriptor> {
 
@@ -76,14 +74,13 @@ public class SecureQueryEncryptProcessor
   public void destroy() {
   }
 
-  private String encode( String string ) throws UnsupportedEncodingException {
-    EncryptionResult result = null;
+  private String encode( String string ) throws Exception {
     try {
-      result = encryptor.encrypt(string);
+      EncryptionResult result = encryptor.encrypt(string);
+      return Base64.encodeBase64URLSafeString(result.toByteAray());
     } catch (Exception e) {
       log.unableToEncryptValue(e);
+      throw e;
     }
-    string = Base64.encodeBase64URLSafeString(result.toByteAray());
-    return string;
   }
 }
diff --git a/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiDispatch.java
b/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiDispatch.java
index 5d53e1e..adc6321 100644
--- a/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiDispatch.java
+++ b/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiDispatch.java
@@ -21,8 +21,6 @@ import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.entity.ContentType;
-import org.apache.knox.gateway.util.MimeTypes;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -74,34 +72,4 @@ public class NiFiDispatch extends DefaultDispatch {
       }
     }
   }
-
-  /**
-   * Overriden due to DefaultDispatch#getInboundResponseContentType(HttpEntity) having private
access, and the method is used by
-   * {@link #writeOutboundResponse(HttpUriRequest, HttpServletRequest, HttpServletResponse,
HttpResponse)}}
-   */
-  private String getInboundResponseContentType( final HttpEntity entity ) {
-    String fullContentType = null;
-    if( entity != null ) {
-      ContentType entityContentType = ContentType.get( entity );
-      if( entityContentType != null ) {
-        if( entityContentType.getCharset() == null ) {
-          final String entityMimeType = entityContentType.getMimeType();
-          final String defaultCharset = MimeTypes.getDefaultCharsetForMimeType( entityMimeType
);
-          if( defaultCharset != null ) {
-            LOG.usingDefaultCharsetForEntity( entityMimeType, defaultCharset );
-            entityContentType = entityContentType.withCharset( defaultCharset );
-          }
-        } else {
-          LOG.usingExplicitCharsetForEntity( entityContentType.getMimeType(), entityContentType.getCharset()
);
-        }
-        fullContentType = entityContentType.toString();
-      }
-    }
-    if( fullContentType == null ) {
-      LOG.unknownResponseEntityContentType();
-    } else {
-      LOG.inboundResponseEntityContentType( fullContentType );
-    }
-    return fullContentType;
-  }
 }
diff --git a/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiHaDispatch.java
b/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiHaDispatch.java
index a423708..6d390a3 100644
--- a/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiHaDispatch.java
+++ b/gateway-service-nifi/src/main/java/org/apache/knox/gateway/dispatch/NiFiHaDispatch.java
@@ -21,9 +21,7 @@ import org.apache.http.Header;
 import org.apache.http.HttpEntity;
 import org.apache.http.HttpResponse;
 import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.entity.ContentType;
 import org.apache.knox.gateway.ha.dispatch.DefaultHaDispatch;
-import org.apache.knox.gateway.util.MimeTypes;
 
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
@@ -85,34 +83,4 @@ public class NiFiHaDispatch extends DefaultHaDispatch {
       }
     }
   }
-
-  /**
-   * Overriden due to DefaultDispatch#getInboundResponseContentType(HttpEntity) having private
access, and the method is used by
-   * {@link #writeOutboundResponse(HttpUriRequest, HttpServletRequest, HttpServletResponse,
HttpResponse)}}
-   */
-  private String getInboundResponseContentType( final HttpEntity entity ) {
-    String fullContentType = null;
-    if( entity != null ) {
-      ContentType entityContentType = ContentType.get( entity );
-      if( entityContentType != null ) {
-        if( entityContentType.getCharset() == null ) {
-          final String entityMimeType = entityContentType.getMimeType();
-          final String defaultCharset = MimeTypes.getDefaultCharsetForMimeType( entityMimeType
);
-          if( defaultCharset != null ) {
-            DefaultDispatch.LOG.usingDefaultCharsetForEntity( entityMimeType, defaultCharset
);
-            entityContentType = entityContentType.withCharset( defaultCharset );
-          }
-        } else {
-          DefaultDispatch.LOG.usingExplicitCharsetForEntity( entityContentType.getMimeType(),
entityContentType.getCharset() );
-        }
-        fullContentType = entityContentType.toString();
-      }
-    }
-    if( fullContentType == null ) {
-      DefaultDispatch.LOG.unknownResponseEntityContentType();
-    } else {
-      DefaultDispatch.LOG.inboundResponseEntityContentType( fullContentType );
-    }
-    return fullContentType;
-  }
 }
diff --git a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/KnoxSh.java b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/KnoxSh.java
index 43d2d02..190db99 100644
--- a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/KnoxSh.java
+++ b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/KnoxSh.java
@@ -261,7 +261,7 @@ public class KnoxSh {
     public void execute() throws Exception {
       Credentials credentials = new Credentials();
       credentials.add("ClearInput", "Enter username: ", "user")
-                      .add("HiddenInput", "Enter pas" + "sword: ", "pass");
+                      .add("HiddenInput", "Enter password: ", "pass");
       credentials.collect();
 
       String username = credentials.get("user").string();
diff --git a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTableHistoryAspect.java
b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTableHistoryAspect.java
index 73d8623..42c79f1 100644
--- a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTableHistoryAspect.java
+++ b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTableHistoryAspect.java
@@ -37,7 +37,6 @@ import org.aspectj.lang.reflect.MethodSignature;
  */
 @Aspect
 public class KnoxShellTableHistoryAspect {
-
   private static final String KNOX_SHELL_TYPE = "org.apache.knox.gateway.shell.table.KnoxShellTable";
 
   @Pointcut("execution(public org.apache.knox.gateway.shell.table.KnoxShellTableFilter org.apache.knox.gateway.shell.table.KnoxShellTable.filter(..))")
@@ -59,7 +58,9 @@ public class KnoxShellTableHistoryAspect {
       filter = (KnoxShellTableFilter) joinPoint.proceed();
       return filter;
     } finally {
-      saveKnoxShellTableCall(joinPoint, filter.filteredTable.id);
+      if (filter != null) {
+        saveKnoxShellTableCall(joinPoint, filter.filteredTable.id);
+      }
     }
   }
 
diff --git a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
index 3c63f2b..ce2bbb3 100644
--- a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
+++ b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/AbstractGatewayDispatch.java
@@ -78,7 +78,7 @@ public abstract class AbstractGatewayDispatch implements Dispatch {
 
   @Override
   public void doGet(URI url, HttpServletRequest request, HttpServletResponse response )
-      throws IOException, URISyntaxException {
+      throws IOException {
     response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
   }
 
@@ -90,25 +90,25 @@ public abstract class AbstractGatewayDispatch implements Dispatch {
 
   @Override
   public void doPut(URI url, HttpServletRequest request, HttpServletResponse response )
-      throws IOException, URISyntaxException {
+      throws IOException {
     response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
   }
 
   @Override
   public void doPatch(URI url, HttpServletRequest request, HttpServletResponse response )
-      throws IOException, URISyntaxException {
+      throws IOException {
     response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
   }
 
   @Override
   public void doDelete(URI url, HttpServletRequest request, HttpServletResponse response
)
-      throws IOException, URISyntaxException {
+      throws IOException {
     response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
   }
 
   @Override
   public void doOptions(URI url, HttpServletRequest request, HttpServletResponse response
)
-      throws IOException, URISyntaxException {
+      throws IOException {
     response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
   }
 
@@ -117,7 +117,7 @@ public abstract class AbstractGatewayDispatch implements Dispatch {
    */
   @Override
   public void doHead(URI url, HttpServletRequest request, HttpServletResponse response )
-      throws IOException, URISyntaxException {
+      throws IOException {
     response.sendError( HttpServletResponse.SC_METHOD_NOT_ALLOWED );
   }
 
diff --git a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultDispatch.java
b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultDispatch.java
index 10bfa2f..a7edec4 100644
--- a/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultDispatch.java
+++ b/gateway-spi/src/main/java/org/apache/knox/gateway/dispatch/DefaultDispatch.java
@@ -60,7 +60,6 @@ import java.util.TreeMap;
 import java.util.stream.Collectors;
 
 public class DefaultDispatch extends AbstractGatewayDispatch {
-
   protected static final String SET_COOKIE = "SET-COOKIE";
   protected static final String WWW_AUTHENTICATE = "WWW-AUTHENTICATE";
 
@@ -70,15 +69,14 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
       AuditConstants.KNOX_SERVICE_NAME, AuditConstants.KNOX_COMPONENT_NAME);
 
   protected static final String EXCLUDE_ALL = "*";
-  private Set<String> outboundResponseExcludeHeaders = new HashSet<>(Arrays.asList(WWW_AUTHENTICATE));
-  private Set<String> outboundResponseExcludedSetCookieHeaderDirectives = new HashSet<>(Arrays.asList(EXCLUDE_ALL));
+  private Set<String> outboundResponseExcludeHeaders = Collections.singleton(WWW_AUTHENTICATE);
+  private Set<String> outboundResponseExcludedSetCookieHeaderDirectives = Collections.singleton(EXCLUDE_ALL);
 
   //Buffer size in bytes
   private int replayBufferSize = -1;
 
   @Override
   public void destroy() {
-
   }
 
   protected int getReplayBufferSize() {
@@ -104,7 +102,6 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
     replayBufferSize = size;
   }
 
-
   protected void executeRequest(
          HttpUriRequest outboundRequest,
          HttpServletRequest inboundRequest,
@@ -171,7 +168,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
     }
   }
 
-  private String getInboundResponseContentType( final HttpEntity entity ) {
+  protected String getInboundResponseContentType( final HttpEntity entity ) {
     String fullContentType = null;
     if( entity != null ) {
       ContentType entityContentType = ContentType.get( entity );
@@ -216,9 +213,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
    protected void addCredentialsToRequest(HttpUriRequest outboundRequest) {
    }
 
-   protected HttpEntity createRequestEntity(HttpServletRequest request)
-         throws IOException {
-
+   protected HttpEntity createRequestEntity(HttpServletRequest request) throws IOException
{
       String contentType = request.getContentType();
       int contentLength = request.getContentLength();
       InputStream contentStream = request.getInputStream();
@@ -251,7 +246,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
 
    @Override
    public void doGet(URI url, HttpServletRequest request, HttpServletResponse response)
-         throws IOException, URISyntaxException {
+         throws IOException {
       HttpGet method = new HttpGet(url);
       // https://issues.apache.org/jira/browse/KNOX-107 - Service URLs not rewritten for
WebHDFS GET redirects
       // This is now taken care of in DefaultHttpClientFactory.createHttpClient
@@ -263,14 +258,14 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
 
    @Override
    public void doOptions(URI url, HttpServletRequest request, HttpServletResponse response)
-         throws IOException, URISyntaxException {
+         throws IOException {
       HttpOptions method = new HttpOptions(url);
       executeRequest(method, request, response);
    }
 
    @Override
    public void doPut(URI url, HttpServletRequest request, HttpServletResponse response)
-         throws IOException, URISyntaxException {
+         throws IOException {
       HttpPut method = new HttpPut(url);
       HttpEntity entity = createRequestEntity(request);
       method.setEntity(entity);
@@ -280,7 +275,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
 
    @Override
    public void doPatch(URI url, HttpServletRequest request, HttpServletResponse response)
-         throws IOException, URISyntaxException {
+         throws IOException {
       HttpPatch method = new HttpPatch(url);
       HttpEntity entity = createRequestEntity(request);
       method.setEntity(entity);
@@ -300,7 +295,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
 
    @Override
    public void doDelete(URI url, HttpServletRequest request, HttpServletResponse response)
-         throws IOException, URISyntaxException {
+         throws IOException {
       HttpDelete method = new HttpDelete(url);
       copyRequestHeaderFields(method, request);
       executeRequest(method, request, response);
@@ -308,7 +303,7 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
 
   @Override
   public void doHead(URI url, HttpServletRequest request, HttpServletResponse response)
-      throws IOException, URISyntaxException {
+      throws IOException {
     final HttpHead method = new HttpHead(url);
     copyRequestHeaderFields(method, request);
     executeRequest(method, request, response);
@@ -316,7 +311,8 @@ public class DefaultDispatch extends AbstractGatewayDispatch {
 
   public void copyResponseHeaderFields(HttpServletResponse outboundResponse, HttpResponse
inboundResponse) {
     final TreeMap<String, Set<String>> excludedHeaderDirectives = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
-    getOutboundResponseExcludeHeaders().stream().forEach(excludeHeader -> excludedHeaderDirectives.put(excludeHeader,
new HashSet<>(Arrays.asList(EXCLUDE_ALL))));
+    getOutboundResponseExcludeHeaders().stream().forEach(excludeHeader ->
+        excludedHeaderDirectives.put(excludeHeader, Collections.singleton(EXCLUDE_ALL)));
     excludedHeaderDirectives.put(SET_COOKIE, getOutboundResponseExcludedSetCookieHeaderDirectives());
 
     for (Header header : inboundResponse.getAllHeaders()) {
diff --git a/gateway-spi/src/main/java/org/apache/knox/gateway/security/SubjectUtils.java
b/gateway-spi/src/main/java/org/apache/knox/gateway/security/SubjectUtils.java
index 8f3e74e..d6ea15b 100644
--- a/gateway-spi/src/main/java/org/apache/knox/gateway/security/SubjectUtils.java
+++ b/gateway-spi/src/main/java/org/apache/knox/gateway/security/SubjectUtils.java
@@ -20,6 +20,7 @@ package org.apache.knox.gateway.security;
 import javax.security.auth.Subject;
 import java.security.AccessController;
 import java.security.Principal;
+import java.util.Optional;
 import java.util.Set;
 
 /**
@@ -41,10 +42,9 @@ public class SubjectUtils {
     // LJM TODO: this implementation assumes the first one found.
     // We should configure through context param based on knowledge
     // of the authentication provider in use
-    Set<Principal> principals = subject.getPrincipals();
-    for (Principal p : principals) {
-      name = p.getName();
-      break;
+    Optional<Principal> principal = subject.getPrincipals().stream().findFirst();
+    if (principal.isPresent()) {
+      name = principal.get().getName();
     }
 
     return name;


Mime
View raw message