| 1 |
/* |
| 2 |
Copyright (c) 2018 VMware, Inc. All Rights Reserved. |
| 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 |
|
| 17 |
package user |
| 18 |
|
| 19 |
import ( |
| 20 |
"context" |
| 21 |
"flag" |
| 22 |
"fmt" |
| 23 |
"io" |
| 24 |
"strings" |
| 25 |
|
| 26 |
"github.com/vmware/govmomi/govc/cli" |
| 27 |
"github.com/vmware/govmomi/govc/flags" |
| 28 |
"github.com/vmware/govmomi/govc/sso" |
| 29 |
"github.com/vmware/govmomi/ssoadmin" |
| 30 |
"github.com/vmware/govmomi/ssoadmin/types" |
| 31 |
) |
| 32 |
|
| 33 |
type id struct { |
| 34 |
*flags.ClientFlag |
| 35 |
*flags.OutputFlag |
| 36 |
} |
| 37 |
|
| 38 |
func init() { |
| 39 |
cli.Register("sso.user.id", &id{}) |
| 40 |
} |
| 41 |
|
| 42 |
func (cmd *id) Register(ctx context.Context, f *flag.FlagSet) { |
| 43 |
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx) |
| 44 |
cmd.ClientFlag.Register(ctx, f) |
| 45 |
|
| 46 |
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx) |
| 47 |
cmd.OutputFlag.Register(ctx, f) |
| 48 |
} |
| 49 |
|
| 50 |
func (cmd *id) Usage() string { |
| 51 |
return "NAME" |
| 52 |
} |
| 53 |
|
| 54 |
func (cmd *id) Description() string { |
| 55 |
return `Print SSO user and group IDs. |
| 56 |
|
| 57 |
Examples: |
| 58 |
govc sso.user.id |
| 59 |
govc sso.user.id Administrator |
| 60 |
govc sso.user.id -json Administrator` |
| 61 |
} |
| 62 |
|
| 63 |
func (cmd *id) Process(ctx context.Context) error { |
| 64 |
if err := cmd.ClientFlag.Process(ctx); err != nil { |
| 65 |
return err |
| 66 |
} |
| 67 |
return cmd.OutputFlag.Process(ctx) |
| 68 |
} |
| 69 |
|
| 70 |
type userID struct { |
| 71 |
User *types.AdminUser |
| 72 |
Group []types.PrincipalId |
| 73 |
} |
| 74 |
|
| 75 |
func (r *userID) Write(w io.Writer) error { |
| 76 |
var groups []string |
| 77 |
for _, g := range r.Group { |
| 78 |
groups = append(groups, g.Name) |
| 79 |
} |
| 80 |
fmt.Fprintf(w, "%s=%s@%s groups=%s\n", r.User.Kind, r.User.Id.Name, r.User.Id.Domain, strings.Join(groups, ",")) |
| 81 |
return nil |
| 82 |
} |
| 83 |
|
| 84 |
func (r *userID) Dump() interface{} { |
| 85 |
return struct { |
| 86 |
User *types.AdminUser |
| 87 |
Group []types.PrincipalId |
| 88 |
}{r.User, r.Group} |
| 89 |
} |
| 90 |
|
| 91 |
func (cmd *id) Run(ctx context.Context, f *flag.FlagSet) error { |
| 92 |
arg := f.Arg(0) |
| 93 |
if arg == "" { |
| 94 |
arg = cmd.Session.URL.User.Username() |
| 95 |
} |
| 96 |
|
| 97 |
return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error { |
| 98 |
var err error |
| 99 |
var u userID |
| 100 |
|
| 101 |
if u.User, err = c.FindUser(ctx, arg); err != nil { |
| 102 |
return err |
| 103 |
} |
| 104 |
if u.User == nil { |
| 105 |
return fmt.Errorf("%q: no such user", arg) |
| 106 |
} |
| 107 |
|
| 108 |
if u.Group, err = c.FindParentGroups(ctx, u.User.Id); err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
|
| 112 |
return cmd.WriteResult(&u) |
| 113 |
}) |
| 114 |
} |