Search in sources :

Example 6 with KeyCertificatePair

use of org.apache.qpid.test.utils.tls.KeyCertificatePair in project qpid-broker-j by apache.

the class PreemptiveAuthenticationTest method configForClientAuth.

private HttpTestHelper configForClientAuth(final String x500Name) throws Exception {
    final KeyCertificatePair clientKeyCertPair = getKeyCertPair(x500Name);
    final byte[] clientCertificate = clientKeyCertPair.getCertificate().getEncoded();
    final String clientKeyStore = createKeyStoreDataUrl(clientKeyCertPair);
    final KeyCertificatePair brokerKeyCertPair = getKeyCertPair(x500Name);
    final String brokerKeyStore = createKeyStoreDataUrl(brokerKeyCertPair);
    final Deque<BaseAction<Void, Exception>> deleteActions = new ArrayDeque<>();
    final Map<String, Object> authAttr = new HashMap<>();
    authAttr.put(ExternalAuthenticationManager.TYPE, "External");
    authAttr.put(ExternalAuthenticationManager.ATTRIBUTE_USE_FULL_DN, false);
    getHelper().submitRequest("authenticationprovider/myexternal", "PUT", authAttr, SC_CREATED);
    deleteActions.add(object -> getHelper().submitRequest("authenticationprovider/myexternal", "DELETE", SC_OK));
    final Map<String, Object> keystoreAttr = new HashMap<>();
    keystoreAttr.put(FileKeyStore.TYPE, "FileKeyStore");
    keystoreAttr.put(FileKeyStore.STORE_URL, brokerKeyStore);
    keystoreAttr.put(FileKeyStore.PASSWORD, STORE_PASSWORD);
    keystoreAttr.put(FileKeyStore.KEY_STORE_TYPE, KeyStore.getDefaultType());
    getHelper().submitRequest("keystore/mykeystore", "PUT", keystoreAttr, SC_CREATED);
    deleteActions.add(object -> getHelper().submitRequest("keystore/mykeystore", "DELETE", SC_OK));
    final Map<String, Object> truststoreAttr = new HashMap<>();
    truststoreAttr.put(ManagedPeerCertificateTrustStore.TYPE, ManagedPeerCertificateTrustStore.TYPE_NAME);
    truststoreAttr.put(ManagedPeerCertificateTrustStore.STORED_CERTIFICATES, Collections.singletonList(Base64.getEncoder().encodeToString(clientCertificate)));
    getHelper().submitRequest("truststore/mytruststore", "PUT", truststoreAttr, SC_CREATED);
    deleteActions.add(object -> getHelper().submitRequest("truststore/mytruststore", "DELETE", SC_OK));
    final Map<String, Object> portAttr = new HashMap<>();
    portAttr.put(Port.TYPE, "HTTP");
    portAttr.put(Port.PORT, 0);
    portAttr.put(Port.AUTHENTICATION_PROVIDER, "myexternal");
    portAttr.put(Port.PROTOCOLS, Collections.singleton(Protocol.HTTP));
    portAttr.put(Port.TRANSPORTS, Collections.singleton(Transport.SSL));
    portAttr.put(Port.NEED_CLIENT_AUTH, true);
    portAttr.put(Port.KEY_STORE, "mykeystore");
    portAttr.put(Port.TRUST_STORES, Collections.singletonList("mytruststore"));
    getHelper().submitRequest("port/myport", "PUT", portAttr, SC_CREATED);
    deleteActions.add(object -> getHelper().submitRequest("port/myport", "DELETE", SC_OK));
    Map<String, Object> clientAuthPort = getHelper().getJsonAsMap("port/myport");
    int boundPort = Integer.parseInt(String.valueOf(clientAuthPort.get("boundPort")));
    assertThat(boundPort, is(greaterThan(0)));
    _tearDownActions = deleteActions;
    HttpTestHelper helper = new HttpTestHelper(getBrokerAdmin(), null, boundPort);
    helper.setTls(true);
    helper.setKeyStore(clientKeyStore, STORE_PASSWORD);
    return helper;
}
Also used : KeyCertificatePair(org.apache.qpid.test.utils.tls.KeyCertificatePair) HashMap(java.util.HashMap) HttpTestHelper(org.apache.qpid.tests.http.HttpTestHelper) BaseAction(org.apache.qpid.server.util.BaseAction) ArrayDeque(java.util.ArrayDeque)

Example 7 with KeyCertificatePair

use of org.apache.qpid.test.utils.tls.KeyCertificatePair in project qpid-broker-j by apache.

the class ReadTest method valueFilteredSecureAttributes.

@Test
@HttpRequestConfig(useVirtualHostAsHost = false)
public void valueFilteredSecureAttributes() throws Exception {
    final KeyCertificatePair keyCertPair = generateCertKeyPair();
    final byte[] privateKey = keyCertPair.getPrivateKey().getEncoded();
    final byte[] cert = keyCertPair.getCertificate().getEncoded();
    final String privateKeyUrl = DataUrlUtils.getDataUrlForBytes(privateKey);
    final String certUrl = DataUrlUtils.getDataUrlForBytes(cert);
    final File privateKeyFile = File.createTempFile("foo" + System.currentTimeMillis(), "key");
    privateKeyFile.deleteOnExit();
    FileUtils.copy(new ByteArrayInputStream(privateKey), privateKeyFile);
    Map<String, Object> base = new HashMap<>();
    base.put(NonJavaKeyStore.TYPE, "NonJavaKeyStore");
    base.put(NonJavaKeyStore.CERTIFICATE_URL, certUrl);
    try {
        {
            final String storeUrl = "keystore/mystoreDataUrl";
            final Map<String, Object> attrs = new HashMap<>(base);
            attrs.put(NonJavaKeyStore.PRIVATE_KEY_URL, privateKeyUrl);
            getHelper().submitRequest(storeUrl, "PUT", attrs, SC_CREATED);
            final Map<String, Object> store = getHelper().getJsonAsMap(storeUrl);
            assertThat(store.get(NonJavaKeyStore.PRIVATE_KEY_URL), is(equalTo(AbstractConfiguredObject.SECURED_STRING_VALUE)));
            getHelper().submitRequest(storeUrl, "DELETE", SC_OK);
        }
        {
            final String privateKeyFileUrl = privateKeyFile.toURI().toString();
            final String storeUrl = "keystore/mystoreFileUrl";
            final Map<String, Object> attrs = new HashMap<>(base);
            attrs.put(NonJavaKeyStore.TYPE, "NonJavaKeyStore");
            attrs.put(NonJavaKeyStore.PRIVATE_KEY_URL, privateKeyFileUrl);
            getHelper().submitRequest(storeUrl, "PUT", attrs, SC_CREATED);
            final Map<String, Object> store = getHelper().getJsonAsMap(String.format("%s?oversize=%d", storeUrl, privateKeyFileUrl.length()));
            assertThat(store.get(NonJavaKeyStore.PRIVATE_KEY_URL), is(equalTo(privateKeyFileUrl)));
            getHelper().submitRequest(storeUrl, "DELETE", SC_OK);
        }
    } finally {
        privateKeyFile.delete();
    }
}
Also used : KeyCertificatePair(org.apache.qpid.test.utils.tls.KeyCertificatePair) ByteArrayInputStream(java.io.ByteArrayInputStream) HashMap(java.util.HashMap) ConfiguredObject(org.apache.qpid.server.model.ConfiguredObject) AbstractConfiguredObject(org.apache.qpid.server.model.AbstractConfiguredObject) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) Test(org.junit.Test) HttpRequestConfig(org.apache.qpid.tests.http.HttpRequestConfig)

Example 8 with KeyCertificatePair

use of org.apache.qpid.test.utils.tls.KeyCertificatePair in project qpid-broker-j by apache.

the class PortTest method updateKeyStoreAndUpdatePortTls.

private File updateKeyStoreAndUpdatePortTls() throws Exception {
    final KeyCertificatePair keyCertPair = generateSelfSignedCertificate();
    submitKeyStoreAttributes(_keyStoreName, SC_OK, keyCertPair);
    updatePortTls();
    return createTrustStore(keyCertPair);
}
Also used : KeyCertificatePair(org.apache.qpid.test.utils.tls.KeyCertificatePair)

Example 9 with KeyCertificatePair

use of org.apache.qpid.test.utils.tls.KeyCertificatePair in project qpid-broker-j by apache.

the class PortTest method createNewKeyStoreAndSetItOnPort.

private File createNewKeyStoreAndSetItOnPort() throws Exception {
    String newKeyStoreName = _keyStoreName + "_2";
    final KeyCertificatePair keyCertPair = generateSelfSignedCertificate();
    submitKeyStoreAttributes(newKeyStoreName, SC_CREATED, keyCertPair);
    getHelper().submitRequest("port/" + _portName, "POST", Collections.<String, Object>singletonMap(Port.KEY_STORE, newKeyStoreName), SC_OK);
    updatePortTls();
    return createTrustStore(keyCertPair);
}
Also used : KeyCertificatePair(org.apache.qpid.test.utils.tls.KeyCertificatePair)

Example 10 with KeyCertificatePair

use of org.apache.qpid.test.utils.tls.KeyCertificatePair in project qpid-broker-j by apache.

the class TlsHelper method getCaKeyCertPair.

public KeyCertificatePair getCaKeyCertPair() {
    final PrivateKey privateKey = _caPair.getPrivateKey();
    final X509Certificate certificate = _caCertificate;
    return new KeyCertificatePair(privateKey, certificate);
}
Also used : KeyCertificatePair(org.apache.qpid.test.utils.tls.KeyCertificatePair) PrivateKey(java.security.PrivateKey) X509Certificate(java.security.cert.X509Certificate)

Aggregations

KeyCertificatePair (org.apache.qpid.test.utils.tls.KeyCertificatePair)18 HashMap (java.util.HashMap)8 Path (java.nio.file.Path)7 Test (org.junit.Test)7 CertificateEntry (org.apache.qpid.test.utils.tls.CertificateEntry)4 X509Certificate (java.security.cert.X509Certificate)3 TrustManager (javax.net.ssl.TrustManager)3 X509TrustManager (javax.net.ssl.X509TrustManager)3 KeyPair (java.security.KeyPair)2 Before (org.junit.Before)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 File (java.io.File)1 PrivateKey (java.security.PrivateKey)1 CertificateException (java.security.cert.CertificateException)1 CertificateExpiredException (java.security.cert.CertificateExpiredException)1 Instant (java.time.Instant)1 ArrayDeque (java.util.ArrayDeque)1 Collections.singletonMap (java.util.Collections.singletonMap)1 Map (java.util.Map)1 IllegalConfigurationException (org.apache.qpid.server.configuration.IllegalConfigurationException)1