Skip to content

Commit

Permalink
+ support for su and whoami command (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Vity01 authored Feb 1, 2018
1 parent 7972a45 commit a37b1e7
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ There are 3 possible usecases:
- It's working on both Windows/Linux Hadoop 2.6.0

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

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


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.security.UserGroupInformation;
import org.springframework.shell.core.CommandMarker;
import org.springframework.shell.core.annotation.CliAvailabilityIndicator;
import org.springframework.shell.core.annotation.CliCommand;
Expand All @@ -12,14 +13,15 @@
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.util.Arrays;

@SuppressWarnings("SameParameterValue")
@Component
public class ContextCommands implements CommandMarker {

private volatile String currentDir;
private volatile Configuration configuration;
private volatile String homeDir;
private String currentDir;
private Configuration configuration;
private String homeDir;

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

@CliCommand(value = "su", help = "Changes current active user [*experimental*]")
public synchronized String su(@CliOption(key = {""}, help = "su [<username>]") String newUser) throws IOException {
if (StringUtils.isEmpty(newUser)) {
return "No username is defined! ";
}
// else {
// newUser = BashUtils.parseArguments(newUser)[0];
// }
final FileSystem fs = getFileSystem();
final Path usersDir = new Path("/user");
if (fs.exists(usersDir)) {
final String finalNewUser = newUser;
final boolean foundUser = Arrays.stream(fs.listStatus(usersDir)).
filter(FileStatus::isDirectory).
anyMatch(fileStatus -> fileStatus.getPath().getName().equals(finalNewUser));
if (!foundUser) {
return "User " + newUser + " does not exist!";
}
}
System.setProperty("HADOOP_USER_NAME", newUser);
UserGroupInformation.loginUserFromSubject(null);
currentDir = null;
return "";
}

@CliCommand(value = "whoami", help = "Print effective username")
public synchronized String whoami() throws IOException {
return UserGroupInformation.getCurrentUser().getUserName();
}

public synchronized String getCurrentDir() {
if (currentDir == null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public EditCommands(HadoopDfsCommands hadoopDfsCommands, ContextCommands context

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

private Path getFilePathForEdit(@CliOption(key = {"_raw_"}, help = "File to edit") String path) {
private Path getFilePathForEdit(@CliOption(key = {""}, help = "File to edit") String path) {
Path p;
if (!path.startsWith("/")) {
p = new Path(contextCommands.getCurrentDir(), path);
Expand Down

0 comments on commit a37b1e7

Please sign in to comment.