diff --git a/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystem.java b/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystem.java index 209d9da71b..f98d9e2c7f 100644 --- a/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystem.java +++ b/other/java/hdfs3/src/main/java/seaweed/hdfs/SeaweedFileSystem.java @@ -76,26 +76,19 @@ public class SeaweedFileSystem extends FileSystem { seaweedFileSystemStore = new SeaweedFileSystemStore(host, port, grpcPort, cn, conf); } - // Options set here override the [filer.basic_auth] and [jwt.filer_signing] values of security.toml. + // Each option set here overrides only its own [filer.basic_auth] or [jwt.filer_signing] value of security.toml. static void applyFilerCredentials(Configuration conf, FilerCredentials credentials) throws IOException { - String user = conf.get(FS_SEAWEED_FILER_BASIC_AUTH_USER, ""); - String password = secret(conf, FS_SEAWEED_FILER_BASIC_AUTH_PASSWORD); - if (!user.isEmpty() || password != null) { - credentials.setBasicAuth(user, password); - } - String key = secret(conf, FS_SEAWEED_FILER_JWT_SIGNING_KEY); - if (key != null) { - credentials.setJwtSigningKey(key, conf.getLong(FS_SEAWEED_FILER_JWT_SIGNING_EXPIRES, 0)); - } - String readKey = secret(conf, FS_SEAWEED_FILER_JWT_SIGNING_READ_KEY); - if (readKey != null) { - credentials.setJwtReadSigningKey(readKey, conf.getLong(FS_SEAWEED_FILER_JWT_SIGNING_READ_EXPIRES, 0)); - } + credentials.setBasicAuth(conf.get(FS_SEAWEED_FILER_BASIC_AUTH_USER), + secret(conf, FS_SEAWEED_FILER_BASIC_AUTH_PASSWORD)); + credentials.setJwtSigningKey(secret(conf, FS_SEAWEED_FILER_JWT_SIGNING_KEY), + conf.getLong(FS_SEAWEED_FILER_JWT_SIGNING_EXPIRES, 0)); + credentials.setJwtReadSigningKey(secret(conf, FS_SEAWEED_FILER_JWT_SIGNING_READ_KEY), + conf.getLong(FS_SEAWEED_FILER_JWT_SIGNING_READ_EXPIRES, 0)); } private static String secret(Configuration conf, String name) throws IOException { char[] value = conf.getPassword(name); - return value == null || value.length == 0 ? null : new String(value); + return value == null ? null : new String(value); } @Override diff --git a/other/java/hdfs3/src/test/java/seaweed/hdfs/SeaweedFileSystemConfigTest.java b/other/java/hdfs3/src/test/java/seaweed/hdfs/SeaweedFileSystemConfigTest.java index a3ee459e59..ce1fa70ad9 100644 --- a/other/java/hdfs3/src/test/java/seaweed/hdfs/SeaweedFileSystemConfigTest.java +++ b/other/java/hdfs3/src/test/java/seaweed/hdfs/SeaweedFileSystemConfigTest.java @@ -108,4 +108,29 @@ public class SeaweedFileSystemConfigTest { SeaweedFileSystem.applyFilerCredentials(conf, credentials); assertTrue(credentials.getAuthorizationHeaderValue(false).startsWith("BEARER ")); } + + @Test + public void testFilerCredentialOptionsOverrideOneValueEach() throws Exception { + FilerCredentials credentials = new FilerCredentials(); + credentials.setBasicAuth("toml-user", "toml-pass"); + credentials.setJwtReadSigningKey("toml-read-key", 0); + + conf.set(SeaweedFileSystem.FS_SEAWEED_FILER_BASIC_AUTH_PASSWORD, "new-pass"); + conf.setLong(SeaweedFileSystem.FS_SEAWEED_FILER_JWT_SIGNING_READ_EXPIRES, 300); + SeaweedFileSystem.applyFilerCredentials(conf, credentials); + + assertEquals("Basic " + java.util.Base64.getEncoder() + .encodeToString("toml-user:new-pass".getBytes(java.nio.charset.StandardCharsets.UTF_8)), + credentials.getAuthorizationHeaderValue(true)); + String[] read = credentials.getAuthorizationHeaderValue(false).substring("BEARER ".length()).split("\\."); + javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA256"); + mac.init(new javax.crypto.spec.SecretKeySpec( + "toml-read-key".getBytes(java.nio.charset.StandardCharsets.UTF_8), "HmacSHA256")); + assertEquals(java.util.Base64.getUrlEncoder().withoutPadding().encodeToString(mac.doFinal( + (read[0] + "." + read[1]).getBytes(java.nio.charset.StandardCharsets.UTF_8))), read[2]); + String payload = new String(java.util.Base64.getUrlDecoder().decode(read[1]), + java.nio.charset.StandardCharsets.UTF_8); + long exp = Long.parseLong(payload.replaceAll("[^0-9]", "")); + assertTrue(exp - System.currentTimeMillis() / 1000 >= 299); + } }