|
4 | 4 | import org.apache.hadoop.fs.FileStatus;
|
5 | 5 | import org.apache.hadoop.fs.FileSystem;
|
6 | 6 | import org.apache.hadoop.fs.Path;
|
| 7 | +import org.apache.hadoop.security.UserGroupInformation; |
7 | 8 | import org.springframework.shell.core.CommandMarker;
|
8 | 9 | import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
|
9 | 10 | import org.springframework.shell.core.annotation.CliCommand;
|
|
12 | 13 | import org.springframework.util.StringUtils;
|
13 | 14 |
|
14 | 15 | import java.io.IOException;
|
| 16 | +import java.util.Arrays; |
15 | 17 |
|
16 | 18 | @SuppressWarnings("SameParameterValue")
|
17 | 19 | @Component
|
18 | 20 | public class ContextCommands implements CommandMarker {
|
19 | 21 |
|
20 |
| - private volatile String currentDir; |
21 |
| - private volatile Configuration configuration; |
22 |
| - private volatile String homeDir; |
| 22 | + private String currentDir; |
| 23 | + private Configuration configuration; |
| 24 | + private String homeDir; |
23 | 25 |
|
24 | 26 | private boolean showResultCode = false;
|
25 | 27 | private boolean failOnError;
|
@@ -69,6 +71,36 @@ public String cd(@CliOption(key = {""}, help = "cd [<path>]") String newDir) {
|
69 | 71 | return "";
|
70 | 72 | }
|
71 | 73 |
|
| 74 | + @CliCommand(value = "su", help = "Changes current active user [*experimental*]") |
| 75 | + public synchronized String su(@CliOption(key = {""}, help = "su [<username>]") String newUser) throws IOException { |
| 76 | + if (StringUtils.isEmpty(newUser)) { |
| 77 | + return "No username is defined! "; |
| 78 | + } |
| 79 | +// else { |
| 80 | +// newUser = BashUtils.parseArguments(newUser)[0]; |
| 81 | +// } |
| 82 | + final FileSystem fs = getFileSystem(); |
| 83 | + final Path usersDir = new Path("/user"); |
| 84 | + if (fs.exists(usersDir)) { |
| 85 | + final String finalNewUser = newUser; |
| 86 | + final boolean foundUser = Arrays.stream(fs.listStatus(usersDir)). |
| 87 | + filter(FileStatus::isDirectory). |
| 88 | + anyMatch(fileStatus -> fileStatus.getPath().getName().equals(finalNewUser)); |
| 89 | + if (!foundUser) { |
| 90 | + return "User " + newUser + " does not exist!"; |
| 91 | + } |
| 92 | + } |
| 93 | + System.setProperty("HADOOP_USER_NAME", newUser); |
| 94 | + UserGroupInformation.loginUserFromSubject(null); |
| 95 | + currentDir = null; |
| 96 | + return ""; |
| 97 | + } |
| 98 | + |
| 99 | + @CliCommand(value = "whoami", help = "Print effective username") |
| 100 | + public synchronized String whoami() throws IOException { |
| 101 | + return UserGroupInformation.getCurrentUser().getUserName(); |
| 102 | + } |
| 103 | + |
72 | 104 | public synchronized String getCurrentDir() {
|
73 | 105 | if (currentDir == null) {
|
74 | 106 | try {
|
|
0 commit comments