Skip to content

Commit 093c148

Browse files
use cyberchannel helpers
1 parent ea747f3 commit 093c148

File tree

1 file changed

+3
-72
lines changed

1 file changed

+3
-72
lines changed

src/ansys/dpf/core/server_types.py

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1202,76 +1202,6 @@ def get_system_path() -> str:
12021202
return sys.path
12031203

12041204

1205-
def create_mtls_channel(
1206-
host: str,
1207-
port: int | str,
1208-
certs_dir: str | Path | None = None,
1209-
grpc_options: list[tuple[str, object]] | None = None,
1210-
):
1211-
"""Create a gRPC channel using Mutual TLS (mTLS).
1212-
1213-
Parameters
1214-
----------
1215-
host : str
1216-
Hostname or IP address of the server.
1217-
port : int | str
1218-
Port in which the server is running.
1219-
certs_dir : str | Path | None
1220-
Directory to use for TLS certificates.
1221-
By default `None` and thus search for the "ANSYS_GRPC_CERTIFICATES" environment variable.
1222-
If not found, it will use the "certs" folder assuming it is in the current working
1223-
directory.
1224-
cert_files: CertificateFiles | None
1225-
Path to the client certificate file, client key file, and issuing certificate authority.
1226-
By default `None`.
1227-
If all three file paths are not all provided, use the certs_dir parameter.
1228-
grpc_options: list[tuple[str, object]] | None
1229-
gRPC channel options to pass when creating the channel.
1230-
Each option is a tuple of the form ("option_name", value).
1231-
By default `None` and thus no extra options are added.
1232-
1233-
Returns
1234-
-------
1235-
grpc.Channel
1236-
The created gRPC channel
1237-
1238-
"""
1239-
import grpc
1240-
1241-
certs_folder = None
1242-
if certs_dir:
1243-
certs_folder = Path(certs_dir)
1244-
elif os.environ.get("ANSYS_GRPC_CERTIFICATES"):
1245-
certs_folder = Path(cast(str, os.environ.get("ANSYS_GRPC_CERTIFICATES")))
1246-
else:
1247-
certs_folder = Path("certs")
1248-
ca_file = certs_folder / "ca.crt"
1249-
cert_file = certs_folder / "client.crt"
1250-
key_file = certs_folder / "client.key"
1251-
1252-
# Load certificates
1253-
try:
1254-
with (ca_file).open("rb") as f:
1255-
trusted_certs = f.read()
1256-
with (cert_file).open("rb") as f:
1257-
client_cert = f.read()
1258-
with (key_file).open("rb") as f:
1259-
client_key = f.read()
1260-
except FileNotFoundError as e:
1261-
error_message = f"Certificate file not found: {e.filename}. "
1262-
if certs_folder is not None:
1263-
error_message += f"Ensure that the certificates are present in the '{certs_folder}' folder or " \
1264-
"set the 'ANSYS_GRPC_CERTIFICATES' environment variable."
1265-
raise FileNotFoundError(error_message) from e
1266-
1267-
# Create SSL credentials
1268-
credentials = grpc.ssl_channel_credentials(
1269-
root_certificates=trusted_certs, private_key=client_key, certificate_chain=client_cert
1270-
)
1271-
1272-
target = f"{host}:{port}"
1273-
return grpc.secure_channel(target, credentials, options=grpc_options)
1274-
12751205
class LegacyGrpcServer(BaseServer):
12761206
"""Provides an instance of the DPF server using InProcess gRPC.
12771207
@@ -1378,10 +1308,11 @@ def __init__(
13781308
self_config = settings.get_runtime_client_config(server=self)
13791309
misc.RUNTIME_CLIENT_CONFIG.copy_config(self_config)
13801310

1311+
from ansys.dpf.core import cyberchannel
13811312
if self._grpc_mode == server_factory.GrpcMode.Insecure:
1382-
self.channel = grpc.insecure_channel(address)
1313+
self.channel = cyberchannel.create_channel(transport_mode="insecure", host=ip, port=port)
13831314
elif self._grpc_mode == server_factory.GrpcMode.mTLS:
1384-
self.channel = create_mtls_channel(ip, port, self._certs_dir)
1315+
self.channel = cyberchannel.create_channel(transport_mode="mtls", host=ip, port=port, certs_dir=self._certs_dir)
13851316

13861317
# store the address for later reference
13871318
self._address = address

0 commit comments

Comments
 (0)