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

Fix basic authentication #158

Merged
merged 1 commit into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@
<artifactId>xstream</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>3.12.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<repositories>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,11 @@ public Boolean invoke(File ws, VirtualChannel channel) {
listener.getLogger().println("Started downloading file from " + resolvedUrl);
HttpHost host = new HttpHost(uri.getScheme(), uri.getHost(), uri.getPort());
BasicCredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), new UsernamePasswordCredentials(resolvedUserName, resolvedPassword.toCharArray()));
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(resolvedUserName, resolvedPassword.toCharArray());
credsProvider.setCredentials(new AuthScope(uri.getHost(), uri.getPort()), credentials);
AuthCache authCache = new BasicAuthCache();
BasicScheme basicAuth = new BasicScheme();
basicAuth.initPreemptive(credentials);
authCache.put(host, basicAuth);

HttpClientBuilder httpClientBuilder = HttpClients.custom()
Expand Down
135 changes: 135 additions & 0 deletions src/test/java/sp/sd/fileoperations/FileDownloadOperationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package sp.sd.fileoperations;

import com.github.tomakehurst.wiremock.junit5.WireMockExtension;
import hidden.jth.org.apache.commons.lang3.RandomStringUtils;
import hudson.model.FreeStyleProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import org.apache.hc.core5.http.HttpHeaders;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import java.io.File;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* Test class to validate {@link FileDownloadOperation}.
*/
@WithJenkins
class FileDownloadOperationTest {

// matches the file in __files
private static final String DUMMY_ZIP = "dummy.zip";
private static final String TEST_PATH = "/test/" + DUMMY_ZIP;

@RegisterExtension
static WireMockExtension wireMock = WireMockExtension.newInstance()
.options(options().dynamicPort())
.build();

@Test
void shouldDownload(JenkinsRule r) throws Exception {

// endpoint without authentication
wireMock.stubFor(get(urlEqualTo(TEST_PATH))
.willReturn(aResponse()
.withStatus(HttpStatus.SC_OK)
.withBodyFile(DUMMY_ZIP) // matches the file in __files
.withHeader(HttpHeaders.CONTENT_TYPE, "application/zip")));

// define operation & run
FreeStyleProject project = r.createFreeStyleProject();
Run run = project.scheduleBuild2(0).get();
FileDownloadOperation operation = new FileDownloadOperation(
wireMock.baseUrl() + TEST_PATH,
"",
"",
run.getRootDir().getAbsolutePath(),
DUMMY_ZIP,
null,
null);
boolean result = operation.runOperation(run, r.jenkins.getWorkspaceFor(project), null, TaskListener.NULL);
File download = new File(operation.getTargetLocation(), operation.getTargetFileName());

// validate
assertTrue(result);
assertTrue(download.exists());
assertTrue(download.length() > 0);
}

@Test
void shouldDownloadWithBasicAuth(JenkinsRule r) throws Exception {
String username = RandomStringUtils.secure().nextAlphabetic(10);
String password = RandomStringUtils.secure().nextAlphabetic(10);

// endpoint with authentication
wireMock.stubFor(get(urlEqualTo(TEST_PATH))
.withBasicAuth(username, password)
.willReturn(aResponse()
.withStatus(HttpStatus.SC_OK)
.withBodyFile(DUMMY_ZIP) // matches the file in __files
.withHeader(HttpHeaders.CONTENT_TYPE, "application/zip")));

// define operation & run
FreeStyleProject project = r.createFreeStyleProject();
Run run = project.scheduleBuild2(0).get();
FileDownloadOperation operation = new FileDownloadOperation(
wireMock.baseUrl() + TEST_PATH,
username,
password,
run.getRootDir().getAbsolutePath(),
DUMMY_ZIP,
null,
null);
boolean result = operation.runOperation(run, r.jenkins.getWorkspaceFor(project), null, TaskListener.NULL);
File download = new File(operation.getTargetLocation(), operation.getTargetFileName());

// validate
assertTrue(result);
assertTrue(download.exists());
assertTrue(download.length() > 0);
}

@Test
void shouldFailWithBadCredentials(JenkinsRule r) throws Exception {
String username = RandomStringUtils.secure().nextAlphabetic(10);
String password = RandomStringUtils.secure().nextAlphabetic(10);

// endpoint with authentication
wireMock.stubFor(get(urlEqualTo(TEST_PATH))
.withBasicAuth(username, password)
.willReturn(aResponse()
.withStatus(HttpStatus.SC_OK)
.withBodyFile(DUMMY_ZIP) // matches the file in __files
.withHeader(HttpHeaders.CONTENT_TYPE, "application/zip")));

// define operation & run
FreeStyleProject project = r.createFreeStyleProject();
Run run = project.scheduleBuild2(0).get();
FileDownloadOperation operation = new FileDownloadOperation(
wireMock.baseUrl() + TEST_PATH,
RandomStringUtils.secure().nextAlphabetic(10),
RandomStringUtils.secure().nextAlphabetic(10),
run.getRootDir().getAbsolutePath(),
DUMMY_ZIP,
null,
null);
boolean result = operation.runOperation(run, r.jenkins.getWorkspaceFor(project), null, TaskListener.NULL);
File download = new File(operation.getTargetLocation(), operation.getTargetFileName());

// validate
assertFalse(result);
assertTrue(download.exists()); // empty file created
assertEquals(0, download.length()); // empty file created
}
}
Binary file added src/test/resources/__files/dummy.zip
Binary file not shown.
Loading