Merge pull request #670 from ericchiang/example-app-debug

cmd/example-app: add a --debug flag
This commit is contained in:
Eric Chiang 2016-11-04 14:29:39 -07:00 committed by GitHub
commit d86a774a29

View File

@ -11,6 +11,7 @@ import (
"log" "log"
"net" "net"
"net/http" "net/http"
"net/http/httputil"
"net/url" "net/url"
"os" "os"
"strings" "strings"
@ -62,6 +63,31 @@ func httpClientForRootCAs(rootCAs string) (*http.Client, error) {
}, nil }, nil
} }
type debugTransport struct {
t http.RoundTripper
}
func (d debugTransport) RoundTrip(req *http.Request) (*http.Response, error) {
reqDump, err := httputil.DumpRequest(req, true)
if err != nil {
return nil, err
}
log.Printf("%s", reqDump)
resp, err := d.t.RoundTrip(req)
if err != nil {
return nil, err
}
respDump, err := httputil.DumpResponse(resp, true)
if err != nil {
resp.Body.Close()
return nil, err
}
log.Printf("%s", respDump)
return resp, nil
}
func cmd() *cobra.Command { func cmd() *cobra.Command {
var ( var (
a app a app
@ -70,6 +96,7 @@ func cmd() *cobra.Command {
tlsCert string tlsCert string
tlsKey string tlsKey string
rootCAs string rootCAs string
debug bool
) )
c := cobra.Command{ c := cobra.Command{
Use: "example-app", Use: "example-app",
@ -101,6 +128,17 @@ func cmd() *cobra.Command {
a.ctx = context.WithValue(a.ctx, oauth2.HTTPClient, client) a.ctx = context.WithValue(a.ctx, oauth2.HTTPClient, client)
} }
if debug {
client, ok := a.ctx.Value(oauth2.HTTPClient).(*http.Client)
if ok {
client.Transport = debugTransport{client.Transport}
} else {
a.ctx = context.WithValue(a.ctx, oauth2.HTTPClient, &http.Client{
Transport: debugTransport{http.DefaultTransport},
})
}
}
// TODO(ericchiang): Retry with backoff // TODO(ericchiang): Retry with backoff
provider, err := oidc.NewProvider(a.ctx, issuerURL) provider, err := oidc.NewProvider(a.ctx, issuerURL)
if err != nil { if err != nil {
@ -161,6 +199,7 @@ func cmd() *cobra.Command {
c.Flags().StringVar(&tlsCert, "tls-cert", "", "X509 cert file to present when serving HTTPS.") c.Flags().StringVar(&tlsCert, "tls-cert", "", "X509 cert file to present when serving HTTPS.")
c.Flags().StringVar(&tlsKey, "tls-key", "", "Private key for the HTTPS cert.") c.Flags().StringVar(&tlsKey, "tls-key", "", "Private key for the HTTPS cert.")
c.Flags().StringVar(&rootCAs, "issuer-root-ca", "", "Root certificate authorities for the issuer. Defaults to host certs.") c.Flags().StringVar(&rootCAs, "issuer-root-ca", "", "Root certificate authorities for the issuer. Defaults to host certs.")
c.Flags().BoolVar(&debug, "debug", false, "Print all request and responses from the OpenID Connect issuer.")
return &c return &c
} }