Skip to content

Commit

Permalink
Merge pull request #158 from xsalefter/platform_157-fix_extra_slash
Browse files Browse the repository at this point in the history
fix extra slash
  • Loading branch information
xsalefter authored Mar 12, 2024
2 parents 7dc9985 + 09511bf commit d4f03d5
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 10 deletions.
4 changes: 2 additions & 2 deletions osgi-bundles/bundles/kpm/CONFIGURATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ org.killbill.billing.plugin.kpm.connectTimeoutSec=60
# 3. This is a required configuration to get Kill Bill version (See more AvailablePluginsComponentsFactory.createVersionsProvider() )
# In codebase, if *.kpm.nexusUrl value not set, or contains "oss.sonatype.org", the final construct of URL would be
# ${*.kpm.nexusUrl} + "/content/repositories" + ${*.kpm.nexusRepository}.
org.killbill.billing.plugin.kpm.nexusUrl=https://oss.sonatype.org/
org.killbill.billing.plugin.kpm.nexusRepository=releases
org.killbill.billing.plugin.kpm.nexusUrl=https://oss.sonatype.org
org.killbill.billing.plugin.kpm.nexusRepository=/releases

# How Authentication header will construct. If none, then KPMPlugin will not send "Authorization" header when download
# any files. If BASIC, then *.kpm.nexusAuthToken value will be ignored and will use username/password values).
Expand Down
2 changes: 1 addition & 1 deletion osgi-bundles/bundles/kpm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ In KPM Plugin, there are 2 types of repositories:
The configurations path are:
```properties
org.killbill.billing.plugin.kpm.nexusUrl=https://dl.cloudsmith.io/<any>/killbill/<account>
org.killbill.billing.plugin.kpm.nexusRepository=maven
org.killbill.billing.plugin.kpm.nexusRepository=/maven
```
Configuration above will try to get killbill information from URI: `https://dl.cloudsmith.io/<any>/killbill/<account>/maven`
You can set `*.kpm.nexusAuthMethod|nexusAuthUsername|nexusAuthPassword|nexusAuthToken` as needed. If your repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ public void start(final BundleContext context) throws Exception {

final Properties properties = configProperties.getProperties();
final KpmProperties kpmProperties = new KpmProperties(properties);
final KpmPropertiesValidator propertiesValidator = new KpmPropertiesValidator(kpmProperties);
try {
propertiesValidator.validate();
} catch (final KPMPluginException e) {
logger.error("Error validating KPM properties", e);
throw e;
}

final DefaultPluginManager pluginManager = new DefaultPluginManager(killbillAPI, kpmProperties);
eventsListener = new EventsListener(pluginManager);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public String getKillbillAdminPassword() {
* @return get {@code org.killbill.billing.plugin.kpm.nexusUrl} property, or {@code https://oss.sonatype.org/} if not set.
*/
public String getNexusUrl() {
return Objects.requireNonNullElse(properties.getProperty(PROPERTY_PREFIX + "nexusUrl"), "https://oss.sonatype.org/");
return Objects.requireNonNullElse(properties.getProperty(PROPERTY_PREFIX + "nexusUrl"), "https://oss.sonatype.org");
}

/**
* @return get {@code org.killbill.billing.plugin.kpm.nexusRepository} property, or {@code releases} if not set.
*/
public String getNexusRepository() {
return Objects.requireNonNullElse(properties.getProperty(PROPERTY_PREFIX + "nexusRepository"), "releases");
return Objects.requireNonNullElse(properties.getProperty(PROPERTY_PREFIX + "nexusRepository"), "/releases");
}

/**
Expand Down Expand Up @@ -243,7 +243,7 @@ public boolean isVerifySHA1() {

/**
* @return get {@code org.killbill.billing.plugin.kpm.pluginsInstall.coordinate.url} property, or
* {@link #getNexusUrl()} {@code + "/" + } {@link #getNexusRepository()}.
* {@link #getNexusUrl()} + {@link #getNexusRepository()}.
*/
public String getUrl() {
return Objects.requireNonNullElse(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2020-2024 Equinix, Inc
* Copyright 2014-2024 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package org.killbill.billing.osgi.bundles.kpm;

import org.killbill.commons.utils.annotation.VisibleForTesting;

public final class KpmPropertiesValidator {

private final KpmProperties kpmProperties;

public KpmPropertiesValidator(final KpmProperties kpmProperties) {
this.kpmProperties = kpmProperties;
}

public void validate() throws KPMPluginException {
this.validateNexusUrl();
this.validateNexusRepository();
}

@VisibleForTesting
void validateNexusUrl() throws KPMPluginException {
if (kpmProperties.getNexusUrl().endsWith("/")) {
throw new KPMPluginException("nexusUrl should not end with a trailing slash");
}
}

@VisibleForTesting
void validateNexusRepository() throws KPMPluginException {
final String toValidate = kpmProperties.getNexusRepository();
if (!toValidate.startsWith("/") || toValidate.endsWith("/")) {
throw new KPMPluginException("nexusRepository should start with a slash and not end with a trailing slash");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public UrlResolverFactory(final KpmProperties kpmProperties) {
// add something like "/content/repositories" at this point.
private String getValidUrlIfSonatype(final String url) {
return url.contains("oss.sonatype.org") ?
String.format("%scontent/repositories/%s", kpmProperties.getNexusUrl(), kpmProperties.getNexusRepository()) :
kpmProperties.getNexusUrl().concat("/content/repositories").concat(kpmProperties.getNexusRepository()) :
url;
}

Expand All @@ -56,17 +56,17 @@ public UriResolver getVersionsProviderUrlResolver() {
final AuthenticationMethod authMethod = AuthenticationMethod.valueOf(kpmProperties.getNexusAuthMethod().toUpperCase());
switch (authMethod) {
case NONE:
String baseUrl = getValidUrlIfSonatype(kpmProperties.getNexusUrl() + kpmProperties.getNexusRepository());
String baseUrl = getValidUrlIfSonatype(kpmProperties.getNexusUrl() + kpmProperties.getNexusRepository());
return new NoneUriResolver(baseUrl);

case BASIC:
baseUrl = kpmProperties.getNexusUrl() + "/" + kpmProperties.getNexusRepository();
baseUrl = kpmProperties.getNexusUrl().concat(kpmProperties.getNexusRepository());
final String username = kpmProperties.getNexusAuthUsername();
final String password = kpmProperties.getNexusAuthPassword();
return new BasicUriResolver(baseUrl, username, password);

case TOKEN:
baseUrl = kpmProperties.getNexusUrl() + "/" + kpmProperties.getNexusRepository();
baseUrl = kpmProperties.getNexusUrl().concat(kpmProperties.getNexusRepository());
return new TokenUriResolver(baseUrl, kpmProperties.getNexusAuthToken());

default: throw new IllegalStateException("Unknown authentication method: " + kpmProperties.getNexusAuthMethod());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2020-2024 Equinix, Inc
* Copyright 2014-2024 The Billing Project, LLC
*
* The Billing Project licenses this file to you under the Apache License, version 2.0
* (the "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package org.killbill.billing.osgi.bundles.kpm;

import org.mockito.Mockito;
import org.testng.Assert;
import org.testng.annotations.Test;

public class TestKpmPropertiesValidator {

@Test(groups = "fast")
public void testValidateNexusUrl_noTrailingSlash() throws KPMPluginException {
// Valid
final KpmProperties kpmProperties = Mockito.mock(KpmProperties.class);
Mockito.when(kpmProperties.getNexusUrl()).thenReturn("http://example.com");
KpmPropertiesValidator validator = new KpmPropertiesValidator(kpmProperties);
validator.validateNexusUrl();

// Invalid: end with "/"
Mockito.when(kpmProperties.getNexusUrl()).thenReturn("http://example.com/");
validator = new KpmPropertiesValidator(kpmProperties);
try {
validator.validateNexusUrl();
} catch (final KPMPluginException e) {
Assert.assertEquals(e.getMessage(), "nexusUrl should not end with a trailing slash");
}
}

@Test(groups = "fast")
public void testValidateNexusRepository() throws KPMPluginException {
final KpmProperties kpmProperties = Mockito.mock(KpmProperties.class);

// Valid
Mockito.when(kpmProperties.getNexusRepository()).thenReturn("/valid/nexus/repository/location");
KpmPropertiesValidator validator = new KpmPropertiesValidator(kpmProperties);
validator.validateNexusRepository();

// Should start with "/"
Mockito.when(kpmProperties.getNexusRepository()).thenReturn("some/invalid/repository");
validator = new KpmPropertiesValidator(kpmProperties);
try {
validator.validateNexusRepository();
} catch (final KPMPluginException e) {
Assert.assertEquals(e.getMessage(), "nexusRepository should start with a slash and not end with a trailing slash");
}

// Should never end with "/"
Mockito.when(kpmProperties.getNexusRepository()).thenReturn("/some/other/invalid/");
validator = new KpmPropertiesValidator(kpmProperties);
try {
validator.validateNexusRepository();
} catch (final KPMPluginException e) {
Assert.assertEquals(e.getMessage(), "nexusRepository should start with a slash and not end with a trailing slash");
}
}
}

0 comments on commit d4f03d5

Please sign in to comment.