use of org.carapaceproxy.utils.RawHttpClient.HttpResponse in project carapaceproxy by diennea.
the class CertificatesTest method testUploadTypedCertificatesWithDaysBeforeRenewal.
@Test
@Parameters({ "acme", "manual" })
public void testUploadTypedCertificatesWithDaysBeforeRenewal(String type) throws Exception {
configureAndStartServer();
int port = server.getLocalPort();
DynamicCertificatesManager dynCertsMan = server.getDynamicCertificatesManager();
KeyPair endUserKeyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
Certificate[] chain = generateSampleChain(endUserKeyPair, false);
byte[] chainData = createKeystore(chain, endUserKeyPair.getPrivate());
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
// Create
HttpResponse resp = uploadCertificate("localhost2", "type=" + type + "&daysbeforerenewal=10", chainData, client, credentials);
if (type.equals("manual")) {
assertTrue(resp.getBodyString().contains("ERROR: param 'daysbeforerenewal' available for type 'acme' only"));
} else {
CertificateData data = dynCertsMan.getCertificateDataForDomain("localhost2");
assertNotNull(data);
assertEquals(10, data.getDaysBeforeRenewal());
}
// negative value
resp = uploadCertificate("localhost-negative", "type=" + type + "&daysbeforerenewal=-10", chainData, client, credentials);
if (type.equals("manual")) {
assertTrue(resp.getBodyString().contains("ERROR: param 'daysbeforerenewal' available for type 'acme' only"));
} else {
assertTrue(resp.getBodyString().contains("ERROR: param 'daysbeforerenewal' has to be a positive number"));
}
// default value
uploadCertificate("localhost-default", "type=" + type, chainData, client, credentials);
CertificateData data = dynCertsMan.getCertificateDataForDomain("localhost-default");
assertNotNull(data);
assertEquals(type.equals("manual") ? 0 : DEFAULT_DAYS_BEFORE_RENEWAL, data.getDaysBeforeRenewal());
// Update
uploadCertificate("localhost2", "type=" + type + "&daysbeforerenewal=45", chainData, client, credentials);
if (type.equals("manual")) {
assertTrue(resp.getBodyString().contains("ERROR: param 'daysbeforerenewal' available for type 'acme' only"));
} else {
data = dynCertsMan.getCertificateDataForDomain("localhost2");
assertNotNull(data);
assertEquals(45, data.getDaysBeforeRenewal());
}
// negative value
resp = uploadCertificate("localhost2", "type=" + type + "&daysbeforerenewal=-10", chainData, client, credentials);
if (type.equals("manual")) {
assertTrue(resp.getBodyString().contains("ERROR: param 'daysbeforerenewal' available for type 'acme' only"));
} else {
assertTrue(resp.getBodyString().contains("ERROR: param 'daysbeforerenewal' has to be a positive number"));
}
// default value
uploadCertificate("localhost2", "type=" + type, chainData, client, credentials);
data = dynCertsMan.getCertificateDataForDomain("localhost2");
assertNotNull(data);
assertEquals(type.equals("manual") ? 0 : DEFAULT_DAYS_BEFORE_RENEWAL, data.getDaysBeforeRenewal());
// changing the type (acme <-> manual)
String other = type.equals("manual") ? "acme" : "manual";
uploadCertificate("localhost2", "type=" + other, chainData, client, credentials);
data = dynCertsMan.getCertificateDataForDomain("localhost2");
assertNotNull(data);
assertEquals(other.equals("manual") ? 0 : DEFAULT_DAYS_BEFORE_RENEWAL, data.getDaysBeforeRenewal());
SSLCertificateConfiguration config = server.getCurrentConfiguration().getCertificates().get("localhost2");
assertEquals(other.equals("manual") ? 0 : DEFAULT_DAYS_BEFORE_RENEWAL, config.getDaysBeforeRenewal());
// checking for "certificate.X.daysbeforerenewal" property delete
ConfigurationStore store = server.getDynamicConfigurationStore();
assertEquals(other.equals("acme"), store.anyPropertyMatches((k, v) -> {
if (k.matches("certificate\\.[0-9]+\\.hostname") && v.equals("localhost2")) {
return store.getProperty(k.replace("hostname", "daysbeforerenewal"), null) != null;
}
return false;
}));
}
}
use of org.carapaceproxy.utils.RawHttpClient.HttpResponse in project carapaceproxy by diennea.
the class CertificatesTest method testCertificatesRenew.
@Test
public void testCertificatesRenew() throws Exception {
configureAndStartServer();
int port = server.getLocalPort();
DynamicCertificatesManager dcMan = server.getDynamicCertificatesManager();
dcMan.setPeriod(0);
// Uploading ACME certificate with data
KeyPair endUserKeyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
Certificate[] chain1 = generateSampleChain(endUserKeyPair, false);
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
byte[] chainData = createKeystore(chain1, endUserKeyPair.getPrivate());
HttpResponse resp = uploadCertificate("localhost", "type=acme&daysbeforerenewal=45", chainData, client, credentials);
assertTrue(resp.getBodyString().contains("SUCCESS"));
CertificateData data = dcMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertEquals(DynamicCertificateState.AVAILABLE, data.getState());
assertEquals(45, data.getDaysBeforeRenewal());
assertFalse(data.isManual());
// check uploaded certificate
try (RawHttpClient c = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse r = c.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", r.getBodyString());
Certificate[] obtainedChain = c.getServerCertificate();
assertNotNull(obtainedChain);
assertTrue(chain1[0].equals(obtainedChain[0]));
}
}
// Renew
KeyPair keyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
ConfigurationStore store = dcMan.getConfigurationStore();
store.saveKeyPairForDomain(keyPair, "localhost", false);
CertificateData cert = dcMan.getCertificateDataForDomain("localhost");
cert.setState(DynamicCertificateState.ORDERING);
cert.setPendingOrderLocation("https://localhost/orderlocation");
cert = dcMan.getCertificateDataForDomain("localhost");
assertNotNull(cert);
assertEquals(DynamicCertificateState.ORDERING, cert.getState());
// ACME mocking
ACMEClient ac = mock(ACMEClient.class);
Order o = mock(Order.class);
when(ac.getLogin()).thenReturn(mock(Login.class));
when(ac.checkResponseForOrder(any())).thenReturn(VALID);
org.shredzone.acme4j.Certificate _cert = mock(org.shredzone.acme4j.Certificate.class);
X509Certificate renewed = (X509Certificate) generateSampleChain(keyPair, false)[0];
when(_cert.getCertificateChain()).thenReturn(Arrays.asList(renewed));
when(ac.fetchCertificateForOrder(any())).thenReturn(_cert);
Whitebox.setInternalState(dcMan, ac);
// Renew
dcMan.run();
CertificateData updated = dcMan.getCertificateDataForDomain("localhost");
assertNotNull(updated);
assertEquals(DynamicCertificateState.AVAILABLE, updated.getState());
assertEquals(45, updated.getDaysBeforeRenewal());
assertFalse(updated.isManual());
// Check renewed certificate
try (RawHttpClient cl = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse r = cl.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", r.getBodyString());
Certificate[] obtainedChain = cl.getServerCertificate();
assertNotNull(obtainedChain);
assertTrue(renewed.equals(obtainedChain[0]));
}
}
use of org.carapaceproxy.utils.RawHttpClient.HttpResponse in project carapaceproxy by diennea.
the class CertificatesTest method test.
/**
* Test case: - Start server with a default certificate - Make request expected default certificate
*
* - Add a manual certificate to config (without upload) - Make request expected default certificate
*
* - Upload the certificate - Make request expected uploaded certificate
*
* - Update the certificate - Make request expected updated certificate
*/
@Test
public void test() throws Exception {
configureAndStartServer();
int port = server.getLocalPort();
// Request #0: expected default certificate
Certificate[] chain0;
try (RawHttpClient client = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse resp = client.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", resp.getBodyString());
chain0 = client.getServerCertificate();
assertNotNull(chain0);
}
// Update settings adding manual certificate (but without upload it)
config.put("certificate.2.hostname", "localhost");
config.put("certificate.2.mode", "manual");
changeDynamicConfiguration(config);
DynamicCertificatesManager dynCertMan = server.getDynamicCertificatesManager();
CertificateData data = dynCertMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertTrue(data.isManual());
// Request #1: still expected default certificate
Certificate[] chain1;
try (RawHttpClient client = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse resp = client.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", resp.getBodyString());
chain1 = client.getServerCertificate();
assertNotNull(chain1);
assertTrue(chain0[0].equals(chain1[0]));
}
// Upload manual certificate
Certificate[] uploadedChain;
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
KeyPair endUserKeyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
uploadedChain = generateSampleChain(endUserKeyPair, false);
byte[] chainData = createKeystore(uploadedChain, endUserKeyPair.getPrivate());
HttpResponse resp = uploadCertificate("localhost", null, chainData, client, credentials);
assertTrue(resp.getBodyString().contains("SUCCESS"));
data = dynCertMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertTrue(data.isManual());
assertTrue(data.getState() == DynamicCertificateState.AVAILABLE);
}
// Request #2: expected uploaded certificate
Certificate[] chain2;
try (RawHttpClient client = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse resp = client.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", resp.getBodyString());
chain2 = client.getServerCertificate();
assertNotNull(chain2);
assertTrue(uploadedChain[0].equals(chain2[0]));
}
// Update manual certificate
Certificate[] uploadedChain2;
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
KeyPair endUserKeyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
uploadedChain2 = generateSampleChain(endUserKeyPair, false);
assertFalse(uploadedChain[0].equals(uploadedChain2[0]));
byte[] chainData = createKeystore(uploadedChain2, endUserKeyPair.getPrivate());
HttpResponse resp = uploadCertificate("localhost", null, chainData, client, credentials);
assertTrue(resp.getBodyString().contains("SUCCESS"));
data = dynCertMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertTrue(data.isManual());
assertTrue(data.getState() == DynamicCertificateState.AVAILABLE);
}
// Request #3: expected last uploaded certificate
Certificate[] chain3;
try (RawHttpClient client = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse resp = client.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", resp.getBodyString());
chain3 = client.getServerCertificate();
assertNotNull(chain3);
assertTrue(uploadedChain2[0].equals(chain3[0]));
}
// this calls "reloadFromDB" > "manual" flag has to be retained even if not stored in db.
dynCertMan.setStateOfCertificate("localhost", DynamicCertificateState.WAITING);
data = dynCertMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertTrue(data.isManual());
}
use of org.carapaceproxy.utils.RawHttpClient.HttpResponse in project carapaceproxy by diennea.
the class CertificatesTest method testUploadTypedCertificate.
@Test
@Parameters({ "acme", "manual" })
public void testUploadTypedCertificate(String type) throws Exception {
configureAndStartServer();
int port = server.getLocalPort();
DynamicCertificatesManager dynCertsMan = server.getDynamicCertificatesManager();
// - for type="manual" is forbidden
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
HttpResponse resp = uploadCertificate("localhost", "type=" + type, new byte[0], client, credentials);
if (type.equals("manual")) {
assertTrue(resp.getBodyString().contains("ERROR: certificate data required"));
} else {
CertificateData data = dynCertsMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertFalse(data.isManual());
// no certificate-data uploaded
assertEquals(DynamicCertificateState.WAITING, dynCertsMan.getStateOfCertificate("localhost"));
}
}
// Uploading certificate with data
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
KeyPair endUserKeyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
Certificate[] chain = generateSampleChain(endUserKeyPair, false);
byte[] chainData = createKeystore(chain, endUserKeyPair.getPrivate());
HttpResponse resp = uploadCertificate("localhost", "type=" + type, chainData, client, credentials);
assertTrue(resp.getBodyString().contains("SUCCESS"));
CertificateData data = dynCertsMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertEquals(type.equals("manual"), data.isManual());
assertEquals(DynamicCertificateState.AVAILABLE, dynCertsMan.getStateOfCertificate("localhost"));
// check uploaded certificate
try (RawHttpClient c = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse r = c.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", r.getBodyString());
Certificate[] obtainedChain = c.getServerCertificate();
assertNotNull(obtainedChain);
assertTrue(chain[0].equals(obtainedChain[0]));
}
}
// Uploading trush: bad "type"
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
HttpResponse resp = uploadCertificate("localhost", "type=undefined", new byte[0], client, credentials);
assertTrue(resp.getBodyString().contains("ERROR: illegal type"));
}
// Uploading same certificate but with different type (will be update)
try (RawHttpClient client = new RawHttpClient("localhost", DEFAULT_ADMIN_PORT)) {
String otherType = type.equals("manual") ? "acme" : "manual";
KeyPair endUserKeyPair = KeyPairUtils.createKeyPair(DEFAULT_KEYPAIRS_SIZE);
Certificate[] chain = generateSampleChain(endUserKeyPair, false);
byte[] chainData = createKeystore(chain, endUserKeyPair.getPrivate());
HttpResponse resp = uploadCertificate("localhost", "type=" + otherType, chainData, client, credentials);
assertTrue(resp.getBodyString().contains("SUCCESS"));
CertificateData data = dynCertsMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertEquals(otherType.equals("manual"), data.isManual());
assertEquals(DynamicCertificateState.AVAILABLE, dynCertsMan.getStateOfCertificate("localhost"));
// check uploaded certificate
try (RawHttpClient c = new RawHttpClient("localhost", port, true, "localhost")) {
RawHttpClient.HttpResponse r = c.get("/index.html", credentials);
assertEquals("it <b>works</b> !!", r.getBodyString());
Certificate[] obtainedChain = c.getServerCertificate();
assertNotNull(obtainedChain);
assertTrue(chain[0].equals(obtainedChain[0]));
}
resp = uploadCertificate("localhost", "type=" + type, new byte[0], client, credentials);
if (type.equals("acme")) {
assertTrue(resp.getBodyString().contains("SUCCESS"));
data = dynCertsMan.getCertificateDataForDomain("localhost");
assertNotNull(data);
assertFalse(data.isManual());
// no certificate-data uploaded
assertEquals(DynamicCertificateState.WAITING, dynCertsMan.getStateOfCertificate("localhost"));
}
}
}
use of org.carapaceproxy.utils.RawHttpClient.HttpResponse in project carapaceproxy by diennea.
the class AuthenticationAPIServerTest method testUnauthorizedRequests.
@Test
public void testUnauthorizedRequests() throws Exception {
Properties prop = new Properties(HTTP_ADMIN_SERVER_CONFIG);
prop.put("userrealm.class", "org.carapaceproxy.utils.TestUserRealm");
prop.put("user.test1", "pass1");
prop.put("user.test2", "pass2");
startServer(prop);
try (RawHttpClient client = new RawHttpClient("localhost", 8761)) {
// valid credentials
BasicAuthCredentials credentials = new BasicAuthCredentials("test1", "pass1");
HttpResponse resp = client.get("/api/up", credentials);
assertHeaderNotContains(resp, "WWW-Authenticate");
}
try (RawHttpClient client = new RawHttpClient("localhost", 8761)) {
// not valid credentials
BasicAuthCredentials credentials = new BasicAuthCredentials("wrongtest1", "wrongtest1");
HttpResponse resp = client.get("/api/up", credentials);
assertHeaderContains(resp, "WWW-Authenticate");
assertThat(resp.getBodyString(), containsString(HttpServletResponse.SC_UNAUTHORIZED + ""));
}
}
Aggregations