Skip to content

Commit

Permalink
chore: abstract requester marshal unmarshal
Browse files Browse the repository at this point in the history
  • Loading branch information
shaj13 committed Feb 3, 2021
1 parent 5529aa7 commit e384b54
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
16 changes: 9 additions & 7 deletions auth/internal/requestor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -22,11 +21,13 @@ type Requester struct {
Client *http.Client
// AdditionalData add more data to http request
AdditionalData func(r *http.Request)
func(data []byte, v interface{}) error
Marshal func(v interface{}) ([]byte, error)
}

// Do sends the HTTP request and parse the HTTP response.
func (r *Requester) Do(ctx context.Context, data, review, status interface{}) error {
body, err := json.Marshal(data)
body, err := r.Marshal(data)
if err != nil {
return fmt.Errorf("Failed to marshal request body data, Type: %T, Err: %w", data, err)
}
Expand All @@ -42,9 +43,6 @@ func (r *Requester) Do(ctx context.Context, data, review, status interface{}) er
r.AdditionalData(req)
}

req.Header.Set("Content-Type", "application/json")
req.Header.Set("Accept", "application/json")

resp, err := r.Client.Do(req)
if err != nil {
return fmt.Errorf("Failed to send the HTTP request, Method: POST, URL: %s, Err: %w", url, err)
Expand All @@ -57,11 +55,11 @@ func (r *Requester) Do(ctx context.Context, data, review, status interface{}) er

defer resp.Body.Close()

if err := json.Unmarshal(body, status); err == nil {
if err := r.Unmarshal(body, status); err == nil {
return nil
}

if err := json.Unmarshal(body, review); err != nil {
if err := r.Unmarshal(body, review); err != nil {
return fmt.Errorf("Failed to unmarshal response body data, Type: %T Err: %w", review, err)
}

Expand All @@ -72,8 +70,12 @@ func (r *Requester) Do(ctx context.Context, data, review, status interface{}) er
func SetRequesterBearerToken(token string) auth.Option {
return auth.OptionFunc(func(v interface{}) {
if r, ok := v.(*Requester); ok {
additionalData := r.AdditionalData
r.AdditionalData = func(r *http.Request) {
r.Header.Set("Authorization", "Bearer "+token)
if additionalData != nil {
additionalData(r)
}
}
}
})
Expand Down
11 changes: 9 additions & 2 deletions auth/strategies/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package kubernetes

import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
Expand Down Expand Up @@ -72,8 +73,14 @@ func newKubeReview(opts ...auth.Option) *kubeReview {

kr := &kubeReview{
requester: &internal.Requester{
Addr: "http://127.0.0.1:6443",
Endpoint: "/apis/authentication.k8s.io/v1/tokenreviews",
Addr: "http://127.0.0.1:6443",
Endpoint: "/apis/authentication.k8s.io/v1/tokenreviews",
Marshal: json.Marshal,
Unmarshal: json.Unmarshal,
AdditionalData: func(r *http.Request) {
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
},
Client: &http.Client{
Transport: &http.Transport{},
},
Expand Down
3 changes: 3 additions & 0 deletions auth/strategies/kubernetes/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import (
)

func TestSetServiceAccountToken(t *testing.T) {
appj := "application/json"
token := "test-token"
opt := SetServiceAccountToken(token)
kr := newKubeReview(opt)
r, _ := http.NewRequest("", "", nil)
kr.requester.AdditionalData(r)
assert.Equal(t, "Bearer "+token, r.Header.Get("Authorization"))
assert.Equal(t, appj, r.Header.Get("Content-Type"))
assert.Equal(t, appj, r.Header.Get("Accept"))
}

func TestSetHTTPClient(t *testing.T) {
Expand Down

0 comments on commit e384b54

Please sign in to comment.