Jenkins has a security mechanism in place so that the administrator of Jenkins can control who gets access to what part of Jenkins.
The key components of this mechanism are the followings:
-
Permission, which represents an activity that requires a security privilege.
This is usually a verb, like "configure", "administer", "tag", etc.
-
Authentication
, which represents the current user and roles (AKA groups) he/she has.
When a thread runs in Jenkins, it always carry an Authentication
object implicitly, which represents the user that the thread is serving. (If a thread is a part of Jenkins and not serving any user request, like Executor{
}s, then it carries an almighty "system" Authentication
object.)
-
ACL, which decides whether the Authentication
object carried by the current thread has the given permission or not.
-
AccessControlled, which is implemented by an object who owns ACL.
So the overall picture is this; various objects in Jenkins (such as Job, Jenkins, User, View, etc.) are AccessControlled objects, and therefore they own ACLs.
The code is then written in such a way that before a security-sensitive operation is performed, it checks ACL.
For example, the following code is taken from the Jenkins class, which lets you shut down the JVM by requesting /exit
.
You can easily imagine that in a security sensitive environment you don’t want random users to invoke this, so it makes sure that the caller has the "ADMINISTER" permission of the system before proceeding to do the work:
public void doExit( StaplerRequest req, StaplerResponse rsp ) throws IOException {
checkPermission(ADMINISTER); (1)
LOGGER.severe(String.format("Shutting down VM as requested by %s from %s",
getAuthentication().getName(), req!=null?req.getRemoteAddr():"???"));
if (rsp!=null) {
rsp.setStatus(HttpServletResponse.SC_OK);
rsp.setContentType("text/plain");
try (PrintWriter w = rsp.getWriter()) {
w.println("Shutting down");
}
}
System.exit(0);
}
1 |
This throws an exception if the user accessing this URL doesn’t have Administer permission. |
If the administrator configured no security mechanism, the checkPermission method simply becomes no-op.
The administrator could configure matrix-based ACL, in which case every AccessControlled
object will share the single ACL (whose contents is controlled by the configuration done by the administrator.) In more elaborate case, each AccessControlled
object might have different ACLs.
In all cases, this is the code you need to write.