Skip to content

Commit

Permalink
Merge pull request #1 from dfarr/feature/basic-auth
Browse files Browse the repository at this point in the history
Consolidate tests and add basic auth flags to cli
  • Loading branch information
susarlanikhilesh authored May 21, 2024
2 parents 6ac61cf + 8f97a61 commit 250c32a
Show file tree
Hide file tree
Showing 15 changed files with 1,059 additions and 2,646 deletions.
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 @@ func CompletePromiseCmds(c client.ResonateClient) []*cobra.Command {
} 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 @@ func CreatePromiseCmd(c client.ResonateClient) *cobra.Command {
} 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 @@ func GetPromiseCmd(c client.ResonateClient) *cobra.Command {
}

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 @@ import (
)

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 @@ func NewCmd(c client.ResonateClient) *cobra.Command {
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 @@ func SearchPromisesCmd(c client.ResonateClient) *cobra.Command {
}

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
26 changes: 9 additions & 17 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ func ServeCmd() *cobra.Command {
api := api.New(config.API.Size, metrics)
aio := aio.New(config.AIO.Size, metrics)

// authorization for http endpoint
config.API.Subsystems.Http.Auth = http.Auth{
Username: viper.GetString("auth-username"),
Password: viper.GetString("auth-password"),
}

// instantiate api subsystems
http := http.New(api, config.API.Subsystems.Http)
grpc := grpc.New(api, config.API.Subsystems.Grpc)
Expand Down Expand Up @@ -190,29 +184,22 @@ func ServeCmd() *cobra.Command {
},
}

// authentication
cmd.Flags().String("auth-username", "", "username for authorization")
cmd.Flags().String("auth-password", "", "password for authorization")

_ = viper.BindPFlag("auth-username", cmd.Flags().Lookup("auth-username"))
_ = viper.BindPFlag("auth-password", cmd.Flags().Lookup("auth-password"))

// 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 @@ -285,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
8 changes: 6 additions & 2 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 @@ -22,8 +23,8 @@ type Auth struct {

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

type Http struct {
Expand All @@ -47,7 +48,10 @@ func New(api api.API, config *Config) api.Subsystem {

// Authentication
authorized := r.Group("/")
if config.Auth.Username != "" && config.Auth.Password != "" {
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,
}
Expand Down
Loading

0 comments on commit 250c32a

Please sign in to comment.