knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From m...@apache.org
Subject [01/23] knox git commit: KNOX-1071 - Replace java.util.Random with SecureRandom
Date Mon, 16 Oct 2017 17:05:55 GMT
Repository: knox
Updated Branches:
  refs/heads/KNOX-998-Package_Restructuring cdac6ff07 -> 7d0bff16e


KNOX-1071 - Replace java.util.Random with SecureRandom


Project: http://git-wip-us.apache.org/repos/asf/knox/repo
Commit: http://git-wip-us.apache.org/repos/asf/knox/commit/145ed5dd
Tree: http://git-wip-us.apache.org/repos/asf/knox/tree/145ed5dd
Diff: http://git-wip-us.apache.org/repos/asf/knox/diff/145ed5dd

Branch: refs/heads/KNOX-998-Package_Restructuring
Commit: 145ed5dd673e89db278ad5b7257690e3b23e3109
Parents: 10b3473
Author: Colm O hEigeartaigh <coheigea@apache.org>
Authored: Thu Sep 28 12:39:00 2017 +0100
Committer: Colm O hEigeartaigh <coheigea@apache.org>
Committed: Thu Sep 28 12:39:00 2017 +0100

----------------------------------------------------------------------
 .../impl/DefaultServiceRegistryService.java     | 50 ++++++++++----------
 .../security/impl/DefaultAliasService.java      | 12 ++---
 2 files changed, 31 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/knox/blob/145ed5dd/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceRegistryService.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceRegistryService.java
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceRegistryService.java
index c590f0d..ec08597 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceRegistryService.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/registry/impl/DefaultServiceRegistryService.java
@@ -35,14 +35,14 @@ import org.apache.hadoop.gateway.services.security.CryptoService;
 
 import java.io.File;
 import java.io.IOException;
+import java.security.SecureRandom;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Random;
 
 public class DefaultServiceRegistryService implements ServiceRegistry, Service {
   private static GatewayMessages LOG = MessagesFactory.get( GatewayMessages.class );
-  
+
   protected char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
   'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
   'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
@@ -53,31 +53,31 @@ public class DefaultServiceRegistryService implements ServiceRegistry,
Service {
   private Registry registry = new Registry();
 
   private String registryFileName;
-  
+
   public DefaultServiceRegistryService() {
   }
-  
+
   public void setCryptoService(CryptoService crypto) {
     this.crypto = crypto;
   }
-  
+
   public String getRegistrationCode(String clusterName) {
     String code = generateRegCode(16);
     byte[] signature = crypto.sign("SHA256withRSA","gateway-identity",code);
     String encodedSig = Base64.encodeBase64URLSafeString(signature);
-    
+
     return code + "::" + encodedSig;
   }
-  
+
   private String generateRegCode(int length) {
-    StringBuffer sb = new StringBuffer();
-    Random r = new Random();
+    StringBuilder sb = new StringBuilder();
+    SecureRandom r = new SecureRandom();
     for (int i = 0; i < length; i++) {
       sb.append(chars[r.nextInt(chars.length)]);
     }
     return sb.toString();
   }
-  
+
   public void removeClusterServices(String clusterName) {
     registry.remove(clusterName);
   }
@@ -89,7 +89,7 @@ public class DefaultServiceRegistryService implements ServiceRegistry, Service
{
       throw new IllegalArgumentException("Registration Code must not be null.");
     }
     String[] parts = regCode.split("::");
-    
+
     // part one is the code and part two is the signature
     boolean verified = crypto.verify("SHA256withRSA", "gateway-identity", parts[0], Base64.decodeBase64(parts[1]));
     if (verified) {
@@ -114,24 +114,24 @@ public class DefaultServiceRegistryService implements ServiceRegistry,
Service {
         e.printStackTrace(); //TODO: I18N
       }
     }
-    
+
     return rc;
   }
-  
+
   private String renderAsJsonString(HashMap<String,HashMap<String,RegEntry>>
registry) {
     String json = null;
     ObjectMapper mapper = new ObjectMapper();
-    
+
     try {
       // write JSON to a file
       json = mapper.writeValueAsString((Object)registry);
-    
+
     } catch ( JsonProcessingException e ) {
       e.printStackTrace(); //TODO: I18N
     }
     return json;
   }
-  
+
   @Override
   public String lookupServiceURL(String clusterName, String serviceName) {
     List<String> urls = lookupServiceURLs( clusterName, serviceName );
@@ -144,22 +144,22 @@ public class DefaultServiceRegistryService implements ServiceRegistry,
Service {
   @Override
   public List<String> lookupServiceURLs( String clusterName, String serviceName ) {
     RegEntry entry = null;
-    HashMap clusterServices = registry.get(clusterName);
+    HashMap<String, RegEntry> clusterServices = registry.get(clusterName);
     if (clusterServices != null) {
-      entry = (RegEntry) clusterServices.get(serviceName);
+      entry = clusterServices.get(serviceName);
       if( entry != null ) {
         return entry.getUrls();
       }
     }
     return null;
   }
-  
+
   private HashMap<String, HashMap<String,RegEntry>> getMapFromJsonString(String
json) {
     Registry map = null;
-    JsonFactory factory = new JsonFactory(); 
-    ObjectMapper mapper = new ObjectMapper(factory); 
-    TypeReference<Registry> typeRef 
-          = new TypeReference<Registry>() {}; 
+    JsonFactory factory = new JsonFactory();
+    ObjectMapper mapper = new ObjectMapper(factory);
+    TypeReference<Registry> typeRef
+          = new TypeReference<Registry>() {};
     try {
       map = mapper.readValue(json, typeRef);
     } catch (JsonParseException e) {
@@ -168,9 +168,9 @@ public class DefaultServiceRegistryService implements ServiceRegistry,
Service {
       LOG.failedToGetMapFromJsonString( json, e );
     } catch (IOException e) {
       LOG.failedToGetMapFromJsonString( json, e );
-    } 
+    }
     return map;
-  }   
+  }
 
   @Override
   public void init(GatewayConfig config, Map<String, String> options)

http://git-wip-us.apache.org/repos/asf/knox/blob/145ed5dd/gateway-server/src/main/java/org/apache/hadoop/gateway/services/security/impl/DefaultAliasService.java
----------------------------------------------------------------------
diff --git a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/security/impl/DefaultAliasService.java
b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/security/impl/DefaultAliasService.java
index 8cf31a5..4a3e754 100644
--- a/gateway-server/src/main/java/org/apache/hadoop/gateway/services/security/impl/DefaultAliasService.java
+++ b/gateway-server/src/main/java/org/apache/hadoop/gateway/services/security/impl/DefaultAliasService.java
@@ -19,12 +19,12 @@ package org.apache.hadoop.gateway.services.security.impl;
 
 import java.security.KeyStore;
 import java.security.KeyStoreException;
+import java.security.SecureRandom;
 import java.security.cert.Certificate;
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.List;
 import java.util.Map;
-import java.util.Random;
 
 import org.apache.hadoop.gateway.GatewayMessages;
 import org.apache.hadoop.gateway.config.GatewayConfig;
@@ -39,7 +39,7 @@ import org.apache.hadoop.gateway.services.security.MasterService;
 public class DefaultAliasService implements AliasService {
   private static final GatewayMessages LOG = MessagesFactory.get( GatewayMessages.class );
 
-  private static final String GATEWAY_IDENTITY_PASSPHRASE = "gateway-identity-passphrase";

+  private static final String GATEWAY_IDENTITY_PASSPHRASE = "gateway-identity-passphrase";
 
   protected char[] chars = { 'a', 'b', 'c', 'd', 'e', 'f', 'g',
   'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
@@ -104,21 +104,21 @@ public class DefaultAliasService implements AliasService {
   }
 
   private String generatePassword(int length) {
-    StringBuffer sb = new StringBuffer();
-    Random r = new Random();
+    StringBuilder sb = new StringBuilder();
+    SecureRandom r = new SecureRandom();
     for (int i = 0; i < length; i++) {
       sb.append(chars[r.nextInt(chars.length)]);
     }
     return sb.toString();
   }
-  
+
   public void setKeystoreService(KeystoreService ks) {
     this.keystoreService = ks;
   }
 
   public void setMasterService(MasterService ms) {
     this.masterService = ms;
-    
+
   }
 
   @Override


Mime
View raw message