Skip to content

Commit

Permalink
JENKINS-43022 - Honor git credential path exclusion and inclusion
Browse files Browse the repository at this point in the history
Tests scp URL form in GitURIRequirementsBuilderTest

Adds assertions for path.getPath(), including incorrect relative scp path.

Credentials PathRequirement incorrectly converts relative path to
absolute path.  Adapt the test to that error, since PathRequirement
is likely used in many other cases where the prefixing of a '/' to the
relative path is the expected and desired behavior.
  • Loading branch information
juan francisco sanchez authored and MarkEWaite committed Jul 1, 2017
1 parent eef4e1d commit a865e01
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 25 deletions.
Expand Up @@ -3,6 +3,7 @@
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.HostnamePortRequirement;
import com.cloudbees.plugins.credentials.domains.HostnameRequirement;
import com.cloudbees.plugins.credentials.domains.PathRequirement;
import com.cloudbees.plugins.credentials.domains.SchemeRequirement;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
Expand Down Expand Up @@ -190,34 +191,37 @@ public GitURIRequirementsBuilder withUri(@CheckForNull String uri) {
if (uri != null) {
Matcher matcher = SINGLE_SLASH_FILE_URI.matcher(uri);
if (matcher.matches()) {
return withScheme("file").withoutHostname().withoutHostnamePort();
return withScheme("file").withPath(matcher.group(2)).withoutHostname().withoutHostnamePort();
}
matcher = FULL_URI.matcher(uri);
if (matcher.matches()) {
withScheme(matcher.group(1));
if (!"file".equals(matcher.group(1)) && matcher.group(4) != null) {
withPath(matcher.group(7));
if (matcher.group(5) != null) {
withHostnamePort(matcher.group(4), Integer.parseInt(matcher.group(5)));
} else {
withHostname(matcher.group(4)).withoutHostnamePort();
}
} else {
withPath(matcher.group(4)+"/"+matcher.group(7));
}
return this;
}
matcher = RELATIVE_SCP_URI.matcher(uri);
if (matcher.matches()) {
return withScheme("ssh").withHostnamePort(matcher.group(3),22);
return withScheme("ssh").withPath(matcher.group(4)).withHostnamePort(matcher.group(3),22);
}
matcher = ABSOLUTE_SCP_URI.matcher(uri);
if (matcher.matches()) {
return withScheme("ssh").withHostnamePort(matcher.group(3),22);
return withScheme("ssh").withPath(matcher.group(4)).withHostnamePort(matcher.group(3),22);
}
matcher = LOCAL_FILE.matcher(uri);
if (matcher.matches()) {
return withScheme("file").withoutHostname().withoutHostnamePort();
return withScheme("file").withPath(matcher.group(2)).withoutHostname().withoutHostnamePort();
}
}
return withoutScheme().withoutHostname().withoutHostnamePort();
return withoutScheme().withoutPath().withoutHostname().withoutHostnamePort();
}

/**
Expand All @@ -236,6 +240,22 @@ public GitURIRequirementsBuilder withoutScheme() {
return this;
}

/**
* Removes any path requirements.
*
* @return {@code this}.
*/
@NonNull
public GitURIRequirementsBuilder withoutPath() {
for (Iterator<DomainRequirement> iterator = requirements.iterator(); iterator.hasNext(); ) {
DomainRequirement r = iterator.next();
if (r instanceof PathRequirement) {
iterator.remove();
}
}
return this;
}

/**
* Removes any hostname or hostname:port requirements.
*
Expand Down Expand Up @@ -283,6 +303,21 @@ public GitURIRequirementsBuilder withScheme(@CheckForNull String scheme) {
return this;
}

/**
* Replace any path requirements with the supplied path.
*
* @param path to use as a requirement
* @return {@code this}.
*/
@NonNull
public GitURIRequirementsBuilder withPath(@CheckForNull String path) {
withoutPath();
if (path != null) {
requirements.add(new PathRequirement(path));
}
return this;
}

/**
* Replace any hostname requirements with the supplied hostname.
*
Expand Down

0 comments on commit a865e01

Please sign in to comment.