Skip to content

Commit

Permalink
Merge branch 'security-stable-1.651' into security-stable-2.7
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Jan 24, 2017
2 parents c29b7bb + 71014c5 commit 2223a6a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
3 changes: 2 additions & 1 deletion core/src/main/java/hudson/slaves/OfflineCause.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import hudson.model.Computer;
import hudson.model.User;

import jenkins.model.Jenkins;
import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.export.Exported;
Expand Down Expand Up @@ -147,7 +148,7 @@ public UserCause(@CheckForNull User user, @CheckForNull String message) {
}

private UserCause(String userId, String message) {
super(hudson.slaves.Messages._SlaveComputer_DisconnectedBy(userId, message));
super(hudson.slaves.Messages._SlaveComputer_DisconnectedBy(userId != null ? userId : Jenkins.ANONYMOUS.getName(), message));
this.userId = userId;
}

Expand Down
39 changes: 39 additions & 0 deletions test/src/test/java/hudson/model/NodeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,15 @@
import java.util.concurrent.Callable;

import jenkins.model.Jenkins;
import jenkins.security.NotReallyRoleSensitiveCallable;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContextHolder;

import static org.hamcrest.core.StringEndsWith.endsWith;
import static org.junit.Assert.*;

import org.jenkinsci.remoting.RoleChecker;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -123,6 +128,40 @@ public void testOfflineCause() throws Exception {
assertNull(computer.getOfflineCause());
}

@Test
public void testOfflineCauseAsAnonymous() throws Exception {
Node node = j.createOnlineSlave();
final Computer computer = node.toComputer();
OfflineCause.UserCause cause;
ACL.impersonate(Jenkins.ANONYMOUS, new NotReallyRoleSensitiveCallable() {
@Override
public Void call() throws Exception {
computer.doToggleOffline("original message");
return null;
}
});

cause = (UserCause) computer.getOfflineCause();
assertThat(cause.toString(), endsWith("Disconnected by anonymous : original message"));
assertEquals(User.getUnknown(), cause.getUser());


final User root = User.get("root@localhost");
ACL.impersonate(root.impersonate(), new NotReallyRoleSensitiveCallable() {
@Override
public Void call() throws Exception {
computer.doChangeOfflineCause("new message");
return null;
}
});
cause = (UserCause) computer.getOfflineCause();
assertThat(cause.toString(), endsWith("Disconnected by root@localhost : new message"));
assertEquals(root, cause.getUser());

computer.doToggleOffline(null);
assertNull(computer.getOfflineCause());
}

@Test
public void testGetLabelCloud() throws Exception {
Node node = j.createOnlineSlave();
Expand Down

0 comments on commit 2223a6a

Please sign in to comment.