Created
August 4, 2020 19:40
-
-
Save aLekSer/645f50f0d06c5b492faf5fe6a3d13595 to your computer and use it in GitHub Desktop.
Reproducing an error with an absent `RootCAs` parameter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// client/client.go | |
package main | |
import ( | |
"crypto/tls" | |
"crypto/x509" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
func main() { | |
cert, err := ioutil.ReadFile("../rootCA.pem") | |
if err != nil { | |
log.Fatalf("could not open certificate file: %v", err) | |
} | |
caCertPool := x509.NewCertPool() | |
caCertPool.AppendCertsFromPEM(cert) | |
client := http.Client{ | |
Timeout: time.Minute * 3, | |
Transport: &http.Transport{ | |
TLSClientConfig: &tls.Config{ | |
RootCAs: caCertPool, | |
}, | |
}, | |
} | |
// change the address to match the common name of the certificate | |
resp, err := client.Get("https://localhost:9090") | |
if err != nil { | |
log.Fatalf("error making get request: %v", err) | |
} | |
if err != nil { | |
log.Fatalf("error making get request: %v", err) | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatalf("error reading response: %v", err) | |
} | |
fmt.Println(string(body)) | |
} | |
// server/server.go | |
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
) | |
func main() { | |
// set up handler to listen to root path | |
handler := http.NewServeMux() | |
handler.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) { | |
log.Println("new request") | |
fmt.Fprintf(writer, "hello world \n") | |
}) | |
// serve on port 9090 of local host | |
server := http.Server{ | |
Addr: ":9090", | |
Handler: handler, | |
} // serve the endpoint with tls encryption | |
if err := server.ListenAndServeTLS("../webhook.crt", "..//webhook.key"); err != nil { | |
log.Fatalf("error listening to port: %v", err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment