Skip to content

Commit

Permalink
govc: add sso.group commands
Browse files Browse the repository at this point in the history
  • Loading branch information
dougm committed Jun 21, 2019
1 parent 9488a6f commit b3adfff
Show file tree
Hide file tree
Showing 12 changed files with 453 additions and 61 deletions.
1 change: 1 addition & 0 deletions govc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ import (
_ "github.com/vmware/govmomi/govc/pool"
_ "github.com/vmware/govmomi/govc/role"
_ "github.com/vmware/govmomi/govc/session"
_ "github.com/vmware/govmomi/govc/sso/group"
_ "github.com/vmware/govmomi/govc/sso/service"
_ "github.com/vmware/govmomi/govc/sso/user"
_ "github.com/vmware/govmomi/govc/tags"
Expand Down
80 changes: 80 additions & 0 deletions govc/sso/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package sso

import (
"context"
"log"
"os"

"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/ssoadmin"
"github.com/vmware/govmomi/sts"
"github.com/vmware/govmomi/vim25/soap"
)

func WithClient(ctx context.Context, cmd *flags.ClientFlag, f func(*ssoadmin.Client) error) error {
vc, err := cmd.Client()
if err != nil {
return err
}

c, err := ssoadmin.NewClient(ctx, vc)
if err != nil {
return err
}

// SSO admin server has its own session manager, so the govc persisted session cookies cannot
// be used to authenticate. There is no SSO token persistence in govc yet, so just use an env
// var for now. If no GOVC_LOGIN_TOKEN is set, issue a new token.
token := os.Getenv("GOVC_LOGIN_TOKEN")
header := soap.Header{
Security: &sts.Signer{
Certificate: vc.Certificate(),
Token: token,
},
}

if token == "" {
tokens, cerr := sts.NewClient(ctx, vc)
if cerr != nil {
return cerr
}

req := sts.TokenRequest{
Certificate: vc.Certificate(),
Userinfo: cmd.Userinfo(),
}

header.Security, cerr = tokens.Issue(ctx, req)
if cerr != nil {
return cerr
}
}

if err = c.Login(c.WithHeader(ctx, header)); err != nil {
return err
}

defer func() {
if err := c.Logout(ctx); err != nil {
log.Printf("user logout error: %v", err)
}
}()

return f(c)
}
66 changes: 66 additions & 0 deletions govc/sso/group/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package group

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/sso"
"github.com/vmware/govmomi/ssoadmin"
"github.com/vmware/govmomi/ssoadmin/types"
)

type create struct {
*flags.ClientFlag

types.AdminGroupDetails
}

func (cmd *create) Usage() string {
return "NAME"
}

func (cmd *create) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

f.StringVar(&cmd.AdminGroupDetails.Description, "d", "", "Group description")
}

func init() {
cli.Register("sso.group.create", &create{})
}

func (cmd *create) Description() string {
return `Create SSO group.
Examples:
govc sso.group.create NAME`
}

func (cmd *create) Run(ctx context.Context, f *flag.FlagSet) error {
if f.NArg() != 1 {
return flag.ErrHelp
}

return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
return c.CreateGroup(ctx, f.Arg(0), cmd.AdminGroupDetails)
})
}
89 changes: 89 additions & 0 deletions govc/sso/group/ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package group

import (
"context"
"flag"
"fmt"
"io"
"text/tabwriter"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/sso"
"github.com/vmware/govmomi/ssoadmin"
"github.com/vmware/govmomi/ssoadmin/types"
)

type ls struct {
*flags.ClientFlag
*flags.OutputFlag
}

func init() {
cli.Register("sso.group.ls", &ls{})
}

func (cmd *ls) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)

cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
cmd.OutputFlag.Register(ctx, f)
}

func (cmd *ls) Description() string {
return `List SSO groups.
Examples:
govc sso.group.ls -s`
}

func (cmd *ls) Process(ctx context.Context) error {
if err := cmd.ClientFlag.Process(ctx); err != nil {
return err
}
return cmd.OutputFlag.Process(ctx)
}

type groupResult []types.AdminGroup

func (r groupResult) Dump() interface{} {
return []types.AdminGroup(r)
}

func (r groupResult) Write(w io.Writer) error {
tw := tabwriter.NewWriter(w, 2, 0, 2, ' ', 0)
for _, info := range r {
fmt.Fprintf(tw, "%s\t%s\n", info.Id.Name, info.Details.Description)
}
return tw.Flush()
}

func (cmd *ls) Run(ctx context.Context, f *flag.FlagSet) error {
arg := f.Arg(0)

return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
info, err := c.FindGroups(ctx, arg)
if err != nil {
return err
}

return cmd.WriteResult(groupResult(info))
})
}
57 changes: 57 additions & 0 deletions govc/sso/group/rm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
Copyright (c) 2019 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package group

import (
"context"
"flag"

"github.com/vmware/govmomi/govc/cli"
"github.com/vmware/govmomi/govc/flags"
"github.com/vmware/govmomi/govc/sso"
"github.com/vmware/govmomi/ssoadmin"
)

type rm struct {
*flags.ClientFlag
}

func init() {
cli.Register("sso.group.rm", &rm{})
}

func (cmd *rm) Register(ctx context.Context, f *flag.FlagSet) {
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
cmd.ClientFlag.Register(ctx, f)
}

func (cmd *rm) Usage() string {
return "NAME"
}

func (cmd *rm) Description() string {
return `Remove SSO group.
Examples:
govc sso.group.rm NAME`
}

func (cmd *rm) Run(ctx context.Context, f *flag.FlagSet) error {
return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
return c.DeletePrincipal(ctx, f.Arg(0))
})
}
Loading

0 comments on commit b3adfff

Please sign in to comment.