Add a provider for Pebble test servers

pull/55/head
Richard Körber 2017-02-11 15:11:09 +01:00
parent 1f4faf803b
commit 4312331fd9
6 changed files with 169 additions and 0 deletions

View File

@ -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 <em>Pebble</em>.
* <p>
* <a href="https://github.com/letsencrypt/pebble">Pebble</a> is a small ACME test server.
* This provider can be used to connect to an instance of a Pebble server.
* <p>
* {@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);
}
}
}

View File

@ -3,3 +3,6 @@ org.shredzone.acme4j.provider.GenericAcmeProvider
# Let's Encrypt: https://letsencrypt.org # Let's Encrypt: https://letsencrypt.org
org.shredzone.acme4j.provider.letsencrypt.LetsEncryptAcmeProvider org.shredzone.acme4j.provider.letsencrypt.LetsEncryptAcmeProvider
# Pebble (ACME Test Server): https://github.com/letsencrypt/pebble
org.shredzone.acme4j.provider.pebble.PebbleAcmeProvider

View File

@ -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
}
}
}

View File

@ -34,3 +34,4 @@ URI website = meta.getWebsite();
In _acme4j_ these providers are available: In _acme4j_ these providers are available:
* [Let's Encrypt](./letsencrypt.html) * [Let's Encrypt](./letsencrypt.html)
* [Pebble](./pebble.html)

View File

@ -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.

View File

@ -45,6 +45,7 @@
</item> </item>
<item name="CAs" href="ca/index.html"> <item name="CAs" href="ca/index.html">
<item name="Let's Encrypt" href="ca/letsencrypt.html"/> <item name="Let's Encrypt" href="ca/letsencrypt.html"/>
<item name="Pebble" href="ca/pebble.html"/>
</item> </item>
<item name="ACME Provider" href="provider.html"/> <item name="ACME Provider" href="provider.html"/>
</menu> </menu>