Skip to content

Commit

Permalink
fix: activate disable tests, resolve linting issues
Browse files Browse the repository at this point in the history
  • Loading branch information
omissis committed Aug 2, 2023
1 parent a3e81fc commit 1310ee5
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 36 deletions.
1 change: 1 addition & 0 deletions .rules/.golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ linters-settings:
- $gostd
- github.com/itchyny/gojq
- github.com/omissis
- github.com/stretchr/testify/assert
- github.com/spf13/cobra
- github.com/spf13/pflag
- github.com/spf13/viper
Expand Down
1 change: 1 addition & 0 deletions pkg/http/middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ func corsMethod(conf CORSConfig) string {
return strings.Join(methods, ", ")
}

//nolint:cyclop // leave this alone
func corsOrigin(conf CORSConfig, req *http.Request) string {
if req == nil {
return ""
Expand Down
68 changes: 40 additions & 28 deletions pkg/http/middlewares_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
"net/http/httptest"
"testing"

kaspHttp "github.com/omissis/kube-apiserver-proxy/pkg/http"
"github.com/stretchr/testify/assert"

kaspHttp "github.com/omissis/kube-apiserver-proxy/pkg/http"
)

func TestCORSMiddleware(t *testing.T) {
Expand All @@ -23,25 +24,28 @@ func TestCORSMiddleware(t *testing.T) {
credentials string
wantCredentials string
}{
// {
// desc: "default config",
// conf: kaspHttp.CORSConfig{},
// origin: "*",
// methods: []string{"*"},
// credentials: "false",
// },
// {
// desc: "multiple origins",
// conf: kaspHttp.CORSConfig{
// AllowOrigins: []string{"https://api.kube-apiserver-proxy.dev", "https://api.kube-apiserver-proxy.test"},
// },
// origin: "https://api.kube-apiserver-proxy.dev",
// wantOrigin: "https://api.kube-apiserver-proxy.dev",
// methods: "*",
// wantMethods: "*",
// credentials: "false",
// wantCredentials: "false",
// },
{
desc: "default config",
conf: kaspHttp.CORSConfig{},
origin: "*",
wantOrigin: "*",
methods: "*",
wantMethods: "*",
credentials: "false",
wantCredentials: "false",
},
{
desc: "multiple origins",
conf: kaspHttp.CORSConfig{
AllowOrigins: []string{"https://api.kube-apiserver-proxy.dev", "https://api.kube-apiserver-proxy.test"},
},
origin: "https://api.kube-apiserver-proxy.dev",
wantOrigin: "https://api.kube-apiserver-proxy.dev",
methods: "*",
wantMethods: "*",
credentials: "false",
wantCredentials: "false",
},
{
desc: "origin with authentication",
conf: kaspHttp.CORSConfig{
Expand All @@ -54,13 +58,18 @@ func TestCORSMiddleware(t *testing.T) {
credentials: "false",
wantCredentials: "false",
},
// {
// desc: "multiple methods",
// conf: kaspHttp.CORSConfig{
// AllowMethods: []string{"GET", "POST"},
// },
// methods: "GET, POST",
// },
{
desc: "multiple methods",
conf: kaspHttp.CORSConfig{
AllowMethods: []string{http.MethodGet, "POST"},
},
origin: "*",
wantOrigin: "*",
methods: "GET, POST",
wantMethods: "GET, POST",
credentials: "false",
wantCredentials: "false",
},
}
for _, tC := range testCases {
tC := tC
Expand All @@ -77,19 +86,22 @@ func TestCORSMiddleware(t *testing.T) {

url := "https://api.kube-apiserver-proxy.dev/api/v1/namespaces/kube-system/pods"

req := httptest.NewRequest("GET", url, nil)
req := httptest.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Origin", tC.origin)

w := httptest.NewRecorder()

handler.ServeHTTP(w, req)

resp := w.Result()

body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

defer resp.Body.Close()

assert.Equal(t, "OK", string(body))
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, tC.wantCredentials, resp.Header.Get("Access-Control-Allow-Credentials"))
Expand Down
28 changes: 20 additions & 8 deletions pkg/kube/proxy/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxy

import (
"context"
"errors"
"fmt"
"log"
"net/http"
Expand All @@ -10,6 +11,17 @@ import (
"github.com/omissis/kube-apiserver-proxy/pkg/kube"
)

var (
ErrCannotApplyResponseTransformers = errors.New("cannot apply response transformers")
ErrCannotCreateRESTClient = errors.New("cannot create rest client")
ErrCannotGetProxiedResponseBody = errors.New("cannot get response of proxied response body")
ErrCannotParseRequestURI = errors.New("cannot parse request URI")
ErrCannotTransformResponseBody = errors.New("cannot transform response body")
ErrCannotWriteResponseBody = errors.New("cannot write body to the response")
ErrContextIsNil = errors.New("context is nil")
ErrResponseWriterIsNil = errors.New("response writer is nil")
)

func NewHTTP(restClientFactory kube.RESTClientFactory, responseTransformers []ResponseBodyTransformer) *HTTP {
return &HTTP{
restClientFactory: restClientFactory,
Expand Down Expand Up @@ -50,16 +62,16 @@ func (h *HTTP) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// to avoid the log.Printf in ServeHTTP, leaving the responsibility of the error handling to the caller.
func (h *HTTP) DoServeHTTP(ctx context.Context, w http.ResponseWriter, r http.Request) error {
if ctx == nil {
return fmt.Errorf("context is nil")
return ErrContextIsNil
}

if w == nil {
return fmt.Errorf("response writer is nil")
return ErrResponseWriterIsNil
}

req, err := h.restClientFactory.Request(r)
if err != nil {
return fmt.Errorf("cannot create rest client: %w", err)
return fmt.Errorf("%w: %w", ErrCannotCreateRESTClient, err)
}

cctx, cancel := context.WithCancel(ctx)
Expand All @@ -69,12 +81,12 @@ func (h *HTTP) DoServeHTTP(ctx context.Context, w http.ResponseWriter, r http.Re

body, err := res.Raw()
if err != nil {
return fmt.Errorf("cannot get response of proxied response body: %w", err)
return fmt.Errorf("%w: %w", ErrCannotGetProxiedResponseBody, err)
}

body, err = h.applyTransformers(r, body)
if err != nil {
return fmt.Errorf("cannot apply response transformers: %w", err)
return fmt.Errorf("%w: %w", ErrCannotApplyResponseTransformers, err)
}

sc := 0
Expand All @@ -83,7 +95,7 @@ func (h *HTTP) DoServeHTTP(ctx context.Context, w http.ResponseWriter, r http.Re

_, err = w.Write(body)
if err != nil {
return fmt.Errorf("cannot write body to the response: %w", err)
return fmt.Errorf("%w: %w", ErrCannotWriteResponseBody, err)
}

return nil
Expand All @@ -92,7 +104,7 @@ func (h *HTTP) DoServeHTTP(ctx context.Context, w http.ResponseWriter, r http.Re
func (h *HTTP) applyTransformers(r http.Request, body []byte) ([]byte, error) {
u, err := url.Parse(r.URL.String())
if err != nil {
return body, fmt.Errorf("cannot parse request uri: %w", err)
return body, fmt.Errorf("%w: %w", ErrCannotParseRequestURI, err)
}

for _, rt := range h.responseTransformers {
Expand All @@ -101,7 +113,7 @@ func (h *HTTP) applyTransformers(r http.Request, body []byte) ([]byte, error) {
if src != "" {
body, err = rt.Run(body, map[string]any{"src": src})
if err != nil {
return body, fmt.Errorf("cannot transform response body: %w", err)
return body, fmt.Errorf("%w: %w", ErrCannotTransformResponseBody, err)
}

return body, nil
Expand Down

0 comments on commit 1310ee5

Please sign in to comment.