|
| 1 | +Securing gRPC connections |
| 2 | +######################### |
| 3 | + |
| 4 | +With the release of Ansys product service packs adding enhanced security to |
| 5 | +gRPC communication, the PyAnsys ecosystem enables various transport modes for |
| 6 | +securing gRPC connections. This page reviews the available transport modes, |
| 7 | +and how to use them. |
| 8 | + |
| 9 | +Supported transport modes |
| 10 | +========================= |
| 11 | + |
| 12 | +PyAnsys supports the following transport modes for gRPC connections: |
| 13 | + |
| 14 | +- **Mutual TLS (mTLS).** This mode, which works both locally and over the |
| 15 | + network, allows secure connections using TLS encryption and client/server |
| 16 | + certificates. It is recommended for production use, especially when |
| 17 | + transmitting sensitive data. |
| 18 | + |
| 19 | +- **Unix Domain Sockets (UDS).** This mode allows connections over a local |
| 20 | + socket file. UDS is only supported for local inter-process communication |
| 21 | + (IPC) on a machine running Linux. |
| 22 | + |
| 23 | +- **Windows Named User Authentication (WNUA).** This mode allows secure local |
| 24 | + connections on Windows machines through user authentication. It is only |
| 25 | + supported in Windows. |
| 26 | + |
| 27 | +- **Insecure.** This mode allows connections without any encryption or |
| 28 | + authentication. It is NOT recommended for production use, but can be useful |
| 29 | + for testing or development purposes. |
| 30 | + |
| 31 | + |
| 32 | +The ``cyberchannel`` module |
| 33 | +============================ |
| 34 | + |
| 35 | +The ``cyberchannel`` module eases the transition to secure gRPC. It is meant to |
| 36 | +be used by client applications to create gRPC channels with the server. |
| 37 | + |
| 38 | +This module implements all transport modes described previously. It also |
| 39 | +abstracts away the details of connection setup and certificate handling, making |
| 40 | +it easier to connect clients to gRPC servers in different environments. |
| 41 | + |
| 42 | +Example usage |
| 43 | +------------- |
| 44 | + |
| 45 | +.. code-block:: python |
| 46 | +
|
| 47 | + from cyberchannel import create_channel |
| 48 | + import hello_pb2_grpc |
| 49 | +
|
| 50 | + channel = create_channel( |
| 51 | + host="localhost", |
| 52 | + port=50051, # Channel details |
| 53 | + transport_mode="mtls", |
| 54 | + certs_dir="path/to/certs", # Security details |
| 55 | + grpc_options=[ # Extra details |
| 56 | + ("grpc.max_receive_message_length", 50 * 1024 * 1024) |
| 57 | + ], |
| 58 | + ) |
| 59 | + stub = hello_pb2_grpc.GreeterStub(channel) |
| 60 | +
|
| 61 | +API reference |
| 62 | +------------- |
| 63 | + |
| 64 | +.. list-table:: |
| 65 | + :header-rows: 1 |
| 66 | + :widths: 20 80 |
| 67 | + |
| 68 | + * - Function |
| 69 | + - Description |
| 70 | + * - ``create_channel(...)`` |
| 71 | + - Main entry point for users. |
| 72 | + * - ``verify_transport_mode(...)`` |
| 73 | + - Check if selected transport mode is valid. If not, it raises an error. |
| 74 | + * - ``verify_uds_socket(...)`` |
| 75 | + - Check if UDS socket file exists. |
| 76 | + |
| 77 | +Environment variables |
| 78 | +--------------------- |
| 79 | + |
| 80 | +.. list-table:: |
| 81 | + :header-rows: 1 |
| 82 | + :widths: 20 60 20 |
| 83 | + |
| 84 | + * - Variable |
| 85 | + - Description |
| 86 | + - Default |
| 87 | + * - ``ANSYS_GRPC_CERTIFICATES`` |
| 88 | + - Path to folder containing ``ca.crt``, ``client.crt``, and ``client.key`` |
| 89 | + for mTLS connections. If not set, defaults to a local ``./certs`` |
| 90 | + directory. |
| 91 | + - ``./certs`` |
| 92 | + |
| 93 | +Generating certificates for mTLS |
| 94 | +================================ |
| 95 | + |
| 96 | +`OpenSSL <https://www.openssl.org/>`__ can be used to generate the necessary |
| 97 | +certificates for mTLS. |
| 98 | + |
| 99 | +.. list-table:: Server certificate files |
| 100 | + :header-rows: 1 |
| 101 | + :widths: auto |
| 102 | + |
| 103 | + * - Required Files |
| 104 | + - Purpose |
| 105 | + * - server.crt |
| 106 | + - Server identity |
| 107 | + * - server.key |
| 108 | + - Server private key |
| 109 | + * - ca.crt |
| 110 | + - To verify client certificates |
| 111 | + |
| 112 | +.. list-table:: Client certificate files |
| 113 | + :header-rows: 1 |
| 114 | + :widths: auto |
| 115 | + |
| 116 | + * - Required Files |
| 117 | + - Purpose |
| 118 | + * - client.crt |
| 119 | + - Client identity |
| 120 | + * - client.key |
| 121 | + - Client private key |
| 122 | + * - ca.crt |
| 123 | + - To verify server certificates |
| 124 | + |
| 125 | +These files can be generated using `OpenSSL <https://www.openssl.org/>`__. |
| 126 | + |
| 127 | +Generate a certificate authority |
| 128 | +-------------------------------- |
| 129 | + |
| 130 | +.. code-block:: bash |
| 131 | +
|
| 132 | + # Generate private key for CA |
| 133 | + openssl genrsa -out ca.key 4096 |
| 134 | +
|
| 135 | + # Generate self-signed CA certificate |
| 136 | + openssl req -x509 -new -nodes -key ca.key -sha256 -days 200 -out ca.crt \ |
| 137 | + -subj "/CN=MyRootCA" |
| 138 | +
|
| 139 | +Generate the server certificate |
| 140 | +------------------------------- |
| 141 | + |
| 142 | +.. code-block:: bash |
| 143 | +
|
| 144 | + # Generate server private key |
| 145 | + openssl genrsa -out server.key 4096 |
| 146 | +
|
| 147 | + # Generate a certificate signing request (CSR) for the server |
| 148 | + openssl req -new -key server.key -out server.csr \ |
| 149 | + -subj "/CN=localhost" |
| 150 | +
|
| 151 | + # Generate server certificate signed by the CA |
| 152 | + openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial \ |
| 153 | + -out server.crt -days 200 -sha256 |
| 154 | +
|
| 155 | +Generate the client certificate |
| 156 | +------------------------------- |
| 157 | + |
| 158 | +.. code-block:: bash |
| 159 | +
|
| 160 | + # Generate client private key |
| 161 | + openssl genrsa -out client.key 4096 |
| 162 | +
|
| 163 | + # Generate a certificate signing request (CSR) for the client |
| 164 | + openssl req -new -key client.key -out client.csr \ |
| 165 | + -subj "/CN=grpc-client" |
| 166 | +
|
| 167 | + # Generate client certificate signed by the CA |
| 168 | + openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial \ |
| 169 | + -out client.crt -days 200 -sha256 |
| 170 | +
|
| 171 | +Verify certificates |
| 172 | +------------------- |
| 173 | + |
| 174 | +.. code-block:: bash |
| 175 | +
|
| 176 | + # Verify server certificate |
| 177 | + openssl verify -CAfile ca.crt server.crt |
| 178 | +
|
| 179 | + # Verify client certificate |
| 180 | + openssl verify -CAfile ca.crt client.crt |
0 commit comments