Skip to content

Commit d747a59

Browse files
authored
Merge pull request #171 from ggriffiths/add_pxc_guestaccess_show
Add pxc auth guest-access commands
2 parents 218692b + fcd64df commit d747a59

File tree

8 files changed

+494
-1
lines changed

8 files changed

+494
-1
lines changed

handler/auth/auth.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
Copyright © 2020 Portworx
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package auth
17+
18+
import (
19+
"github.com/portworx/pxc/cmd"
20+
"github.com/portworx/pxc/pkg/commander"
21+
"github.com/portworx/pxc/pkg/util"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// authCmd represents the cluster command
26+
var authCmd *cobra.Command
27+
28+
var _ = commander.RegisterCommandVar(func() {
29+
authCmd = &cobra.Command{
30+
Use: "auth",
31+
Short: "Manage Portworx authentication",
32+
Run: func(cmd *cobra.Command, args []string) {
33+
util.Printf("Please see pxc auth --help for more commands\n")
34+
},
35+
}
36+
})
37+
38+
var _ = commander.RegisterCommandInit(func() {
39+
cmd.RootAddCommand(authCmd)
40+
})
41+
42+
// AuthAddCommand adds a command to the auth handler
43+
func AuthAddCommand(cmd *cobra.Command) {
44+
authCmd.AddCommand(cmd)
45+
}

handler/auth/guestaccess/disable.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright © 2020 Portworx
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package guestaccess
17+
18+
import (
19+
"github.com/portworx/pxc/pkg/cliops"
20+
"github.com/portworx/pxc/pkg/commander"
21+
"github.com/portworx/pxc/pkg/portworx"
22+
"github.com/portworx/pxc/pkg/util"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var disableGuestAccessCmd *cobra.Command
27+
28+
var _ = commander.RegisterCommandVar(func() {
29+
disableGuestAccessCmd = &cobra.Command{
30+
Use: "disable",
31+
Short: "Disable guest access role",
32+
RunE: disableGuestAccessExec,
33+
}
34+
})
35+
36+
var _ = commander.RegisterCommandInit(func() {
37+
GuestAccessAddCommand(disableGuestAccessCmd)
38+
})
39+
40+
func disableGuestAccessExec(cmd *cobra.Command, args []string) error {
41+
ctx, conn, err := portworx.PxConnectDefault()
42+
_ = ctx
43+
if err != nil {
44+
return err
45+
}
46+
defer conn.Close()
47+
// Parse out all of the common cli volume flags
48+
cai := cliops.GetCliAuthInputs(cmd, args)
49+
50+
// Create a cliVolumeOps object
51+
authOps := cliops.NewCliAuthOps(cai)
52+
53+
// initialize alertOP interface
54+
authOps.AuthOps = portworx.NewAuthOps()
55+
56+
err = authOps.AuthOps.UpdateRole(&portworx.RoleGuestDisabled)
57+
if err == nil {
58+
util.Printf("Guest access disabled successfully\n")
59+
}
60+
61+
return err
62+
}

handler/auth/guestaccess/enable.go

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright © 2020 Portworx
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package guestaccess
17+
18+
import (
19+
"github.com/portworx/pxc/pkg/cliops"
20+
"github.com/portworx/pxc/pkg/commander"
21+
"github.com/portworx/pxc/pkg/portworx"
22+
"github.com/portworx/pxc/pkg/util"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var enableGuestAccessCmd *cobra.Command
27+
28+
var _ = commander.RegisterCommandVar(func() {
29+
enableGuestAccessCmd = &cobra.Command{
30+
Use: "enable",
31+
Short: "enable guest access role",
32+
RunE: enableGuestAccessExec,
33+
}
34+
})
35+
36+
var _ = commander.RegisterCommandInit(func() {
37+
GuestAccessAddCommand(enableGuestAccessCmd)
38+
})
39+
40+
func enableGuestAccessExec(cmd *cobra.Command, args []string) error {
41+
ctx, conn, err := portworx.PxConnectDefault()
42+
_ = ctx
43+
if err != nil {
44+
return err
45+
}
46+
defer conn.Close()
47+
// Parse out all of the common cli volume flags
48+
cai := cliops.GetCliAuthInputs(cmd, args)
49+
50+
// Create a cliVolumeOps object
51+
authOps := cliops.NewCliAuthOps(cai)
52+
53+
// initialize alertOP interface
54+
authOps.AuthOps = portworx.NewAuthOps()
55+
56+
err = authOps.AuthOps.UpdateRole(&portworx.RoleGuestEnabled)
57+
if err == nil {
58+
util.Printf("Guest access enabled successfully\n")
59+
}
60+
61+
return err
62+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright © 2020 Portworx
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package guestaccess
17+
18+
import (
19+
"github.com/portworx/pxc/handler/auth"
20+
"github.com/portworx/pxc/pkg/commander"
21+
"github.com/portworx/pxc/pkg/util"
22+
"github.com/spf13/cobra"
23+
)
24+
25+
// guestAccessCmd represents the alert command
26+
var guestAccessCmd *cobra.Command
27+
28+
var _ = commander.RegisterCommandVar(func() {
29+
guestAccessCmd = &cobra.Command{
30+
Use: "guest-access",
31+
Aliases: []string{"ga"},
32+
Short: "Manage guest access on a Portworx cluster",
33+
Run: func(cmd *cobra.Command, args []string) {
34+
util.Printf("Please see pxc auth guest-access --help for more commands\n")
35+
},
36+
}
37+
})
38+
39+
var _ = commander.RegisterCommandInit(func() {
40+
auth.AuthAddCommand(guestAccessCmd)
41+
})
42+
43+
// GuestAccessAddCommand adds a guest access command
44+
func GuestAccessAddCommand(cmd *cobra.Command) {
45+
guestAccessCmd.AddCommand(cmd)
46+
}

handler/auth/guestaccess/show.go

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
Copyright © 2020 Portworx
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package guestaccess
17+
18+
import (
19+
"github.com/portworx/pxc/pkg/cliops"
20+
"github.com/portworx/pxc/pkg/commander"
21+
"github.com/portworx/pxc/pkg/portworx"
22+
"github.com/portworx/pxc/pkg/util"
23+
"github.com/spf13/cobra"
24+
)
25+
26+
var showGuestAccessCmd *cobra.Command
27+
28+
var _ = commander.RegisterCommandVar(func() {
29+
showGuestAccessCmd = &cobra.Command{
30+
Use: "show",
31+
Short: "show guest access role",
32+
RunE: showGuestAccessExec,
33+
}
34+
})
35+
36+
var _ = commander.RegisterCommandInit(func() {
37+
GuestAccessAddCommand(showGuestAccessCmd)
38+
39+
showGuestAccessCmd.Flags().StringP("output", "o", "", "Output in yaml|json|wide")
40+
})
41+
42+
func showGuestAccessExec(cmd *cobra.Command, args []string) error {
43+
ctx, conn, err := portworx.PxConnectDefault()
44+
_ = ctx
45+
if err != nil {
46+
return err
47+
}
48+
defer conn.Close()
49+
// Parse out all of the common cli volume flags
50+
cai := cliops.GetCliAuthInputs(cmd, args)
51+
52+
// Create a cliVolumeOps object
53+
authOps := cliops.NewCliAuthOps(cai)
54+
55+
// initialize alertOP interface
56+
authOps.AuthOps = portworx.NewAuthOps()
57+
58+
// Create the parser object
59+
authgf := NewGuestAccessShowFormatter(authOps)
60+
return util.PrintFormatted(authgf)
61+
}
62+
63+
type guestAccessShowFormatter struct {
64+
cliops.CliAuthOps
65+
}
66+
67+
// NewGuestAccessShowFormatter creates a new guest access formatter
68+
func NewGuestAccessShowFormatter(cvOps *cliops.CliAuthOps) *guestAccessShowFormatter {
69+
return &guestAccessShowFormatter{
70+
CliAuthOps: *cvOps,
71+
}
72+
}
73+
74+
// YamlFormat returns the yaml representation of the object
75+
func (f *guestAccessShowFormatter) YamlFormat() (string, error) {
76+
role, err := f.AuthOps.GetRole("system.guest")
77+
if err != nil {
78+
return "", err
79+
}
80+
return util.ToYaml(role)
81+
}
82+
83+
// JsonFormat returns the json representation of the object
84+
func (f *guestAccessShowFormatter) JsonFormat() (string, error) {
85+
role, err := f.AuthOps.GetRole("system.guest")
86+
if err != nil {
87+
return "", err
88+
}
89+
return util.ToJson(role)
90+
}
91+
92+
// WideFormat returns the wide string representation of the object
93+
func (f *guestAccessShowFormatter) WideFormat() (string, error) {
94+
f.Wide = true
95+
return f.DefaultFormat()
96+
}
97+
98+
// DefaultFormat returns the default string representation of the object
99+
func (f *guestAccessShowFormatter) DefaultFormat() (string, error) {
100+
role, err := f.AuthOps.GetRole("system.guest")
101+
if err != nil {
102+
return "", err
103+
}
104+
105+
if role.String() == portworx.RoleGuestDisabled.String() {
106+
util.Printf("Guest access disabled\n")
107+
108+
} else {
109+
util.Printf("Guest access enabled\n")
110+
}
111+
112+
return "", nil
113+
}

handler/handler.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright © 2019 Portworx
2+
Copyright © 2020 Portworx
33
44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.
@@ -16,7 +16,11 @@ limitations under the License.
1616
package handler
1717

1818
import (
19+
// import all handlers to register them
20+
_ "github.com/portworx/pxc/handler/auth"
21+
_ "github.com/portworx/pxc/handler/auth/guestaccess"
1922
_ "github.com/portworx/pxc/handler/cluster"
23+
_ "github.com/portworx/pxc/handler/cluster/alerts"
2024
_ "github.com/portworx/pxc/handler/config"
2125
_ "github.com/portworx/pxc/handler/login"
2226
_ "github.com/portworx/pxc/handler/node"

pkg/cliops/authops.go

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Copyright © 2020 Portworx
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cliops
17+
18+
import (
19+
"github.com/portworx/pxc/pkg/portworx"
20+
"github.com/portworx/pxc/pkg/util"
21+
"github.com/spf13/cobra"
22+
)
23+
24+
// CliAuthOps represents an interface for auth commands
25+
type CliAuthOps struct {
26+
portworx.CliAuthInputs
27+
AuthOps portworx.AuthOps
28+
}
29+
30+
// GetCliAuthInputs gets all CLI auth inputs
31+
func GetCliAuthInputs(cmd *cobra.Command, args []string) *portworx.CliAuthInputs {
32+
output, _ := cmd.Flags().GetString("output")
33+
return &portworx.CliAuthInputs{
34+
BaseFormatOutput: util.BaseFormatOutput{
35+
FormatType: output,
36+
},
37+
}
38+
}
39+
40+
// NewCliAuthOps creates a new cliAuthOps object
41+
func NewCliAuthOps(
42+
cvi *portworx.CliAuthInputs,
43+
) *CliAuthOps {
44+
return &CliAuthOps{
45+
CliAuthInputs: *cvi,
46+
}
47+
}

0 commit comments

Comments
 (0)