Skip to content

Commit 2627dad

Browse files
vivekratnavelbharatviswa504
authored andcommitted
HDDS-1265. ozone sh s3 getsecret throws Null Pointer Exception for unsecured clusters.
Closes #611
1 parent 091a664 commit 2627dad

2 files changed

Lines changed: 24 additions & 29 deletions

File tree

hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/ozone/ozShell/TestOzoneShell.java

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,14 @@
8181
import org.junit.After;
8282
import org.junit.AfterClass;
8383
import org.junit.Assert;
84+
85+
import static org.apache.hadoop.ozone.web.ozShell.s3.GetS3SecretHandler.OZONE_GETS3SECRET_ERROR;
8486
import static org.junit.Assert.assertEquals;
8587
import static org.junit.Assert.assertNotNull;
8688
import static org.junit.Assert.assertTrue;
8789
import static org.junit.Assert.fail;
8890
import org.junit.Before;
8991
import org.junit.BeforeClass;
90-
import org.junit.Ignore;
9192
import org.junit.Rule;
9293
import org.junit.Test;
9394
import org.junit.rules.Timeout;
@@ -1214,36 +1215,18 @@ public void testS3BucketMapping() throws IOException {
12141215
}
12151216

12161217
@Test
1217-
@Ignore("Can't run without secure cluster.")
12181218
public void testS3Secret() throws Exception {
12191219
String setOmAddress =
12201220
"--set=" + OZONE_OM_ADDRESS_KEY + "=" + getOmAddress();
12211221

1222-
err.reset();
1223-
String outputFirstAttempt;
1224-
String outputSecondAttempt;
1222+
String output;
12251223

1226-
//First attempt: If secrets are not found in database, they will be created
12271224
String[] args = new String[] {setOmAddress, "s3", "getsecret"};
12281225
execute(shell, args);
1229-
outputFirstAttempt = out.toString();
1230-
//Extracting awsAccessKey & awsSecret value from output
1231-
String[] output = outputFirstAttempt.split("\n");
1232-
String awsAccessKey = output[0].split("=")[1];
1233-
String awsSecret = output[1].split("=")[1];
1234-
assertTrue((awsAccessKey != null && awsAccessKey.length() > 0) &&
1235-
(awsSecret != null && awsSecret.length() > 0));
1236-
1237-
out.reset();
1238-
1239-
//Second attempt: Since secrets were created in previous attempt, it
1240-
// should return the same value
1241-
args = new String[] {setOmAddress, "s3", "getsecret"};
1242-
execute(shell, args);
1243-
outputSecondAttempt = out.toString();
1226+
// Get the first line of output
1227+
output = out.toString().split("\n")[0];
12441228

1245-
//verifying if secrets from both attempts are same
1246-
assertTrue(outputFirstAttempt.equals(outputSecondAttempt));
1229+
assertTrue(output.equals(OZONE_GETS3SECRET_ERROR));
12471230
}
12481231

12491232
private void createS3Bucket(String userName, String s3Bucket) {

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/web/ozShell/s3/GetS3SecretHandler.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,44 @@
1717
*/
1818
package org.apache.hadoop.ozone.web.ozShell.s3;
1919

20+
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
2021
import org.apache.hadoop.ozone.client.OzoneClient;
2122
import org.apache.hadoop.ozone.web.ozShell.Handler;
2223
import org.apache.hadoop.ozone.web.ozShell.OzoneAddress;
2324
import org.apache.hadoop.security.UserGroupInformation;
2425
import picocli.CommandLine.Command;
2526

27+
import static org.apache.hadoop.ozone.OzoneConfigKeys.OZONE_SECURITY_ENABLED_KEY;
28+
2629
/**
2730
* Executes getsecret calls.
2831
*/
2932
@Command(name = "getsecret",
3033
description = "returns s3 secret for current user")
3134
public class GetS3SecretHandler extends Handler {
3235

36+
public static final String OZONE_GETS3SECRET_ERROR = "This command is not" +
37+
" supported in unsecure clusters.";
3338
/**
3439
* Executes getS3Secret.
3540
*/
3641
@Override
3742
public Void call() throws Exception {
43+
OzoneConfiguration ozoneConfiguration = createOzoneConfiguration();
3844
OzoneClient client =
39-
new OzoneAddress().createClient(createOzoneConfiguration());
45+
new OzoneAddress().createClient(ozoneConfiguration);
4046

41-
System.out.println(
42-
client.getObjectStore().getS3Secret(
43-
UserGroupInformation.getCurrentUser().getUserName()
44-
).toString()
45-
);
47+
// getS3Secret works only with secured clusters
48+
if (ozoneConfiguration.getBoolean(OZONE_SECURITY_ENABLED_KEY, false)) {
49+
System.out.println(
50+
client.getObjectStore().getS3Secret(
51+
UserGroupInformation.getCurrentUser().getUserName()
52+
).toString()
53+
);
54+
} else {
55+
// log a warning message for unsecured cluster
56+
System.out.println(OZONE_GETS3SECRET_ERROR);
57+
}
4658

4759
return null;
4860
}

0 commit comments

Comments
 (0)