/
proxy.go
190 lines (154 loc) · 6.04 KB
/
proxy.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package proxy
import (
"bytes"
"context"
"encoding/json"
"io"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/csrf"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/httperrors"
"github.com/kyma-project/kyma/components/central-application-gateway/internal/metadata/model"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/apperrors"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/authorization"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/authorization/clientcert"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/httpconsts"
"github.com/kyma-project/kyma/components/central-application-gateway/pkg/proxyconfig"
)
type proxy struct {
cache Cache
proxyTimeout int
authorizationStrategyFactory authorization.StrategyFactory
csrfTokenStrategyFactory csrf.TokenStrategyFactory
extractPathFunc pathExtractorFunc
extractGatewayFunc gatewayURLExtractorFunc
apiExtractor APIExtractor
}
//go:generate mockery --name=APIExtractor
type APIExtractor interface {
Get(identifier model.APIIdentifier) (*model.API, apperrors.AppError)
}
// Config stores Proxy config
type Config struct {
ProxyTimeout int
Application string
ProxyCacheTTL int
}
func (p *proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
apiIdentifier, path, gwURL, err := p.extractPath(r.URL)
if err != nil {
handleErrors(w, err)
return
}
serviceAPI, err := p.apiExtractor.Get(apiIdentifier)
if err != nil {
handleErrors(w, err)
return
}
r.URL.Path = path.Path
if !serviceAPI.EncodeUrl {
r.URL.RawPath = path.RawPath
}
cacheEntry, err := p.getOrCreateCacheEntry(apiIdentifier, *serviceAPI)
if err != nil {
handleErrors(w, err)
return
}
newRequest, cancel := p.setRequestTimeout(r)
defer cancel()
err = p.addAuthorization(newRequest, cacheEntry, serviceAPI.SkipVerify)
if err != nil {
handleErrors(w, err)
return
}
cacheEntry.Proxy.ModifyResponse = responseModifier(gwURL, serviceAPI.TargetUrl, urlRewriter)
cacheEntry.Proxy.ServeHTTP(w, newRequest)
}
func (p *proxy) extractPath(u *url.URL) (model.APIIdentifier, *url.URL, *url.URL, apperrors.AppError) {
apiIdentifier, path, gwURL, err := p.extractPathFunc(u)
if err != nil {
return model.APIIdentifier{}, nil, nil, apperrors.WrongInput("failed to extract API Identifier from path")
}
return apiIdentifier, path, gwURL, nil
}
func (p *proxy) getOrCreateCacheEntry(apiIdentifier model.APIIdentifier, serviceAPI model.API) (*CacheEntry, apperrors.AppError) {
cacheObj, found := p.cache.Get(apiIdentifier.Application, apiIdentifier.Service, apiIdentifier.Entry)
if found {
return cacheObj, nil
}
return p.createCacheEntry(apiIdentifier, serviceAPI)
}
func (p *proxy) createCacheEntry(apiIdentifier model.APIIdentifier, serviceAPI model.API) (*CacheEntry, apperrors.AppError) {
clientCertificate := clientcert.NewClientCertificate(nil)
authorizationStrategy := p.newAuthorizationStrategy(serviceAPI.Credentials)
csrfTokenStrategy := p.newCSRFTokenStrategy(authorizationStrategy, serviceAPI.Credentials)
proxy, err := makeProxy(serviceAPI.TargetUrl, serviceAPI.RequestParameters, apiIdentifier.Service, serviceAPI.SkipVerify, authorizationStrategy, csrfTokenStrategy, clientCertificate, p.proxyTimeout)
if err != nil {
return nil, err
}
return p.cache.Put(apiIdentifier.Application, apiIdentifier.Service, apiIdentifier.Entry, proxy, authorizationStrategy, csrfTokenStrategy, clientCertificate), nil
}
func (p *proxy) newAuthorizationStrategy(credentials *authorization.Credentials) authorization.Strategy {
return p.authorizationStrategyFactory.Create(credentials)
}
func (p *proxy) newCSRFTokenStrategy(authorizationStrategy authorization.Strategy, credentials *authorization.Credentials) csrf.TokenStrategy {
csrfTokenEndpointURL := ""
if credentials != nil {
csrfTokenEndpointURL = credentials.CSRFTokenEndpointURL
}
return p.csrfTokenStrategyFactory.Create(authorizationStrategy, csrfTokenEndpointURL)
}
func (p *proxy) newCSRFTokenStrategyFromCSRFConfig(authorizationStrategy authorization.Strategy, csrfConfig *proxyconfig.CSRFConfig) csrf.TokenStrategy {
csrfTokenEndpointURL := ""
if csrfConfig != nil {
csrfTokenEndpointURL = csrfConfig.TokenURL
}
return p.csrfTokenStrategyFactory.Create(authorizationStrategy, csrfTokenEndpointURL)
}
func (p *proxy) setRequestTimeout(r *http.Request) (*http.Request, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(p.proxyTimeout)*time.Second)
newRequest := r.WithContext(ctx)
return newRequest, cancel
}
func (p *proxy) addAuthorization(r *http.Request, cacheEntry *CacheEntry, skipTLSVerify bool) apperrors.AppError {
err := cacheEntry.AuthorizationStrategy.AddAuthorization(r, skipTLSVerify)
if err != nil {
return err
}
return cacheEntry.CSRFTokenStrategy.AddCSRFToken(r, skipTLSVerify)
}
func copyRequestBody(r *http.Request) (io.ReadCloser, apperrors.AppError) {
if r.Body == nil {
return nil, nil
}
bodyCopy, secondRequestBody, err := drainBody(r.Body)
if err != nil {
return nil, apperrors.Internal("failed to drain request body, %s", err)
}
r.Body = bodyCopy
return secondRequestBody, nil
}
func drainBody(b io.ReadCloser) (r1, r2 io.ReadCloser, err error) {
if b == http.NoBody {
return http.NoBody, http.NoBody, nil
}
var buf bytes.Buffer
if _, err = buf.ReadFrom(b); err != nil {
return nil, b, err
}
if err = b.Close(); err != nil {
return nil, b, err
}
return ioutil.NopCloser(&buf), ioutil.NopCloser(bytes.NewReader(buf.Bytes())), nil
}
func handleErrors(w http.ResponseWriter, apperr apperrors.AppError) {
code, body := httperrors.AppErrorToResponse(apperr)
respondWithBody(w, code, body)
}
func respondWithBody(w http.ResponseWriter, code int, body httperrors.ErrorResponse) {
w.Header().Set(httpconsts.HeaderContentType, httpconsts.ContentTypeApplicationJson)
w.WriteHeader(code)
json.NewEncoder(w).Encode(body)
}