Encrypting Connections with TLS

    Using TLS to connect to a server that verifies the client’s identity is straightforward. The client has to provide a certificate and private key. The NATS client will use these to prove it’s identity to the server. For the client to verify the server’s identity, the CA certificate is provided as well.

    Use example certificates created in self signed certificates for testing.

    1. nc, err := nats.Connect("localhost",
    2. nats.ClientCert("client-cert.pem", "client-key.pem"),
    3. nats.RootCAs("rootCA.pem"))
    4. if err != nil {
    5. log.Fatal(err)
    6. }
    7. defer nc.Close()
    8. // Do something with the connection

    Java

    JavaScript

    1. let caCert = fs.readFileSync("rootCA.pem");
    2. let clientCert = fs.readFileSync("client-cert.pem");
    3. let clientKey = fs.readFileSync("client-key.pem");
    4. let nc = NATS.connect({
    5. url: url,
    6. tls: {
    7. ca: [caCert],
    8. key: [clientKey],
    9. cert: [clientCert]
    10. }
    11. });

    Ruby

    1. EM.run do
    2. options = {
    3. :servers => [
    4. 'nats://localhost:4222',
    5. ],
    6. :private_key_file => 'client-key.pem',
    7. :ca_file => 'rootCA.pem'
    8. }
    9. }
    10. NATS.connect(options) do |nc|
    11. puts "#{Time.now.to_f} - Connected to NATS at #{nc.connected_server}"
    12. nc.subscribe("hello") do |msg|
    13. puts "#{Time.now.to_f} - Received: #{msg}"
    14. end
    15. nc.flush do
    16. nc.publish("hello", "world")
    17. end
    18. EM.add_periodic_timer(0.1) do
    19. next unless nc.connected?
    20. nc.publish("hello", "hello")
    21. end
    22. # Set default callbacks
    23. nc.on_error do |e|
    24. puts "#{Time.now.to_f } - Error: #{e}"
    25. end
    26. nc.on_disconnect do |reason|
    27. end
    28. nc.on_reconnect do |nc|
    29. puts "#{Time.now.to_f} - Reconnected to NATS server at #{nc.connected_server}"
    30. end
    31. nc.on_close do
    32. puts "#{Time.now.to_f} - Connection to NATS closed"
    33. EM.stop
    34. end
    35. end
    36. end

    TypeScript

    1. natsConnection *conn = NULL;
    2. natsOptions *opts = NULL;
    3. natsStatus s = NATS_OK;
    4. s = natsOptions_Create(&opts);
    5. if (s == NATS_OK)
    6. s = natsOptions_LoadCertificatesChain(opts, "client-cert.pem", "client-key.pem");
    7. if (s == NATS_OK)
    8. s = natsOptions_LoadCATrustedCertificates(opts, "rootCA.pem");
    9. if (s == NATS_OK)
    10. s = natsConnection_Connect(&conn, opts);
    11. (...)
    12. // Destroy objects that were created
    13. natsConnection_Destroy(conn);

    Connecting with the TLS Protocol

    Clients (such as Go, Java, Javascript, Ruby and Type Script) support providing a URL containing the tls protocol to the NATS connect call. This will turn on TLS without the need for further code changes. However, in that case there is likely some form of default or environmental settings to allow the TLS libraries of your programming language to find certificate and trusted CAs. Unless these settings are taken into accounts or otherwise modified, this way of connecting is very likely to fail.