beautypg.com

6 python code examples, 1 prerequisites – HP OneView User Manual

Page 221

background image

4.

Create a PKCS12 keystore from the private key and the public certificate.

openssl pkcs12 -export -name myclientcert -in default-client.crt -inkey default-client.key -out myclient.p12

5.

Convert the PKCS12 keystore into a JKS keystore.

keytool -importkeystore -destkeystore c:\\MyKeyStore -srckeystore myclient.p12 -srcstoretype pkcs12 -alias
myclient

Example 5 Java code example

//c://MyKeyStore contains client certificate and private key. Load it into Java Keystore
final char[] keyPassphrase = "MyKeyStorePassword".toCharArray();
final KeyStore ks = KeyStore.getInstance("jks");
ks.load(new FileInputStream("c://MyKeyStore"), keyPassphrase);
final KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
kmf.init(ks, keyPassphrase);

//c://MyTrustStore contains CA certificate. Load it into Java Trust Store
final char[] trustPassphrase = "MyTrustStorePassword".toCharArray();
final KeyStore ks = KeyStore.getInstance("jks");
tks.load(new FileInputStream("c:\\MyTrustStore"), trustPassphrase);
final TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(tks);

//load SSLContext with keystore and truststore.
final SSLContext c = SSLContext.getInstance("SSL");
c.init(kmf.getKeyManagers(), tmf.getTrustManagers(), new SecureRandom());

final ConnectionFactory factory = new ConnectionFactory();
factory.setHost("192.168.2.144");

//Set Auth mechanism to "EXTERNAL" so that commonName of the client certificate is mapped to AMQP user name.
Hence, No need to set userId/Password here.
factory.setSaslConfig(DefaultSaslConfig.EXTERNAL);
factory.setPort(5671);
factory.useSslProtocol(c);

final Connection conn = factory.newConnection();
final Channel channel = conn.createChannel();

//do not specify queue name. AMQP will create a queue with random name starting with amq.gen* e.g.
amq.gen-32sfQz95QJ85K_lMBhU6HA
final DeclareOk queue = channel.queueDeclare("", true, false, true, null);

//Now get the queue name from above call and bind it to required Exchange with required routing key.
channel.queueBind(queue.getQueue(), "scmb", "scmb.#");

//Now you should be able to receive messages from queue
final GetResponse chResponse = channel.basicGet(queue.getQueue(), false);
if (chResponse == null)
{
System.out.println("No message retrieved");
}
else
{
final byte[] body = chResponse.getBody();
System.out.println("Received: " + new String(body));
}

channel.close();
conn.close();

30.6 Python code examples

The Python code examples show how to connect and subscribe to the SCMB. For more information
about Python (Pika AMQP client library and AMQP client library), see

http://pika.readthedocs.org

,

https://pypi.python.org/pypi/pika

, and

https://pypi.python.org/pypi/amqplib

.

“Prerequisites” (page 221)

“Pika” (page 222)

“AMQP” (page 223)

30.6.1 Prerequisites

1.

Install the pika and amqp libraries.

30.6 Python code examples 221