Skip to content
This repository has been archived by the owner on Oct 27, 2020. It is now read-only.

Commit

Permalink
Merge branch 'master' into subpath-regex
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank B Greco Jr committed Nov 2, 2017
2 parents 32ccb50 + d664911 commit 960a88f
Show file tree
Hide file tree
Showing 17 changed files with 101 additions and 169 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.2.1] - 2017-10-13
### Added
- Adding `ClusterRole` in `scripts/install.sh` so that install works with latest Minikube releases.
### Changed
- Upstream URLs will now be properly encoded/decoded.
- Improved test coverage.
### Removed
- Gzip support. Kanali will be a transparent proxy.

## [1.2.0] - 2017-09-24
### Added
Expand Down
9 changes: 4 additions & 5 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import:
version: 0.11.5
- package: github.com/spf13/pflag
- package: github.com/spf13/cobra
- package: github.com/NYTimes/gziphandler
- package: github.com/PuerkitoBio/purell
version: v1.1.0
- package: github.com/spf13/viper
version: v1.0.0
- package: github.com/armon/go-proxyproto
Expand Down
15 changes: 4 additions & 11 deletions handlers/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ func (h Handler) serveHTTP(w http.ResponseWriter, r *http.Request) {
t0 := time.Now()
m := &metrics.Metrics{}

normalize(r)

defer func() {
m.Add(
metrics.Metric{Name: "total_time", Value: int(time.Now().Sub(t0) / time.Millisecond), Index: false},
metrics.Metric{Name: "http_method", Value: r.Method, Index: true},
metrics.Metric{Name: "http_uri", Value: r.URL.EscapedPath(), Index: false},
metrics.Metric{Name: "http_uri", Value: utils.ComputeURLPath(r.URL), Index: false},
metrics.Metric{Name: "client_ip", Value: strings.Split(r.RemoteAddr, ":")[0], Index: false},
)
go func() {
Expand All @@ -69,7 +67,7 @@ func (h Handler) serveHTTP(w http.ResponseWriter, r *http.Request) {

sp := opentracing.StartSpan(fmt.Sprintf("%s %s",
r.Method,
r.URL.EscapedPath(),
utils.ComputeURLPath(r.URL),
))
defer sp.Finish()

Expand All @@ -92,7 +90,7 @@ func (h Handler) serveHTTP(w http.ResponseWriter, r *http.Request) {
// log error
logrus.WithFields(logrus.Fields{
"method": r.Method,
"uri": r.URL.EscapedPath(),
"uri": utils.ComputeURLPath(r.URL),
}).Error(e.Error())

m.Add(metrics.Metric{Name: "http_response_code", Value: strconv.Itoa(e.Status()), Index: true})
Expand All @@ -119,7 +117,7 @@ func (h Handler) serveHTTP(w http.ResponseWriter, r *http.Request) {
// log error
logrus.WithFields(logrus.Fields{
"method": r.Method,
"uri": r.URL.EscapedPath(),
"uri": utils.ComputeURLPath(r.URL),
}).Error("unknown error")

m.Add(metrics.Metric{Name: "http_response_code", Value: strconv.Itoa(http.StatusInternalServerError), Index: true})
Expand All @@ -141,8 +139,3 @@ func (h Handler) serveHTTP(w http.ResponseWriter, r *http.Request) {

}
}

func normalize(r *http.Request) {
r.URL.Path = utils.NormalizePath(r.URL.Path)
r.URL.RawPath = r.URL.EscapedPath()
}
62 changes: 0 additions & 62 deletions handlers/handler_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion handlers/incoming.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/northwesternmutual/kanali/metrics"
"github.com/northwesternmutual/kanali/spec"
"github.com/northwesternmutual/kanali/steps"
"github.com/northwesternmutual/kanali/utils"
"github.com/opentracing/opentracing-go"
"github.com/spf13/viper"
)
Expand All @@ -46,7 +47,7 @@ func IncomingRequest(ctx context.Context, proxy *spec.APIProxy, m *metrics.Metri
steps.ValidateProxyStep{},
steps.PluginsOnRequestStep{},
)
if viper.GetBool(config.FlagProxyEnableMockResponses.GetLong()) && mockIsDefined(r.URL.Path) {
if viper.GetBool(config.FlagProxyEnableMockResponses.GetLong()) && mockIsDefined(utils.ComputeURLPath(r.URL)) {
f.Add(steps.MockServiceStep{})
} else {
f.Add(steps.ProxyPassStep{})
Expand Down
8 changes: 3 additions & 5 deletions handlers/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,25 @@ import (
"net/http"
"strings"

"github.com/NYTimes/gziphandler"
"github.com/Sirupsen/logrus"
"github.com/northwesternmutual/kanali/utils"
)

// Logger creates a custom http.Handler that logs details around a request
// along with creating contextual request metrics. When the request is complete
// these metrics will be writtin to Influxdb
func Logger(inner Handler) http.Handler {

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

inner.serveHTTP(w, r)

logrus.WithFields(logrus.Fields{
"client ip": strings.Split(r.RemoteAddr, ":")[0],
"method": r.Method,
"uri": r.URL.EscapedPath(),
"uri": utils.ComputeURLPath(r.URL),
}).Info("request details")

})

return gziphandler.GzipHandler(h)

}
2 changes: 1 addition & 1 deletion helm/templates/clusterRoleBinding.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ metadata:
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kanali-role
name: kanali
subjects:
- kind: ServiceAccount
name: kanali
Expand Down
2 changes: 1 addition & 1 deletion helm/values.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

imageRegistry: northwesternmutual

dockerImageTag: v1.2.0
dockerImageTag: v1.2.1

pullPolicy: Always

Expand Down
2 changes: 1 addition & 1 deletion kubernetes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ spec:
containers:
- name: kanali
imagePullPolicy: IfNotPresent
image: northwesternmutual/kanali:v1.1.5
image: northwesternmutual/kanali:v1.2.1
command:
- /kanali
- start
Expand Down
4 changes: 2 additions & 2 deletions spec/apiproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,6 @@ func (p Plugin) GetFileName() string {
}

func normalize(p *APIProxy) {
(*p).Spec.Path = utils.NormalizePath(p.Spec.Path)
(*p).Spec.Target = utils.NormalizePath(p.Spec.Target)
(*p).Spec.Path = utils.NormalizeURLPath(p.Spec.Path)
(*p).Spec.Target = utils.NormalizeURLPath(p.Spec.Target)
}
2 changes: 1 addition & 1 deletion steps/mockservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (step MockServiceStep) GetName() string {
// Do executes the logic of the MockServiceStep step
func (step MockServiceStep) Do(ctx context.Context, proxy *spec.APIProxy, m *metrics.Metrics, w http.ResponseWriter, r *http.Request, resp *http.Response, trace opentracing.Span) error {

targetPath := utils.ComputeTargetPath(proxy.Spec.Path, proxy.Spec.Target, r.URL.EscapedPath())
targetPath := utils.ComputeTargetPath(proxy.Spec.Path, proxy.Spec.Target, utils.ComputeURLPath(r.URL))

untypedMr, err := spec.MockResponseStore.Get(proxy.ObjectMeta.Namespace, proxy.Spec.Mock.ConfigMapName, targetPath, r.Method)
if err != nil {
Expand Down
11 changes: 8 additions & 3 deletions steps/proxypass.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func preformTargetProxy(client httpClient, request *http.Request, m *metrics.Met

sp := opentracing.StartSpan(fmt.Sprintf("%s %s",
request.Method,
request.URL.EscapedPath(),
utils.ComputeURLPath(request.URL),
), opentracing.ChildOf(span.Context()))
defer sp.Finish()

Expand Down Expand Up @@ -218,14 +218,19 @@ func getTargetURL(proxy *spec.APIProxy, originalRequest *http.Request) (*url.URL
uri = svc.ClusterIP
}

u, err := url.Parse(utils.ComputeTargetPath(proxy.Spec.Path, proxy.Spec.Target, originalRequest.URL.EscapedPath()))
if err != nil {
return nil, err
}

return &url.URL{
Scheme: scheme,
Host: fmt.Sprintf("%s:%d",
uri,
proxy.Spec.Service.Port,
),
Path: utils.ComputeTargetPath(proxy.Spec.Path, proxy.Spec.Target, originalRequest.URL.Path),
RawPath: utils.ComputeTargetPath(proxy.Spec.Path, proxy.Spec.Target, originalRequest.URL.EscapedPath()),
Path: u.Path,
RawPath: u.RawPath,
ForceQuery: originalRequest.URL.ForceQuery,
RawQuery: originalRequest.URL.RawQuery,
Fragment: originalRequest.URL.Fragment,
Expand Down
20 changes: 15 additions & 5 deletions steps/proxypass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestCreateTargetRequest(t *testing.T) {
Scheme: "http",
Host: "bar.foo.svc.cluster.local:8080",
Path: "/",
RawPath: "/",
RawPath: "",
ForceQuery: false,
})
}
Expand Down Expand Up @@ -262,6 +262,7 @@ func TestGetTargetURL(t *testing.T) {
Port: 8080,
})
req, _ := http.NewRequest("GET", "http://foo.bar.com/api/v1/accounts", nil)
reqTwo, _ := http.NewRequest("GET", "http://foo.bar.com/api/v1/accounts"+"/https%3A%2F%2Fgoogle.com", nil)

proxyOne := &spec.APIProxy{
ObjectMeta: api.ObjectMeta{
Expand All @@ -284,7 +285,7 @@ func TestGetTargetURL(t *testing.T) {
Scheme: "http",
Host: "bar.foo.svc.cluster.local:8080",
Path: "/",
RawPath: "/",
RawPath: "",
ForceQuery: false,
})

Expand All @@ -295,7 +296,7 @@ func TestGetTargetURL(t *testing.T) {
Scheme: "http",
Host: "1.2.3.4:8080",
Path: "/",
RawPath: "/",
RawPath: "",
ForceQuery: false,
})

Expand All @@ -309,7 +310,7 @@ func TestGetTargetURL(t *testing.T) {
Scheme: "https",
Host: "bar.foo.svc.cluster.local:8080",
Path: "/",
RawPath: "/",
RawPath: "",
ForceQuery: false,
})

Expand All @@ -320,7 +321,16 @@ func TestGetTargetURL(t *testing.T) {
Scheme: "https",
Host: "1.2.3.4:8080",
Path: "/",
RawPath: "/",
RawPath: "",
ForceQuery: false,
})

urlThree, _ := getTargetURL(proxyOne, reqTwo)
assert.Equal(t, *urlThree, url.URL{
Scheme: "https",
Host: "1.2.3.4:8080",
Path: "/https://google.com",
RawPath: "/https%3A%2F%2Fgoogle.com",
ForceQuery: false,
})
}
2 changes: 1 addition & 1 deletion steps/validateproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (step ValidateProxyStep) GetName() string {
// Do executes the logic of the ValidateProxyStep step
func (step ValidateProxyStep) Do(ctx context.Context, proxy *spec.APIProxy, m *metrics.Metrics, w http.ResponseWriter, r *http.Request, resp *http.Response, trace opentracing.Span) error {

untypedProxy, err := spec.ProxyStore.Get(r.URL.EscapedPath())
untypedProxy, err := spec.ProxyStore.Get(utils.ComputeURLPath(r.URL))
if err != nil || untypedProxy == nil {
if err != nil {
logrus.Error(err.Error())
Expand Down
Loading

0 comments on commit 960a88f

Please sign in to comment.