Class Security

java.lang.Object
org.acemq.amqp.security.Security

public final class Security extends Object
How a connection is protected.

Three states, and the names are the documentation:


 Security.required()                    // verify the chain and the hostname. The default.
 Security.fromKeystore(Path.of("certs")) // the same, with a client certificate for mutual TLS
 Security.disabled()                    // plaintext. For tests.
 Security.insecure()                    // TLS with no verification. Almost always wrong.
 

insecure() exists because sometimes it is genuinely needed, and because a library that omits it gets a verify=false added to a properties file by somebody in a hurry. Making it a named method keeps it greppable: one search finds every place it was used, which a boolean buried in configuration does not.

Hostname verification is the setting people turn off first and should turn off last. Without it any valid certificate satisfies the connection, whoever it was issued to, which reduces TLS to an encrypted conversation with whoever happened to answer.

  • Field Details

    • DEVELOPMENT_MARKER

      public static final String DEVELOPMENT_MARKER
      Subject the development certificate generator stamps, so this library can refuse it.
      See Also:
    • DEFAULT_KEYSTORE_PASSWORD

      public static final String DEFAULT_KEYSTORE_PASSWORD
      What fromKeystore(java.nio.file.Path) assumes when nothing says otherwise, and what acemq-security-dev writes.

      Six characters, because keytool refuses to create a PKCS12 keystore with a shorter password. The previous default was five, which meant the library's own default described a keystore the standard JDK tooling could not produce — a default nobody could use is worse than no default.

      It protects a store on a developer's machine and is not a secret. Anything real passes keystorePassword(String) with a value from a secret store.

      See Also:
  • Method Details

    • required

      public static Security required()
      Returns:
      TLS with the chain and the hostname verified, using the JVM's trust store
    • fromKeystore

      public static Security fromKeystore(Path directory)
      TLS using the keystores in a directory, as written by the development generator or by your own tooling: keystore.p12 for this client, truststore.p12 for the certificates it will accept.
      Parameters:
      directory - where the two stores live
      Returns:
      the policy
    • disabled

      public static Security disabled()
      Returns:
      plaintext, for tests and for a broker on this machine
    • insecure

      public static Security insecure()
      TLS with no verification at all.

      Encrypts the conversation and proves nothing about who is on the other end, which stops a passive eavesdropper and does nothing about an active one. Named so that finding every use of it is one search.

      Returns:
      the policy
    • keystorePassword

      public Security keystorePassword(String password)
      Parameters:
      password - the password protecting the keystores
      Returns:
      a copy of this policy using it
    • allowDevelopmentCertificates

      public Security allowDevelopmentCertificates()
      Permits certificates the development generator produced.

      Needed on a developer's machine and nowhere else. Without it, a certificate carrying DEVELOPMENT_MARKER is refused however the trust store was configured — because a throwaway certificate authority that drifts into production is worse than no encryption: everything looks protected and nothing is.

      Returns:
      a copy of this policy permitting them
    • withCredentials

      public Security withCredentials(CredentialsProvider provider)
      Parameters:
      provider - where credentials come from, consulted on every connection
      Returns:
      a copy of this policy using it
    • mode

      public Security.Mode mode()
      Returns:
      what this policy does about transport security
    • verifiesHostname

      public boolean verifiesHostname()
      Returns:
      whether the hostname on the certificate must match the host connected to
    • credentials

      public Optional<CredentialsProvider> credentials()
      Returns:
      where credentials come from, if this policy carries a provider
    • sslContext

      public Optional<SSLContext> sslContext()
      Builds the context to connect with.
      Returns:
      an SSLContext, or empty when this policy is Security.Mode.DISABLED
      Throws:
      SecurityConfigurationException - if the keystores cannot be read
    • toString

      public String toString()
      Overrides:
      toString in class Object