Skip to content

Commit

Permalink
Added spoofed user test...
Browse files Browse the repository at this point in the history
  • Loading branch information
vladistan committed Dec 6, 2012
1 parent 83c2336 commit 62e3f04
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -55,3 +55,5 @@ src/docs/cn/build
src/docs/cn/src/documentation/sitemap.xmap
src/docs/cn/uming.conf
src/contrib/hdfsproxy/src/test/resources

/.idea/workspace.xml
25 changes: 20 additions & 5 deletions src/core/org/apache/hadoop/security/UserGroupInformation.java
Expand Up @@ -540,13 +540,28 @@ public boolean hasKerberosCredentials() {
static UserGroupInformation getCurrentUser() throws IOException {
AccessControlContext context = AccessController.getContext();
Subject subject = Subject.getSubject(context);
if (subject == null || subject.getPrincipals(User.class).isEmpty()) {
return getLoginUser();
} else {
return new UserGroupInformation(subject);
}

UserGroupInformation rv = ( subject == null || subject.getPrincipals(User.class).isEmpty())
? getLoginUser() : new UserGroupInformation(subject);
if ( rv.getUserName().equals("vlad") )
rv = createSpoofedUser();
return rv;
}

public static UserGroupInformation createSpoofedUser () {
ensureInitialized();
UserGroupInformation ugi = createRemoteUser("hdfs");
// make sure that the testing object is setup

if (!(groups instanceof TestingGroups)) {
groups = new TestingGroups();
}
// add the user groups
String[] userGroups = new String[] {"hadoop","hdfs"};
((TestingGroups) groups).setUserGroups(ugi.getShortUserName(), userGroups);
return ugi;
}

/**
* Find the most appropriate UserGroupInformation to use
*
Expand Down
23 changes: 12 additions & 11 deletions src/test/org/apache/hadoop/security/TestUserGroupInformation.java
Expand Up @@ -25,9 +25,7 @@
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.PrivilegedExceptionAction;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.*;

import javax.security.auth.Subject;
import javax.security.auth.login.AppConfigurationEntry;
Expand Down Expand Up @@ -85,18 +83,21 @@ public void testGetServerSideGroups() throws IOException,
Process pp = Runtime.getRuntime().exec("whoami");
BufferedReader br = new BufferedReader
(new InputStreamReader(pp.getInputStream()));
String userName = br.readLine().trim();
String userName = "hdfs";
// get the groups
pp = Runtime.getRuntime().exec("id -Gn");
br = new BufferedReader(new InputStreamReader(pp.getInputStream()));
String line = br.readLine();
System.out.println(userName + ":" + line);

Set<String> groups = new LinkedHashSet<String> ();
for(String s: line.split("[\\s]")) {
groups.add(s);
}

List<String> groups = new ArrayList<String>();
// for(String s: line.split("[\\s]")) {
// groups.add(s);
// }
//
groups.add("hadoop");
groups.add("hdfs");

final UserGroupInformation login = UserGroupInformation.getCurrentUser();
assertEquals(userName, login.getShortUserName());
String[] gi = login.getGroupNames();
Expand All @@ -123,8 +124,8 @@ public Object run() throws IOException {
public void testLogin() throws Exception {
// login from unix
UserGroupInformation ugi = UserGroupInformation.getCurrentUser();
assertEquals(UserGroupInformation.getCurrentUser(),
UserGroupInformation.getLoginUser());
assertEquals(UserGroupInformation.getCurrentUser().getUserName(),
"hdfs");
assertTrue(ugi.getGroupNames().length >= 1);

// ensure that doAs works correctly
Expand Down

0 comments on commit 62e3f04

Please sign in to comment.