Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add --log-http option #326

Merged
merged 10 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/cmd/kn.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Manage your Knative building blocks:
--config string kn config file (default is $HOME/.kn/config.yaml)
-h, --help help for kn
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
--lookup-plugins-in-path look for kn plugins in $PATH
--plugins-dir string kn plugins directory (default "~/.kn/plugins")
```
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ kn plugin [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_plugin_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ kn plugin list [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_revision.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kn revision [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_revision_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ kn revision delete NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_revision_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kn revision describe NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_revision_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ kn revision list [name] [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_route.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kn route [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_route_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kn route describe NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_route_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ kn route list NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kn service [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ kn service create NAME --image IMAGE [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ kn service delete NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ kn service describe NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ kn service list [name] [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_service_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ kn service update NAME [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
1 change: 1 addition & 0 deletions docs/cmd/kn_version.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ kn version [flags]
```
--config string kn config file (default is $HOME/.kn/config.yaml)
--kubeconfig string kubectl config file (default is $HOME/.kube/config)
--log-http log http traffic
```

### SEE ALSO
Expand Down
10 changes: 10 additions & 0 deletions pkg/kn/commands/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"

serving_kn_v1alpha1 "github.com/knative/client/pkg/serving/v1alpha1"
"github.com/knative/client/pkg/util"
serving_v1alpha1_client "github.com/knative/serving/pkg/client/clientset/versioned/typed/serving/v1alpha1"
"k8s.io/client-go/tools/clientcmd"
)
Expand All @@ -45,6 +46,9 @@ type KnParams struct {
ClientConfig clientcmd.ClientConfig
NewClient func(namespace string) (serving_kn_v1alpha1.KnClient, error)

// General global options
LogHttp bool

// Set this if you want to nail down the namespace
fixedCurrentNamespace string
}
Expand Down Expand Up @@ -78,6 +82,12 @@ func (params *KnParams) GetConfig() (serving_v1alpha1_client.ServingV1alpha1Inte
if err != nil {
return nil, err
}
if params.LogHttp {
// TODO: When we update to the newer version of client-go, replace with
// config.Wrap() for future compat.
config.WrapTransport = util.NewLoggingTransport
}

return serving_v1alpha1_client.NewForConfig(config)
}

Expand Down
46 changes: 45 additions & 1 deletion pkg/kn/commands/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,56 @@ import (
type getConfigTestCase struct {
clientConfig clientcmd.ClientConfig
expectedErrString string
logHttp bool
}

var BASIC_KUBECONFIG = `apiVersion: v1
kind: Config
preferences: {}
users:
- name: a
user:
client-certificate-data: ""
client-key-data: ""
clusters:
- name: a
cluster:
insecure-skip-tls-verify: true
server: https://127.0.0.1:8080
contexts:
- name: a
context:
cluster: a
user: a
current-context: a
`

func TestGetConfig(t *testing.T) {
basic, err := clientcmd.NewClientConfigFromBytes([]byte(BASIC_KUBECONFIG))
if err != nil {
t.Error(err)
}
for i, tc := range []getConfigTestCase{
{
clientcmd.NewDefaultClientConfig(clientcmdapi.Config{}, &clientcmd.ConfigOverrides{}),
"no configuration has been provided",
false,
},
{
basic,
"",
false,
},
{ // Test that the cast to wrap the http client in a logger works
basic,
"",
true,
},
} {
p := &KnParams{
ClientConfig: tc.clientConfig}
ClientConfig: tc.clientConfig,
LogHttp: tc.logHttp,
}

_, err := p.GetConfig()

Expand Down Expand Up @@ -70,6 +109,11 @@ func TestGetClientConfig(t *testing.T) {

multiConfigs = multiConfigs
for _, tc := range []typeTestCase{
{
"",
clientcmd.NewDefaultClientConfigLoadingRules().ExplicitPath,
"",
},
sixolet marked this conversation as resolved.
Show resolved Hide resolved
{
"",
clientcmd.NewDefaultClientConfigLoadingRules().ExplicitPath,
Expand Down
1 change: 1 addition & 0 deletions pkg/kn/core/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ func NewKnCommand(params ...commands.KnParams) *cobra.Command {
// Persistent flags
rootCmd.PersistentFlags().StringVar(&commands.CfgFile, "config", "", "kn config file (default is $HOME/.kn/config.yaml)")
rootCmd.PersistentFlags().StringVar(&p.KubeCfgPath, "kubeconfig", "", "kubectl config file (default is $HOME/.kube/config)")
rootCmd.PersistentFlags().BoolVar(&p.LogHttp, "log-http", false, "log http traffic")

plugin.AddPluginFlags(rootCmd)
plugin.BindPluginsFlagToViper(rootCmd)
Expand Down
74 changes: 74 additions & 0 deletions pkg/util/logging_http_transport.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright © 2018 The Knative Authors
sixolet marked this conversation as resolved.
Show resolved Hide resolved
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"fmt"
"io"
"net/http"
"net/http/httputil"
"os"
)

type LoggingHttpTransport struct {
transport http.RoundTripper
stream io.Writer
}

func NewLoggingTransport(transport http.RoundTripper) http.RoundTripper {
return &LoggingHttpTransport{transport, nil}
}

func NewLoggingTransportWithStream(transport http.RoundTripper, s io.Writer) http.RoundTripper {
return &LoggingHttpTransport{transport, s}
}

var SENSITIVE_HEADERS = map[string]bool{
sixolet marked this conversation as resolved.
Show resolved Hide resolved
"Authorization": true,
"WWW-Authenticate": true,
"Cookie": true,
"Proxy-Authorization": true,
}

func (t *LoggingHttpTransport) RoundTrip(r *http.Request) (*http.Response, error) {
stream := t.stream
if stream == nil {
stream = os.Stderr
}
reqCopy := *r
reqCopy.Header = make(http.Header, len(r.Header))
for k, v := range r.Header {
sensitive := SENSITIVE_HEADERS[k]
if sensitive {
reqCopy.Header.Set(k, "ELIDED")
sixolet marked this conversation as resolved.
Show resolved Hide resolved
} else {
reqCopy.Header[k] = v
}
}
reqBytes, _ := httputil.DumpRequestOut(&reqCopy, true)
fmt.Fprintln(stream, "===== REQUEST =====")
fmt.Fprintln(stream, string(reqBytes))
sixolet marked this conversation as resolved.
Show resolved Hide resolved
resp, err := t.transport.RoundTrip(r)
if err != nil {
fmt.Fprintln(stream, "===== ERROR =====")
fmt.Fprintln(stream, err)
} else {
respBytes, _ := httputil.DumpResponse(resp, true)
fmt.Fprintln(stream, "===== RESPONSE =====")
fmt.Fprintln(stream, string(respBytes))
fmt.Fprintln(stream, " * * * * * *")
}
return resp, err
}
79 changes: 79 additions & 0 deletions pkg/util/logging_http_transport_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © 2019 The Knative Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"strings"
"testing"

"gotest.tools/assert"
)

type dummyTransport struct{}

func (d *dummyTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return &http.Response{
Status: "200 OK",
StatusCode: 200,
Proto: "HTTP/1.0",
Body: ioutil.NopCloser(strings.NewReader("")),
}, nil
}

type errorTransport struct{}

func (d *errorTransport) RoundTrip(r *http.Request) (*http.Response, error) {
return nil, errors.New("This is an error")
}

func TestWritesRequestResponse(t *testing.T) {
out := &bytes.Buffer{}
transport := NewLoggingTransportWithStream(&dummyTransport{}, out)
req, _ := http.NewRequest("GET", "http://example.com", nil)
_, e := transport.RoundTrip(req)
assert.NilError(t, e)
s := out.String()
assert.Assert(t, strings.Contains(s, "REQUEST"))
assert.Assert(t, strings.Contains(s, "RESPONSE"))
}

func TestElideAuthorizationHeader(t *testing.T) {
out := &bytes.Buffer{}
transport := NewLoggingTransportWithStream(&dummyTransport{}, out)
req, _ := http.NewRequest("GET", "http://example.com", nil)
req.Header.Set("X-Normal-Header", "la la normal text")
req.Header.Set("Authorization", "Bearer: SECRET")
_, e := transport.RoundTrip(req)
assert.NilError(t, e)
s := out.String()
assert.Assert(t, strings.Contains(s, "REQUEST"))
assert.Assert(t, strings.Contains(s, "la la normal text"))
assert.Assert(t, !strings.Contains(s, "SECRET"))
assert.Assert(t, strings.Contains(s, "RESPONSE"))
}

func TestWritesRequestError(t *testing.T) {
out := &bytes.Buffer{}
transport := NewLoggingTransportWithStream(&errorTransport{}, out)
req, _ := http.NewRequest("GET", "http://example.com", nil)
transport.RoundTrip(req)
s := out.String()
assert.Assert(t, strings.Contains(s, "REQUEST"))
assert.Assert(t, strings.Contains(s, "ERROR"))
}