/
adaptertransport.go
228 lines (182 loc) · 6.45 KB
/
adaptertransport.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package proxy
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"strings"
"github.com/google/uuid"
"github.com/form3tech-oss/jwt-go"
"github.com/kyma-incubator/compass/components/director/pkg/correlation"
"github.com/kyma-incubator/compass/components/director/pkg/log"
"github.com/kyma-incubator/compass/components/gateway/pkg/httpcommon"
"github.com/pkg/errors"
)
// AdapterConfig stores the configuration for the adapter transport
type AdapterConfig struct {
MsgBodySizeLimit int
}
type AdapterTransport struct {
config AdapterConfig
http.RoundTripper
auditlogSink AuditlogService
auditlogSvc PreAuditlogService
}
func NewAdapterTransport(sink AuditlogService, svc PreAuditlogService, trip RoundTrip, config AdapterConfig) *AdapterTransport {
return &AdapterTransport{
RoundTripper: trip,
auditlogSink: sink,
auditlogSvc: svc,
config: config,
}
}
type auditLogContext struct {
preAuditLog PreAuditlogService
postAuditLog AuditlogService
cfg AdapterConfig
correlationHeaders correlation.Headers
claims Claims
logId uuid.UUID
}
func newAuditLogContext(t *AdapterTransport, req *http.Request) (*auditLogContext, error) {
correlationHeaders := correlation.HeadersForRequest(req)
claims, err := getClaims(req.Context(), req.Header)
if err != nil {
return nil, errors.Wrap(err, "while parsing JWT")
}
transactionId, err := uuid.NewRandom()
if nil != err {
return nil, errors.Wrap(err, "Failed to generate transaction UUID")
}
return &auditLogContext{
preAuditLog: t.auditlogSvc,
postAuditLog: t.auditlogSink,
cfg: t.config,
correlationHeaders: correlationHeaders,
claims: claims,
logId: transactionId,
}, nil
}
func (t *AdapterTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
if req.Body == nil || req.Method == http.MethodGet {
return t.RoundTripper.RoundTrip(req)
}
defer httpcommon.CloseBody(req.Context(), req.Body)
buffer := &bytes.Buffer{}
_, err = io.Copy(buffer, req.Body)
if err != nil {
return nil, err
}
req.Body = io.NopCloser(buffer)
auditLogCtx, err := newAuditLogContext(t, req)
if nil != err {
return nil, err
}
ctx := context.WithValue(req.Context(), correlation.RequestIDHeaderKey, auditLogCtx.correlationHeaders)
if err := doPreAuditLog(ctx, auditLogCtx, buffer.Bytes()); nil != err {
return nil, errors.Wrap(err, "Failed to do pre-change auditlog")
}
log.C(ctx).Info("Proxying request...")
resp, err = t.RoundTripper.RoundTrip(req)
if err != nil {
return nil, errors.Wrap(err, "on request round trip")
}
defer httpcommon.CloseBody(req.Context(), resp.Body)
responseBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
resp.Body = ioutil.NopCloser(bytes.NewReader(responseBody))
if err := doPostAuditLog(ctx, auditLogCtx, responseBody); nil != err {
log.C(ctx).WithError(err).Error("failed to send post-change audit-log")
}
return resp, nil
}
func doPreAuditLog(ctx context.Context, auditLogCtx *auditLogContext, requestBody []byte) error {
log.C(ctx).Infof("Maximum request body length per audit log message: %d", auditLogCtx.cfg.MsgBodySizeLimit)
log.C(ctx).Infof("Length of received request body: %d", len(requestBody))
transactionId := auditLogCtx.logId.String()
shardLength := calculateShardLength(ctx, auditLogCtx, requestBody, transactionId)
log.C(ctx).Infof("Writing pre-change auditlog for %s...", transactionId)
for len(requestBody) > 0 {
length := min(shardLength, len(requestBody))
shard := requestBody[:length]
requestBody = requestBody[length:]
err := auditLogCtx.preAuditLog.PreLog(ctx, AuditlogMessage{
CorrelationIDHeaders: auditLogCtx.correlationHeaders,
Request: transactionId + ": " + string(shard),
Response: "",
Claims: auditLogCtx.claims,
})
if nil != err {
return errors.Wrapf(err, "while sending pre-change auditlog message to auditlog service for %s", transactionId)
}
}
return nil
}
func doPostAuditLog(ctx context.Context, auditLogCtx *auditLogContext, responseBody []byte) error {
log.C(ctx).Infof("Length of received response body: %d", len(responseBody))
transactionId := auditLogCtx.logId.String()
shardLength := calculateShardLength(ctx, auditLogCtx, responseBody, transactionId)
log.C(ctx).Infof("Writing post-change auditlog for %s...", transactionId)
for len(responseBody) > 0 {
length := min(shardLength, len(responseBody))
shard := responseBody[:length]
responseBody = responseBody[length:]
err := auditLogCtx.postAuditLog.Log(ctx, AuditlogMessage{
CorrelationIDHeaders: auditLogCtx.correlationHeaders,
Request: "",
Response: transactionId + ": " + string(shard),
Claims: auditLogCtx.claims,
})
if nil != err {
return errors.Wrapf(err, "while sending post-change auditlog message to auditlog service for %s", transactionId)
}
}
return nil
}
func calculateShardLength(ctx context.Context, auditLogCtx *auditLogContext, bodyBytes []byte, transactionId string) int {
shards := len(bodyBytes) / auditLogCtx.cfg.MsgBodySizeLimit
if len(bodyBytes)%auditLogCtx.cfg.MsgBodySizeLimit != 0 {
shards += 1
}
// Compute the maximum size of each auditlog message
shardLength := min(auditLogCtx.cfg.MsgBodySizeLimit, len(bodyBytes))
if shards > 1 {
shardLength = (len(bodyBytes) + 1) / shards
}
log.C(ctx).Infof("The data for %s will be split to %d shards of approximately %d bytes", transactionId, shards, shardLength)
return shardLength
}
type AdapterTokenClaims struct {
ExtraAttributes map[string]interface{} `json:"ext_attr"`
Scopes []string `json:"scope"`
ConsumerID string `json:"client_id"`
jwt.StandardClaims
}
func (t *AdapterTransport) getClaims(headers http.Header) (Claims, error) {
tokenClaims := AdapterTokenClaims{}
token := headers.Get("Authorization")
if token == "" {
return Claims{}, errors.New("no bearer token")
}
token = strings.TrimPrefix(token, "Bearer ")
parser := jwt.Parser{SkipClaimsValidation: true}
_, _, err := parser.ParseUnverified(token, &tokenClaims)
if err != nil {
return Claims{}, errors.Wrap(err, "while parsing bearer token")
}
claims := Claims{
Tenant: tokenClaims.ExtraAttributes["subaccountid"].(string),
Scopes: strings.Join(tokenClaims.Scopes, ", "),
ConsumerID: tokenClaims.ConsumerID,
}
return claims, nil
}
func min(a int, b int) int {
if a <= b {
return a
}
return b
}