Skip to content

Commit 92cb671

Browse files
author
Anuj Modi
committed
Resolved Comments
1 parent db03c5f commit 92cb671

File tree

8 files changed

+133
-113
lines changed

8 files changed

+133
-113
lines changed

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/AbfsConfiguration.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -942,24 +942,31 @@ public AccessTokenProvider getTokenProvider() throws TokenAccessProviderExceptio
942942
}
943943

944944
/**
945-
* The following method chooses between a configured fixed sas token, and a user implementation of the SASTokenProvider interface,
946-
* depending on which one is available. In case a user SASTokenProvider implementation is not present, and a fixed token is configured,
947-
* it simply returns null, to set the sasTokenProvider object for current configuration instance to null.
948-
* The fixed token is read and used later. This is done to:
949-
* 1. check for cases where both are not set, while initializing AbfsConfiguration,
950-
* to not proceed further than thi stage itself when none of the options are available.
951-
* 2. avoid using similar tokenProvider implementation to just read the configured fixed token,
952-
* as this could create confusion. The configuration is introduced
953-
* primarily to avoid using any tokenProvider class/interface. Also,implementing the SASTokenProvider requires relying on the raw configurations.
954-
* It is more stable to depend on the AbfsConfiguration with which a filesystem is initialized,
945+
* The user can choose between a configured fixed sas token, and a user
946+
* implementation of the SASTokenProvider interface. Preference will be given
947+
* to SASTokenProvider class provided as the value of "fs.azure.sas.token.provider.type".
948+
* If above config is not set, it is expected that user wants to use a
949+
* fixed SAS Token provided as value of "fs.azure.sas.fixed.token".
950+
* <ol>
951+
* <li>If both the configs are not provided,
952+
* initialization fails and {@link TokenAccessProviderException} is thrown.</li>
953+
* <li>If both are present, SASTokenProvider class will be used to generate SAS Token.</li>
954+
* <li>If only fixed SAS Token is configured, this will return null
955+
* and Fixed SAS token will be used to sign requests.</li>
956+
* </ol>
957+
* Avoid using a tokenProvider implementation just to read the configured fixed token,
958+
* as this could create confusion. Also,implementing the SASTokenProvider
959+
* requires relying on the raw configurations. It is more stable to depend on the
960+
* AbfsConfiguration with which a filesystem is initialized,
955961
* and eliminate chances of dynamic modifications and spurious situations.
956962
* @return sasTokenProvider object
957963
* @throws AzureBlobFileSystemException
958964
*/
959965
public SASTokenProvider getSASTokenProvider() throws AzureBlobFileSystemException {
960966
AuthType authType = getEnum(FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME, AuthType.SharedKey);
961967
if (authType != AuthType.SAS) {
962-
throw new SASTokenProviderException(String.format("Invalid auth type: %s is being used, expecting SAS", authType));
968+
throw new SASTokenProviderException(String.format(
969+
"Invalid auth type: %s is being used, expecting SAS.", authType));
963970
}
964971

965972
try {
@@ -970,30 +977,30 @@ public SASTokenProvider getSASTokenProvider() throws AzureBlobFileSystemExceptio
970977
String configuredFixedToken = this.rawConfig.get(FS_AZURE_SAS_FIXED_TOKEN,
971978
null);
972979

973-
Preconditions.checkArgument(!(sasTokenProviderImplementation == null
974-
&& configuredFixedToken == null),
975-
String.format(
976-
"The value for both \"%s\" and \"%s\" cannot be invalid.",
977-
FS_AZURE_SAS_TOKEN_PROVIDER_TYPE, FS_AZURE_SAS_FIXED_TOKEN));
980+
Preconditions.checkArgument(
981+
sasTokenProviderImplementation != null || configuredFixedToken != null,
982+
"At least one of the \"%s\" and \"%s\" must be set.",
983+
FS_AZURE_SAS_TOKEN_PROVIDER_TYPE, FS_AZURE_SAS_FIXED_TOKEN);
978984

985+
// Prefer SASTokenProvider Implementation if configured.
979986
if (sasTokenProviderImplementation != null) {
980-
LOG.trace(
981-
"Using SASTokenProvider class because it is given precedence when it is set");
987+
LOG.trace("Using SASTokenProvider class because it is given precedence when it is set.");
982988
SASTokenProvider sasTokenProvider = ReflectionUtils.newInstance(
983989
sasTokenProviderImplementation, rawConfig);
984990
Preconditions.checkArgument(sasTokenProvider != null,
985-
String.format("Failed to initialize %s",
986-
sasTokenProviderImplementation));
991+
"Failed to initialize %s", sasTokenProviderImplementation);
987992

988993
LOG.trace("Initializing {}", sasTokenProviderImplementation.getName());
989994
sasTokenProvider.initialize(rawConfig, accountName);
990995
LOG.trace("{} init complete", sasTokenProviderImplementation.getName());
991996
return sasTokenProvider;
992997
} else {
998+
// Configured Fixed SAS Token will be used to sign the requests.
993999
return null;
9941000
}
9951001
} catch (Exception e) {
996-
throw new TokenAccessProviderException("Unable to load SAS token provider class: " + e, e);
1002+
throw new TokenAccessProviderException(
1003+
"Unable to load SAS token provider class: " + e, e);
9971004
}
9981005
}
9991006

@@ -1006,14 +1013,14 @@ public EncryptionContextProvider createEncryptionContextProvider() {
10061013
Class<? extends EncryptionContextProvider> encryptionContextClass =
10071014
getAccountSpecificClass(configKey, null,
10081015
EncryptionContextProvider.class);
1009-
Preconditions.checkArgument(encryptionContextClass != null, String.format(
1016+
Preconditions.checkArgument(encryptionContextClass != null,
10101017
"The configuration value for %s is invalid, or config key is not account-specific",
1011-
configKey));
1018+
configKey);
10121019

10131020
EncryptionContextProvider encryptionContextProvider =
10141021
ReflectionUtils.newInstance(encryptionContextClass, rawConfig);
10151022
Preconditions.checkArgument(encryptionContextProvider != null,
1016-
String.format("Failed to initialize %s", encryptionContextClass));
1023+
"Failed to initialize %s", encryptionContextClass);
10171024

10181025
LOG.trace("{} init complete", encryptionContextClass.getName());
10191026
return encryptionContextProvider;

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/constants/ConfigurationKeys.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,17 +262,13 @@ public final class ConfigurationKeys {
262262
/** Add extra resilience to rename failures, at the expense of performance. */
263263
public static final String FS_AZURE_ABFS_RENAME_RESILIENCE = "fs.azure.enable.rename.resilience";
264264

265-
public static String accountProperty(String property, String account) {
266-
return property + "." + account;
267-
}
268-
269265
public static final String FS_AZURE_ENABLE_DELEGATION_TOKEN = "fs.azure.enable.delegation.token";
270266
public static final String FS_AZURE_DELEGATION_TOKEN_PROVIDER_TYPE = "fs.azure.delegation.token.provider.type";
271267

272-
/** Key for fixed SAS token **/
268+
/** Key for fixed SAS token: {@value}. **/
273269
public static final String FS_AZURE_SAS_FIXED_TOKEN = "fs.azure.sas.fixed.token";
274270

275-
/** Key for SAS token provider **/
271+
/** Key for SAS token provider: {@value}. **/
276272
public static final String FS_AZURE_SAS_TOKEN_PROVIDER_TYPE = "fs.azure.sas.token.provider.type";
277273

278274
/** For performance, AbfsInputStream/AbfsOutputStream re-use SAS tokens until the expiry is within this number of seconds. **/

hadoop-tools/hadoop-azure/src/main/java/org/apache/hadoop/fs/azurebfs/services/AbfsClient.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public AbfsRestOperation createFilesystem(TracingContext tracingContext)
310310

311311
final AbfsUriQueryBuilder abfsUriQueryBuilder = new AbfsUriQueryBuilder();
312312
abfsUriQueryBuilder.addQuery(QUERY_PARAM_RESOURCE, FILESYSTEM);
313-
// appending SAS Token to query
313+
314314
appendSASTokenToQuery(ROOT_PATH, "", abfsUriQueryBuilder);
315315

316316
final URL url = createRequestUrl(abfsUriQueryBuilder.toString());
@@ -334,7 +334,7 @@ public AbfsRestOperation setFilesystemProperties(final String properties,
334334

335335
final AbfsUriQueryBuilder abfsUriQueryBuilder = createDefaultUriQueryBuilder();
336336
abfsUriQueryBuilder.addQuery(QUERY_PARAM_RESOURCE, FILESYSTEM);
337-
// appending SAS Token to query
337+
338338
appendSASTokenToQuery(ROOT_PATH, "", abfsUriQueryBuilder);
339339

340340
final URL url = createRequestUrl(abfsUriQueryBuilder.toString());
@@ -376,7 +376,7 @@ public AbfsRestOperation getFilesystemProperties(TracingContext tracingContext)
376376

377377
final AbfsUriQueryBuilder abfsUriQueryBuilder = createDefaultUriQueryBuilder();
378378
abfsUriQueryBuilder.addQuery(QUERY_PARAM_RESOURCE, FILESYSTEM);
379-
// appending SAS Token to query
379+
380380
appendSASTokenToQuery(ROOT_PATH, "", abfsUriQueryBuilder);
381381

382382
final URL url = createRequestUrl(abfsUriQueryBuilder.toString());
@@ -394,7 +394,7 @@ public AbfsRestOperation deleteFilesystem(TracingContext tracingContext) throws
394394

395395
final AbfsUriQueryBuilder abfsUriQueryBuilder = createDefaultUriQueryBuilder();
396396
abfsUriQueryBuilder.addQuery(QUERY_PARAM_RESOURCE, FILESYSTEM);
397-
// appending SAS Token to query
397+
398398
appendSASTokenToQuery(ROOT_PATH, "", abfsUriQueryBuilder);
399399

400400
final URL url = createRequestUrl(abfsUriQueryBuilder.toString());
@@ -954,6 +954,7 @@ public AbfsRestOperation flush(final String path, final long position,
954954
abfsUriQueryBuilder.addQuery(QUERY_PARAM_POSITION, Long.toString(position));
955955
abfsUriQueryBuilder.addQuery(QUERY_PARAM_RETAIN_UNCOMMITTED_DATA, String.valueOf(retainUncommittedData));
956956
abfsUriQueryBuilder.addQuery(QUERY_PARAM_CLOSE, String.valueOf(isClose));
957+
957958
// AbfsInputStream/AbfsOutputStream reuse SAS tokens for better performance
958959
String sasTokenForReuse = appendSASTokenToQuery(path, SASTokenProvider.WRITE_OPERATION,
959960
abfsUriQueryBuilder, cachedSasToken);
@@ -1044,6 +1045,7 @@ public AbfsRestOperation read(final String path,
10441045
requestHeaders.add(new AbfsHttpHeader(IF_MATCH, eTag));
10451046

10461047
final AbfsUriQueryBuilder abfsUriQueryBuilder = createDefaultUriQueryBuilder();
1048+
10471049
// AbfsInputStream/AbfsOutputStream reuse SAS tokens for better performance
10481050
String sasTokenForReuse = appendSASTokenToQuery(path, SASTokenProvider.READ_OPERATION,
10491051
abfsUriQueryBuilder, cachedSasToken);
@@ -1294,8 +1296,16 @@ public static String getDirectoryQueryParameter(final String path) {
12941296
return directory;
12951297
}
12961298

1299+
/**
1300+
* Chooses between the SAS token provided by SASTokeProvider class and the configured fixed SAS token.
1301+
* Preference given to SASTokenProvider implementation to generate the SAS.
1302+
* If SASTokenProvider is null, returns the fixed SAS Token configured.
1303+
* @param operation
1304+
* @param path
1305+
* @return sasToken
1306+
* @throws IOException
1307+
*/
12971308
private String chooseSASToken(String operation, String path) throws IOException {
1298-
// chooses the SAS token provider class if it is configured, otherwise reads the configured fixed token
12991309
if (sasTokenProvider == null) {
13001310
return abfsConfiguration.get(ConfigurationKeys.FS_AZURE_SAS_FIXED_TOKEN);
13011311
}
@@ -1341,16 +1351,17 @@ private String appendSASTokenToQuery(String path,
13411351
sasToken = cachedSasToken;
13421352
LOG.trace("Using cached SAS token.");
13431353
}
1354+
13441355
// if SAS Token contains a prefix of ?, it should be removed
13451356
if (sasToken.charAt(0) == '?') {
13461357
sasToken = sasToken.substring(1);
13471358
}
1359+
13481360
queryBuilder.setSASToken(sasToken);
13491361
LOG.trace("SAS token fetch complete for {} on {}", operation, path);
13501362
} catch (Exception ex) {
1351-
throw new SASTokenProviderException(String.format("Failed to acquire a SAS token for %s on %s due to %s",
1352-
operation,
1353-
path,
1363+
throw new SASTokenProviderException(String.format(
1364+
"Failed to acquire a SAS token for %s on %s due to %s", operation, path,
13541365
ex.toString()));
13551366
}
13561367
}

0 commit comments

Comments
 (0)