You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ta...@apache.org on 2021/02/01 22:00:05 UTC

[tika] 01/02: TIKA-3287 -- fix aes initialization

This is an automated email from the ASF dual-hosted git repository.

tallison pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/tika.git

commit f9f73f47cab414e7071d07737588736c8d5de90d
Author: tballison <ta...@apache.org>
AuthorDate: Mon Feb 1 10:41:24 2021 -0500

    TIKA-3287 -- fix aes initialization
---
 .../tika/pipes/fetcher/http/HttpFetcher.java       |  2 +-
 .../org/apache/tika/client/HttpClientFactory.java  | 24 +++++++++++++---------
 2 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/tika-pipes/tika-fetchers/tika-fetcher-http/src/main/java/org/apache/tika/pipes/fetcher/http/HttpFetcher.java b/tika-pipes/tika-fetchers/tika-fetcher-http/src/main/java/org/apache/tika/pipes/fetcher/http/HttpFetcher.java
index 75bd1a1..a6d2325 100644
--- a/tika-pipes/tika-fetchers/tika-fetcher-http/src/main/java/org/apache/tika/pipes/fetcher/http/HttpFetcher.java
+++ b/tika-pipes/tika-fetchers/tika-fetcher-http/src/main/java/org/apache/tika/pipes/fetcher/http/HttpFetcher.java
@@ -49,7 +49,7 @@ public class HttpFetcher extends AbstractFetcher implements Initializable {
     private HttpClientFactory httpClientFactory;
     private HttpClient httpClient;
 
-    public HttpFetcher() {
+    public HttpFetcher() throws TikaConfigException {
         httpClientFactory = new HttpClientFactory();
     }
     @Override
diff --git a/tika-pipes/tika-httpclient-commons/src/main/java/org/apache/tika/client/HttpClientFactory.java b/tika-pipes/tika-httpclient-commons/src/main/java/org/apache/tika/client/HttpClientFactory.java
index eacb461..8cbc26b 100644
--- a/tika-pipes/tika-httpclient-commons/src/main/java/org/apache/tika/client/HttpClientFactory.java
+++ b/tika-pipes/tika-httpclient-commons/src/main/java/org/apache/tika/client/HttpClientFactory.java
@@ -86,9 +86,10 @@ import java.util.Set;
  */
 public class HttpClientFactory {
 
+    public static final String AES_ENV_VAR = "AES_KEY";
     private static final Logger LOG = LoggerFactory.getLogger(HttpClientFactory.class);
 
-    private AES aes;
+    private AES aes = null;
 
     private String proxyHost;
     private int proxyPort;
@@ -105,6 +106,16 @@ public class HttpClientFactory {
     private String authScheme = "basic"; //ntlm or basic
     private boolean credentialsAESEncrypted = false;
 
+
+    public HttpClientFactory() throws TikaConfigException {
+        if (credentialsAESEncrypted && System.getenv(AES_ENV_VAR) == null) {
+            throw new TikaConfigException(
+                    "must specify aes key in the environment variable: " + AES_ENV_VAR);
+        }
+        if (credentialsAESEncrypted) {
+            aes = new AES();
+        }
+    }
     public String getProxyHost() {
         return proxyHost;
     }
@@ -305,15 +316,9 @@ public class HttpClientFactory {
     }
 
     private String decrypt(String encrypted) throws TikaConfigException {
-        if (! credentialsAESEncrypted) {
+        if (aes == null || encrypted == null) {
             return encrypted;
         }
-        if (encrypted == null) {
-            return encrypted;
-        }
-        if (aes == null) {
-            aes = new AES();
-        }
         return aes.decrypt(encrypted);
     }
 
@@ -397,8 +402,7 @@ public class HttpClientFactory {
         private byte[] key;
 
         private AES() throws TikaConfigException {
-            //TODO: clean this up -- potential race condition, etc...
-            secretKey = setKey(System.getenv("AES_KEY"));
+            secretKey = setKey(System.getenv(AES_ENV_VAR));
         }
 
         private SecretKeySpec setKey(String myKey) throws TikaConfigException {