Skip to content

Commit

Permalink
Metadata Repositories: Support Bearer Token Authentication NuGet
Browse files Browse the repository at this point in the history
Signed-off-by: Valentijn Scholten <[email protected]>
  • Loading branch information
valentijnscholten committed Jan 4, 2025
1 parent 7de8f33 commit 80198f8
Showing 1 changed file with 20 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,17 @@
*/
package org.dependencytrack.tasks.repositories;

import com.github.packageurl.PackageURL;
import static org.dependencytrack.tasks.repositories.NugetMetaAnalyzer.SUPPORTED_DATE_FORMATS;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.http.HttpHeaders;
import org.dependencytrack.model.Component;
import org.dependencytrack.model.RepositoryType;
Expand All @@ -29,16 +39,7 @@
import org.mockserver.client.MockServerClient;
import org.mockserver.integration.ClientAndServer;

import java.nio.file.Files;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import static org.dependencytrack.tasks.repositories.NugetMetaAnalyzer.SUPPORTED_DATE_FORMATS;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import com.github.packageurl.PackageURL;

public class NugetMetaAnalyzerTest {

Expand Down Expand Up @@ -71,8 +72,8 @@ public void testAnalyzer() throws Exception {


// This test is to check if the analyzer is excluding pre-release versions
// The test is transitent depending on the current version of the package
// retrieved from the repository at the time of running.
// The test is transitent depending on the current version of the package
// retrieved from the repository at the time of running.
// When it was created, the latest release version was 9.0.0-preview.1.24080.9
@Test
public void testAnalyzerExcludingPreRelease() throws Exception {
Expand All @@ -91,8 +92,8 @@ public void testAnalyzerExcludingPreRelease() throws Exception {
}

// This test is to check if the analyzer is including pre-release versions
// The test is transitent depending on the current version of the package
// retrieved from the repository at the time of running.
// The test is transitent depending on the current version of the package
// retrieved from the repository at the time of running.
// When it was created, the latest release version was 9.0.0-preview.1.24080.9
@Test
public void testAnalyzerIncludingPreRelease() throws Exception {
Expand Down Expand Up @@ -125,7 +126,7 @@ public void testAnalyzerWithPrivatePackageRepository() throws Exception {
.withHeader(HttpHeaders.CONTENT_TYPE, "application/json")
.withBody(mockIndexResponse)
);
String encodedBasicHeader = "Basic OnBhc3N3b3Jk";
String encodedAuthHeader = "Bearer OnBhc3N3b3Jk";

String mockVersionResponse = readResourceFileToString("/unit/tasks/repositories/https---localhost-1080-v3-flat2" +
"-nunitprivate-index.json");
Expand All @@ -134,7 +135,7 @@ public void testAnalyzerWithPrivatePackageRepository() throws Exception {
request()
.withMethod("GET")
.withPath("/v3/flat2/nunitprivate/index.json")
.withHeader("Authorization", encodedBasicHeader)
.withHeader("Authorization", encodedAuthHeader)
)
.respond(
response()
Expand All @@ -150,7 +151,7 @@ public void testAnalyzerWithPrivatePackageRepository() throws Exception {
request()
.withMethod("GET")
.withPath("/v3/registrations2/nunitprivate/5.0.2.json")
.withHeader("Authorization", encodedBasicHeader)
.withHeader("Authorization", encodedAuthHeader)
)
.respond(
response()
Expand All @@ -161,7 +162,7 @@ public void testAnalyzerWithPrivatePackageRepository() throws Exception {
Component component = new Component();
component.setPurl(new PackageURL("pkg:nuget/[email protected]"));
NugetMetaAnalyzer analyzer = new NugetMetaAnalyzer();
analyzer.setRepositoryUsernameAndPassword(null, "password");
analyzer.setRepositoryUsernameAndPassword(null, "OnBhc3N3b3Jk");
analyzer.setRepositoryBaseUrl("http://localhost:1080");
MetaModel metaModel = analyzer.analyze(component);
Assert.assertEquals("5.0.2", metaModel.getLatestVersion());
Expand Down

0 comments on commit 80198f8

Please sign in to comment.