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

Addition of basic Auth functionality as mentioned in issue Basic Auth #330 #333

Closed
wants to merge 7 commits into from
Closed
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
28 changes: 27 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"github.com/resonatehq/resonate/internal/creds"
"log/slog"
"os"
"strings"
Expand All @@ -16,7 +17,7 @@ import (
)

var (
server, cfgFile string
server, cfgFile, credFile string
)

var rootCmd = &cobra.Command{
Expand Down Expand Up @@ -48,7 +49,32 @@ func init() {
rootCmd.SetErr(os.Stderr)
}

func initCreds() {
if credFile != "" {
viper.SetConfigFile(credFile)
} else {
viper.SetConfigName("sample-creds")
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME")
}

err := viper.ReadInConfig()
if err != nil {
slog.Error("Error reading creds file", "error", err)
return
}

err = viper.Unmarshal(&creds.CredsFromFile)
if err != nil {
slog.Error("Unable to decode creds from file into struct", "error", err)
return
}
}

func initConfig() {
initCreds()
viper.Reset()

if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
Expand Down
8 changes: 8 additions & 0 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package serve

import (
"fmt"
"github.com/resonatehq/resonate/internal/creds"
"log/slog"
netHttp "net/http"
"os"
Expand Down Expand Up @@ -56,6 +57,13 @@ func ServeCmd() *cobra.Command {
api := api.New(config.API.Size, metrics)
aio := aio.New(config.AIO.Size, metrics)

creds, err := creds.GetCredentials()
if err != nil {
slog.Error("failed to get credentials.", "error", err)
return err
}
config.API.Subsystems.Http.Auth = creds

// instantiate api subsystems
http := http.New(api, config.API.Subsystems.Http)
grpc := grpc.New(api, config.API.Subsystems.Grpc)
Expand Down
39 changes: 25 additions & 14 deletions internal/app/subsystems/api/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package http

import (
"context"
"github.com/resonatehq/resonate/internal/creds"
"net/http"
"time"

"github.com/go-playground/validator/v10"
"github.com/resonatehq/resonate/internal/app/subsystems/api/service"

"log/slog"

"github.com/gin-gonic/gin"
Expand All @@ -18,13 +18,22 @@ import (
type Config struct {
Addr string
Timeout time.Duration
Auth creds.CredentialsList
}

type Http struct {
config *Config
server *http.Server
}

func BasicAuthMiddleware(config *Config) gin.HandlerFunc {
credentials := creds.GetProcessedCreds(config.Auth)
if credentials != nil {
return gin.BasicAuth(credentials)
}
return nil
}

func New(api api.API, config *Config) api.Subsystem {
gin.SetMode(gin.ReleaseMode)

Expand All @@ -38,27 +47,29 @@ func New(api api.API, config *Config) api.Subsystem {

// Middleware
r.Use(s.log)
authorized := r.Group("/")
authorized.Use(BasicAuthMiddleware(config))

// Promises API
r.POST("/promises", s.createPromise)
r.GET("/promises", s.searchPromises)
r.GET("/promises/*id", s.readPromise)
r.PATCH("/promises/*id", s.completePromise)
authorized.POST("/promises", s.createPromise)
authorized.GET("/promises", s.searchPromises)
authorized.GET("/promises/*id", s.readPromise)
authorized.PATCH("/promises/*id", s.completePromise)

// Schedules API
r.POST("/schedules", s.createSchedule)
r.GET("/schedules", s.searchSchedules)
r.GET("/schedules/*id", s.readSchedule)
r.DELETE("/schedules/*id", s.deleteSchedule)
authorized.POST("/schedules", s.createSchedule)
authorized.GET("/schedules", s.searchSchedules)
authorized.GET("/schedules/*id", s.readSchedule)
authorized.DELETE("/schedules/*id", s.deleteSchedule)

// Distributed Locks API
r.POST("/locks/acquire", s.acquireLock)
r.POST("/locks/heartbeat", s.heartbeatLocks)
r.POST("/locks/release", s.releaseLock)
authorized.POST("/locks/acquire", s.acquireLock)
authorized.POST("/locks/heartbeat", s.heartbeatLocks)
authorized.POST("/locks/release", s.releaseLock)

// Task API
r.POST("/tasks/claim", s.claimTask)
r.POST("/tasks/complete", s.completeTask)
authorized.POST("/tasks/claim", s.claimTask)
authorized.POST("/tasks/complete", s.completeTask)

return &Http{
config: config,
Expand Down
Loading