Skip to content

Instantly share code, notes, and snippets.

@kidoman kidoman/main.go
Created Jul 28, 2014

Embed
What would you like to do?
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
You can’t perform that action at this time.