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

[JEP-224] System read permissions #27

Merged
merged 3 commits into from
Jun 19, 2020
Merged
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
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<properties>
<jenkins.version>2.176.4</jenkins.version>
<java.level>8</java.level>
<useBeta>true</useBeta>
aheritier marked this conversation as resolved.
Show resolved Hide resolved
</properties>

<licenses>
Expand Down Expand Up @@ -97,6 +98,14 @@
</pluginRepository>
</pluginRepositories>

<dependencies>
<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>extended-read-permission</artifactId>
<version>3.2</version>
</dependency>
</dependencies>

<build>
<pluginManagement>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class QuickDiskUsageInitializer {
*/
@Initializer(after = InitMilestone.JOB_LOADED)
public static void initialize() {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
QuickDiskUsagePlugin plugin = jenkins.getPlugin(QuickDiskUsagePlugin.class);
if (plugin == null) return;
plugin.refreshDataOnStartup();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import hudson.Extension;
import hudson.model.ManagementLink;
import hudson.plugins.extendedread.SystemReadPermission;
import hudson.security.Permission;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.StaplerProxy;

Expand All @@ -50,6 +52,12 @@ public String getUrlName() {
return "disk-usage-simple";
}

@Override
public Permission getRequiredPermission() {
// replace with Jenkins.SYSTEM_READ after baseline >= 2.222
return SystemReadPermission.SYSTEM_READ;
}

/**
* Name of the category for this management link. Exists so that plugins with core dependency pre-dating the version
* when this was introduced can define a category.
Expand All @@ -68,8 +76,8 @@ public String getCategoryName() {
*/
@Override
public Object getTarget() {
Jenkins jenkins = Jenkins.getInstance();
jenkins.checkPermission(Jenkins.ADMINISTER);
Jenkins jenkins = Jenkins.get();
jenkins.checkPermission(SystemReadPermission.SYSTEM_READ);
return jenkins.getPlugin(QuickDiskUsagePlugin.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void doRefresh(StaplerRequest req, StaplerResponse res) throws IOExceptio

@RequirePOST
public void doClean(StaplerRequest req, StaplerResponse res) throws IOException, ServletException {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
final Job job = jenkins.getItemByFullName(req.getParameter("job"), Job.class);
Timer.get().submit(new Runnable() {
@Override
Expand Down Expand Up @@ -188,7 +188,7 @@ public void onCompleted(Path dir, long usage) {
}

private void registerJobs(UsageComputation uc) throws IOException, InterruptedException {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();

// Remove useless entries for jobs
for (JobDiskItem item : jobsUsages) {
Expand All @@ -206,7 +206,7 @@ private void registerJobs(UsageComputation uc) throws IOException, InterruptedEx
}

private void registerDirectories(UsageComputation uc) throws IOException, InterruptedException {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
Map<File, String> directoriesToProcess = new HashMap<>();
// Display JENKINS_HOME size
directoriesToProcess.put(jenkins.getRootDir(), "JENKINS_HOME");
Expand Down Expand Up @@ -249,7 +249,7 @@ public void run() {
logger.info("Re-estimating disk usage");
progress.set(0);
lastRunStart = System.currentTimeMillis();
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
try (ACLContext old = ACL.as(ACL.SYSTEM)) {
UsageComputation uc = new UsageComputation(Arrays.asList(Paths.get(System.getProperty("java.io.tmpdir")), jenkins.getRootDir().toPath()));
registerJobs(uc);
Expand All @@ -273,7 +273,7 @@ public void run() {

private transient final Runnable computeDiskUsageOnStartup = new Runnable() {
public void run() {
Jenkins jenkins = Jenkins.getInstance();
Jenkins jenkins = Jenkins.get();
while (jenkins.getInitLevel() != InitMilestone.COMPLETED) {
try {
logger.log(Level.INFO, "Waiting for Jenkins to be up before computing disk usage");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) th
// this is to speed up the FS freeze operation which is otherwise slowed down
if (System.currentTimeMillis() - writableLastCheckTime.get() > 10000) {
writableLastCheckTime.set(System.currentTimeMillis());
FilePath jenkinsHome = Jenkins.getInstance().getRootPath();
FilePath jenkinsHome = Jenkins.get().getRootPath();
try {
jenkinsHome.touch(System.currentTimeMillis());
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout">
<l:layout title="${%Disk usage}" permission="${app.ADMINISTER}">
<l:layout title="${%Disk usage}" permission="${app.SYSTEM_READ}">
<l:breadcrumb title="${%Disk usage}" href="./directories"/>
<st:include page="sidepanel.jelly" />
<l:main-panel>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:l="/lib/layout" xmlns:s="/lib/form">
<l:layout title="${%Disk usage}" permission="${app.ADMINISTER}">
<l:layout title="${%Disk usage}" permission="${app.SYSTEM_READ}">
<l:breadcrumb title="${%Disk usage}" href="."/>
<st:include page="sidepanel.jelly" />
<l:main-panel>
Expand Down