Created
July 28, 2014 21:53
-
-
Save kidoman/f01d925a9ca0c4fcb9bc to your computer and use it in GitHub Desktop.
TLS Authenticated Docker
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
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