-
Notifications
You must be signed in to change notification settings - Fork 84
/
hydrator_client.go
73 lines (59 loc) · 2.08 KB
/
hydrator_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package clients
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"testing"
httputil "github.com/kyma-incubator/compass/components/director/pkg/http"
"github.com/kyma-incubator/compass/components/connector/pkg/oathkeeper"
"github.com/stretchr/testify/require"
)
type HydratorClient struct {
httpClient *http.Client
validatorURL string
}
func NewHydratorClient(validatorURL string) *HydratorClient {
httpClient := &http.Client{
Transport: httputil.NewServiceAccountTokenTransport(httputil.NewHTTPTransportWrapper(&http.Transport{ // Needed because hydrators are behind PeerAuthentication
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
})),
}
return &HydratorClient{
httpClient: httpClient,
validatorURL: validatorURL,
}
}
func (vc *HydratorClient) ExecuteHydratorRequest(t *testing.T, path string, headers map[string][]string) oathkeeper.AuthenticationSession {
authSession := oathkeeper.AuthenticationSession{}
marshalledSession, err := json.Marshal(authSession)
require.NoError(t, err)
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", vc.validatorURL, path), bytes.NewBuffer(marshalledSession))
require.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
for h, vals := range headers {
for _, v := range vals {
req.Header.Set(h, v)
}
}
response, err := vc.httpClient.Do(req)
defer func() {
err := response.Body.Close()
require.NoError(t, err)
}()
require.NoError(t, err)
require.Equal(t, http.StatusOK, response.StatusCode)
var authSessionResponse oathkeeper.AuthenticationSession
err = json.NewDecoder(response.Body).Decode(&authSessionResponse)
require.NoError(t, err)
return authSessionResponse
}
func (vc *HydratorClient) ResolveCertificateData(t *testing.T, headers map[string][]string) oathkeeper.AuthenticationSession {
return vc.ExecuteHydratorRequest(t, "/v1/certificate/data/resolve", headers)
}
func (vc *HydratorClient) ResolveToken(t *testing.T, headers map[string][]string) oathkeeper.AuthenticationSession {
return vc.ExecuteHydratorRequest(t, "/v1/tokens/resolve", headers)
}