Skip to content

Commit

Permalink
all: implement --fake-tls-termination flag (#493)
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored Jun 5, 2017
1 parent 48c229b commit 79580e1
Show file tree
Hide file tree
Showing 20 changed files with 236 additions and 154 deletions.
7 changes: 7 additions & 0 deletions client/manager_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@ type HTTPManager struct {
Client *http.Client
Endpoint *url.URL
Dry bool
FakeTLSTermination bool
}

func (m *HTTPManager) GetConcreteClient(id string) (*Client, error) {
var c Client
var r = pkg.NewSuperAgent(pkg.JoinURL(m.Endpoint, id).String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination

if err := r.Get(&c); err != nil {
return nil, errors.WithStack(err)
}
Expand All @@ -37,20 +40,23 @@ func (m *HTTPManager) UpdateClient(c *Client) error {
var r = pkg.NewSuperAgent(pkg.JoinURL(m.Endpoint, c.ID).String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
return r.Update(c)
}

func (m *HTTPManager) CreateClient(c *Client) error {
var r = pkg.NewSuperAgent(m.Endpoint.String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
return r.Create(c)
}

func (m *HTTPManager) DeleteClient(id string) error {
var r = pkg.NewSuperAgent(pkg.JoinURL(m.Endpoint, id).String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
return r.Delete()
}

Expand All @@ -59,6 +65,7 @@ func (m *HTTPManager) GetClients() (map[string]Client, error) {
var r = pkg.NewSuperAgent(m.Endpoint.String())
r.Client = m.Client
r.Dry = m.Dry
r.FakeTLSTermination = m.FakeTLSTermination
if err := r.Get(&cs); err != nil {
return nil, errors.WithStack(err)
}
Expand Down
40 changes: 23 additions & 17 deletions cmd/cli/handler_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,29 @@ import (

type ClientHandler struct {
Config *config.Config
M *client.HTTPManager
}

func newClientHandler(c *config.Config) *ClientHandler {
return &ClientHandler{
Config: c,
M: &client.HTTPManager{},
}
}

func (h *ClientHandler) newClientManager(cmd *cobra.Command) *client.HTTPManager {
dry, _ := cmd.Flags().GetBool("dry")
term, _ := cmd.Flags().GetBool("fake-tls-termination")

return &client.HTTPManager{
Dry: dry,
Endpoint: h.Config.Resolve("/clients"),
Client: h.Config.OAuth2Client(cmd),
FakeTLSTermination: term,
}
}

func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {
h.M.Endpoint = h.Config.Resolve("/clients")
h.M.Client = h.Config.OAuth2Client(cmd)
m := h.newClientManager(cmd)

if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
Expand All @@ -39,8 +49,8 @@ func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {
err = json.NewDecoder(reader).Decode(&c)
pkg.Must(err, "Could not parse JSON: %s", err)

err = h.M.CreateClient(&c)
if h.M.Dry {
err = m.CreateClient(&c)
if m.Dry {
fmt.Printf("%s\n", err)
continue
}
Expand All @@ -51,11 +61,7 @@ func (h *ClientHandler) ImportClients(cmd *cobra.Command, args []string) {

func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
var err error

h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/clients")
h.M.Client = h.Config.OAuth2Client(cmd)

m := h.newClientManager(cmd)
responseTypes, _ := cmd.Flags().GetStringSlice("response-types")
grantTypes, _ := cmd.Flags().GetStringSlice("grant-types")
allowedScopes, _ := cmd.Flags().GetStringSlice("allowed-scopes")
Expand Down Expand Up @@ -84,8 +90,8 @@ func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
Name: name,
Public: public,
}
err = h.M.CreateClient(cc)
if h.M.Dry {
err = m.CreateClient(cc)
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -96,16 +102,16 @@ func (h *ClientHandler) CreateClient(cmd *cobra.Command, args []string) {
}

func (h *ClientHandler) DeleteClient(cmd *cobra.Command, args []string) {
h.M.Endpoint = h.Config.Resolve("/clients")
h.M.Client = h.Config.OAuth2Client(cmd)
m := h.newClientManager(cmd)

if len(args) == 0 {
fmt.Print(cmd.UsageString())
return
}

for _, c := range args {
err := h.M.DeleteClient(c)
if h.M.Dry {
err := m.DeleteClient(c)
if m.Dry {
fmt.Printf("%s\n", err)
continue
}
Expand Down
60 changes: 27 additions & 33 deletions cmd/cli/handler_groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,23 @@ import (

type GroupHandler struct {
Config *config.Config
M *group.HTTPManager
}

func (h *GroupHandler) newGroupManager(cmd *cobra.Command) *group.HTTPManager {
dry, _ := cmd.Flags().GetBool("dry")
term, _ := cmd.Flags().GetBool("fake-tls-termination")

return &group.HTTPManager{
Dry: dry,
Endpoint: h.Config.Resolve("/warden/groups"),
Client: h.Config.OAuth2Client(cmd),
FakeTLSTermination: term,
}
}

func newGroupHandler(c *config.Config) *GroupHandler {
return &GroupHandler{
Config: c,
M: &group.HTTPManager{},
}
}

Expand All @@ -26,15 +36,12 @@ func (h *GroupHandler) CreateGroup(cmd *cobra.Command, args []string) {
fmt.Print(cmd.UsageString())
return
}
m := h.newGroupManager(cmd)

var err error
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/warden/groups")
h.M.Client = h.Config.OAuth2Client(cmd)

cc := &group.Group{ID: args[0]}
err = h.M.CreateGroup(cc)
if h.M.Dry {
err = m.CreateGroup(cc)
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -48,14 +55,11 @@ func (h *GroupHandler) DeleteGroup(cmd *cobra.Command, args []string) {
fmt.Print(cmd.UsageString())
return
}
m := h.newGroupManager(cmd)

var err error
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/warden/groups")
h.M.Client = h.Config.OAuth2Client(cmd)

err = h.M.DeleteGroup(args[0])
if h.M.Dry {
err = m.DeleteGroup(args[0])
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -69,14 +73,11 @@ func (h *GroupHandler) AddMembers(cmd *cobra.Command, args []string) {
fmt.Print(cmd.UsageString())
return
}
m := h.newGroupManager(cmd)

var err error
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/warden/groups")
h.M.Client = h.Config.OAuth2Client(cmd)

err = h.M.AddGroupMembers(args[0], args[1:])
if h.M.Dry {
err = m.AddGroupMembers(args[0], args[1:])
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -90,14 +91,11 @@ func (h *GroupHandler) RemoveMembers(cmd *cobra.Command, args []string) {
fmt.Print(cmd.UsageString())
return
}
m := h.newGroupManager(cmd)

var err error
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/warden/groups")
h.M.Client = h.Config.OAuth2Client(cmd)

err = h.M.RemoveGroupMembers(args[0], args[1:])
if h.M.Dry {
err = m.RemoveGroupMembers(args[0], args[1:])
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -111,13 +109,9 @@ func (h *GroupHandler) FindGroups(cmd *cobra.Command, args []string) {
fmt.Print(cmd.UsageString())
return
}

h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/warden/groups")
h.M.Client = h.Config.OAuth2Client(cmd)

gn, err := h.M.FindGroupNames(args[0])
if h.M.Dry {
m := h.newGroupManager(cmd)
gn, err := m.FindGroupNames(args[0])
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand Down
38 changes: 21 additions & 17 deletions cmd/cli/handler_jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,36 @@ import (

type JWKHandler struct {
Config *config.Config
M *jwk.HTTPManager
}

func (h *JWKHandler) newJwkManager(cmd *cobra.Command) *jwk.HTTPManager {
dry, _ := cmd.Flags().GetBool("dry")
term, _ := cmd.Flags().GetBool("fake-tls-termination")

return &jwk.HTTPManager{
Dry: dry,
Endpoint: h.Config.Resolve("/keys"),
Client: h.Config.OAuth2Client(cmd),
FakeTLSTermination: term,
}
}

func newJWKHandler(c *config.Config) *JWKHandler {
return &JWKHandler{
Config: c,
M: &jwk.HTTPManager{},
}
}

func (h *JWKHandler) CreateKeys(cmd *cobra.Command, args []string) {
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/keys")
h.M.Client = h.Config.OAuth2Client(cmd)
m := h.newJwkManager(cmd)
if len(args) == 0 {
fmt.Println(cmd.UsageString())
return
}

alg, _ := cmd.Flags().GetString("alg")
keys, err := h.M.CreateKeys(args[0], alg)
if h.M.Dry {
keys, err := m.CreateKeys(args[0], alg)
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -46,16 +54,14 @@ func (h *JWKHandler) CreateKeys(cmd *cobra.Command, args []string) {
}

func (h *JWKHandler) GetKeys(cmd *cobra.Command, args []string) {
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/keys")
h.M.Client = h.Config.OAuth2Client(cmd)
m := h.newJwkManager(cmd)
if len(args) == 0 {
fmt.Println(cmd.UsageString())
return
}

keys, err := h.M.GetKeySet(args[0])
if h.M.Dry {
keys, err := m.GetKeySet(args[0])
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand All @@ -68,16 +74,14 @@ func (h *JWKHandler) GetKeys(cmd *cobra.Command, args []string) {
}

func (h *JWKHandler) DeleteKeys(cmd *cobra.Command, args []string) {
h.M.Dry, _ = cmd.Flags().GetBool("dry")
h.M.Endpoint = h.Config.Resolve("/keys")
h.M.Client = h.Config.OAuth2Client(cmd)
m := h.newJwkManager(cmd)
if len(args) == 0 {
fmt.Println(cmd.UsageString())
return
}

err := h.M.DeleteKeySet(args[0])
if h.M.Dry {
err := m.DeleteKeySet(args[0])
if m.Dry {
fmt.Printf("%s\n", err)
return
}
Expand Down
Loading

0 comments on commit 79580e1

Please sign in to comment.