-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from Vlatombe/JENKINS-40533
[JENKINS-40533] Allow definition of sandboxed libraries at global scope
- Loading branch information
Showing
11 changed files
with
364 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
src/main/java/org/jenkinsci/plugins/workflow/libs/AbstractGlobalLibraries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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 org.jenkinsci.plugins.workflow.libs; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.model.ItemGroup; | ||
import hudson.model.Job; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
import jenkins.model.GlobalConfiguration; | ||
import jenkins.model.Jenkins; | ||
import net.sf.json.JSONObject; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
/** | ||
* Common code between {@link GlobalLibraries} and {@link GlobalUntrustedLibraries}. | ||
*/ | ||
public abstract class AbstractGlobalLibraries extends GlobalConfiguration { | ||
private List<LibraryConfiguration> libraries = new ArrayList<>(); | ||
|
||
protected AbstractGlobalLibraries() { | ||
load(); | ||
} | ||
|
||
public abstract String getDescription(); | ||
|
||
public List<LibraryConfiguration> getLibraries() { | ||
return libraries; | ||
} | ||
|
||
public void setLibraries(List<LibraryConfiguration> libraries) { | ||
this.libraries = libraries; | ||
save(); | ||
} | ||
|
||
@Override public boolean configure(StaplerRequest req, JSONObject json) throws FormException { | ||
if (Jenkins.get().hasPermission(getRequiredGlobalConfigPagePermission())) { | ||
setLibraries(Collections.emptyList()); // allow last library to be deleted | ||
return super.configure(req, json); | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
abstract static class AbstractForJob extends LibraryResolver { | ||
@NonNull | ||
protected abstract AbstractGlobalLibraries getConfiguration(); | ||
|
||
@NonNull @Override public final Collection<LibraryConfiguration> forJob(@NonNull Job<?,?> job, @NonNull Map<String,String> libraryVersions) { | ||
return getLibraries(); | ||
} | ||
|
||
@NonNull @Override public final Collection<LibraryConfiguration> fromConfiguration(@NonNull StaplerRequest request) { | ||
if (Jenkins.get().hasPermission(getConfiguration().getRequiredGlobalConfigPagePermission())) { | ||
return getLibraries(); | ||
} | ||
return Collections.emptySet(); | ||
} | ||
|
||
@NonNull @Override public final Collection<LibraryConfiguration> suggestedConfigurations(@NonNull ItemGroup<?> group) { | ||
return getLibraries(); | ||
} | ||
|
||
private List<LibraryConfiguration> getLibraries() { | ||
return getConfiguration() | ||
.getLibraries() | ||
.stream() | ||
.map(this::mayWrapLibrary) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@NonNull | ||
protected abstract LibraryConfiguration mayWrapLibrary(@NonNull LibraryConfiguration library); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/main/java/org/jenkinsci/plugins/workflow/libs/GlobalUntrustedLibraries.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright 2024 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 org.jenkinsci.plugins.workflow.libs; | ||
|
||
import edu.umd.cs.findbugs.annotations.NonNull; | ||
import hudson.Extension; | ||
import hudson.ExtensionList; | ||
import hudson.security.Permission; | ||
import jenkins.model.Jenkins; | ||
|
||
/** | ||
* Manages untrusted libraries available to any job in the system. | ||
*/ | ||
@Extension public class GlobalUntrustedLibraries extends AbstractGlobalLibraries { | ||
|
||
public GlobalUntrustedLibraries() { | ||
super(); | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return Messages.GlobalUntrustedLibraries_Description(); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public String getDisplayName() { | ||
return Messages.GlobalUntrustedLibraries_DisplayName(); | ||
} | ||
|
||
public static @NonNull GlobalUntrustedLibraries get() { | ||
return ExtensionList.lookupSingleton(GlobalUntrustedLibraries.class); | ||
} | ||
|
||
@NonNull | ||
@Override | ||
public Permission getRequiredGlobalConfigPagePermission() { | ||
return Jenkins.MANAGE; | ||
} | ||
|
||
@Extension(ordinal=1) public static class ForJob extends AbstractForJob { | ||
@NonNull | ||
protected GlobalUntrustedLibraries getConfiguration() { | ||
return get(); | ||
} | ||
|
||
@Override | ||
public boolean isTrusted() { | ||
return false; | ||
} | ||
|
||
@NonNull | ||
@Override | ||
protected LibraryConfiguration mayWrapLibrary(@NonNull LibraryConfiguration library) { | ||
return new ResolvedLibraryConfiguration(library, getClass().getName()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 0 additions & 25 deletions
25
src/main/resources/org/jenkinsci/plugins/workflow/libs/GlobalLibraries/config.properties
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.