Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Map;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Preconditions;
Expand Down Expand Up @@ -219,16 +218,13 @@ public class AbfsConfiguration{
DefaultValue = DEFAULT_SAS_TOKEN_RENEW_PERIOD_FOR_STREAMS_IN_SECONDS)
private long sasTokenRenewPeriodForStreamsInSeconds;

private Map<String, String> storageAccountKeys;

public AbfsConfiguration(final Configuration rawConfig, String accountName)
throws IllegalAccessException, InvalidConfigurationValueException, IOException {
this.rawConfig = ProviderUtils.excludeIncompatibleCredentialProviders(
rawConfig, AzureBlobFileSystem.class);
this.accountName = accountName;
this.isSecure = getBoolean(FS_AZURE_SECURE_MODE, false);

validateStorageAccountKeys();
Field[] fields = this.getClass().getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
Expand Down Expand Up @@ -740,16 +736,6 @@ public SASTokenProvider getSASTokenProvider() throws AzureBlobFileSystemExceptio
}
}

void validateStorageAccountKeys() throws InvalidConfigurationValueException {
Base64StringConfigurationBasicValidator validator = new Base64StringConfigurationBasicValidator(
FS_AZURE_ACCOUNT_KEY_PROPERTY_NAME, "", true);
this.storageAccountKeys = rawConfig.getValByRegex(FS_AZURE_ACCOUNT_KEY_PROPERTY_NAME_REGX);

for (Map.Entry<String, String> account : storageAccountKeys.entrySet()) {
validator.validate(account.getValue());
}
}

int validateInt(Field field) throws IllegalAccessException, InvalidConfigurationValueException {
IntegerConfigurationValidatorAnnotation validator = field.getAnnotation(IntegerConfigurationValidatorAnnotation.class);
String value = get(validator.ConfigurationKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@

import java.io.IOException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.azurebfs.AbfsConfiguration;
import org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys;
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.KeyProviderException;
import org.apache.hadoop.fs.azurebfs.contracts.exceptions.InvalidConfigurationValueException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.hadoop.fs.azurebfs.diagnostics.Base64StringConfigurationBasicValidator;

/**
* Key provider that simply returns the storage account key from the
Expand All @@ -49,6 +51,27 @@ public String getStorageAccountKey(String accountName, Configuration rawConfig)
LOG.warn("Unable to get key from credential providers. {}", ioe);
}

// Validating the key.
try {
validateStorageAccountKey(key);
} catch (InvalidConfigurationValueException e) {
e.printStackTrace();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw it; we want to fail fast rather than wait for auth failures talking to the far end.

Copy link
Contributor Author

@mehakmeet mehakmeet Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will it be fine to surround the throw new InvalidConfigurationValueException() with one more try-catch block? Since adding an exception to the method would break a test already present.
Also, inside validateStorageAccountKey() we are throwing an InvalidConfigurationValueException() in the validate() method.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just throw KeyProviderException like it is done in line 49. Or better to call validateStorageAccountKey() just after L48 only so that you don't have to try and catch separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to place at L48 as well in the beginning but a test was getting broken, I found at that it expects a certain exception, it passes a null key, which makes the validate() method throw a different error. I think by adding a condition in the catch to throw that particular error if the key is null might solve this. So, I'll be changing it in the next commit.

Copy link
Contributor

@steveloughran steveloughran Jul 15, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't worry about breaking the test, we've moved the vaildation and should expect it. Instead the test will need to handle the way invalid configurations are raised.
Or go with mukund -just make sure that the wrapping exception uses the message of the caught exception and adds it as the cause

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think the test is fine, I just needed to throw this specific exception when the config value is null which this test is trying to achieve, so I'll throw the exception when it is null else, it'll be like it always has been.

}

return key;
}

/**
* A method to validate the storage key.
*
* @param key the key to be validated.
* @throws InvalidConfigurationValueException
*/
private void validateStorageAccountKey(String key)
throws InvalidConfigurationValueException {
Base64StringConfigurationBasicValidator validator = new Base64StringConfigurationBasicValidator(
ConfigurationKeys.FS_AZURE_ACCOUNT_KEY_PROPERTY_NAME, "", true);

validator.validate(key);
}
}