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

Adapt to SECURITY-170 #13

Merged
merged 2 commits into from
May 30, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* The MIT License
*
* Copyright (c) 2016, 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.registry.notification;

import hudson.EnvVars;
import hudson.Extension;
import hudson.model.EnvironmentContributor;
import hudson.model.ParameterValue;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.jenkinsci.plugins.registry.notification.webhook.WebHookCause;

import javax.annotation.Nonnull;
import java.io.IOException;
import java.util.Set;

/**
* Provides environment variables to builds triggered by the plugin.
*
* @author Robert Sandell <rsandell@cloudbees.com>.
*/
@Extension
public class EnvContributor extends EnvironmentContributor {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer EnvironmentContributingAction (global performance reason)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add @Restricted just to support switch in the future

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

EnvironmentContributingAction is only applicable for AbstractBuilds


@Override
public void buildEnvironmentFor(@Nonnull Run r, @Nonnull EnvVars envs, @Nonnull TaskListener listener) throws IOException, InterruptedException {
WebHookCause cause = (WebHookCause)r.getCause(WebHookCause.class);
if (cause != null) {
Set<ParameterValue> parameters = cause.getPushNotification().getRunParameters();
for (ParameterValue parameter : parameters) {
parameter.buildEnvironment(r, envs);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public void run() {
}

private void schedule(@Nonnull final Job job, @Nonnull final PushNotification pushNotification) {
if (new JobbMixIn(job).schedule(pushNotification.getCause(), pushNotification.getJobParamerers())) {
if (new JobbMixIn(job).schedule(pushNotification.getCause())) {
logger.info(pushNotification.getCauseMessage());
Coordinator coordinator = Coordinator.getInstance();
if (coordinator != null) {
Expand Down Expand Up @@ -174,7 +174,7 @@ static class JobbMixIn<JobT extends Job<JobT, RunT> & ParameterizedJobMixIn.Para
private JobT the;


public JobbMixIn(JobT the) {
JobbMixIn(JobT the) {
this.the = the;
}

Expand All @@ -183,13 +183,13 @@ protected JobT asJob() {
return the;
}

public boolean schedule(Cause cause, Set<ParameterValue> parameters) {
boolean schedule(Cause cause) {
if (!asJob().isBuildable()) {
return false;
}
List<Action> queueActions = new LinkedList<Action>();

queueActions.add(new ParametersAction(getParameterValues(parameters)));
queueActions.add(new ParametersAction(getParameterValues()));
queueActions.add(new CauseAction(cause));

int quiet = Math.max(MIN_QUIET, asJob().getQuietPeriod());
Expand All @@ -207,12 +207,11 @@ public boolean schedule(Cause cause, Set<ParameterValue> parameters) {
return i != null && i.getFuture() != null;
}

private List<ParameterValue> getParameterValues(Set<ParameterValue> parameters) {
private List<ParameterValue> getParameterValues() {
Set<ParameterValue> result = new HashSet<ParameterValue>();
if (isParameterized()) {
result.addAll(getDefaultParametersValues());
}
result.addAll(parameters);
return Collections.unmodifiableList(new LinkedList<ParameterValue>(result));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
*/
package org.jenkinsci.plugins.registry.notification.webhook;

import hudson.Util;
import hudson.model.Cause;
import hudson.model.ParameterValue;
import hudson.model.Run;
import org.jenkinsci.plugins.registry.notification.webhook.dockerhub.DockerHubPushNotification;

import javax.annotation.CheckForNull;
import java.util.Collections;
import java.util.Date;
import java.util.Set;

Expand Down Expand Up @@ -73,7 +75,25 @@ public int hashCode() {

abstract public Cause getCause();

abstract public Set<ParameterValue> getJobParamerers();
/**
* Provide parameters to be put into a build.
* @return the parameters
* @deprecated misspelled and wrong context naming. Use {@link #getRunParameters()}
*/
@Deprecated
public Set<ParameterValue> getJobParamerers() {
if (Util.isOverridden(PushNotification.class, getClass(), "getRunParameters")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🐜 Minore code duplication

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where?

return getRunParameters();
}
return Collections.emptySet();
}

public Set<ParameterValue> getRunParameters() {
if (Util.isOverridden(PushNotification.class, getClass(), "getJobParamerers")) {
return getJobParamerers();
}
return Collections.emptySet();
}

abstract public String getCauseMessage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public Cause getCause() {
}

@Override
public Set<ParameterValue> getJobParamerers() {
public Set<ParameterValue> getRunParameters() {
Set<ParameterValue> parameters = new HashSet<ParameterValue>();
parameters.add(new StringParameterValue(KEY_REPO_NAME, getRepoName()));
String host = getRegistryHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public Cause getCause() {
}

@Override
public Set<ParameterValue> getJobParamerers() {
public Set<ParameterValue> getRunParameters() {
Set<ParameterValue> parameters = new HashSet<ParameterValue>();
parameters.add(new StringParameterValue(KEY_REPO_NAME, getRepoName()));
String host = getRegistryHost();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,26 @@
*/
package org.jenkinsci.plugins.registry.notification;

import hudson.EnvVars;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.FreeStyleBuild;
import hudson.tasks.BuildStep;
import hudson.tasks.Builder;
import jenkins.tasks.SimpleBuildStep;
import org.jenkinsci.plugins.registry.notification.opt.impl.TriggerOnSpecifiedImageNames;
import hudson.model.FreeStyleProject;
import hudson.model.Result;
import org.jenkinsci.plugins.registry.notification.webhook.dockerhub.DockerHubPushNotification;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.MockBuilder;

import java.io.IOException;
import java.util.Map;

public class DockerHubWebHookTest {

@Rule
Expand All @@ -48,4 +60,30 @@ public void testDoIndex() throws Exception {

j.assertLogContains(repoName, project.getLastBuild());
}

@Test(timeout = 60000)
public void testEnvironment() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
final String repoName = "cb/jenkins";
project.addTrigger(new DockerHubTrigger(new TriggerOnSpecifiedImageNames(repoName)));
project.getBuildersList().add(new PrintEnvironment());
project.getBuildersList().add(new MockBuilder(Result.SUCCESS));
j.createWebClient().goTo("dockerhub-webhook/debug?image=" + repoName);

j.waitUntilNoActivity();
FreeStyleBuild build = project.getLastBuild();
j.assertLogContains(repoName, build);
j.assertLogContains(DockerHubPushNotification.KEY_REPO_NAME + " = " + repoName, build);
}

static class PrintEnvironment extends Builder {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
EnvVars vars = build.getEnvironment(listener);
for (Map.Entry<String, String> var : vars.entrySet()) {
listener.getLogger().println(var.getKey() + " = " + var.getValue());
}
return true;
}
}
}