Skip to content
Connect > Client Libraries

Connecting and Securing a Client

A 3forge Enterprise client reaches an instance through up to 3 listeners, authenticates with a client certificate over mutual TLS, and works within the timeouts and row limits set for its connections.

Ports

A client connects to up to 3 listeners. These are the defaults; an instance can change them in config/local.properties.

Connection Property Default
SQL connection (the Center's JDBC listener) ami.db.jdbc.port 3280
Center connection (the Center port) ami.center.port 3270
Relay connection (the Relay port) ami.port 3289

How a default install authenticates a client

A default install requires mutual TLS on all 3 ports. Each listener has a keystore, keys/3forge.jks, and checks the client's certificate against the authorized-clients file, keys/3forge.auth.

  • The server checks the key, not the certificate: it compares the public key of the client's certificate with the ssh-rsa lines in the authorized-clients file, and does not check the issuer or the expiry date.
  • Only RSA keys are accepted: lines of any other key type are ignored, with no warning when the server starts. A client with an EC key is refused as if its key were missing.
  • The server adds its own key: on start, a listener adds the server's own key to the file if it is missing, so the server's internal connections are admitted.
  • The file is read when the server starts: see Authorizing a client.

A rejected client is logged on the server as SSLException: java.security.cert.CertificateException: Client public key does not match any authorized keys, or by a line containing null cert chain when it presented no certificate. On the client, the rejection is easy to miss; see Troubleshooting.

For the server side, which listeners require mutual TLS and how the server creates its own key on first start, see Mandatory Mutual TLS.

Creating a client certificate

The Python and C++ clients, and the ODBC driver, use a PEM certificate and key; the Java client uses a keystore made from them.

  1. Create a private key and a self-signed certificate. -nodes leaves the key unencrypted:

    openssl req -x509 -newkey rsa:2048 -nodes -keyout client.key.pem -out client.crt.pem -days 365 -batch
    
  2. Write the public key as an ssh-rsa line for the authorized-clients file:

    ssh-keygen -y -f client.key.pem > client.pub
    
  3. Confirm that the key was written: run cat client.pub, and the output is one line starting ssh-rsa AAAA.

Creating a Java keystore

The Java client presents its certificate from a keystore. Make one from the PEM files above, with the JDK's keytool.

  1. Package the certificate and key, with encryption a Java 8 runtime can read:

    openssl pkcs12 -export -in client.crt.pem -inkey client.key.pem -name client -out client.p12 \
        -passout pass:changeit -keypbe PBE-SHA1-3DES -certpbe PBE-SHA1-3DES -macalg sha1
    
  2. Convert the package to a keystore:

    keytool -importkeystore -srckeystore client.p12 -srcstoretype PKCS12 -srcstorepass changeit \
        -destkeystore client.jks -deststoretype JKS -deststorepass changeit -destkeypass changeit -noprompt
    
  3. Check that keytool reports 1 entries successfully imported.

Replace changeit with a password of your own. The alias, client here, can be any name.

Authorizing a client

A client certificate is accepted once its public key is in the authorized-clients file and the server has restarted.

  1. Append the client's ssh-rsa line to keys/3forge.auth on the server, from the installation directory:

    cat /path/to/client.pub >> keys/3forge.auth
    
  2. Restart the instance.

  3. Confirm that the server accepts the key: connect a client presenting the certificate and run SELECT 1, and it returns one row.

Adding or removing a key needs a restart

The server reads the authorized-clients file only when it starts. A key added to a running server is still refused, and a key removed from a running server is still accepted, until the next restart. Removing a key does not revoke access by itself.

Server pinning and TLS settings

A client pins the server by giving it the server's public key file, so it connects only to a server holding the matching key. This is certificate pinning: the client checks the server's key, not its certificate chain. The server's public key is keys/3forge.pub on a default install. The file is written beside the keystore only when the server generated the keystore itself.

  • With a pin file, the client accepts a server whose certificate's public key matches a line in the file, without checking the chain or expiry.
  • Without a pin file, the client accepts any server certificate.
  • With the wrong pin file, the client shows the same symptoms as with a refused client certificate, except that the C++ client on Windows warns certificate verify failed (SSL routines). The server logs a line containing Received fatal alert: unknown_ca.

The TLS settings are 4 fields, set on each shard of each connection:

Python (AmiSslOptions) C++ (SslOptions) What it sets
trusted_server_pub_file trusted_server_pub_file The server's public key file, to pin the server. Optional
client_cert_file client_cert_file The client certificate, PEM
client_key_file client_key_file The client's private key, PEM
client_key_password client_key_password The key's passphrase, if it is encrypted
1
2
3
4
5
6
7
from pyami.rt.ami_client import AmiDatabaseConfig, AmiSslOptions

keys = AmiSslOptions(trusted_server_pub_file="3forge.pub",
                     client_cert_file="client.crt.pem",
                     client_key_file="client.key.pem")
sql_config = AmiDatabaseConfig("3280", True, "demo", "demo123")
sql_config.shards[0].ssl = keys

The options go on every connection the program uses, because each configuration has its own shards, and each shard its own TLS settings.

We recommend these settings for a client in production:

  • Pin the server: set trusted_server_pub_file on every shard, or pass 3forge.pub to the Java keystore helper. Without it, the client accepts any server certificate.
  • Give each client program its own key: removing that key's line from keys/3forge.auth, followed by a restart, then refuses that program only.
  • Replace keys on a schedule: the server does not check a certificate's expiry date, so an expired certificate is still accepted until its key is removed.
  • Protect the private key file: the key created in Creating a client certificate is unencrypted, so make it readable only by the account that runs the program.

Credentials

Each connection identifies the client differently:

  • SQL connection: a username and password, which the server checks. A wrong password is reported as JDBC login refused by server: 005-AUTHENTICATION FAILED: BAD_CREDENTIALS.
  • Center connection: the certificate alone, for a plain Center, with the username and password left empty.
  • Relay connection: a login id, which names the publishing application. The Relay checks no password; access is by certificate.

Through a Gateway, the Center connection also needs a username and password, which the Gateway's authenticator checks:

  • Without credentials, nothing arrives: the subscription returns nothing, and nothing is logged.
  • A wrong password is logged: Center/gateway login REFUSED (status=1); the server will ignore subscriptions on this connection, so no updates will arrive. ....
  • Subscribe after on_logged_in: add_cluster(sync=True) returns before the Gateway accepts the login, and a subscription sent before then delivers nothing.

Failover and sharding

Each connection holds a list of shards, and each shard can list failover endpoints, as host and port pairs.

1
2
3
4
5
6
from pyami.rt.ami_client import AmiCluster

cluster = AmiCluster("N1", "server-a")
cluster.database_config = sql_config
cluster.database_config.add_failover_url("server-b", "3280")
cluster.add_failover_host("server-c")
  • add_failover_url(host, port): adds an endpoint to a connection's last shard.
  • add_failover_host(host, port=None): adds an endpoint to the first shard of every connection in the cluster, reusing each connection's own port when none is given.
  • add_shard(port, use_ssl=True, failover_endpoints=[]): adds a parallel connection.
  • set_distribution_fn(fn): on the Relay configuration, picks shards for each published object. fn(cluster_name, message) returns a list of shard indexes; an empty list sends to every shard.

On a dropped connection the client moves to the next endpoint. It tries every endpoint, waits 1 second, and doubles the wait up to 60 seconds between rounds. After a reconnect, the Center connection subscribes again and resends the current rows, and the Relay connection resends objects that were queued while it was disconnected.

Timeouts and limits

Setting Default What it limits
query_timeout_ms 30000 (Python and C++); 60000 (DB-API, SQLAlchemy, and ODBC) The query timeout the client sends to the server when it logs in
query_limit 10000 Rows returned by one query
AMI_SYNC_TIMEOUT_MS environment variable 30000; 0 waits indefinitely How long a synchronous call waits for an answer
AMI_IO_THREADS environment variable Processor cores, at most 16 Threads for Relay connections
AMI_MAX_FRAME_BYTES environment variable 512 MiB The largest message accepted on the SQL and Center connections
  • The row limit truncates silently: a query that matches more rows than query_limit returns exactly query_limit rows, and nothing in the result says rows were left out. Raise the limit, or count first with SELECT count(*).
  • A synchronous call that times out returns: it logs Synchronous operation did not complete within <n>ms; returning without it. ... and returns without a result.
  • Subscriptions are not capped: the client sets no limit on their number, and no connection compresses data.

What this does not support

  • Turning off the certificate check on a secured port: a Center, JDBC, or Relay listener with a keystore always requires a client certificate, and no property turns the check off.
  • Authorizing keys other than RSA: the authorized-clients file accepts ssh-rsa lines only.
  • Changing the authorized clients without a restart: the file is read when the server starts.