use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class CopyArtifactTest method testPermissionWhenParameterized.
/**
* When the source project name is parameterized, cannot check at configure time whether
* the project is accessible. In this case, permission check is done when the build runs.
* Only jobs accessible to all authenticated users are allowed.
*/
@Test
public void testPermissionWhenParameterized() throws Exception {
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
MockAuthorizationStrategy auth = new MockAuthorizationStrategy().grant(Jenkins.READ).onRoot().toEveryone();
rule.jenkins.setAuthorizationStrategy(auth);
FreeStyleProject testJob = rule.createFreeStyleProject("testJob");
auth.grant(Item.READ).onItems(testJob).to("joe");
testJob.getBuildersList().add(new FileWriteBuilder("foo.txt", "bar"));
testJob.getPublishersList().add(new ArtifactArchiver("*.txt"));
rule.assertBuildStatusSuccess(testJob.scheduleBuild2(0));
FreeStyleProject testJob2 = rule.createFreeStyleProject("testJob2");
auth.grant(Item.READ).onItems(testJob2).toAuthenticated();
testJob2.getBuildersList().add(new FileWriteBuilder("foo2.txt", "bar"));
testJob2.getPublishersList().add(new ArtifactArchiver("*.txt"));
rule.assertBuildStatusSuccess(testJob2.scheduleBuild2(0));
FreeStyleProject p = createProject("test$JOB", null, "", "", false, false, false, true);
ParameterDefinition paramDef = new StringParameterDefinition("JOB", "job1");
ParametersDefinitionProperty paramsDef = new ParametersDefinitionProperty(paramDef);
p.addProperty(paramsDef);
// Build step should succeed when this parameter expands to a job accessible
// to authenticated users (even if triggered by anonymous, as in this case):
SecurityContextHolder.clearContext();
FreeStyleBuild b = p.scheduleBuild2(0, new UserCause(), new ParametersAction(new StringParameterValue("JOB", "Job2"))).get();
assertFile(true, "foo2.txt", b);
rule.assertBuildStatusSuccess(b);
// Build step should fail for a job not accessible to all authenticated users,
// even when accessible to the user starting the job, as in this case:
SecurityContext old = ACL.impersonate(new UsernamePasswordAuthenticationToken("joe", "joe"));
try {
b = p.scheduleBuild2(0, new UserCause(), new ParametersAction(new StringParameterValue("JOB", "Job"))).get();
assertFile(false, "foo.txt", b);
rule.assertBuildStatus(Result.FAILURE, b);
} finally {
SecurityContextHolder.setContext(old);
}
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class CopyArtifactTest method artifactsPermissionAnonymousSuccess.
@Test
public void artifactsPermissionAnonymousSuccess() throws Exception {
System.setProperty("hudson.security.ArtifactsPermission", "true");
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
MockAuthorizationStrategy authStrategy = new MockAuthorizationStrategy();
rule.jenkins.setAuthorizationStrategy(authStrategy);
FreeStyleProject src = rule.createFreeStyleProject();
src.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
src.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
authStrategy.grant(Item.READ).onItems(src).toEveryone();
authStrategy.grant(Run.ARTIFACTS).onItems(src).toEveryone();
rule.assertBuildStatusSuccess(src.scheduleBuild2(0));
FreeStyleProject dest = rule.createFreeStyleProject();
dest.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(src.getName(), "", new StatusBuildSelector(), "", "", false, false, true));
rule.assertBuildStatusSuccess(dest.scheduleBuild2(0));
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class CopyArtifactTest method artifactsPermissionWithAuthFailure.
@Test
public void artifactsPermissionWithAuthFailure() throws Exception {
System.setProperty("hudson.security.ArtifactsPermission", "true");
rule.jenkins.setSecurityRealm(rule.createDummySecurityRealm());
MockAuthorizationStrategy authStrategy = new MockAuthorizationStrategy();
rule.jenkins.setAuthorizationStrategy(authStrategy);
authStrategy.grant(Item.BUILD).onRoot().to("joe");
authStrategy.grant(Computer.BUILD).onRoot().to("joe");
FreeStyleProject src = rule.createFreeStyleProject();
src.getBuildersList().add(new FileWriteBuilder("artifact.txt", "foobar"));
src.getPublishersList().add(new ArtifactArchiver("artifact.txt"));
authStrategy.grant(Item.READ).onItems(src).to("joe");
// Watch out: No Run.Artifacts permission.
// authStrategy.grant(Run.ARTIFACTS).onItems(src).to("joe");
rule.assertBuildStatusSuccess(src.scheduleBuild2(0));
FreeStyleProject dest = rule.createFreeStyleProject();
dest.getBuildersList().add(CopyArtifactUtil.createCopyArtifact(src.getName(), "", new StatusBuildSelector(), "", "", false, false, true));
Map<String, Authentication> authMap = new HashMap<>();
authMap.put(dest.getFullName(), User.getById("joe", true).impersonate());
QueueItemAuthenticatorConfiguration.get().getAuthenticators().clear();
QueueItemAuthenticatorConfiguration.get().getAuthenticators().add(new MockQueueItemAuthenticator(authMap));
rule.assertBuildStatus(Result.FAILURE, dest.scheduleBuild2(0));
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class LegacyJobConfigMigrationMonitorTest method setupAnonymousJob.
private void setupAnonymousJob() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy auth = new MockAuthorizationStrategy();
j.jenkins.setAuthorizationStrategy(auth);
FreeStyleProject toBeCopied = j.createFreeStyleProject("to-be-copied");
auth.grant(Item.READ).onItems(toBeCopied).toEveryone();
toBeCopied.getBuildersList().add(new FileWriteBuilder("test.txt", "test"));
toBeCopied.getPublishersList().add(new ArtifactArchiver("**"));
FreeStyleProject copier = j.createFreeStyleProject("copier");
copier.getBuildersList().add(new CopyArtifact(toBeCopied.getFullName()));
}
use of hudson.plugins.copyartifact.testutils.FileWriteBuilder in project copyartifact-plugin by jenkinsci.
the class LegacyJobConfigMigrationMonitorTest method setupWorkflowJob.
private void setupWorkflowJob() throws Exception {
j.jenkins.setSecurityRealm(j.createDummySecurityRealm());
MockAuthorizationStrategy auth = new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin").grant(Jenkins.READ).onRoot().toEveryone().grant(Computer.BUILD).everywhere().toEveryone();
j.jenkins.setAuthorizationStrategy(auth);
FreeStyleProject toBeCopiedAnonymous = j.createFreeStyleProject("to-be-copied_anonymous");
auth.grant(Item.READ, Item.CONFIGURE).onItems(toBeCopiedAnonymous).toEveryone();
toBeCopiedAnonymous.getBuildersList().add(new FileWriteBuilder("test.txt", "test"));
toBeCopiedAnonymous.getPublishersList().add(new ArtifactArchiver("**"));
// Originally this is configured without AuthorizationMatrixProperty,
// which results inheriting the global configuration,
// in contract to that other jobs doesn't inherit the global configuration
// MockAuthorizationStrategy doesn't provide feature to refuse inheriting
// in a specific job, so this job is configured to grant Item.READ to everyone,
// which was originally configured in the global configuration.
FreeStyleProject toBeCopiedNoauth = j.createFreeStyleProject("to-be-copied_noauth");
auth.grant(Item.READ).onItems(toBeCopiedNoauth).toEveryone();
toBeCopiedNoauth.getBuildersList().add(new FileWriteBuilder("test.txt", "test"));
toBeCopiedNoauth.getPublishersList().add(new ArtifactArchiver("**"));
FreeStyleProject toBeCopiedRestricted = j.createFreeStyleProject("to-be-copied_restricted");
auth.grant(Item.READ).onItems(toBeCopiedRestricted).to("leader", "admin");
toBeCopiedRestricted.getBuildersList().add(new FileWriteBuilder("test.txt", "test"));
toBeCopiedRestricted.getPublishersList().add(new ArtifactArchiver("**"));
WorkflowJob copierAnonymous = j.createWorkflow("copier_anonymous", "copyArtifacts(projectName: 'to-be-copied_anonymous');");
copierAnonymous.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("sourceProject", "")));
WorkflowJob copierNoauth = j.createWorkflow("copier_noauth", "copyArtifacts(projectName: 'to-be-copied_noauth');");
copierNoauth.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("sourceProject", "")));
WorkflowJob copierParam = j.createWorkflow("copier_param", "copyArtifacts(projectName: '${sourceProject}');");
copierParam.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("sourceProject", "")));
WorkflowJob copierRestricted = j.createWorkflow("copier_restricted", "copyArtifacts(projectName: 'to-be-copied_restricted');");
copierRestricted.addProperty(new ParametersDefinitionProperty(new StringParameterDefinition("sourceProject", "")));
}
Aggregations