Skip to content

Commit

Permalink
Adding token credential support #183 #165
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Sep 28, 2020
1 parent 22bc077 commit e923e8d
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 32 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ Changelog of Generic Webhook Plugin.
[c0cfc1314988ab5](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/c0cfc1314988ab5) Tomas Bjerre *2020-08-05 04:57:42*


### No issue

**Build: stepping plugins**


[22bc077679fa927](https://github.com/jenkinsci/generic-webhook-trigger-plugin/commit/22bc077679fa927) Tomas Bjerre *2020-09-28 15:52:55*


## 1.67 (2020-03-13 15:50:31)
### GitHub [#150](https://github.com/jenkinsci/generic-webhook-trigger-plugin/pull/150) Fix minor typo

Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ pipelineJob('Generic Job Example') {
}
}
token('abc123')
tokenCredentialId('')
printContributedVariables(true)
printPostContent(true)
silentResponse(false)
Expand Down Expand Up @@ -298,6 +299,7 @@ node {
causeString: 'Triggered on $ref',
token: 'abc123',
tokenCredentialId: '',
printContributedVariables: true,
printPostContent: true,
Expand Down Expand Up @@ -338,6 +340,7 @@ pipeline {
causeString: 'Triggered on $ref',
token: 'abc123',
tokenCredentialId: '',
printContributedVariables: true,
printPostContent: true,
Expand Down
2 changes: 1 addition & 1 deletion logging.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Logging
handlers = java.util.logging.ConsoleHandler
org.jenkinsci.plugins.jvcts.level = FINE
org.jenkinsci.plugins.gwt.level = FINE

# Console Logging
java.util.logging.ConsoleHandler.level = FINE
82 changes: 55 additions & 27 deletions src/main/java/org/jenkinsci/plugins/gwt/GenericTrigger.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
import hudson.model.ParametersDefinitionProperty;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.util.List;
import java.util.Map;
import jenkins.model.ParameterizedJobMixIn;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.gwt.global.CredentialsHelper;
import org.jenkinsci.plugins.gwt.resolvers.VariablesResolver;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;

public class GenericTrigger extends Trigger<Job<?, ?>> {

Expand All @@ -33,6 +38,7 @@ public class GenericTrigger extends Trigger<Job<?, ?>> {
private boolean printContributedVariables;
private String causeString;
private String token;
private String tokenCredentialId;
private boolean silentResponse;
private boolean overrideQuietPeriod;

Expand All @@ -48,6 +54,15 @@ public boolean isApplicable(final Item item) {
public String getDisplayName() {
return "Generic Webhook Trigger";
}

public ListBoxModel doFillTokenCredentialIdItems(
@AncestorInPath final Item item, @QueryParameter final String credentialsId) {
return CredentialsHelper.doFillCredentialsIdItems(item, credentialsId);
}

public FormValidation doCheckTokenCredentialIdItems(@QueryParameter final String value) {
return CredentialsHelper.doCheckFillCredentialsId(value);
}
}

@DataBoundConstructor
Expand All @@ -70,7 +85,7 @@ public void setCauseString(final String causeString) {
}

public String getCauseString() {
return causeString;
return this.causeString;
}

@DataBoundSetter
Expand All @@ -94,19 +109,19 @@ public void setOverrideQuietPeriod(final boolean overrideQuietPeriod) {
}

public boolean getOverrideQuietPeriod() {
return overrideQuietPeriod;
return this.overrideQuietPeriod;
}

public boolean isSilentResponse() {
return silentResponse;
return this.silentResponse;
}

public boolean isPrintContributedVariables() {
return printContributedVariables;
return this.printContributedVariables;
}

public boolean isPrintPostContent() {
return printPostContent;
return this.printPostContent;
}

@DataBoundSetter
Expand All @@ -115,7 +130,16 @@ public void setToken(final String token) {
}

public String getToken() {
return token;
return this.token;
}

@DataBoundSetter
public void setTokenCredentialId(final String tokenCredentialId) {
this.tokenCredentialId = tokenCredentialId;
}

public String getTokenCredentialId() {
return this.tokenCredentialId;
}

@Extension public static final GenericDescriptor DESCRIPTOR = new GenericDescriptor();
Expand All @@ -132,30 +156,34 @@ public GenericTriggerResults trigger(
headers,
parameterMap,
postContent,
genericVariables,
genericRequestVariables,
genericHeaderVariables)
this.genericVariables,
this.genericRequestVariables,
this.genericHeaderVariables)
.getVariables();

final String renderedRegexpFilterText = renderText(regexpFilterText, resolvedVariables);
final boolean isMatching = isMatching(renderedRegexpFilterText, regexpFilterExpression);
final String renderedRegexpFilterText = renderText(this.regexpFilterText, resolvedVariables);
final boolean isMatching = isMatching(renderedRegexpFilterText, this.regexpFilterExpression);

hudson.model.Queue.Item item = null;
if (isMatching) {
final String cause = renderText(causeString, resolvedVariables);
final String cause = renderText(this.causeString, resolvedVariables);
final GenericCause genericCause =
new GenericCause(
postContent, resolvedVariables, printContributedVariables, printPostContent, cause);
postContent,
resolvedVariables,
this.printContributedVariables,
this.printPostContent,
cause);
final ParametersDefinitionProperty parametersDefinitionProperty =
job.getProperty(ParametersDefinitionProperty.class);
this.job.getProperty(ParametersDefinitionProperty.class);
final ParametersAction parameters =
createParameterAction(parametersDefinitionProperty, resolvedVariables);
item =
retrieveScheduleJob(job) //
.scheduleBuild2(job, quietPeriod, new CauseAction(genericCause), parameters);
this.retrieveScheduleJob(this.job) //
.scheduleBuild2(this.job, quietPeriod, new CauseAction(genericCause), parameters);
}
return new GenericTriggerResults(
item, resolvedVariables, renderedRegexpFilterText, regexpFilterExpression);
item, resolvedVariables, renderedRegexpFilterText, this.regexpFilterExpression);
}

@SuppressWarnings("rawtypes")
Expand All @@ -169,37 +197,37 @@ public GenericTriggerResults trigger(
}

public List<GenericVariable> getGenericVariables() {
return genericVariables;
return this.genericVariables;
}

public String getRegexpFilterExpression() {
return regexpFilterExpression;
return this.regexpFilterExpression;
}

public List<GenericRequestVariable> getGenericRequestVariables() {
return genericRequestVariables;
return this.genericRequestVariables;
}

public List<GenericHeaderVariable> getGenericHeaderVariables() {
return genericHeaderVariables;
return this.genericHeaderVariables;
}

public String getRegexpFilterText() {
return regexpFilterText;
return this.regexpFilterText;
}

@Override
public String toString() {
return "GenericTrigger [genericVariables="
+ genericVariables
+ this.genericVariables
+ ", regexpFilterText="
+ regexpFilterText
+ this.regexpFilterText
+ ", regexpFilterExpression="
+ regexpFilterExpression
+ this.regexpFilterExpression
+ ", genericRequestVariables="
+ genericRequestVariables
+ this.genericRequestVariables
+ ", genericHeaderVariables="
+ genericHeaderVariables
+ this.genericHeaderVariables
+ "]";
}
}
55 changes: 52 additions & 3 deletions src/main/java/org/jenkinsci/plugins/gwt/jobfinder/JobFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,25 @@
import static com.google.common.base.Strings.isNullOrEmpty;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Optional;
import hudson.model.BuildAuthorizationToken;
import hudson.model.Item;
import hudson.triggers.Trigger;
import hudson.triggers.TriggerDescriptor;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.ParameterizedJobMixIn.ParameterizedJob;
import org.jenkinsci.plugins.gwt.FoundJob;
import org.jenkinsci.plugins.gwt.GenericTrigger;
import org.jenkinsci.plugins.plaincredentials.StringCredentials;

public final class JobFinder {

private static Logger LOG = Logger.getLogger(JobFinder.class.getSimpleName());

private JobFinder() {}

private static JobFinderImpersonater jobFinderImpersonater = new JobFinderImpersonater();
Expand All @@ -34,16 +41,58 @@ public static List<FoundJob> findAllJobsWithTrigger(final String givenToken) {
for (final ParameterizedJob candidateJob : candidateProjects) {
final GenericTrigger genericTriggerOpt = findGenericTrigger(candidateJob.getTriggers());
if (genericTriggerOpt != null) {
if (authenticationTokenMatches(
givenToken, candidateJob.getAuthToken(), genericTriggerOpt.getToken())) {
found.add(new FoundJob(candidateJob.getFullName(), genericTriggerOpt));
final String configuredToken =
determineTokenValue(
candidateJob,
genericTriggerOpt.getToken(),
genericTriggerOpt.getTokenCredentialId());
final boolean authenticationTokenMatches =
authenticationTokenMatches(givenToken, candidateJob.getAuthToken(), configuredToken);
if (authenticationTokenMatches) {
final FoundJob foundJob = new FoundJob(candidateJob.getFullName(), genericTriggerOpt);
found.add(foundJob);
}
}
}

return found;
}

private static String determineTokenValue(
final Item item, final String token, final String tokenCredentialsId) {
if (isNullOrEmpty(tokenCredentialsId)) {
LOG.log(Level.FINE, "Found no credential configured in " + item.getFullDisplayName());
return token;
}
if (!isNullOrEmpty(tokenCredentialsId) && !isNullOrEmpty(token)) {
LOG.log(
Level.WARNING,
"The job "
+ item.getFullDisplayName()
+ " is configured with both static token and token from credential "
+ tokenCredentialsId
+ ".");
}
final Optional<StringCredentials> credentialsOpt =
org.jenkinsci.plugins.gwt.global.CredentialsHelper.findCredentials(tokenCredentialsId);
if (credentialsOpt.isPresent()) {
LOG.log(
Level.FINE,
"Found credential from "
+ tokenCredentialsId
+ " configured in "
+ item.getFullDisplayName());
return credentialsOpt.get().getSecret().getPlainText();
}
LOG.log(
Level.SEVERE,
"Cannot find credential ("
+ tokenCredentialsId
+ ") configured in "
+ item.getFullDisplayName());
return token;
}

private static boolean authenticationTokenMatches(
final String givenToken,
@SuppressWarnings("deprecation") final BuildAuthorizationToken authToken,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:c="/lib/credentials">
<f:description>
<div>
<p>
Expand Down Expand Up @@ -56,6 +56,14 @@
</f:description>
</f:entry>

<f:entry field="tokenCredentialId" title="Token Credential">
<c:select />
<f:description>
Same as <b>token</b> above, but configured with a <i>secret text</i> credential.
</f:description>
</f:entry>


<f:entry title="Cause">
<f:textbox field="causeString" default="Generic Cause"/>
<f:description>
Expand Down

0 comments on commit e923e8d

Please sign in to comment.