Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JENKINS-62443 Add Full Name lookup for SIDs #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.jenkins.plugins.folderauth.roles;

import hudson.model.User;
import hudson.security.SecurityRealm;
import io.jenkins.plugins.folderauth.misc.PermissionWrapper;
import jenkins.model.Jenkins;
import org.acegisecurity.userdetails.UsernameNotFoundException;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.springframework.dao.DataAccessException;

import javax.annotation.Nonnull;
import java.util.Collections;
Expand Down Expand Up @@ -113,7 +118,21 @@ public Set<String> getSids() {
@Nonnull
@SuppressWarnings("unused") // used by index.jelly
public String getSidsCommaSeparated() {
String string = new TreeSet<>(sids).toString();
return string.substring(1, string.length() - 1);
Jenkins jenkinsInstance = Jenkins.get();
SecurityRealm sr = jenkinsInstance.getSecurityRealm();
StringBuilder sb = new StringBuilder();
for (String sid: new TreeSet<>(sids)) {
try {
sr.loadUserByUsername(sid);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a large number of sids this looks like it can be potentially very slow (if it requires and LDAP lookup). Which IIUC will block the page from loading.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1. Also, this logi will lead to exceptions when handling group membership SIDs

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll look at it again once #58 is done

User u = User.getById(sid, false);
String fullName = u.getFullName();
sb.append(sid).append("(").append(fullName).append("), ");
} catch (UsernameNotFoundException | DataAccessException | NullPointerException e) {
// on any exception just add the sid
// this could happen either because SID lookup error or no FullName set
sb.append(sid).append(", ");
}
}
return sb.length()==0?"":sb.substring(0,sb.length()-2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.AuthorizationStrategy;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import io.jenkins.plugins.folderauth.roles.FolderRole;
Expand All @@ -21,6 +22,7 @@
import java.util.HashSet;

import static io.jenkins.plugins.folderauth.misc.PermissionWrapper.wrapPermissions;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;

Expand Down Expand Up @@ -65,6 +67,7 @@ public void setUp() throws Exception {
ImmutableSet.of("root")));
FolderAuthorizationStrategyAPI.assignSidToFolderRole("user1", "folderRole1");
FolderAuthorizationStrategyAPI.assignSidToFolderRole("user2", "folderRole1");
FolderAuthorizationStrategyAPI.assignSidToFolderRole("anonymous", "folderRole1");

FolderAuthorizationStrategyAPI.addFolderRole(new FolderRole("folderRole2", wrapPermissions(Item.CONFIGURE, Item.DELETE),
ImmutableSet.of("root/child1")));
Expand Down Expand Up @@ -93,6 +96,7 @@ public void setUp() throws Exception {
admin = User.getById("admin", true);
user1 = User.getById("user1", true);
user2 = User.getById("user2", true);
user2.setFullName("Test User2");
}

@Test
Expand Down Expand Up @@ -127,4 +131,19 @@ public void permissionTest() {
assertFalse(job1.hasPermission(Item.CONFIGURE));
}
}

@Test
public void SIDToFullNameLookupTest() {
Jenkins jenkins = jenkinsRule.jenkins;

AuthorizationStrategy strategy = jenkins.getAuthorizationStrategy();
if (strategy instanceof FolderBasedAuthorizationStrategy) {
FolderBasedAuthorizationStrategy actualStrategy = (FolderBasedAuthorizationStrategy) strategy;
for (FolderRole role: actualStrategy.getFolderRoles()) {
if (role.getName().equals("folderRole1")) {
assertEquals("anonymous, user1(user1), user2(Test User2)",role.getSidsCommaSeparated());
}
}
}
}
}