Skip to content

Instantly share code, notes, and snippets.

@kidoman
Created July 28, 2014 21:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kidoman/f01d925a9ca0c4fcb9bc to your computer and use it in GitHub Desktop.
Save kidoman/f01d925a9ca0c4fcb9bc to your computer and use it in GitHub Desktop.
TLS Authenticated Docker
package main
import (
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
func main() {
cert, err := tls.LoadX509KeyPair("client-cert.pem", "client-key.pem")
if err != nil {
panic(err)
}
ca, err := ioutil.ReadFile("ca.pem")
if err != nil {
panic(err)
}
cpool := x509.NewCertPool()
if !cpool.AppendCertsFromPEM(ca) {
panic("Could not add ca.pem")
}
conf := &tls.Config{
Certificates: []tls.Certificate{cert},
RootCAs: cpool,
}
transport := &http.Transport{
TLSClientConfig: conf,
}
client := http.Client{
Transport: transport,
}
resp, err := client.Get("https://host:port/info")
if err != nil {
panic(err)
}
defer resp.Body.Close()
fmt.Printf("Status = %v\n", resp.StatusCode)
var body map[string]interface{}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
panic(err)
}
fmt.Printf("Body = %v\n", body)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment