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

add user basic authentication #332

Merged
merged 6 commits into from
May 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion cmd/promises/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
} else if resp.StatusCode() == 200 {
cmd.Printf("%s promise: %s (deduplicated)\n", state.PastT, id)
} else {
cmd.PrintErrln(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))

Check warning on line 87 in cmd/promises/complete.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/complete.go#L87

Added line #L87 was not covered by tests
}
},
}
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion cmd/promises/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
} else if resp.StatusCode() == 200 {
cmd.Printf("Created promise: %s (deduplicated)\n", id)
} else {
cmd.PrintErrln(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))

Check warning on line 80 in cmd/promises/create.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/create.go#L80

Added line #L80 was not covered by tests
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/promises/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
}

if resp.StatusCode() != 200 {
cmd.PrintErrln(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))

Check warning on line 39 in cmd/promises/get.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/get.go#L39

Added line #L39 was not covered by tests
return
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/promises/promises.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@
)

func NewCmd(c client.ResonateClient) *cobra.Command {
var (
username string
password string
)

Check warning on line 18 in cmd/promises/promises.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/promises.go#L15-L18

Added lines #L15 - L18 were not covered by tests

cmd := &cobra.Command{
Use: "promises",
Aliases: []string{"promise"},
Short: "Manage durable promises",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {

Check warning on line 27 in cmd/promises/promises.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/promises.go#L27

Added line #L27 was not covered by tests
// Set basic auth if provided
if username != "" || password != "" {
c.SetBasicAuth(username, password)

Check warning on line 30 in cmd/promises/promises.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/promises.go#L29-L30

Added lines #L29 - L30 were not covered by tests
}
},
}

// Add subcommands
Expand All @@ -27,6 +38,10 @@
cmd.AddCommand(CreatePromiseCmd(c))
cmd.AddCommand(CompletePromiseCmds(c)...)

// Flags
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "Basic auth username")
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "Basic auth password")

Check warning on line 43 in cmd/promises/promises.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/promises.go#L42-L43

Added lines #L42 - L43 were not covered by tests

return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/promises/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
}

if resp.StatusCode() != 200 {
cmd.PrintErr(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))

Check warning on line 72 in cmd/promises/search.go

View check run for this annotation

Codecov / codecov/patch

cmd/promises/search.go#L72

Added line #L72 was not covered by tests
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/schedules/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func CreateScheduleCmd(c client.ResonateClient) *cobra.Command {
} else if resp.StatusCode() == 200 {
cmd.Printf("Created schedule: %s (deduplicated)\n", id)
} else {
cmd.PrintErrln(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))
}
},
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/schedules/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func DeleteScheduleCmd(c client.ResonateClient) *cobra.Command {
}

if resp.StatusCode() != 204 {
cmd.PrintErrln(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))
return
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/schedules/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func GetScheduleCmd(c client.ResonateClient) *cobra.Command {
}

if resp.StatusCode() != 200 {
cmd.PrintErrln(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))
return
}

Expand Down
15 changes: 15 additions & 0 deletions cmd/schedules/schedules.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,24 @@ import (
)

func NewCmd(c client.ResonateClient) *cobra.Command {
var (
username string
password string
)

cmd := &cobra.Command{
Use: "schedules",
Aliases: []string{"schedule"},
Short: "Manage durable schedules",
Run: func(cmd *cobra.Command, args []string) {
_ = cmd.Help()
},
PersistentPreRun: func(cmd *cobra.Command, args []string) {
// Set basic auth if provided
if username != "" || password != "" {
c.SetBasicAuth(username, password)
}
},
}

// Add subcommands
Expand All @@ -27,6 +38,10 @@ func NewCmd(c client.ResonateClient) *cobra.Command {
cmd.AddCommand(CreateScheduleCmd(c))
cmd.AddCommand(DeleteScheduleCmd(c))

// Flags
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "Basic auth username")
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "Basic auth password")

return cmd
}

Expand Down
2 changes: 1 addition & 1 deletion cmd/schedules/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func SearchSchedulesCmd(c client.ResonateClient) *cobra.Command {
}

if resp.StatusCode() != 200 {
cmd.PrintErr(string(resp.Body))
cmd.PrintErrln(resp.Status(), string(resp.Body))
return
}

Expand Down
13 changes: 9 additions & 4 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,22 +184,22 @@ func ServeCmd() *cobra.Command {
},
}

// assert
cmd.Flags().Bool("ignore-asserts", false, "ignore-asserts mode")
_ = viper.BindPFlag("ignore-asserts", cmd.Flags().Lookup("ignore-asserts"))

// api
cmd.Flags().Int("api-size", 100, "size of the submission queue buffered channel")
cmd.Flags().String("api-http-addr", "0.0.0.0:8001", "http server address")
cmd.Flags().Duration("api-http-timeout", 10*time.Second, "http server graceful shutdown timeout")
cmd.Flags().String("api-grpc-addr", "0.0.0.0:50051", "grpc server address")
cmd.Flags().String("api-base-url", "http://localhost:8001", "base url to automatically generate absolute URLs for the server's resources")
cmd.Flags().String("api-http-auth-username", "", "username for basic auth")
cmd.Flags().String("api-http-auth-password", "", "password for basic auth")

_ = viper.BindPFlag("api.size", cmd.Flags().Lookup("api-size"))
_ = viper.BindPFlag("api.subsystems.http.addr", cmd.Flags().Lookup("api-http-addr"))
_ = viper.BindPFlag("api.subsystems.http.timeout", cmd.Flags().Lookup("api-http-timeout"))
_ = viper.BindPFlag("api.subsystems.grpc.addr", cmd.Flags().Lookup("api-grpc-addr"))
_ = viper.BindPFlag("api.baseUrl", cmd.Flags().Lookup("api-base-url"))
_ = viper.BindPFlag("api.subsystems.http.auth.username", cmd.Flags().Lookup("api-http-auth-username"))
_ = viper.BindPFlag("api.subsystems.http.auth.password", cmd.Flags().Lookup("api-http-auth-password"))

// aio
// Store
Expand Down Expand Up @@ -272,10 +272,15 @@ func ServeCmd() *cobra.Command {
_ = viper.BindPFlag("system.submissionBatchSize", cmd.Flags().Lookup("system-submission-batch-size"))
_ = viper.BindPFlag("system.completionBatchSize", cmd.Flags().Lookup("system-completion-batch-size"))
_ = viper.BindPFlag("system.scheduleBatchSize", cmd.Flags().Lookup("system-schedule-batch-size"))

// metrics
cmd.Flags().Int("metrics-port", 9090, "prometheus metrics server port")
_ = viper.BindPFlag("metrics.port", cmd.Flags().Lookup("metrics-port"))

// assert
cmd.Flags().Bool("ignore-asserts", false, "ignore-asserts mode")
_ = viper.BindPFlag("ignore-asserts", cmd.Flags().Lookup("ignore-asserts"))

cmd.Flags().SortFlags = false

return cmd
Expand Down
46 changes: 33 additions & 13 deletions internal/app/subsystems/api/http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

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

"log/slog"

Expand All @@ -15,8 +16,14 @@ import (
"github.com/resonatehq/resonate/internal/api"
)

type Auth struct {
Username string
Password string
}

type Config struct {
Addr string
Auth *Auth
Timeout time.Duration
}

Expand All @@ -39,26 +46,39 @@ func New(api api.API, config *Config) api.Subsystem {
// Middleware
r.Use(s.log)

// Authentication
authorized := r.Group("/")
if config.Auth.Username != "" || config.Auth.Password != "" {
util.Assert(config.Auth.Username != "", "http basic auth username is required")
util.Assert(config.Auth.Password != "", "http basic auth password is required")

accounts := gin.Accounts{
config.Auth.Username: config.Auth.Password,
}
basicAuthMiddleware := gin.BasicAuth(accounts)
authorized.Use(basicAuthMiddleware)
}

// 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