[New post] Microservices in Go Part – VI: Go Client to HTTPS Server
Tarun posted: " Hello everyone , Welcome back! In our previous blog, we have seen how to create an HTTPS Server in GoLang, we have also seen fetching the data using API on browser using HTTPS. But, how can a programmed client can fetch data from HTTPS server. Let's che"
Hello everyone , Welcome back! In our previous blog, we have seen how to create an HTTPS Server in GoLang, we have also seen fetching the data using API on browser using HTTPS. But, how can a programmed client can fetch data from HTTPS server. Let's check that out in this blog.
Normal HTTP Client
Let's write a Go client to our server, and see what happens when we make a HTTP request to an HTTPS server.
Create a directory called client in the root directory of our code base. Paste the following code in the main.go file under client directory.
Before running the client, let's start our server.
go run .\main.go
Run the above code using the following command from the root directory. Note that we have added authorization header, because our server has basic auth enabled. Refer to this blog for more details.
go run .\client\main.go
As you must have seen, the client request fails due to below error.
What should we do?
Lets change the protocol in the URL from http to https in line 13, and re-run the same code. We should now see this.
The error rightly implies, that the incoming certificate from the server, as part of TLS handshake is not signed by any of the trusted authorities existing in your machine.
Way out!
So, there are two options now. Trust the authority which signed the server certificate in our machine, or just make client trust it programmatically. Let's get the second option working now, to make sure our code works on any machine.
Adding Transport Configuration to HTTP client
We will now add the transport configuration to our basic http client inside our client. Modify the main.go under client directory as below.
Aah, one more error to solve. But don't worry It's an opportunity to learn. The latest TLS protocols rely on SAN's for hostname matching, which we haven't configured while creating our certificates. So, let's now do this.
Adding SAN's to certificate
Create a text file named san.txt, and let's add SAN's to it.
This should generate a set of certificates in our folder. Make sure everything is generated.
Restart server with new certificates
Before restart, make a small change to GET handlers in our handlers.go file.
Important: Modify the GET Handlers in our handlers.go to return 200 OK instead of 302 StatusFound, as it's used for redirection.
Restart the server with the new generated certificates, using the following command.
go run .\main.go
Re-run the client
As now we have self generated CA, lets use it to trust incoming server certificates. For the one-last time modify the main.go file under client directory as below. Change the filename from server.crt to ca.crt in line 14.
Now run the HTTPS client using the following command.
go run .\client\main.go
Hurray!! We now successfully connect to a HTTPS enabled server programmatically. It becomes more easy if you trust the CA certificate on your system, which also enables secure flag on your browser(if added to browser trusted CA). But, as mentioned our aim is to make our code work anywhere!
Hey! There's other way to do so(without trust, without adding certs to client code), by containerizing our client application. Let's discuss that in our upcoming blogs. Until then, stay safe. Cheers
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.