forked from vmware/govmomi
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes vmware#1483
- Loading branch information
Showing
12 changed files
with
453 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}) | ||
} |
Oops, something went wrong.