Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/serve/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

const (
flagAddr = "addr"
flagTTL = "ttl"
flagGHToken = "github-token"

flagTraceServiceURL = "trace-service-url"
Expand Down Expand Up @@ -36,6 +37,11 @@ func Command() *cli.Command {
Usage: "Addr to listen on.",
EnvVars: []string{strcase.ToSNAKE(flagAddr)},
},
&cli.StringFlag{
Name: flagTTL,
Usage: "Control TTL of download responses.",
EnvVars: []string{strcase.ToSNAKE(flagTTL)},
},
&cli.StringFlag{
Name: flagGHToken,
Usage: "GitHub Token",
Expand Down Expand Up @@ -79,6 +85,7 @@ func buildConfig(cliCtx *cli.Context) Config {
Username: cliCtx.String(flagGoProxyUsername),
Password: cliCtx.String(flagGoProxyPassword),
},
TTL: cliCtx.Duration(flagTTL),
}
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/serve/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package serve

import (
"time"

"github.com/traefik/plugin-service/pkg/db/mongodb"
"github.com/traefik/plugin-service/pkg/tracer"
)
Expand All @@ -15,6 +17,8 @@ type Config struct {
MongoDB mongodb.Config
Tracing tracer.Config
GoProxy GoProxy

TTL time.Duration
}

// GoProxy holds the go-proxy configuration.
Expand Down
38 changes: 5 additions & 33 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"fmt"
"net/http"

"github.com/google/go-github/v57/github"
"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
"github.com/ldez/grignotin/goproxy"
"github.com/traefik/hub-trace-kpi/trace"
"github.com/traefik/plugin-service/cmd/internal"
"github.com/traefik/plugin-service/pkg/handlers"
Expand All @@ -17,7 +15,6 @@ import (
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"golang.org/x/oauth2"
)

func run(ctx context.Context, cfg Config) error {
Expand All @@ -37,17 +34,18 @@ func run(ctx context.Context, cfg Config) error {
return fmt.Errorf("unable to bootstrap database: %w", err)
}

gpClient, err := newGoProxyClient(cfg.GoProxy)
var gpClient handlers.GoproxyPluginClient
gpClient, err = handlers.NewGoproxyClient(cfg.GoProxy.URL, cfg.GoProxy.Username, cfg.GoProxy.Password)
if err != nil {
return fmt.Errorf("unable to create go proxy client: %w", err)
}

var ghClient *github.Client
var ghClient handlers.GithubPluginClient
if cfg.GitHubToken != "" {
ghClient = newGitHubClient(context.Background(), cfg.GitHubToken)
ghClient = handlers.NewGithubClient(context.Background(), cfg.GitHubToken)
}

handler := handlers.New(store, gpClient, ghClient)
handler := handlers.New(store, gpClient, ghClient, cfg.TTL)

healthChecker := healthcheck.Client{DB: store}

Expand Down Expand Up @@ -101,32 +99,6 @@ func buildExternalRouter(handler handlers.Handlers) http.Handler {
return http.StripPrefix("/external", r)
}

func newGoProxyClient(cfg GoProxy) (*goproxy.Client, error) {
gpClient := goproxy.NewClient(cfg.URL)

if cfg.URL != "" && cfg.Username != "" && cfg.Password != "" {
tr, err := goproxy.NewBasicAuthTransport(cfg.Username, cfg.Password)
if err != nil {
return nil, err
}

gpClient.HTTPClient = tr.Client()
}

return gpClient, nil
}

func newGitHubClient(ctx context.Context, tk string) *github.Client {
if tk == "" {
return github.NewClient(nil)
}

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: tk},
)
return github.NewClient(oauth2.NewClient(ctx, ts))
}

func setupTracing(ctx context.Context, cfg tracer.Config, traceServiceURL string) (func(), error) {
tracePropagator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{})
traceProvider, err := tracer.NewOTLPProvider(ctx, cfg)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0
go.opentelemetry.io/otel/sdk v1.27.0
go.opentelemetry.io/otel/trace v1.27.0
golang.org/x/mod v0.18.0
golang.org/x/oauth2 v0.21.0
)

Expand All @@ -39,6 +40,7 @@ require (
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/objx v0.5.2 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
Expand All @@ -47,7 +49,6 @@ require (
go.opentelemetry.io/otel/metric v1.27.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/crypto v0.24.0 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/net v0.26.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/traefik/hub-trace-kpi v0.13.0 h1:2iWUgt77TVBW0XRm02IxNtlathBNO1gtCgTGESUn0SY=
Expand Down
61 changes: 0 additions & 61 deletions pkg/handlers/dbmock_test.go

This file was deleted.

45 changes: 45 additions & 0 deletions pkg/handlers/github.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package handlers

import (
"context"
"net/http"
"net/url"

"github.com/google/go-github/v57/github"
"golang.org/x/oauth2"
)

type GithubClient struct {
client *github.Client
}

type GithubPluginClient interface {
Do(ctx context.Context, req *http.Request, v interface{}) (*github.Response, error)
GetArchiveLink(ctx context.Context, owner, repo string, archiveformat github.ArchiveFormat, opts *github.RepositoryContentGetOptions, maxRedirects int) (*url.URL, *github.Response, error)
GetReleaseByTag(ctx context.Context, owner, repo, tag string) (*github.RepositoryRelease, *github.Response, error)
}

// NewGithubClient create a new GitHub client.
func NewGithubClient(ctx context.Context, token string) *GithubClient {
if token == "" {
return &GithubClient{github.NewClient(nil)}
}

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)

return &GithubClient{github.NewClient(oauth2.NewClient(ctx, ts))}
}

func (c GithubClient) Do(ctx context.Context, req *http.Request, v interface{}) (*github.Response, error) {
return c.client.Do(ctx, req, v)
}

func (c GithubClient) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat github.ArchiveFormat, opts *github.RepositoryContentGetOptions, maxRedirects int) (*url.URL, *github.Response, error) {
return c.client.Repositories.GetArchiveLink(ctx, owner, repo, archiveformat, opts, maxRedirects)
}

func (c GithubClient) GetReleaseByTag(ctx context.Context, owner, repo, tag string) (*github.RepositoryRelease, *github.Response, error) {
return c.client.Repositories.GetReleaseByTag(ctx, owner, repo, tag)
}
41 changes: 41 additions & 0 deletions pkg/handlers/goproxy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package handlers

import (
"io"

"github.com/ldez/grignotin/goproxy"
"golang.org/x/mod/modfile"
)

type GoproxyClient struct {
client *goproxy.Client
}

type GoproxyPluginClient interface {
DownloadSources(moduleName, version string) (io.ReadCloser, error)
GetModFile(moduleName, version string) (*modfile.File, error)
}

// NewGoproxyClient creates a new Goproxy client.
func NewGoproxyClient(url, username, password string) (*GoproxyClient, error) {
gpClient := goproxy.NewClient(url)

if url != "" && username != "" && password != "" {
tr, err := goproxy.NewBasicAuthTransport(username, password)
if err != nil {
return nil, err
}

gpClient.HTTPClient = tr.Client()
}

return &GoproxyClient{client: gpClient}, nil
}

func (c GoproxyClient) DownloadSources(moduleName, version string) (io.ReadCloser, error) {
return c.client.DownloadSources(moduleName, version)
}

func (c GoproxyClient) GetModFile(moduleName, version string) (*modfile.File, error) {
return c.client.GetModFile(moduleName, version)
}
11 changes: 6 additions & 5 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ import (
"net/url"
"regexp"
"strconv"
"time"

"github.com/google/go-github/v57/github"
"github.com/ldez/grignotin/goproxy"
"github.com/rs/zerolog/log"
"github.com/traefik/plugin-service/pkg/db"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -40,18 +39,20 @@ type PluginStorer interface {
// Handlers a set of handlers.
type Handlers struct {
store PluginStorer
goProxy *goproxy.Client
gh *github.Client
goProxy GoproxyPluginClient
gh GithubPluginClient
tracer trace.Tracer
ttl int
}

// New creates all HTTP handlers.
func New(store PluginStorer, goProxy *goproxy.Client, gh *github.Client) Handlers {
func New(store PluginStorer, goProxy GoproxyPluginClient, gh GithubPluginClient, ttl time.Duration) Handlers {
return Handlers{
store: store,
goProxy: goProxy,
gh: gh,
tracer: otel.GetTracerProvider().Tracer("handler"),
ttl: int(ttl.Seconds()),
}
}

Expand Down
Loading