Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: protect HTTP API using access token #328

Merged
merged 4 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions cmd/lassie/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ var daemonFlags = []cli.Flag{
FlagBitswapConcurrency,
FlagGlobalTimeout,
FlagProviderTimeout,
&cli.StringFlag{
Name: "access-token",
Usage: "require HTTP clients to authorize using Bearer scheme and the configured access token",
bajtos marked this conversation as resolved.
Show resolved Hide resolved
Value: "",
},
}

var daemonCmd = &cli.Command{
Expand Down Expand Up @@ -119,7 +124,8 @@ func daemonAction(cctx *cli.Context) error {
port := cctx.Uint("port")
tempDir := cctx.String("tempdir")
maxBlocks := cctx.Uint64("maxblocks")
httpServerCfg := getHttpServerConfigForDaemon(address, port, tempDir, maxBlocks)
accessToken := cctx.String("access-token")
httpServerCfg := getHttpServerConfigForDaemon(address, port, tempDir, maxBlocks, accessToken)

// event recorder config
eventRecorderURL := cctx.String("event-recorder-url")
Expand Down Expand Up @@ -200,11 +206,12 @@ func defaultDaemonRun(
}

// getHttpServerConfigForDaemon returns a HttpServerConfig for the daemon command.
func getHttpServerConfigForDaemon(address string, port uint, tempDir string, maxBlocks uint64) httpserver.HttpServerConfig {
func getHttpServerConfigForDaemon(address string, port uint, tempDir string, maxBlocks uint64, accessToken string) httpserver.HttpServerConfig {
return httpserver.HttpServerConfig{
Address: address,
Port: port,
TempDir: tempDir,
MaxBlocksPerRequest: maxBlocks,
AccessToken: accessToken,
}
}
20 changes: 20 additions & 0 deletions pkg/server/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type HttpServerConfig struct {
Port uint
TempDir string
MaxBlocksPerRequest uint64
AccessToken string
}

type contextKey struct {
Expand All @@ -52,6 +53,11 @@ func NewHttpServer(ctx context.Context, lassie *lassie.Lassie, cfg HttpServerCon
// create server
mux := http.NewServeMux()
handler := servertiming.Middleware(mux, nil)

if cfg.AccessToken != "" {
handler = authorizationMiddleware(handler, cfg.AccessToken)
}

server := &http.Server{
Addr: fmt.Sprintf(":%d", cfg.Port),
BaseContext: func(listener net.Listener) context.Context { return ctx },
Expand Down Expand Up @@ -102,3 +108,17 @@ func (s *HttpServer) Close() error {
s.cancel()
return s.server.Shutdown(context.Background())
}

func authorizationMiddleware(next http.Handler, accessToken string) http.Handler {
requiredHeaderValue := "Bearer " + accessToken
bajtos marked this conversation as resolved.
Show resolved Hide resolved
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") == requiredHeaderValue {
next.ServeHTTP(w, r)
return
}

// Unauthorized
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintln(w, "Unauthorized")
})
}