-
Notifications
You must be signed in to change notification settings - Fork 83
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-46795] TrustworthyBuild
extension point
#180
base: master
Are you sure you want to change the base?
Changes from 9 commits
23aadee
4d9ad1a
6e7c68f
ebe3d6f
57bfecc
7e98857
013950c
e5707b7
7ea44f8
f6ec0d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2022 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package jenkins.scm.api; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.ExtensionPoint; | ||
import hudson.model.Run; | ||
import hudson.model.TaskListener; | ||
import hudson.triggers.SCMTrigger; | ||
import hudson.triggers.TimerTrigger; | ||
|
||
/** | ||
* Allows plugins to declare that builds were triggered deliberately. | ||
* This allows an authorized user to run CI on (say) a pull request filed by an outsider, | ||
* having confirmed that there is nothing malicious about the contents. | ||
* @see SCMSource#getTrustedRevisionForBuild | ||
*/ | ||
public interface TrustworthyBuild extends ExtensionPoint { | ||
|
||
/** | ||
* Should this build be trusted to load sensitive source files? | ||
* If any implementation returns true then it is trusted. | ||
* Examples of build-triggering causes which should <em>not</em> be trusted include: | ||
* <ul> | ||
* <li>{@link TimerTrigger.TimerTriggerCause} | ||
* <li>{@link SCMTrigger.SCMTriggerCause} | ||
* <li>{@code BranchIndexingCause} | ||
* <li>{@code BranchEventCause} | ||
* </ul> | ||
*/ | ||
boolean shouldBeTrusted(@NonNull Run<?, ?> build, @NonNull TaskListener listener); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2022 CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package jenkins.scm.impl; | ||
|
||
import hudson.Extension; | ||
import hudson.model.Cause; | ||
import hudson.model.Item; | ||
import hudson.model.Run; | ||
import hudson.model.User; | ||
import jenkins.scm.api.TrustworthyBuild; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
|
||
public class TrustworthyBuilds { | ||
|
||
// Also effectively handles ReplayCause since that is only ever added in conjunction with UserIdCause. (see ReplayAction.run2) | ||
@Extension | ||
public static TrustworthyBuild byUserId() { | ||
return (build, listener) -> { | ||
var cause = build.getCause(Cause.UserIdCause.class); | ||
if (cause == null) { | ||
// probably some other cause; do not print anything | ||
return false; | ||
} | ||
var userId = cause.getUserId(); | ||
if (userId == null) { | ||
listener.getLogger().println("Not trusting build since no user name was recorded"); | ||
return false; | ||
} | ||
var user = User.getById(userId, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could pass |
||
if (user == null) { | ||
listener.getLogger().printf("Not trusting build since no user ‘%s’ is known%n", userId); | ||
return false; | ||
} | ||
try { | ||
var permission = Run.PERMISSIONS.find("Replay"); // ReplayAction.REPLAY | ||
if (permission == null) { // no workflow-cps | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could be defined more modularly as an extension in In practice the callers of |
||
permission = Item.CONFIGURE; | ||
} | ||
if (build.hasPermission2(user.impersonate2(), permission)) { | ||
listener.getLogger().printf("Trusting build since it was started by user ‘%s’%n", userId); | ||
return true; | ||
} else { | ||
listener.getLogger().printf("Not trusting build since user ‘%s’ lacks %s/%s permission%n", userId, permission.group.title, permission.name); | ||
return false; | ||
} | ||
} catch (UsernameNotFoundException x) { | ||
listener.getLogger().printf("Not trusting build since user ‘%s’ is invalid%n", userId); | ||
return false; | ||
} | ||
}; | ||
} | ||
|
||
// TODO until github-checks can declare a dep on a sufficiently new scm-api | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
@Extension | ||
public static TrustworthyBuild byGitHubChecks() { | ||
return (build, listener) -> { | ||
for (Cause cause : build.getCauses()) { | ||
if (cause.getClass().getName().equals("io.jenkins.plugins.checks.github.CheckRunGHEventSubscriber$GitHubChecksRerunActionCause")) { | ||
listener.getLogger().println("Trusting build since it was a rerun request through GitHub checks API"); | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
} | ||
|
||
private TrustworthyBuilds() {} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While it should be rare, there can be multiple causes of the same type for a build that aren't collapsed into one entry. Whether a build is approved or not could depend on the (insertion) order of the
CauseAction#causeBag
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The API does not prevent it but this ought never happen—it is the responsible of code triggering the build to pass at most one
Cause
of any given type. At worst a build is not considered trusted when it could have been, so this does not seem like a problem in practice.