Skip to content

Commit

Permalink
Fix NPE when using deprecated Azure settings (#28769)
Browse files Browse the repository at this point in the history
* Fix NPE when using deprecated Azure settings

When someone migrates from 5.6 to 6.x with deprecated settings like:

```
cloud:
    azure:
        storage:
            foo:
                account: <my_account>
                key: <my_key>
```

It produces a NPE anytime someone wants to use a repository which name is not `default`.

This has been introduced by #23518 when I backported it to 6.x branch.
In this case, when we generate an OperationContext, we only try to get azure settings from "normal" `storageSettings` with:

```java
this.storageSettings.get(clientName)
```

But in the context of deprecated settings, this returns `null` which causes the NPE just after.

This commit adds a check and if no settings are found in the "normal" `storageSettings`, we look at settings from `deprecatedStorageSettings`.
The reason I missed it in the 7.0 version (master branch) is because actually `deprecatedStorageSettings` has been removed there already.

Also I renamed the `testGetSelectedClientDefault` method to `testGenerateOperationContext`
as it was only doing exactly the same thing as `testGetDefaultClientWithPrimaryAndSecondaries`.

Closes #28299.

Backport of #28769 in 6.2 branch.
  • Loading branch information
dadoonet committed Feb 26, 2018
1 parent b70f9ab commit ca1b639
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,13 @@ CloudBlobClient getSelectedClient(String account, LocationMode mode) {
return client;
}

private OperationContext generateOperationContext(String clientName) {
// Package private for testing in 6.x only: not needed anymore after
OperationContext generateOperationContext(String clientName) {
OperationContext context = new OperationContext();
AzureStorageSettings azureStorageSettings = this.storageSettings.get(clientName);
if (azureStorageSettings == null) {
azureStorageSettings = deprecatedStorageSettings.get(clientName);
}

if (azureStorageSettings.getProxy() != null) {
context.setProxy(azureStorageSettings.getProxy());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,10 +390,11 @@ public void testGetDefaultClientWithPrimaryAndSecondaries() {
}

@Deprecated
public void testGetSelectedClientDefault() {
public void testGenerateOperationContext() {
AzureStorageServiceImpl azureStorageService = new AzureStorageServiceMock(deprecatedSettings);
CloudBlobClient client = azureStorageService.getSelectedClient("default", LocationMode.PRIMARY_ONLY);
assertThat(client.getEndpoint(), is(URI.create("https://azure1")));
// This was producing a NPE when calling any operation with deprecated settings.
// See https://github.com/elastic/elasticsearch/issues/28299
azureStorageService.generateOperationContext("default");
assertDeprecatedWarnings();
}

Expand Down

0 comments on commit ca1b639

Please sign in to comment.