Skip to content

NewServerTLS vs NewClientTLS in example #1

@nleiva

Description

@nleiva

Thanks for your blog post, very inspiring. One thing I noticed while trying to implement this on a gRPC client is your example calls NewServerTLSFromCert instead of NewClientTLSFromCert. I personally use client configs for Dial options (I might be off here).

conn, err := grpc.Dial(*serverAddr, grpc.NewServerTLSFromCert(tlsCert))
if err != nil {
	...
}
defer conn.Close()

So, in my case I had to create a certPool with the PeerCertificates that I can pass to NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string).

certPool := x509.NewCertPool()
for _, cert := range tconn.ConnectionState().PeerCertificates {
	certPool.AddCert(cert)
}

The good news is that it works!, I could connect to the devices (server) without manually providing the .pem certificate file. On the other hand, I'm still wrapping my head around this as NewClientTLSFromCert pass this cert as RootCAs.

func NewClientTLSFromCert(cp *x509.CertPool, serverNameOverride string) TransportCredentials {
	return NewTLS(&tls.Config{ServerName: serverNameOverride, RootCAs: cp})
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions