Skip to content

Commit a37b1e7

Browse files
authored
+ support for su and whoami command (#10)
1 parent 7972a45 commit a37b1e7

File tree

3 files changed

+40
-6
lines changed

3 files changed

+40
-6
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ There are 3 possible usecases:
3737
- It's working on both Windows/Linux Hadoop 2.6.0
3838

3939
#### Download
40-
- [Download binary](https://github.com/avast/hdfs-shell/releases/download/v1.0.5/hdfs-shell-1.0.5.zip)
40+
- [Download binary](https://github.com/avast/hdfs-shell/releases/download/v1.0.6/hdfs-shell-1.0.6.zip)
4141

4242
#### Configuring launch script(s) for your environment
4343
HDFS-Shell is a standard Java application. For its launch you need to define 2 things on your classpath:
@@ -70,6 +70,8 @@ Pre-defined launch scripts are located in the zip file. You can modify it locall
7070
For our purposes we also integrated following commands:
7171
- ```set showResultCodeON``` and ```set showResultCodeOFF``` - if it's enabled, it will write command result code after its completion
7272
- ```cd```, ```pwd```
73+
- ```su <username>``` - ***experimental*** - changes current active user - it won't probably work on secured HDFS (KERBEROS)
74+
- ```whoami``` - prints effective username
7375
- ```edit 'my file'``` - see the config below
7476

7577

src/main/java/com/avast/server/hdfsshell/commands/ContextCommands.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.hadoop.fs.FileStatus;
55
import org.apache.hadoop.fs.FileSystem;
66
import org.apache.hadoop.fs.Path;
7+
import org.apache.hadoop.security.UserGroupInformation;
78
import org.springframework.shell.core.CommandMarker;
89
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
910
import org.springframework.shell.core.annotation.CliCommand;
@@ -12,14 +13,15 @@
1213
import org.springframework.util.StringUtils;
1314

1415
import java.io.IOException;
16+
import java.util.Arrays;
1517

1618
@SuppressWarnings("SameParameterValue")
1719
@Component
1820
public class ContextCommands implements CommandMarker {
1921

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;
2325

2426
private boolean showResultCode = false;
2527
private boolean failOnError;
@@ -69,6 +71,36 @@ public String cd(@CliOption(key = {""}, help = "cd [<path>]") String newDir) {
6971
return "";
7072
}
7173

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+
72104
public synchronized String getCurrentDir() {
73105
if (currentDir == null) {
74106
try {

src/main/java/com/avast/server/hdfsshell/commands/EditCommands.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public EditCommands(HadoopDfsCommands hadoopDfsCommands, ContextCommands context
4444

4545
@SuppressWarnings("ResultOfMethodCallIgnored")
4646
@CliCommand(value = "edit", help = "Get file to local file system, edit and put it back to HDFS")
47-
public String set(@CliOption(key = {"_raw_"}, help = "File to edit") String path) throws IOException {
47+
public String set(@CliOption(key = {""}, help = "File to edit") String path) throws IOException {
4848
if (StringUtils.isEmpty(path)) {
4949
return "You have to define path param";
5050
}
@@ -79,7 +79,7 @@ public String set(@CliOption(key = {"_raw_"}, help = "File to edit") String path
7979
return "";
8080
}
8181

82-
private Path getFilePathForEdit(@CliOption(key = {"_raw_"}, help = "File to edit") String path) {
82+
private Path getFilePathForEdit(@CliOption(key = {""}, help = "File to edit") String path) {
8383
Path p;
8484
if (!path.startsWith("/")) {
8585
p = new Path(contextCommands.getCurrentDir(), path);

0 commit comments

Comments
 (0)