This is an automated email from the ASF dual-hosted git repository.

hemant pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 99acf10508 HDDS-10559. Add a warning or a check to run repair tool as 
System user (#6574)
99acf10508 is described below

commit 99acf10508a951ece1179dd0d2c3c71463f08cc9
Author: DaveTeng0 <[email protected]>
AuthorDate: Wed May 15 13:04:08 2024 -0700

    HDDS-10559. Add a warning or a check to run repair tool as System user 
(#6574)
---
 .../apache/hadoop/ozone/repair/OzoneRepair.java    | 30 ++++++++
 .../hadoop/ozone/repair/TestOzoneRepair.java       | 88 ++++++++++++++++++++++
 2 files changed, 118 insertions(+)

diff --git 
a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java
 
b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java
index 3bbbded580..1664452280 100644
--- 
a/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java
+++ 
b/hadoop-ozone/tools/src/main/java/org/apache/hadoop/ozone/repair/OzoneRepair.java
@@ -24,6 +24,9 @@ import org.apache.hadoop.hdds.cli.HddsVersionProvider;
 import org.apache.hadoop.hdds.conf.OzoneConfiguration;
 import picocli.CommandLine;
 
+import java.nio.charset.StandardCharsets;
+import java.util.Scanner;
+
 /**
  * Ozone Repair Command line tool.
  */
@@ -33,6 +36,11 @@ import picocli.CommandLine;
     mixinStandardHelpOptions = true)
 public class OzoneRepair extends GenericCli {
 
+  public static final String WARNING_SYS_USER_MESSAGE =
+      "ATTENTION: Running as user %s. Make sure this is the same user used to 
run the Ozone process." +
+          " Are you sure you want to continue (y/N)? ";
+
+
   private OzoneConfiguration ozoneConf;
 
   public OzoneRepair() {
@@ -61,4 +69,26 @@ public class OzoneRepair extends GenericCli {
   public static void main(String[] argv) throws Exception {
     new OzoneRepair().run(argv);
   }
+
+  @Override
+  public int execute(String[] argv) {
+    String currentUser = getSystemUserName();
+    if (!("y".equalsIgnoreCase(getConsoleReadLineWithFormat(currentUser)))) {
+      System.out.println("Aborting command.");
+      return 1;
+    }
+    System.out.println("Run as user: " + currentUser);
+
+    return super.execute(argv);
+  }
+
+  public  String getSystemUserName() {
+    return System.getProperty("user.name");
+  }
+
+  public  String getConsoleReadLineWithFormat(String currentUser) {
+    System.err.printf(WARNING_SYS_USER_MESSAGE, currentUser);
+    return (new Scanner(System.in, 
StandardCharsets.UTF_8.name())).nextLine().trim();
+  }
+
 }
diff --git 
a/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/repair/TestOzoneRepair.java
 
b/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/repair/TestOzoneRepair.java
new file mode 100644
index 0000000000..272bf24c06
--- /dev/null
+++ 
b/hadoop-ozone/tools/src/test/java/org/apache/hadoop/ozone/repair/TestOzoneRepair.java
@@ -0,0 +1,88 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.hadoop.ozone.repair;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+/**
+ * Tests the ozone repair command.
+ */
+public class TestOzoneRepair {
+
+  private final ByteArrayOutputStream out = new ByteArrayOutputStream();
+  private final ByteArrayOutputStream err = new ByteArrayOutputStream();
+  private static final PrintStream OLD_OUT = System.out;
+  private static final PrintStream OLD_ERR = System.err;
+  private static final String DEFAULT_ENCODING = UTF_8.name();
+
+  private static final String OZONE_USER = "ozone";
+  private static final String OLD_USER = System.getProperty("user.name");
+
+  @BeforeEach
+  public void setup() throws Exception {
+    System.setOut(new PrintStream(out, false, DEFAULT_ENCODING));
+    System.setErr(new PrintStream(err, false, DEFAULT_ENCODING));
+    System.setProperty("user.name", OZONE_USER);
+  }
+
+  @AfterEach
+  public void reset() {
+    // reset stream after each unit test
+    out.reset();
+    err.reset();
+
+    // restore system streams
+    System.setOut(OLD_OUT);
+    System.setErr(OLD_ERR);
+    System.setProperty("user.name", OLD_USER);
+  }
+
+  @Test
+  void testOzoneRepairWhenUserIsRemindedSystemUserAndDeclinesToProceed() 
throws Exception {
+    OzoneRepair ozoneRepair = new OzoneRepair();
+    System.setIn(new ByteArrayInputStream("N".getBytes(DEFAULT_ENCODING)));
+
+    int res = ozoneRepair.execute(new String[]{});
+    assertEquals(1, res);
+    assertThat(out.toString(DEFAULT_ENCODING)).contains("Aborting command.");
+    // prompt should contain the current user name as well
+    assertThat(err.toString(DEFAULT_ENCODING)).contains("ATTENTION: Running as 
user " + OZONE_USER);
+  }
+
+  @Test
+  void testOzoneRepairWhenUserIsRemindedSystemUserAndAgreesToProceed() throws 
Exception {
+    OzoneRepair ozoneRepair = new OzoneRepair();
+    System.setIn(new ByteArrayInputStream("y".getBytes(DEFAULT_ENCODING)));
+
+    ozoneRepair.execute(new String[]{});
+    assertThat(out.toString(DEFAULT_ENCODING)).contains("Run as user: " + 
OZONE_USER);
+    // prompt should contain the current user name as well
+    assertThat(err.toString(DEFAULT_ENCODING)).contains("ATTENTION: Running as 
user " + OZONE_USER);
+  }
+
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to