下面列出了怎么用hudson.model.Hudson的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void shouldFailForAnonymousRead() throws IOException {
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(false);
realm.createAccount("alice","alice");
j.jenkins.setSecurityRealm(realm);
GlobalMatrixAuthorizationStrategy as = new GlobalMatrixAuthorizationStrategy();
j.jenkins.setAuthorizationStrategy(as);
as.add(Hudson.READ,"alice");
Map resp = new RequestBuilder(baseUrl)
.status(403)
.get("/users/")
.build(Map.class);
assertEquals(403, resp.get("code"));
}
@Test
public void shouldSucceedForAnonymousRead() throws IOException {
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(false);
realm.createAccount("alice","alice");
j.jenkins.setSecurityRealm(realm);
GlobalMatrixAuthorizationStrategy as = new GlobalMatrixAuthorizationStrategy();
j.jenkins.setAuthorizationStrategy(as);
as.add(Hudson.READ,"anonymous");
List resp = new RequestBuilder(baseUrl)
.status(200)
.get("/users/")
.build(List.class);
assertEquals(1, resp.size());
}
@Test
public void shouldFailForUnauthorizedUser() throws IOException, UnirestException {
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(false);
realm.createAccount("alice","alice");
realm.createAccount("bob","bob");
j.jenkins.setSecurityRealm(realm);
GlobalMatrixAuthorizationStrategy as = new GlobalMatrixAuthorizationStrategy();
j.jenkins.setAuthorizationStrategy(as);
as.add(Hudson.READ,"alice");
Map resp = new RequestBuilder(baseUrl)
.status(403)
.auth("bob", "bob")
.get("/users/")
.build(Map.class);
assertEquals(403, resp.get("code"));
}
private void launchInstance() throws Exception {
ImmutablePair<Server, ServletContext> results = JenkinsRule._createWebServer(contextPath, localPort::setValue,
getClass().getClassLoader(), localPort.getValue(), JenkinsRule::_configureUserRealm);
server = results.left;
ServletContext webServer = results.right;
jenkins = new Hudson(temporaryDirectoryAllocator.allocate(), webServer);
JenkinsRule._configureJenkinsForTest(jenkins);
JenkinsRule._configureUpdateCenter(jenkins);
jenkins.getActions().add(this);
String url = Objects.requireNonNull(getJenkinsURL()).toString();
Objects.requireNonNull(JenkinsLocationConfiguration.get()).setUrl(url);
LOGGER.log(Level.INFO, "Running on {0}", url);
}
/**
* {@inheritDoc}
*/
@Override
public String getAvatarImageOf(String size) {
if (avatar == null) {
// fall back to the generic github org icon
String image = avatarIconClassNameImageOf(getAvatarIconClassName(), size);
return image != null
? image
: (Stapler.getCurrentRequest().getContextPath() + Hudson.RESOURCE_PATH
+ "/plugin/github-branch-source/images/" + size + "/github-logo.png");
} else {
String[] xy = size.split("x");
if (xy.length == 0) return avatar;
if (avatar.contains("?")) return avatar + "&s=" + xy[0];
else return avatar + "?s=" + xy[0];
}
}
@Nonnull
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // used by index.jelly
public Set<Permission> getFolderPermissions() {
HashSet<PermissionGroup> groups = new HashSet<>(PermissionGroup.getAll());
groups.remove(PermissionGroup.get(Hudson.class));
groups.remove(PermissionGroup.get(Computer.class));
groups.remove(PermissionGroup.get(Permission.class));
return getSafePermissions(groups);
}
@Nonnull
@Restricted(NoExternalUse.class)
@SuppressWarnings("unused") // used by index.jelly
public Set<Permission> getAgentPermissions() {
HashSet<PermissionGroup> groups = new HashSet<>(PermissionGroup.getAll());
groups.remove(PermissionGroup.get(Run.class));
groups.remove(PermissionGroup.get(SCM.class));
groups.remove(PermissionGroup.get(View.class));
groups.remove(PermissionGroup.get(Item.class));
groups.remove(PermissionGroup.get(Hudson.class));
groups.remove(PermissionGroup.get(Permission.class));
return getSafePermissions(groups);
}
/**
* Uses matrix-auth to provide artifacts permission.
*
* If hudson.security.ArtifactsPermission is set then the user must have Run.ARTIFACTS set.
*
* @throws Exception
*/
@Issue("SECURITY-564")
@Test
public void testArtifactsWithPermissions() throws Exception {
String JOB_NAME = "artifactPermissions";
String artifactPath = "a/b/c";
HudsonPrivateSecurityRealm realm = new HudsonPrivateSecurityRealm(false);
realm.createAccount("alice","alice");
realm.createAccount("bob","bob");
j.jenkins.setSecurityRealm(realm);
GlobalMatrixAuthorizationStrategy as = new GlobalMatrixAuthorizationStrategy();
j.jenkins.setAuthorizationStrategy(as);
as.add(Hudson.READ,"alice");
as.add(Item.READ,"alice");
as.add(Run.ARTIFACTS,"alice");
as.add(Hudson.READ,"bob");
as.add(Item.READ,"bob");
FreeStyleProject p = j.createFreeStyleProject(JOB_NAME);
p.getBuildersList().add(new ArtifactBuilder(artifactPath, 100));
p.getPublishersList().add(new ArtifactArchiver("**/*"));
Run r = p.scheduleBuild2(0).waitForStart();
r = j.waitForCompletion(r);
List artifacts = request().authAlice().get("/organizations/jenkins/pipelines/"+JOB_NAME+"/runs/"+r.getId()+"/artifacts").build(List.class);
Assert.assertEquals(100, artifacts.size());
Assert.assertEquals(0, ((Map) artifacts.get(0)).get("size"));
Assert.assertEquals(artifactPath + "/0.txt", ((Map) artifacts.get(0)).get("path"));
Assert.assertEquals("/job/artifactPermissions/1/artifact/"+ artifactPath +"/0.txt", ((Map) artifacts.get(0)).get("url"));
List artifactsBob = request().auth("bob", "bob").get("/organizations/jenkins/pipelines/"+JOB_NAME+"/runs/"+r.getId()+"/artifacts").build(List.class);
Assert.assertEquals(0, artifactsBob.size());
}
/**
* Creates a new instance of {@link jenkins.model.Jenkins}. If the derived class wants to create it in a different way,
* you can override it.
*/
protected Hudson newHudson() throws Exception {
jettyLevel(Level.WARNING);
ServletContext webServer = createWebServer();
File home = homeLoader.allocate();
for (JenkinsRecipe.Runner r : recipes)
r.decorateHome(this,home);
try {
return new Hudson(home, webServer, getPluginManager());
} catch (InterruptedException x) {
throw new AssumptionViolatedException("Jenkins startup interrupted", x);
} finally {
jettyLevel(Level.INFO);
}
}
@DataBoundConstructor
public EnvDashboardView(final String name, final String envOrder, final String compOrder,
final String deployHistory) {
super(name, Hudson.getInstance());
this.envOrder = envOrder;
this.compOrder = compOrder;
this.deployHistory = deployHistory;
}
@Override
public Item doCreateItem(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
return Hudson.getInstance().doCreateItem(req, rsp);
}
public String getImageOf(final String size) {
return Stapler.getCurrentRequest().getContextPath() + Hudson.RESOURCE_PATH + "/images/" + size + "/folder.png";
}
public Descriptor<OrganizationGravatarIcon> getDescriptor() {
return Hudson.getInstance().getDescriptorOrDie(getClass());
}
public static DescriptorExtensionList<OrganizationGravatarIcon, DescriptorImpl> all() {
return Hudson.getInstance().<OrganizationGravatarIcon, DescriptorImpl>getDescriptorList(OrganizationGravatarIcon.class);
}
@Override
public TopLevelItemDescriptor getDescriptor() {
return (DescriptorImpl) Hudson.getInstance().getDescriptorOrDie(getClass());
}
public DynamicProject createProject(final Class<DynamicProject> type, final String projectName) throws IOException {
return type.cast(createProject((TopLevelItemDescriptor) Hudson.getInstance().getDescriptor(type), projectName));
}