Skip to content

Commit

Permalink
WIP: Add remote report authorization
Browse files Browse the repository at this point in the history
  • Loading branch information
csweichel committed Jul 1, 2023
1 parent 3e6f8c7 commit 999a2a3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion cmd/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ func getBuildOpts(cmd *cobra.Command) ([]leeway.BuildOption, *leeway.FilesystemC
if ep, err := cmd.Flags().GetString("remote-report"); err != nil {
log.Fatal(err)
} else if ep != "" {
reporter = append(reporter, remotereporter.NewReporter(ep))
reporter = append(reporter, remotereporter.NewReporter(ep, os.Getenv("LEEWAY_REMOTE_REPORT_TOKEN")))
}

dontTest, err := cmd.Flags().GetBool("dont-test")
Expand Down
11 changes: 9 additions & 2 deletions pkg/remotereporter/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@ import (
"github.com/sirupsen/logrus"
)

func NewReporter(endpoint string) *Reporter {
func NewReporter(endpoint, token string) *Reporter {
id, err := uuid.NewRandom()
if err != nil {
panic(fmt.Sprintf("cannot create remote reporting sesison UUID: %v.\nTry running without --remote-report", err))
}

httpclient := &http.Client{Timeout: 2 * time.Second}
client := v1connect.NewReporterServiceClient(httpclient, endpoint)
client := v1connect.NewReporterServiceClient(httpclient, endpoint, connect_go.WithInterceptors(connect_go.UnaryInterceptorFunc(func(uf connect_go.UnaryFunc) connect_go.UnaryFunc {
return func(ctx context.Context, req connect_go.AnyRequest) (connect_go.AnyResponse, error) {
if token != "" {
req.Header().Set("Authorization", token)
}
return uf(ctx, req)
}
})))
return &Reporter{
sessionID: id.String(),
times: make(map[string]time.Time),
Expand Down
12 changes: 11 additions & 1 deletion tracker/backend/ingestor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
Expand All @@ -13,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
"github.com/bufbuild/connect-go"
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
segment "github.com/segmentio/analytics-go/v3"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -64,7 +66,15 @@ func main() {
default:
logrus.Fatalf("unsupported --sample-sink: %s", *sink)
}
mux.Handle(v1connect.NewReporterServiceHandler(handler.NewBuildReportHandler(store)))
mux.Handle(v1connect.NewReporterServiceHandler(handler.NewBuildReportHandler(store), connect.WithInterceptors(connect.UnaryInterceptorFunc(func(uf connect.UnaryFunc) connect.UnaryFunc {
return func(ctx context.Context, req connect.AnyRequest) (connect.AnyResponse, error) {
tkn := req.Header().Get("Authorization")
if tkn == "" {
return nil, connect.NewError(connect.CodePermissionDenied, fmt.Errorf("no token present"))
}
return uf(ctx, req)
}
}))))

reflector := grpcreflect.NewStaticReflector(
v1connect.ReporterServiceName,
Expand Down

0 comments on commit 999a2a3

Please sign in to comment.