Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mawinter69 authored and daniel-beck committed Mar 15, 2023
1 parent 2872c41 commit 850a20a
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>role-strategy</artifactId>
<version>${changelist}</version>
<version>587.${changelist}</version>
<packaging>hpi</packaging>

<name>Role-based Authorization Strategy</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public class RoleMap {

private static final Logger LOGGER = Logger.getLogger(RoleMap.class.getName());

private static final ConcurrentMap<Permission, Set<Permission>> implyingPermissionCache = new ConcurrentHashMap<>();
private static final Cache<Permission, Set<Permission>> implyingPermissionCache = Caffeine.newBuilder().maximumSize(100)
.expireAfterWrite(20, TimeUnit.SECONDS).build();

static {
Permission.getAll().forEach(RoleMap::cacheImplyingPermissions);
Expand Down Expand Up @@ -185,12 +186,7 @@ public void perform(Role current) {
* @return set of permissions which imply {@code p}
*/
private static Set<Permission> getImplyingPermissions(Permission p) {
Set<Permission> implyingPermissions = implyingPermissionCache.get(p);
if (implyingPermissions != null) {
return implyingPermissions;
} else {
return cacheImplyingPermissions(p);
}
return implyingPermissionCache.get(p, RoleMap::cacheImplyingPermissions);
}

/**
Expand All @@ -211,11 +207,13 @@ private static Set<Permission> cacheImplyingPermissions(Permission permission) {

// Get the implying permissions
for (Permission p = permission; p != null; p = p.impliedBy) {
if (!p.getEnabled()) {
continue;
}
implyingPermissions.add(p);
}
}

implyingPermissionCache.put(permission, implyingPermissions);
return implyingPermissions;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.michelin.cio.hudson.plugins.rolestrategy;

import static org.junit.Assert.assertFalse;

import hudson.model.Item;
import hudson.model.Job;
import hudson.model.User;
import hudson.security.ACL;
import hudson.security.ACLContext;
import hudson.security.HudsonPrivateSecurityRealm;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import jenkins.model.Jenkins;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class GrantingDisabledPermissionTest {

@Rule
public JenkinsRule r = new JenkinsRule();

@Test
public void grantDisabledPermissionTest() throws Exception {
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(true, false, null);
realm.createAccount("admin", "admin");
realm.createAccount("alice", "alice");
r.jenkins.setSecurityRealm(realm);

RoleMap roleMap = new RoleMap();
Role adminRole = new Role("admin-role", new HashSet<>(Collections.singletonList(Jenkins.ADMINISTER)));
roleMap.addRole(adminRole);
Role manage = new Role("manage-role", new HashSet<>(Collections.singletonList(Jenkins.MANAGE)));
roleMap.addRole(manage);
roleMap.assignRole(adminRole, "admin");
roleMap.assignRole(manage, "alice");

Map<String, RoleMap> constructorArg = new HashMap<>();
constructorArg.put("globalRoles", roleMap);

r.jenkins.setAuthorizationStrategy(new RoleBasedAuthorizationStrategy(constructorArg));

try (ACLContext ctx = ACL.as2(User.get("alice").impersonate2())) {
assertFalse(Jenkins.get().hasPermission(Jenkins.MANAGE));
}
}
}

0 comments on commit 850a20a

Please sign in to comment.