kumaab commented on code in PR #928:
URL: https://github.com/apache/ranger/pull/928#discussion_r3165147450


##########
authz-remote/src/main/java/org/apache/ranger/authz/remote/authn/RangerRemoteJwtProvider.java:
##########
@@ -0,0 +1,122 @@
+/*
+ * 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.ranger.authz.remote.authn;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.ranger.authz.api.RangerAuthzException;
+import org.apache.ranger.authz.remote.RangerRemoteAuthzConfig;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+
+import static 
org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.INVALID_PROPERTY_VALUE;
+import static 
org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.MISSING_AUTH_CONFIG;
+import static 
org.apache.ranger.authz.remote.RangerRemoteAuthzErrorCode.REMOTE_REQUEST_FAILED;
+
+public class RangerRemoteJwtProvider {
+    private static final String SOURCE_ENV  = "env";
+    private static final String SOURCE_FILE = "file";
+
+    private final String source;
+    private final String envVar;
+    private final String filePath;
+
+    private       long    fileLastModified;
+    private volatile String jwt;
+
+    private RangerRemoteJwtProvider(String source, String envVar, String 
filePath) {
+        this.source   = source;
+        this.envVar   = envVar;
+        this.filePath = filePath;
+    }
+
+    public static RangerRemoteJwtProvider create(RangerRemoteAuthzConfig 
config) throws RangerAuthzException {
+        String source = config.getJwtSource().trim().toLowerCase();
+
+        switch (source) {
+            case SOURCE_ENV:
+                return new RangerRemoteJwtProvider(source, 
config.getJwtEnvVar(), null);
+
+            case SOURCE_FILE:
+                return new RangerRemoteJwtProvider(source, null, 
config.getJwtFile());
+
+            default:
+                throw new RangerAuthzException(INVALID_PROPERTY_VALUE, 
RangerRemoteAuthzConfig.PROP_REMOTE_AUTH_JWT_SOURCE, source);
+        }
+    }
+
+    public String getJwt() throws RangerAuthzException {
+        switch (source) {
+            case SOURCE_ENV:
+                jwt = System.getenv(envVar);
+                break;
+
+            case SOURCE_FILE:
+                refreshJwtFromFile();
+                break;
+
+            default:
+                throw new RangerAuthzException(INVALID_PROPERTY_VALUE, 
RangerRemoteAuthzConfig.PROP_REMOTE_AUTH_JWT_SOURCE, source);
+        }
+
+        if (StringUtils.isBlank(jwt)) {
+            throw new RangerAuthzException(MISSING_AUTH_CONFIG, 
getSourcePropertyName());
+        }
+
+        return jwt.trim();

Review Comment:
   `jwt` won't be null at line 86 as if check above: `StringUtils.isBlank(jwt)` 
will check for `null` and throw an exception



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