diff --git a/acme4j-client/src/main/java/org/shredzone/acme4j/provider/pebble/PebbleAcmeProvider.java b/acme4j-client/src/main/java/org/shredzone/acme4j/provider/pebble/PebbleAcmeProvider.java new file mode 100644 index 00000000..99336d3f --- /dev/null +++ b/acme4j-client/src/main/java/org/shredzone/acme4j/provider/pebble/PebbleAcmeProvider.java @@ -0,0 +1,73 @@ +/* + * acme4j - Java ACME client + * + * Copyright (C) 2017 Richard "Shred" Körber + * http://acme4j.shredzone.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +package org.shredzone.acme4j.provider.pebble; + +import java.net.MalformedURLException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.shredzone.acme4j.provider.AbstractAcmeProvider; +import org.shredzone.acme4j.provider.AcmeProvider; + +/** + * An {@link AcmeProvider} for Pebble. + *

+ * Pebble is a small ACME test server. + * This provider can be used to connect to an instance of a Pebble server. + *

+ * {@code "acme://pebble"} connects to a Pebble server running on localhost and listening + * on the standard port 14000. Using {@code "acme://pebble/other-host:12345"}, it is + * possible to connect to an external Pebble server on the given {@code other-host} and + * port. The port is optional, and if omitted, the standard port is used. + */ +public class PebbleAcmeProvider extends AbstractAcmeProvider { + + private static final Pattern HOST_PATTERN = Pattern.compile("^/([^:/]+)(?:\\:(\\d+))?/?$"); + + @Override + public boolean accepts(URI serverUri) { + return "acme".equals(serverUri.getScheme()) && "pebble".equals(serverUri.getHost()); + } + + @Override + public URI resolve(URI serverUri) { + try { + String path = serverUri.getPath(); + + URL baseUrl = new URL("http://localhost:14000/dir"); + + if (path != null && !path.isEmpty() && !"/".equals(path)) { + Matcher m = HOST_PATTERN.matcher(path); + if (m.matches()) { + String host = m.group(1); + int port = 14000; + if (m.group(2) != null) { + port = Integer.parseInt(m.group(2)); + } + baseUrl = new URL("http", host, port, "/dir"); + } else { + throw new IllegalArgumentException("Invalid Pebble host/port: " + path); + } + } + + return baseUrl.toURI(); + } catch (MalformedURLException | URISyntaxException ex) { + throw new IllegalArgumentException("Bad server URI " + serverUri, ex); + } + } + +} diff --git a/acme4j-client/src/main/resources/META-INF/services/org.shredzone.acme4j.provider.AcmeProvider b/acme4j-client/src/main/resources/META-INF/services/org.shredzone.acme4j.provider.AcmeProvider index 345dedc9..57713718 100644 --- a/acme4j-client/src/main/resources/META-INF/services/org.shredzone.acme4j.provider.AcmeProvider +++ b/acme4j-client/src/main/resources/META-INF/services/org.shredzone.acme4j.provider.AcmeProvider @@ -3,3 +3,6 @@ org.shredzone.acme4j.provider.GenericAcmeProvider # Let's Encrypt: https://letsencrypt.org org.shredzone.acme4j.provider.letsencrypt.LetsEncryptAcmeProvider + +# Pebble (ACME Test Server): https://github.com/letsencrypt/pebble +org.shredzone.acme4j.provider.pebble.PebbleAcmeProvider diff --git a/acme4j-client/src/test/java/org/shredzone/acme4j/provider/pebble/PebbleAcmeProviderTest.java b/acme4j-client/src/test/java/org/shredzone/acme4j/provider/pebble/PebbleAcmeProviderTest.java new file mode 100644 index 00000000..d7d837d0 --- /dev/null +++ b/acme4j-client/src/test/java/org/shredzone/acme4j/provider/pebble/PebbleAcmeProviderTest.java @@ -0,0 +1,78 @@ +/* + * acme4j - Java ACME client + * + * Copyright (C) 2017 Richard "Shred" Körber + * http://acme4j.shredzone.org + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + */ +package org.shredzone.acme4j.provider.pebble; + +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.*; + +import java.net.URI; +import java.net.URISyntaxException; + +import org.junit.Test; + +/** + * Unit tests for {@link PebbleAcmeProvider}. + */ +public class PebbleAcmeProviderTest { + + /** + * Tests if the provider accepts the correct URIs. + */ + @Test + public void testAccepts() throws URISyntaxException { + PebbleAcmeProvider provider = new PebbleAcmeProvider(); + + assertThat(provider.accepts(new URI("acme://pebble")), is(true)); + assertThat(provider.accepts(new URI("acme://pebble/")), is(true)); + assertThat(provider.accepts(new URI("acme://pebble/some-host.example.com")), is(true)); + assertThat(provider.accepts(new URI("acme://pebble/some-host.example.com:12345")), is(true)); + assertThat(provider.accepts(new URI("acme://example.com")), is(false)); + assertThat(provider.accepts(new URI("http://example.com/acme")), is(false)); + assertThat(provider.accepts(new URI("https://example.com/acme")), is(false)); + } + + /** + * Test if acme URIs are properly resolved. + */ + @Test + public void testResolve() throws URISyntaxException { + PebbleAcmeProvider provider = new PebbleAcmeProvider(); + + assertThat(provider.resolve(new URI("acme://pebble")), + is(new URI("http://localhost:14000/dir"))); + assertThat(provider.resolve(new URI("acme://pebble/")), + is(new URI("http://localhost:14000/dir"))); + assertThat(provider.resolve(new URI("acme://pebble/pebble.example.com")), + is(new URI("http://pebble.example.com:14000/dir"))); + assertThat(provider.resolve(new URI("acme://pebble/pebble.example.com:12345")), + is(new URI("http://pebble.example.com:12345/dir"))); + assertThat(provider.resolve(new URI("acme://pebble/pebble.example.com:12345/")), + is(new URI("http://pebble.example.com:12345/dir"))); + + try { + provider.resolve(new URI("acme://pebble/bad.example.com:port")); + fail("accepted bad port"); + } catch (IllegalArgumentException ex) { + // expected + } + + try { + provider.resolve(new URI("acme://pebble/bad.example.com:1234/foo")); + fail("accepted invalid path"); + } catch (IllegalArgumentException ex) { + // expected + } + } + +} diff --git a/src/site/markdown/ca/index.md b/src/site/markdown/ca/index.md index da42ad70..5d9376e9 100644 --- a/src/site/markdown/ca/index.md +++ b/src/site/markdown/ca/index.md @@ -34,3 +34,4 @@ URI website = meta.getWebsite(); In _acme4j_ these providers are available: * [Let's Encrypt](./letsencrypt.html) +* [Pebble](./pebble.html) diff --git a/src/site/markdown/ca/pebble.md b/src/site/markdown/ca/pebble.md new file mode 100644 index 00000000..b79b6829 --- /dev/null +++ b/src/site/markdown/ca/pebble.md @@ -0,0 +1,13 @@ +# Pebble + +[Pebble](https://github.com/letsencrypt/pebble) is a small ACME test server. + +This ACME provider can be used to connect to a local Pebble server instance, mainly for running integration tests. + +## Connection URIs + +* `acme://pebble` - Connect to a Pebble server at `localhost` and standard port 14000. +* `acme://pebble/pebble.example.com` - Connect to a Pebble server at `pebble.example.com` and standard port 14000. +* `acme://pebble/pebble.example.com:12345` - Connect to a Pebble server at `pebble.example.com` and port 12345. + +Pebble contains an integrated web server that only accepts HTTP connections, so HTTPS connections are not supported by this provider. diff --git a/src/site/site.xml b/src/site/site.xml index 0c98a1f7..4664b3c8 100644 --- a/src/site/site.xml +++ b/src/site/site.xml @@ -45,6 +45,7 @@ +