ssl.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // Example of using a TLS/SSL connection. Note that the server must be
  2. // configured to accept SSL connections; see, for example,
  3. // http://www.rabbitmq.com/ssl.html.
  4. //
  5. // When trying this out, I followed the RabbitMQ SSL guide above,
  6. // almost verbatim. I set the CN of the server certificate to
  7. // 'localhost' rather than $(hostname) (since on my MBP hostname ends
  8. // up being "<blah>.local", which is just weird). My client
  9. // certificates etc., are in `../etc/client/`. My testca certificate
  10. // is in `../etc/testca` and server certs etc., in `../etc/server`,
  11. // and I've made a `rabbitmq.config` file, with which I start
  12. // RabbitMQ:
  13. //
  14. // RABBITMQ_CONFIG_FILE=`pwd`/../etc/server/rabbitmq \
  15. // /usr/local/sbin/rabbitmq-server &
  16. //
  17. // A way to check RabbitMQ's running with SSL OK is to use
  18. //
  19. // openssl s_client -connect localhost:5671
  20. const amqp = require('../');
  21. const fs = require('fs');
  22. // Assemble the SSL options; for verification we need at least
  23. // * a certificate to present to the server ('cert', in PEM format)
  24. // * the private key for the certificate ('key', in PEM format)
  25. // * (possibly) a passphrase for the private key
  26. //
  27. // The first two may be replaced with a PKCS12 file ('pfx', in pkcs12
  28. // format)
  29. // We will also want to list the CA certificates that we will trust,
  30. // since we're using a self-signed certificate. It is NOT recommended
  31. // to use `rejectUnauthorized: false`.
  32. // Options for full client and server verification:
  33. const opts = {
  34. cert: fs.readFileSync('../etc/client/cert.pem'),
  35. key: fs.readFileSync('../etc/client/key.pem'),
  36. // cert and key or
  37. // pfx: fs.readFileSync('../etc/client/keycert.p12'),
  38. passphrase: 'MySecretPassword',
  39. ca: [fs.readFileSync('../etc/testca/cacert.pem')]
  40. };
  41. // Options for just confidentiality. This requires RabbitMQ's SSL
  42. // configuration to include the items
  43. //
  44. // {verify, verify_none},
  45. // {fail_if_no_peer_cert,false}
  46. //
  47. // const opts = { ca: [fs.readFileSync('../etc/testca/cacert.pem')] };
  48. // Option to use the SSL client certificate for authentication
  49. // opts.credentials = amqp.credentials.external();
  50. (async () => {
  51. const connection = await amqp.connect('amqp://localhost', opts);
  52. const channel = await connection.createChannel();
  53. process.on('SIGINT', async () => {
  54. await channel.close();
  55. await connection.close();
  56. });
  57. channel.sendToQueue('foo', Buffer.from('Hello World!'));
  58. console.log(' [x] To exit press CTRL+C.');
  59. })();