Skip to content

Instantly share code, notes, and snippets.

@dopey
Created September 19, 2019 23:57
Show Gist options
  • Save dopey/639e41cd7e934b6ecc42f7b2bf3d5725 to your computer and use it in GitHub Desktop.
Save dopey/639e41cd7e934b6ecc42f7b2bf3d5725 to your computer and use it in GitHub Desktop.
package main
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"log"
"net/http"
)
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Strict-Transport-Security", "max-age=63072000; includeSubDomains")
w.Write([]byte("Hello mTLS.\n"))
})
caCert, _ := ioutil.ReadFile("../step/.step/certs/root_ca.crt")
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(caCert)
tlsConfig := &tls.Config{
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
}
tlsConfig.BuildNameToCertificate()
server := &http.Server{
Addr: ":9443",
TLSConfig: tlsConfig,
Handler: mux,
}
log.Fatal(server.ListenAndServeTLS("../cli/server.crt", "../cli/server.key"))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment