Skip to content

Commit

Permalink
Move cartel client to internal logger as well
Browse files Browse the repository at this point in the history
Signed-off-by: Andy Lo-A-Foe <[email protected]>
  • Loading branch information
loafoe committed Apr 26, 2021
1 parent b8d3722 commit bc2a1e9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 33 deletions.
38 changes: 10 additions & 28 deletions cartel/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/philips-software/go-hsdp-api/internal"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strings"

"github.com/philips-software/go-hsdp-api/internal"

autoconf "github.com/philips-software/go-hsdp-api/config"

"github.com/google/go-querystring/query"
Expand Down Expand Up @@ -115,6 +115,14 @@ func NewClient(httpClient *http.Client, config *Config) (*Client, error) {
cartel.httpClient = httpClient
cartel.userAgent = userAgent

if config.DebugLog != "" {
var err error
cartel.debugFile, err = os.OpenFile(config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
httpClient.Transport = internal.NewLoggingRoundTripper(httpClient.Transport, cartel.debugFile)
}
}

// Make sure the given URL ends with a slash
host := fmt.Sprintf("https://%s", cartel.config.Host)
if config.NoTLS {
Expand All @@ -129,48 +137,22 @@ func NewClient(httpClient *http.Client, config *Config) (*Client, error) {
return nil, err
}

configDebug(&cartel)
return &cartel, nil
}

func configDebug(cartel *Client) {
if cartel.config.DebugLog != "" {
debugFile, err := os.OpenFile(cartel.config.DebugLog, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
if err == nil {
cartel.debugFile = debugFile
}
}
if cartel.debugFile == nil {
cartel.debugFile = os.Stderr
}
}

// do sends an API request and returns the API response. The API response is
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it.
func (c *Client) do(req *http.Request, v interface{}) (*Response, error) {
if c.debugFile != nil {
dumped, _ := httputil.DumpRequest(req, true)
_, _ = fmt.Fprintf(c.debugFile, "REQUEST: %s\n", string(dumped))
}
req.Close = true // Always close request
resp, err := c.httpClient.Do(req)
if resp == nil || (err != nil && err != io.EOF) {
return nil, fmt.Errorf("client.do: %w", err)
}
defer resp.Body.Close()
response := newResponse(resp)

if c.debugFile != nil {
if resp != nil {
dumped, _ := httputil.DumpResponse(resp, true)
_, _ = fmt.Fprintf(c.debugFile, "RESPONSE: %s\n", string(dumped))
} else {
_, _ = fmt.Fprintf(c.debugFile, "Error sending response: %s\n", err)
}
}
if v != nil {
if w, ok := v.(io.Writer); ok {
_, err = io.Copy(w, resp.Body)
Expand Down
11 changes: 6 additions & 5 deletions internal/round_trippers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ type filter struct {
}

var filterList = []filter{
{regexp.MustCompile(`Authorization: (.*)\n`), "Authorization: [filtered]\n"},
{regexp.MustCompile(`password=\w+`), "password=[filtered]"},
{regexp.MustCompile(`"refresh_token":"[^"]+"`), `"refresh_token":"[filtered]"`},
{regexp.MustCompile(`"access_token":"[^"]+"`), `"access_token":"[filtered]"`},
{regexp.MustCompile(`"id_token":"[^"]+"`), `"id_token":"[filtered]"`},
{regexp.MustCompile(`Authorization: (.*)\n`), "Authorization: <sensitive>\n"},
{regexp.MustCompile(`password=\w+`), "password=<sensitive>"},
{regexp.MustCompile(`"refresh_token":"[^"]+"`), `"refresh_token":"<sensitive>"`},
{regexp.MustCompile(`"access_token":"[^"]+"`), `"access_token":"<sensitive>"`},
{regexp.MustCompile(`"id_token":"[^"]+"`), `"id_token":"<sensitive>"`},
{regexp.MustCompile(`"token":"[^"]+"`), `"token":"<sensitive>"`},
}

type HeaderRoundTripper struct {
Expand Down

0 comments on commit bc2a1e9

Please sign in to comment.