Skip to content

Commit

Permalink
breaking: dropping support for BuildAuthorizationToken (refs #290) (#293
Browse files Browse the repository at this point in the history
)
  • Loading branch information
tomasbjerre committed Jan 27, 2024
1 parent aeea0fe commit d1d7ad6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 44 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ When using the plugin in several jobs, you will have the same URL trigger all jo

There is a special `token` parameter. When supplied, the invocation will only trigger jobs with that exact token. The token also allows invocations without any other authentication credentials.

*In version 2.x of the plugin it no longer supports specifying the token outside of the plugin in [BuildAuthorizationToken](https://github.com/jenkinsci/jenkins/blob/master/core/src/main/java/hudson/model/BuildAuthorizationToken.java).*

![Parameter](/sandbox/configure-token.png)

The token can be supplied as a:
Expand Down
50 changes: 11 additions & 39 deletions src/main/java/org/jenkinsci/plugins/gwt/jobfinder/JobFinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

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;
Expand Down Expand Up @@ -49,7 +48,7 @@ public static List<FoundJob> findAllJobsWithTrigger(final String givenToken) {
genericTriggerOpt.getToken(),
genericTriggerOpt.getTokenCredentialId());
final boolean authenticationTokenMatches =
authenticationTokenMatches(givenToken, candidateJob.getAuthToken(), configuredToken);
authenticationTokenMatches(givenToken, configuredToken);
if (authenticationTokenMatches) {
final FoundJob foundJob = new FoundJob(candidateJob.getFullName(), genericTriggerOpt);
found.add(foundJob);
Expand Down Expand Up @@ -97,54 +96,27 @@ private static String determineTokenValue(
}

private static boolean authenticationTokenMatches(
final String givenToken,
@SuppressWarnings("deprecation") final BuildAuthorizationToken authToken,
final String genericToken) {
final String givenToken, final String configuredToken) {
final boolean noTokenGiven = isNullOrEmpty(givenToken);
final boolean noKindOfTokenConfigured =
isNullOrEmpty(genericToken) && !jobHasAuthToken(authToken);
final boolean genericTokenNotConfigured = isNullOrEmpty(genericToken);
final boolean authTokenNotConfigured = !jobHasAuthToken(authToken);
return genericTokenNotConfigured && authenticationTokenMatches(authToken, givenToken)
|| authTokenNotConfigured && authenticationTokenMatchesGeneric(genericToken, givenToken)
|| noTokenGiven && noKindOfTokenConfigured;
final boolean noTokenConfigured = isNullOrEmpty(configuredToken);
return authenticationTokenMatchesGeneric(configuredToken, givenToken)

Check warning on line 102 in src/main/java/org/jenkinsci/plugins/gwt/jobfinder/JobFinder.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 102 is only partially covered, one branch is missing
|| noTokenGiven && noTokenConfigured;
}

/** This is the token configured in this plugin. */
private static boolean authenticationTokenMatchesGeneric(
final String token, final String givenToken) {
final boolean jobHasAuthToken = !isNullOrEmpty(token);
final boolean authTokenWasGiven = !isNullOrEmpty(givenToken);
if (jobHasAuthToken && authTokenWasGiven) {
return isEqual(token.getBytes(UTF_8), givenToken.getBytes(UTF_8));
final String configuredToken, final String givenToken) {
final boolean jobHasConfiguredToken = !isNullOrEmpty(configuredToken);
final boolean tokenWasGiven = !isNullOrEmpty(givenToken);
if (jobHasConfiguredToken && tokenWasGiven) {
return isEqual(configuredToken.getBytes(UTF_8), givenToken.getBytes(UTF_8));
}
if (!jobHasAuthToken && !authTokenWasGiven) {
if (!jobHasConfiguredToken && !tokenWasGiven) {
return true;
}
return false;
}

/** This is the token configured in the job. A feature found in Jenkins core. */
@SuppressWarnings("deprecation")
private static boolean authenticationTokenMatches(
final hudson.model.BuildAuthorizationToken authToken, final String givenToken) {

final boolean jobHasAuthToken = jobHasAuthToken(authToken);
final boolean authTokenWasGiven = !isNullOrEmpty(givenToken);
if (jobHasAuthToken && authTokenWasGiven) {
return isEqual(authToken.getToken().getBytes(UTF_8), givenToken.getBytes(UTF_8));
}
if (!jobHasAuthToken && !authTokenWasGiven) {
return true;
}
return false;
}

@SuppressWarnings("deprecation")
private static boolean jobHasAuthToken(final hudson.model.BuildAuthorizationToken authToken) {
return authToken != null && !isNullOrEmpty(authToken.getToken());
}

private static GenericTrigger findGenericTrigger(
final Map<TriggerDescriptor, Trigger<?>> triggers) {
if (triggers == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ public void testThatJobsWithTokenIsFoundWhenTokenSuppliedAndMatchesABC() {
final List<String> actual = this.findAllJobs(givenToken);

assertThat(actual) //
.containsExactly(
this.job3WithAuthTokenAbc.getFullName(), this.job5WithGenericTokenAbc.getFullName());
.containsExactly(this.job5WithGenericTokenAbc.getFullName());
assertThat(this.didImpersonate) //
.isTrue();
}
Expand All @@ -115,8 +114,7 @@ public void testThatJobsWithTokenIsFoundWhenTokenSuppliedAndMatchesDEF() {
final List<String> actual = this.findAllJobs(givenToken);

assertThat(actual) //
.containsExactly(
this.job4WithAuthTokenDef.getFullName(), this.job6WithGenericTokenDef.getFullName());
.containsExactly(this.job6WithGenericTokenDef.getFullName());
assertThat(this.didImpersonate) //
.isTrue();
}
Expand All @@ -128,7 +126,11 @@ public void testThatJobsWithoutTokenIsFoundWhenTokenNotSupplied() {
final List<String> actual = this.findAllJobs(givenToken);

assertThat(actual) //
.containsExactly(this.job1WithNoToken.getFullName(), this.job2WithNoToken.getFullName());
.containsExactly(
this.job1WithNoToken.getFullName(),
this.job2WithNoToken.getFullName(),
this.job3WithAuthTokenAbc.getFullName(),
this.job4WithAuthTokenDef.getFullName());
assertThat(this.didImpersonate) //
.isFalse();
}
Expand Down

0 comments on commit d1d7ad6

Please sign in to comment.