python gRPC authentication issues with a self signed certificates

41 views Asked by At

Referred to the grpc docs: grpc docs and attempting secure authentication, I have already the certificate (.cert file), server URL, and credentials. Moreover, I am able to consume the gRPC proto using API-DOG without any issues.

However, while writing a Python gRPC client, I am facing issues at the authentication level (StatusCode.UNAUTHENTICATED). It seems like I am still missing some pieces or there might be a compatibility issue between Python and gRPC with self-signed certificates.

# Read certificate:
with open('./foo.crt', 'rb') as file:
    certificate = file.read()

channel_creds = ssl_channel_credentials(root_certificates=certificate, private_key=None, certificate_chain=None)
channel = secure_channel(target=server_url, credentials=channel_creds, options=None, compression=None)
token = base64.b64encode(f'{username}:{password}'.encode('utf-8')).decode('ascii')
auth = GrpcAuth(f'Basic {token}')
metadata_call_credentials(metadata_plugin=auth)

# Create stub for the gRPC service
stub = report_service_pb2_grpc.ABCServiceStub(channel=channel)

# Generate request data
req_data = input_param_to_protobuf(request_data)

# Check response
try:
    response = stub.def(req_data)
    print("Response received:", response)
except grpc.RpcError as e:
    print("Error:", e)

Error:

Error: <_InactiveRpcError of RPC that terminated with:
status = StatusCode.UNAUTHENTICATED
details = "Received http2 header with status: 401"
0

There are 0 answers