Skip to content

Commit 80db12e

Browse files
add user basic authentication (#332)
* add user basic authentication * add test which expects unauthorised on basic auth * Consolidate tests and config * Add basic auth to cli --------- Co-authored-by: David Farr <[email protected]>
1 parent d5a4a3d commit 80db12e

File tree

15 files changed

+1092
-950
lines changed

15 files changed

+1092
-950
lines changed

cmd/promises/complete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func CompletePromiseCmds(c client.ResonateClient) []*cobra.Command {
8484
} else if resp.StatusCode() == 200 {
8585
cmd.Printf("%s promise: %s (deduplicated)\n", state.PastT, id)
8686
} else {
87-
cmd.PrintErrln(string(resp.Body))
87+
cmd.PrintErrln(resp.Status(), string(resp.Body))
8888
}
8989
},
9090
}
File renamed without changes.

cmd/promises/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func CreatePromiseCmd(c client.ResonateClient) *cobra.Command {
7777
} else if resp.StatusCode() == 200 {
7878
cmd.Printf("Created promise: %s (deduplicated)\n", id)
7979
} else {
80-
cmd.PrintErrln(string(resp.Body))
80+
cmd.PrintErrln(resp.Status(), string(resp.Body))
8181
}
8282
},
8383
}

cmd/promises/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func GetPromiseCmd(c client.ResonateClient) *cobra.Command {
3636
}
3737

3838
if resp.StatusCode() != 200 {
39-
cmd.PrintErrln(string(resp.Body))
39+
cmd.PrintErrln(resp.Status(), string(resp.Body))
4040
return
4141
}
4242

cmd/promises/promises.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ import (
1212
)
1313

1414
func NewCmd(c client.ResonateClient) *cobra.Command {
15+
var (
16+
username string
17+
password string
18+
)
19+
1520
cmd := &cobra.Command{
1621
Use: "promises",
1722
Aliases: []string{"promise"},
1823
Short: "Manage durable promises",
1924
Run: func(cmd *cobra.Command, args []string) {
2025
_ = cmd.Help()
2126
},
27+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
28+
// Set basic auth if provided
29+
if username != "" || password != "" {
30+
c.SetBasicAuth(username, password)
31+
}
32+
},
2233
}
2334

2435
// Add subcommands
@@ -27,6 +38,10 @@ func NewCmd(c client.ResonateClient) *cobra.Command {
2738
cmd.AddCommand(CreatePromiseCmd(c))
2839
cmd.AddCommand(CompletePromiseCmds(c)...)
2940

41+
// Flags
42+
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "Basic auth username")
43+
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "Basic auth password")
44+
3045
return cmd
3146
}
3247

cmd/promises/search.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func SearchPromisesCmd(c client.ResonateClient) *cobra.Command {
6969
}
7070

7171
if resp.StatusCode() != 200 {
72-
cmd.PrintErr(string(resp.Body))
72+
cmd.PrintErrln(resp.Status(), string(resp.Body))
7373
return
7474
}
7575

cmd/schedules/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func CreateScheduleCmd(c client.ResonateClient) *cobra.Command {
8282
} else if resp.StatusCode() == 200 {
8383
cmd.Printf("Created schedule: %s (deduplicated)\n", id)
8484
} else {
85-
cmd.PrintErrln(string(resp.Body))
85+
cmd.PrintErrln(resp.Status(), string(resp.Body))
8686
}
8787
},
8888
}

cmd/schedules/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func DeleteScheduleCmd(c client.ResonateClient) *cobra.Command {
3434
}
3535

3636
if resp.StatusCode() != 204 {
37-
cmd.PrintErrln(string(resp.Body))
37+
cmd.PrintErrln(resp.Status(), string(resp.Body))
3838
return
3939
}
4040

cmd/schedules/get.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func GetScheduleCmd(c client.ResonateClient) *cobra.Command {
3737
}
3838

3939
if resp.StatusCode() != 200 {
40-
cmd.PrintErrln(string(resp.Body))
40+
cmd.PrintErrln(resp.Status(), string(resp.Body))
4141
return
4242
}
4343

cmd/schedules/schedules.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,24 @@ import (
1212
)
1313

1414
func NewCmd(c client.ResonateClient) *cobra.Command {
15+
var (
16+
username string
17+
password string
18+
)
19+
1520
cmd := &cobra.Command{
1621
Use: "schedules",
1722
Aliases: []string{"schedule"},
1823
Short: "Manage durable schedules",
1924
Run: func(cmd *cobra.Command, args []string) {
2025
_ = cmd.Help()
2126
},
27+
PersistentPreRun: func(cmd *cobra.Command, args []string) {
28+
// Set basic auth if provided
29+
if username != "" || password != "" {
30+
c.SetBasicAuth(username, password)
31+
}
32+
},
2233
}
2334

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

41+
// Flags
42+
cmd.PersistentFlags().StringVarP(&username, "username", "U", "", "Basic auth username")
43+
cmd.PersistentFlags().StringVarP(&password, "password", "P", "", "Basic auth password")
44+
3045
return cmd
3146
}
3247

0 commit comments

Comments
 (0)