Search in sources :

Example 16 with PromotionProcess

use of hudson.plugins.promoted_builds.PromotionProcess in project promoted-builds-plugin by jenkinsci.

the class ManualConditionTest method testManualPromotionPermissions.

@Test
@Issue("SECURITY-190")
public void testManualPromotionPermissions() throws Exception {
    enableSecurity(j);
    FreeStyleProject p = j.createFreeStyleProject();
    PromotionProcess pp = addPromotionProcess(p, "foo");
    ManualCondition cond = new ManualCondition();
    pp.conditions.add(cond);
    {
        // No approvers specified and user does not have Promotion/Promote
        FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
        SecurityContext previous = ACL.impersonate(User.get("non-promoter").impersonate());
        cond.approve(b, pp, Collections.EMPTY_LIST);
        ACL.impersonate(previous.getAuthentication());
        ManualApproval approval = b.getAction(ManualApproval.class);
        assertThat("If no users are specified, then users without Promotion/Promote permissions should not be able to approve the promotion", approval, nullValue());
    }
    {
        // No approvers specified and user does have Promotion/Promote
        FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
        SecurityContext previous = ACL.impersonate(User.get("promoter").impersonate());
        j.assertBuildStatusSuccess(cond.approve(b, pp, Collections.EMPTY_LIST));
        ACL.impersonate(previous.getAuthentication());
        ManualApproval approval = b.getAction(ManualApproval.class);
        assertThat("If no users are specified, then users with Promotion/Promote permissions should be able to approve the promotion", approval, notNullValue());
    }
    {
        // Approvers specified, user is approver, but does not have Promotion/Promote
        cond.setUsers("non-promoter");
        FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
        SecurityContext previous = ACL.impersonate(User.get("non-promoter").impersonate());
        j.assertBuildStatusSuccess(cond.approve(b, pp, Collections.EMPTY_LIST));
        ACL.impersonate(previous.getAuthentication());
        ManualApproval approval = b.getAction(ManualApproval.class);
        assertThat("If users are specified, then users in that list should be able to approve even without Promotion/Promote permissions", approval, notNullValue());
    }
    {
        // Approvers specified, user is not approver, but does have Promotion/Promote
        cond.setUsers("non-promoter");
        FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
        SecurityContext previous = ACL.impersonate(User.get("promoter").impersonate());
        cond.approve(b, pp, Collections.EMPTY_LIST);
        ACL.impersonate(previous.getAuthentication());
        ManualApproval approval = b.getAction(ManualApproval.class);
        assertThat("If users are specified, then users not in the list should not be able to approve regardless of their permissions", approval, nullValue());
    }
}
Also used : ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) SecurityContext(org.acegisecurity.context.SecurityContext) FreeStyleBuild(hudson.model.FreeStyleBuild) FreeStyleProject(hudson.model.FreeStyleProject) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 17 with PromotionProcess

use of hudson.plugins.promoted_builds.PromotionProcess in project promoted-builds-plugin by jenkinsci.

the class ManualConditionTest method testManualPromotionProcessWithInvalidParam.

@Issue("SECURITY-170")
@Test
public /**
 * Verify that the plugin is tolerant against SECURITY-170 in Manual conditions
 */
void testManualPromotionProcessWithInvalidParam() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject();
    ExtensionList<Descriptor> list = j.jenkins.getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(p);
    p.addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("FOO", "BAR", "Test parameter"));
    foo.conditions.add(condition);
    FreeStyleBuild b1 = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
    // Promote a build. Also add one invalid parameter
    List<ParameterValue> paramValues = condition.createDefaultValues();
    paramValues.add(new StringParameterValue("INVALID_PARAM", "hacked!"));
    j.assertBuildStatusSuccess(condition.approve(b1, foo, paramValues));
    ManualApproval manualApproval = b1.getAction(ManualApproval.class);
    assertNotNull(manualApproval);
    List<ParameterValue> parameterValues = manualApproval.badge.getParameterValues();
    // Verify that the build succeeds && has no INVALID_PARAM
    PromotedBuildAction statuses = b1.getAction(PromotedBuildAction.class);
    assertNotNull(statuses);
    assertNotNull(statuses.getPromotions());
    assertFalse(statuses.getPromotions().isEmpty());
    Promotion pb = base.getItem("foo").getBuildByNumber(1);
    assertNotNull("INVALID_PARAM should not be injected into the environment", pb.getEnvironment(TaskListener.NULL).get("INVALID_PARAM", null));
}
Also used : ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) StringParameterDefinition(hudson.model.StringParameterDefinition) StringParameterValue(hudson.model.StringParameterValue) ParameterValue(hudson.model.ParameterValue) StringParameterValue(hudson.model.StringParameterValue) FreeStyleBuild(hudson.model.FreeStyleBuild) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) FreeStyleProject(hudson.model.FreeStyleProject) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Promotion(hudson.plugins.promoted_builds.Promotion) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Descriptor(hudson.model.Descriptor) Issue(org.jvnet.hudson.test.Issue) Test(org.junit.Test)

Example 18 with PromotionProcess

use of hudson.plugins.promoted_builds.PromotionProcess in project promoted-builds-plugin by jenkinsci.

the class ManualConditionTest method testManualPromotionProcessViaWebClient.

@Test
public void testManualPromotionProcessViaWebClient() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject();
    ExtensionList<Descriptor> list = j.jenkins.getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(p);
    p.addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_1", "bogus_value_1", "Bog parameter"));
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_2", "bogus_value_2", "Bog parameter"));
    foo.conditions.add(condition);
    FreeStyleBuild b1 = j.assertBuildStatusSuccess(p.scheduleBuild2(0));
    assertNull(b1.getAction(ManualApproval.class));
    HtmlPage page = j.createWebClient().getPage(b1, "promotion");
    // Approve Promotion
    List<HtmlForm> forms = getFormsByName(page, "approve");
    assertFalse(forms.isEmpty());
    assertEquals(1, forms.size());
    HtmlForm form = forms.get(0);
    List<HtmlElement> parameters = getFormParameters(form);
    assertEquals(parameters.size(), condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "1");
    }
    submit(forms.get(0));
    ManualApproval approval = b1.getAction(ManualApproval.class);
    assertNotNull(approval);
    SortedMap<Integer, Promotion> builds = foo.getBuildsAsMap();
    assertNotNull(builds);
    assertEquals(1, builds.size());
    // Re-Execute approved promotion
    page = j.createWebClient().getPage(b1, "promotion");
    forms = getFormsByName(page, "build");
    assertFalse(forms.isEmpty());
    assertEquals(1, forms.size());
    form = forms.get(0);
    parameters = getFormParameters(form);
    assertEquals(parameters.size(), condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "2");
    }
    submit(form);
    builds = foo.getBuildsAsMap();
    assertEquals(2, builds.size());
    List<ManualApproval> actions = b1.getActions(ManualApproval.class);
    assertEquals(1, actions.size());
    PromotedBuildAction buildActions = b1.getAction(PromotedBuildAction.class);
    int buildIndex = 1;
    String valueSufix = "1";
    List<Promotion> promotions = new ArrayList<Promotion>();
    promotions.addAll(builds.values());
    Collections.reverse(promotions);
    for (Promotion build : promotions) {
        List<ParameterDefinition> values = build.getParameterDefinitionsWithValue();
        assertEquals(values.size(), condition.getParameterDefinitions().size());
        for (ParameterDefinition v : values) {
            assertTrue(v instanceof StringParameterDefinition);
            String pvalue = ((StringParameterDefinition) v).getDefaultValue();
            assertTrue(pvalue.endsWith(valueSufix));
        }
        buildIndex++;
        valueSufix += buildIndex;
    }
    for (Status status : buildActions.getPromotions()) {
        assertNotNull(status.getLast() != null);
        List<ParameterDefinition> values = status.getLast().getParameterDefinitionsWithValue();
        assertEquals(values.size(), condition.getParameterDefinitions().size());
    }
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) FreeStyleBuild(hudson.model.FreeStyleBuild) Promotion(hudson.plugins.promoted_builds.Promotion) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) Status(hudson.plugins.promoted_builds.Status) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) FreeStyleProject(hudson.model.FreeStyleProject) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Descriptor(hudson.model.Descriptor) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition) Test(org.junit.Test)

Example 19 with PromotionProcess

use of hudson.plugins.promoted_builds.PromotionProcess in project promoted-builds-plugin by jenkinsci.

the class ManualConditionInheritanceTest method testManualPromotionProcessViaWebClient.

@Test
public void testManualPromotionProcessViaWebClient() throws Exception {
    InheritanceProjectsPair inheritanceProjectsPair = j.createInheritanceProjectDerivedWithBase();
    ExtensionList<Descriptor> list = Jenkins.get().getExtensionList(Descriptor.class);
    list.add(new JobPropertyImpl.DescriptorImpl(JobPropertyImpl.class));
    JobPropertyImpl base = new JobPropertyImpl(inheritanceProjectsPair.getBase());
    inheritanceProjectsPair.getDerived().addProperty(base);
    PromotionProcess foo = base.addProcess("foo");
    ManualCondition condition = new ManualCondition();
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_1", "bogus_value_1", "Bog parameter"));
    condition.getParameterDefinitions().add(new StringParameterDefinition("bogus_string_param_2", "bogus_value_2", "Bog parameter"));
    foo.conditions.add(condition);
    InheritanceBuild b1 = j.assertBuildStatusSuccess(inheritanceProjectsPair.getDerived().scheduleBuild2(0));
    assertNull(b1.getAction(ManualApproval.class));
    HtmlPage page = j.createWebClient().getPage(b1, "promotion");
    // Approve Promotion
    List<HtmlForm> forms = getFormsByName(page, "approve");
    assertFalse(forms.isEmpty());
    assertEquals(1, forms.size());
    HtmlForm form = forms.get(0);
    List<HtmlElement> parameters = getFormParameters(form);
    assertEquals(parameters.size(), condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "1");
    }
    j.submit(forms.get(0));
    j.waitUntilNoActivity();
    // We cannot assume that the process will contain builds because the process added to base project is different to the one in derived.
    final JobPropertyImpl jobProperty = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                because that version of the plugin uses inheritance only for certain predefined cases: 
                -specific methods on the call stack
                -url paths.
                This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
               */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty);
    final PromotionProcess fooDerived = jobProperty.getItem("foo");
    ManualApproval approval = b1.getAction(ManualApproval.class);
    assertNotNull(approval);
    SortedMap<Integer, Promotion> builds = fooDerived.getBuildsAsMap();
    assertNotNull(builds);
    assertEquals(1, builds.size());
    // Re-Execute approved promotion
    page = j.createWebClient().getPage(b1, "promotion");
    forms = getFormsByName(page, "build");
    assertFalse(forms.isEmpty());
    assertEquals(1, forms.size());
    form = forms.get(0);
    parameters = getFormParameters(form);
    assertEquals(parameters.size(), condition.getParameterDefinitions().size());
    for (HtmlElement param : parameters) {
        HtmlElement v = param.getElementsByAttribute("input", "name", "value").get(0);
        v.setAttribute("value", v.getAttribute("value") + "2");
    }
    j.submit(form);
    j.waitUntilNoActivity();
    final JobPropertyImpl jobProperty2 = inheritanceProjectsPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                  because that version of the plugin uses inheritance only for certain predefined cases: 
                  -specific methods on the call stack
                  -url paths.
                  This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
                 */
    IMode.INHERIT_FORCED);
    assertNotNull("derived jobProperty is null", jobProperty2);
    final PromotionProcess fooDerived2 = jobProperty2.getItem("foo");
    builds = fooDerived2.getBuildsAsMap();
    assertEquals(2, builds.size());
    List<ManualApproval> actions = b1.getActions(ManualApproval.class);
    assertEquals(1, actions.size());
    PromotedBuildAction buildActions = b1.getAction(PromotedBuildAction.class);
    int buildIndex = 1;
    String valueSufix = "1";
    List<Promotion> promotions = new ArrayList<Promotion>();
    promotions.addAll(builds.values());
    Collections.reverse(promotions);
    for (Promotion build : promotions) {
        List<ParameterDefinition> values = build.getParameterDefinitionsWithValue();
        assertEquals(values.size(), condition.getParameterDefinitions().size());
        for (ParameterDefinition v : values) {
            assertTrue(v instanceof StringParameterDefinition);
            String pvalue = ((StringParameterDefinition) v).getDefaultValue();
            assertTrue(pvalue.endsWith(valueSufix));
        }
        buildIndex++;
        valueSufix += buildIndex;
    }
}
Also used : StringParameterDefinition(hudson.model.StringParameterDefinition) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) ArrayList(java.util.ArrayList) Promotion(hudson.plugins.promoted_builds.Promotion) ManualCondition(hudson.plugins.promoted_builds.conditions.ManualCondition) HtmlForm(com.gargoylesoftware.htmlunit.html.HtmlForm) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) InheritanceProjectsPair(hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair) ManualApproval(hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval) InheritanceBuild(hudson.plugins.project_inheritance.projects.InheritanceBuild) HtmlElement(com.gargoylesoftware.htmlunit.html.HtmlElement) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Descriptor(hudson.model.Descriptor) ParameterDefinition(hudson.model.ParameterDefinition) StringParameterDefinition(hudson.model.StringParameterDefinition) Test(org.junit.Test)

Example 20 with PromotionProcess

use of hudson.plugins.promoted_builds.PromotionProcess in project promoted-builds-plugin by jenkinsci.

the class SelfPromotionInheritanceTest method testUnstable.

@Test
public void testUnstable() throws Exception {
    InheritanceProjectsPair inheritanceProjectPair = j.createInheritanceProjectDerivedWithBase();
    // promote if the downstream passes
    JobPropertyImpl promotion = new JobPropertyImpl(inheritanceProjectPair.getBase());
    inheritanceProjectPair.getBase().addProperty(promotion);
    PromotionProcess promo1 = promotion.addProcess("promo1");
    promo1.conditions.add(new SelfPromotionCondition(false));
    PromotionProcess promo2 = promotion.addProcess("promo2");
    promo2.conditions.add(new SelfPromotionCondition(true));
    inheritanceProjectPair.getDerived().getBuildersList().add(unstableBuilder());
    InheritanceBuild b = j.assertBuildStatus(Result.UNSTABLE, inheritanceProjectPair.getDerived().scheduleBuild2(0).get());
    j.waitUntilNoActivity();
    // rebind
    promotion = inheritanceProjectPair.getDerived().getProperty(JobPropertyImpl.class, /*Forcing inheritance as temporary hack for inheritance plugin 1.53 
                because that version of the plugin uses inheritance only for certain predefined cases: 
                -specific methods on the call stack
                -url paths.
                This has been changed as pull request https://github.com/i-m-c/jenkins-inheritance-plugin/pull/40
               */
    IMode.INHERIT_FORCED);
    promo1 = promotion.getItem("promo1");
    promo2 = promotion.getItem("promo2");
    // internally, the promotion is still an asynchronous process. It just happens
    // right away after the build is complete.
    // verify that only one promotions happened
    assertTrue(promo1.getBuilds().isEmpty());
    Promotion pb = promo2.getBuilds().get(0);
    assertSame(pb.getTargetBuildOrFail(), b);
    PromotedBuildAction badge = (PromotedBuildAction) b.getBadgeActions().get(0);
    assertFalse(badge.contains(promo1));
    assertTrue(badge.contains(promo2));
}
Also used : InheritanceProjectsPair(hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair) InheritanceBuild(hudson.plugins.project_inheritance.projects.InheritanceBuild) PromotionProcess(hudson.plugins.promoted_builds.PromotionProcess) SelfPromotionCondition(hudson.plugins.promoted_builds.conditions.SelfPromotionCondition) PromotedBuildAction(hudson.plugins.promoted_builds.PromotedBuildAction) JobPropertyImpl(hudson.plugins.promoted_builds.JobPropertyImpl) Promotion(hudson.plugins.promoted_builds.Promotion) Test(org.junit.Test)

Aggregations

PromotionProcess (hudson.plugins.promoted_builds.PromotionProcess)23 Test (org.junit.Test)22 JobPropertyImpl (hudson.plugins.promoted_builds.JobPropertyImpl)20 FreeStyleBuild (hudson.model.FreeStyleBuild)15 FreeStyleProject (hudson.model.FreeStyleProject)15 PromotedBuildAction (hudson.plugins.promoted_builds.PromotedBuildAction)14 Promotion (hudson.plugins.promoted_builds.Promotion)11 StringParameterDefinition (hudson.model.StringParameterDefinition)10 ManualApproval (hudson.plugins.promoted_builds.conditions.ManualCondition.ManualApproval)8 InheritanceProjectsPair (hudson.plugins.promoted_builds.inheritance.helpers.InheritanceProjectsPair)7 Descriptor (hudson.model.Descriptor)6 InheritanceBuild (hudson.plugins.project_inheritance.projects.InheritanceBuild)6 Issue (org.jvnet.hudson.test.Issue)6 StringParameterValue (hudson.model.StringParameterValue)5 ParameterValue (hudson.model.ParameterValue)4 Status (hudson.plugins.promoted_builds.Status)4 SelfPromotionCondition (hudson.plugins.promoted_builds.conditions.SelfPromotionCondition)4 ParameterDefinition (hudson.model.ParameterDefinition)3 ManualCondition (hudson.plugins.promoted_builds.conditions.ManualCondition)3 SecureGroovyScript (org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript)3