I'm currently researching and trying to implement hashing/salting of password in my Golang
server. Thanks to this answer, I learned about the stuff I needed. I already got the hashing and salting part, then I researched about TLS. I almost used the regular Golang
way of doing things but I was also using gRPC
so I looked for how to add TLS for that, and it was quite simple. I added the codes mentioned on the official documentation, but somehow that doesn't work.
To generate the .cert
and .key
, I followed this tutorial. Then this is what my code looks like.
//Server
certFile, certFileErr := os.ReadFile("server.crt")
if certFileErr != nil {
log.Fatalf("Failed to read cert file: %v", certFileErr)
}
keyFile, keyFileErr := os.ReadFile("server.key")
if keyFileErr != nil {
log.Fatalf("Failed to read key file: %v", keyFileErr)
}
creds, _ := credentials.NewServerTLSFromFile(string(certFile), string(keyFile))
mServ := grpc.NewServer(grpc.Creds(creds))
lis, err := net.Listen("tcp", "localhost:50051")
//Client
certFile, certFileErr := os.ReadFile("server.crt")
if certFileErr != nil {
log.Fatalf("Failed to read cert file: %v", certFileErr)
}
creds, _ := credentials.NewClientTLSFromFile(string(certFile), "")
conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("did not connect: %v", err)
}
I'm not quite sure how to pass a reference of the files to gRPC
, so I thought I'd just do a regular file read. With all those stuff, when I a run the server side, it works fine. But when I run the client side, I get this error.
grpc: no transport security set (use grpc.WithTransportCredentials(insecure.NewCredentials()) explicitly or set credentials)
The error is quite confusing. I already want to not use security, but it's complaining that I'm downgrading. It's my first time doing this stuff, so I have no idea what's the regular process to implement security on a login.
Update:
After logging the error, it seems like my code can't find the .cert
file. Which is weird because the path I used was already auto suggested by my IDE itself.