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

feat: drop the v1/ suffix from base URLs #122

Merged
merged 1 commit into from
Nov 19, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ snyk.api.organization=

# The base URL for all Snyk API endpoints.
# Documentation: https://snyk.docs.apiary.io/#introduction/api-url
# Default: https://api.snyk.io/v1/
#snyk.api.url=https://api.snyk.io/v1/
# Default: https://api.snyk.io/
#snyk.api.url=https://api.snyk.io/

# Path to an SSL Certificate for Snyk API in PEM format.
#snyk.api.sslCertificatePath=
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.snyk.plugins.artifactory;

import io.snyk.plugins.artifactory.audit.AuditModule;
import io.snyk.plugins.artifactory.configuration.BaseUrlSanitiser;
import io.snyk.plugins.artifactory.configuration.UserAgent;
import io.snyk.plugins.artifactory.configuration.properties.ArtifactProperty;
import io.snyk.plugins.artifactory.configuration.ConfigurationModule;
Expand Down Expand Up @@ -171,12 +172,7 @@ private SnykClient createSnykClient(@Nonnull ConfigurationModule configurationMo
trustAllCertificates = true;
}

if (!baseUrl.endsWith("/")) {
if (LOG.isWarnEnabled()) {
LOG.warn("'{}' must end in /, your value is '{}'", API_URL.propertyKey(), baseUrl);
}
baseUrl = baseUrl + "/";
}
baseUrl = new BaseUrlSanitiser().sanitise(baseUrl);

String sslCertificatePath = configurationModule.getPropertyOrDefault(API_SSL_CERTIFICATE_PATH);
String httpProxyHost = configurationModule.getPropertyOrDefault(HTTP_PROXY_HOST);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.snyk.plugins.artifactory.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseUrlSanitiser {

private static final Logger LOG = LoggerFactory.getLogger(BaseUrlSanitiser.class);

public String sanitise(String baseUrl) {
baseUrl = baseUrl.trim();
if (!baseUrl.endsWith("/")) {
baseUrl = baseUrl + "/";
}
if (baseUrl.endsWith("v1/")) {
baseUrl = baseUrl.replaceAll("v1/$", "");
LOG.warn("Stripping off trailing 'v1' from base url path");
}
LOG.info("Sanitised base URL: {}", baseUrl);
return baseUrl;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public enum PluginConfiguration implements Configuration {
// general settings
API_URL("snyk.api.url", "https://api.snyk.io/v1/"),
API_URL("snyk.api.url", "https://api.snyk.io/"),
API_TOKEN("snyk.api.token", ""),
API_ORGANIZATION("snyk.api.organization", ""),
API_SSL_CERTIFICATE_PATH("snyk.api.sslCertificatePath", ""),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public io.snyk.plugins.artifactory.model.TestResult scan(FileLayoutInfo fileLayo
LOG.debug("Running Snyk test: {}", repoPath);
result = snykClient.get(TestResult.class, request ->
request
.withPath(String.format("test/maven/%s/%s/%s",
.withPath(String.format("v1/test/maven/%s/%s/%s",
URLEncoder.encode(groupID, UTF_8),
URLEncoder.encode(artifactID, UTF_8),
URLEncoder.encode(artifactVersion, UTF_8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public io.snyk.plugins.artifactory.model.TestResult scan(FileLayoutInfo fileLayo
LOG.debug("Running Snyk test: {}", repoPath);
result = snykClient.get(TestResult.class, request ->
request
.withPath(String.format("test/npm/%s/%s",
.withPath(String.format("v1/test/npm/%s/%s",
URLEncoder.encode(details.name, UTF_8),
URLEncoder.encode(details.version, UTF_8)
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public io.snyk.plugins.artifactory.model.TestResult scan(FileLayoutInfo fileLayo
LOG.debug("Running Snyk test: {}", repoPath);
result = snykClient.get(TestResult.class, request ->
request
.withPath(String.format("test/pip/%s/%s",
.withPath(String.format("v1/test/pip/%s/%s",
URLEncoder.encode(details.name, UTF_8),
URLEncoder.encode(details.version, UTF_8))
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.snyk.plugins.artifactory.configuration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class BaseUrlSanitiserTest {

BaseUrlSanitiser sanitiser;

@BeforeEach
void setUp() {
sanitiser = new BaseUrlSanitiser();
}

@Test
void ensuresUrlEndsWithSlash() {
assertThat(sanitiser.sanitise("https://api.snyk.io")).isEqualTo("https://api.snyk.io/");
assertThat(sanitiser.sanitise("https://api.snyk.io/")).isEqualTo("https://api.snyk.io/");
}

@Test
void stripOffTrailingV1() {
assertThat(sanitiser.sanitise("https://api.snyk.io/v1")).isEqualTo("https://api.snyk.io/");
assertThat(sanitiser.sanitise("https://api.snyk.io/v1/")).isEqualTo("https://api.snyk.io/");
}

@Test
void handlesUntrimmedUrls() {
assertThat(sanitiser.sanitise("\r\nhttps://api.snyk.io/\r\n")).isEqualTo("https://api.snyk.io/");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PluginConfigurationTest {
@Test
void checkDefaultValues() {
assertAll("should be not empty",
() -> assertEquals("https://api.snyk.io/v1/", API_URL.defaultValue(), getAssertionMessage(API_URL, "default value must be 'https://api.snyk.io/v1/'")),
() -> assertEquals("https://api.snyk.io/", API_URL.defaultValue(), getAssertionMessage(API_URL, "default value must be 'https://api.snyk.io/'")),
() -> assertEquals("false", SCANNER_BLOCK_ON_API_FAILURE.defaultValue(), getAssertionMessage(SCANNER_BLOCK_ON_API_FAILURE, "default value must be 'false'")),
() -> assertEquals("low", SCANNER_VULNERABILITY_THRESHOLD.defaultValue(), getAssertionMessage(SCANNER_VULNERABILITY_THRESHOLD, "default value must be 'low'")),
() -> assertEquals("low", SCANNER_LICENSE_THRESHOLD.defaultValue(), getAssertionMessage(SCANNER_LICENSE_THRESHOLD, "default value must be 'low'"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
snyk.api.url=https://api.snyk.io/v1/
snyk.api.url=https://api.snyk.io/
snyk.api.token=my-api-token
snyk.api.organization=my-api-organization
#
Expand Down
55 changes: 0 additions & 55 deletions snyk-sdk-tests/pom.xml

This file was deleted.

33 changes: 0 additions & 33 deletions snyk-sdk-tests/src/test/java/io/snyk/sdk/SnykClientTest.java

This file was deleted.

2 changes: 1 addition & 1 deletion snyk-sdk/src/main/java/io/snyk/sdk/SnykConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public static SnykConfig withDefaults() {

public static class Builder {
private String token;
private String baseUrl = "https://api.snyk.io/v1/";
private String baseUrl = "https://api.snyk.io/";
private String userAgent = "snyk-sdk-java";
private boolean trustAllCertificates = false;
private String sslCertificatePath = "";
Expand Down
2 changes: 1 addition & 1 deletion snyk-sdk/src/main/java/io/snyk/sdk/api/SnykClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public <TResult> SnykResult<TResult> get(Class<TResult> resultType, Function<Sny
public SnykResult<NotificationSettings> getNotificationSettings(String org) throws java.io.IOException, java.lang.InterruptedException {
return get(NotificationSettings.class, request ->
request
.withPath(String.format("user/me/notification-settings/org/%s", URLEncoder.encode(org, UTF_8)))
.withPath(String.format("v1/user/me/notification-settings/org/%s", URLEncoder.encode(org, UTF_8)))
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.snyk.sdk.api.v1;
package io.snyk.sdk.api;

import io.snyk.sdk.SnykConfig;
import io.snyk.sdk.api.SnykHttpRequestBuilder;
import org.junit.jupiter.api.Test;

import java.util.Optional;
Expand All @@ -17,7 +16,7 @@ void shouldIncludeBaseUrlWithPath() {
assertEquals(SnykHttpRequestBuilder.create(configWithDefaultBaseUrl)
.build()
.uri().toString(),
"https://api.snyk.io/v1/");
"https://api.snyk.io/");

String otherBaseUrl = "https://other-host/some-prefix/";
SnykConfig configWithDifferentBaseUrl = SnykConfig.newBuilder().setBaseUrl(otherBaseUrl).build();
Expand All @@ -35,7 +34,7 @@ void shouldIncludeBaseUrlWithPath() {
void shouldOnlyIncludeNonNullQueryParameters() {
SnykConfig config = SnykConfig.withDefaults();

assertEquals("https://api.snyk.io/v1/some/endpoint?org=abc123",
assertEquals("https://api.snyk.io/some/endpoint?org=abc123",
SnykHttpRequestBuilder.create(config)
.withPath("some/endpoint")
.withQueryParam("org", "abc123")
Expand All @@ -49,7 +48,7 @@ void shouldOnlyIncludeNonNullQueryParameters() {
@Test
void shouldOnlyIncludePresentQueryParameters() {
SnykConfig config = SnykConfig.withDefaults();
assertEquals("https://api.snyk.io/v1/some/endpoint?org=abc123",
assertEquals("https://api.snyk.io/some/endpoint?org=abc123",
SnykHttpRequestBuilder.create(config)
.withPath("some/endpoint")
.withQueryParam("org", Optional.of("abc123"))
Expand All @@ -63,7 +62,7 @@ void shouldOnlyIncludePresentQueryParameters() {
@Test
void shouldIncludeMultipleQueryParameters() {
SnykConfig config = SnykConfig.withDefaults();
assertEquals("https://api.snyk.io/v1/some/endpoint?org=abc123&foo=bar",
assertEquals("https://api.snyk.io/some/endpoint?org=abc123&foo=bar",
SnykHttpRequestBuilder.create(config)
.withPath("some/endpoint")
.withQueryParam("org", "abc123")
Expand Down
Loading