Skip to content

Commit 51ee4be

Browse files
committed
chore: wip suggestion for interceptions
1 parent 09aa7d4 commit 51ee4be

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package interceptor
2+
3+
import (
4+
"github.com/elazarl/goproxy"
5+
"net/http"
6+
)
7+
8+
type Interceptor interface {
9+
GetCondition() (goproxy.ReqCondition, error)
10+
GetHandler() func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response)
11+
}
12+
13+
func GetRegisteredInterceptors() []Interceptor {
14+
return []Interceptor{
15+
NewV1AnalyticsInterceptor(),
16+
}
17+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package interceptor
2+
3+
import (
4+
"compress/gzip"
5+
"fmt"
6+
"github.com/elazarl/goproxy"
7+
"io"
8+
"net/http"
9+
"os"
10+
"regexp"
11+
)
12+
13+
// v1AnalyticsInterceptor looks for requests to the (now deprecated) v1 analytics endpoint, and re-directs these
14+
// requests to the v2 analytics service. This is a temporary measure to allow users to migrate to the new service.
15+
type v1AnalyticsInterceptor struct {
16+
requestCondition goproxy.ReqCondition
17+
handler func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response)
18+
}
19+
20+
func v1AnalyticsHandlerFunc(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
21+
reader, err := gzip.NewReader(req.Body)
22+
if err != nil {
23+
fmt.Fprintf(os.Stderr, "gzip.NewReader: %v\n", err)
24+
}
25+
defer reader.Close()
26+
bodyBytes, bodyErr := io.ReadAll(reader)
27+
fmt.Println(string(bodyBytes), bodyErr)
28+
29+
return req, nil
30+
}
31+
32+
func (v v1AnalyticsInterceptor) GetCondition() (goproxy.ReqCondition, error) {
33+
if v.requestCondition == nil {
34+
return nil, fmt.Errorf("request condition is not set")
35+
}
36+
37+
return v.requestCondition, nil
38+
}
39+
40+
func (v v1AnalyticsInterceptor) GetHandler() func(req *http.Request, ctx *goproxy.ProxyCtx) (*http.Request, *http.Response) {
41+
return v.handler
42+
}
43+
44+
func NewV1AnalyticsInterceptor() Interceptor {
45+
i := v1AnalyticsInterceptor{goproxy.UrlMatches(regexp.MustCompile("^*/v1/analytics/cli")), v1AnalyticsHandlerFunc}
46+
return i
47+
}

cliv2/internal/proxy/proxy.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414

1515
"github.com/google/uuid"
1616
"github.com/rs/zerolog"
17+
"github.com/snyk/cli/cliv2/internal/proxy/interceptor"
1718
"github.com/snyk/go-application-framework/pkg/configuration"
1819
"github.com/snyk/go-application-framework/pkg/networking"
1920
"github.com/snyk/go-application-framework/pkg/networking/certs"
@@ -243,6 +244,15 @@ func (p *WrapperProxy) Start() error {
243244
// zerolog based logger also works but it will print empty lines between logs
244245
proxy.Logger = log.New(&pkg_utils.ToZeroLogDebug{Logger: p.DebugLogger}, "", 0)
245246
proxy.OnRequest().DoFunc(p.replaceVersionHandler)
247+
248+
for _, i := range interceptor.GetRegisteredInterceptors() {
249+
condition, err := i.GetCondition()
250+
if err != nil {
251+
return err
252+
}
253+
proxy.OnRequest(condition).DoFunc(i.GetHandler())
254+
}
255+
246256
proxy.OnRequest().HandleConnect(p)
247257
proxy.OnResponse().DoFunc(p.handleResponse)
248258
proxy.Verbose = true

0 commit comments

Comments
 (0)