Skip to content

Commit

Permalink
Feature/SK-1330 | Use host cert store instead of downloading cert (#793)
Browse files Browse the repository at this point in the history
* Remove download of cert

* Removed unused imports
  • Loading branch information
stefanhellander authored Feb 4, 2025
1 parent ed0c95a commit e574ac6
Showing 1 changed file with 1 addition and 21 deletions.
22 changes: 1 addition & 21 deletions fedn/network/clients/grpc_handler.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
import json
import os
import socket
import time
from datetime import datetime
from io import BytesIO
from typing import Any, Callable

import grpc
from cryptography.hazmat.primitives.serialization import Encoding
from google.protobuf.json_format import MessageToJson
from OpenSSL import SSL

import fedn.network.grpc.fedn_pb2 as fedn
import fedn.network.grpc.fedn_pb2_grpc as rpc
Expand Down Expand Up @@ -44,21 +41,6 @@ def __call__(self, context, callback):
callback((("authorization", f"{FEDN_AUTH_SCHEME} {self._key}"),), None)


def _get_ssl_certificate(domain, port=443):
context = SSL.Context(SSL.TLSv1_2_METHOD)
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((domain, port))
ssl_sock = SSL.Connection(context, sock)
ssl_sock.set_tlsext_host_name(domain.encode())
ssl_sock.set_connect_state()
ssl_sock.do_handshake()
cert = ssl_sock.get_peer_certificate()
ssl_sock.close()
sock.close()
cert = cert.to_cryptography().public_bytes(Encoding.PEM).decode()
return cert


class GrpcHandler:
def __init__(self, host: str, port: int, name: str, token: str, combiner_name: str):
self.metadata = [
Expand Down Expand Up @@ -95,9 +77,7 @@ def _init_secure_channel(self, host: str, port: int, token: str):
self.channel = grpc.secure_channel("{}:{}".format(host, str(port)), credentials)
return

logger.info(f"Fetching SSL certificate for {host}")
cert = _get_ssl_certificate(host, port)
credentials = grpc.ssl_channel_credentials(cert.encode("utf-8"))
credentials = grpc.ssl_channel_credentials()
auth_creds = grpc.metadata_call_credentials(GrpcAuth(token))
self.channel = grpc.secure_channel(
"{}:{}".format(host, str(port)),
Expand Down

0 comments on commit e574ac6

Please sign in to comment.