Skip to content

Commit 8f5c496

Browse files
authored
Merge pull request #2976 from abonat/issue-2975
govc: Add sso.lpp.get and sso.lpp.update commands
2 parents 17e669d + 0dbf717 commit 8f5c496

File tree

5 files changed

+295
-0
lines changed

5 files changed

+295
-0
lines changed

govc/USAGE.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ but appear via `govc $cmd -h`:
283283
- [sso.group.rm](#ssogrouprm)
284284
- [sso.group.update](#ssogroupupdate)
285285
- [sso.idp.ls](#ssoidpls)
286+
- [sso.lpp.info](#ssolppinfo)
287+
- [sso.lpp.update](#ssolppupdate)
286288
- [sso.service.ls](#ssoservicels)
287289
- [sso.user.create](#ssousercreate)
288290
- [sso.user.id](#ssouserid)
@@ -4783,6 +4785,44 @@ Examples:
47834785
Options:
47844786
```
47854787

4788+
## sso.lpp.info
4789+
4790+
```
4791+
Usage: govc sso.lpp.info [OPTIONS]
4792+
4793+
Get SSO local password policy.
4794+
4795+
Examples:
4796+
govc sso.lpp.info
4797+
govc sso.lpp.info -json
4798+
4799+
Options:
4800+
```
4801+
4802+
## sso.lpp.update
4803+
4804+
```
4805+
Usage: govc sso.lpp.update [OPTIONS] NAME
4806+
4807+
Update SSO local password policy.
4808+
4809+
Examples:
4810+
govc sso.lpp.update -PasswordLifetimeDays 0
4811+
4812+
Options:
4813+
-Description= Description
4814+
-MaxIdenticalAdjacentCharacters=0 Maximum identical adjacent characters
4815+
-MaxLength=0 Maximum length
4816+
-MinAlphabeticCount=<nil> Minimum alphabetic count
4817+
-MinLength=<nil> Minimim length
4818+
-MinLowercaseCount=<nil> Minimum lowercase count
4819+
-MinNumericCount=<nil> Minimum numeric count
4820+
-MinSpecialCharCount=<nil> Minimum special characters count
4821+
-MinUppercaseCount=<nil> Minimum uppercase count
4822+
-PasswordLifetimeDays=<nil> Password lifetime days
4823+
-ProhibitedPreviousPasswordsCount=0 Prohibited previous passwords count
4824+
```
4825+
47864826
## sso.service.ls
47874827

47884828
```

govc/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ import (
8585
_ "github.com/vmware/govmomi/govc/session"
8686
_ "github.com/vmware/govmomi/govc/sso/group"
8787
_ "github.com/vmware/govmomi/govc/sso/idp"
88+
_ "github.com/vmware/govmomi/govc/sso/lpp"
8889
_ "github.com/vmware/govmomi/govc/sso/service"
8990
_ "github.com/vmware/govmomi/govc/sso/user"
9091
_ "github.com/vmware/govmomi/govc/storage/policy"

govc/sso/lpp/info.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
Copyright (c) 2022-2022 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 lpp
18+
19+
import (
20+
"context"
21+
"flag"
22+
"fmt"
23+
"io"
24+
25+
"github.com/vmware/govmomi/govc/cli"
26+
"github.com/vmware/govmomi/govc/flags"
27+
"github.com/vmware/govmomi/govc/sso"
28+
"github.com/vmware/govmomi/ssoadmin"
29+
"github.com/vmware/govmomi/ssoadmin/types"
30+
)
31+
32+
type info struct {
33+
*flags.ClientFlag
34+
*flags.OutputFlag
35+
}
36+
37+
func init() {
38+
cli.Register("sso.lpp.info", &info{})
39+
}
40+
41+
func (cmd *info) Register(ctx context.Context, f *flag.FlagSet) {
42+
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
43+
cmd.ClientFlag.Register(ctx, f)
44+
45+
cmd.OutputFlag, ctx = flags.NewOutputFlag(ctx)
46+
cmd.OutputFlag.Register(ctx, f)
47+
}
48+
49+
func (cmd *info) Description() string {
50+
return `Get SSO local password policy.
51+
52+
Examples:
53+
govc sso.lpp.info
54+
govc sso.lpp.info -json`
55+
}
56+
57+
func (cmd *info) Process(ctx context.Context) error {
58+
if err := cmd.ClientFlag.Process(ctx); err != nil {
59+
return err
60+
}
61+
return cmd.OutputFlag.Process(ctx)
62+
}
63+
64+
type lppInfo struct {
65+
LocalPasswordPolicy *types.AdminPasswordPolicy
66+
}
67+
68+
func (r *lppInfo) Write(w io.Writer) error {
69+
fmt.Fprintf(
70+
w,
71+
"Description: %s\n"+
72+
"MinLength: %d\n"+
73+
"MaxLength: %d\n"+
74+
"MinAlphabeticCount: %d\n"+
75+
"MinUppercaseCount: %d\n"+
76+
"MinLowercaseCount: %d\n"+
77+
"MinNumericCount: %d\n"+
78+
"MinSpecialCharCount: %d\n"+
79+
"MaxIdenticalAdjacentCharacters: %d\n"+
80+
"ProhibitedPreviousPasswordsCount: %d\n"+
81+
"PasswordLifetimeDays: %d\n",
82+
r.LocalPasswordPolicy.Description,
83+
r.LocalPasswordPolicy.PasswordFormat.LengthRestriction.MinLength,
84+
r.LocalPasswordPolicy.PasswordFormat.LengthRestriction.MaxLength,
85+
r.LocalPasswordPolicy.PasswordFormat.AlphabeticRestriction.MinAlphabeticCount,
86+
r.LocalPasswordPolicy.PasswordFormat.AlphabeticRestriction.MinUppercaseCount,
87+
r.LocalPasswordPolicy.PasswordFormat.AlphabeticRestriction.MinLowercaseCount,
88+
r.LocalPasswordPolicy.PasswordFormat.MinNumericCount,
89+
r.LocalPasswordPolicy.PasswordFormat.MinSpecialCharCount,
90+
r.LocalPasswordPolicy.PasswordFormat.MaxIdenticalAdjacentCharacters,
91+
r.LocalPasswordPolicy.ProhibitedPreviousPasswordsCount,
92+
r.LocalPasswordPolicy.PasswordLifetimeDays,
93+
)
94+
return nil
95+
}
96+
97+
func (r *lppInfo) Dump() interface{} {
98+
return r.LocalPasswordPolicy
99+
}
100+
101+
func (cmd *info) Run(ctx context.Context, f *flag.FlagSet) error {
102+
return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
103+
var err error
104+
var pol lppInfo
105+
pol.LocalPasswordPolicy, err = c.GetLocalPasswordPolicy(ctx)
106+
if err != nil {
107+
return err
108+
}
109+
return cmd.WriteResult(&pol)
110+
})
111+
}

govc/sso/lpp/update.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
/*
2+
Copyright (c) 2022-2022 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 lpp
18+
19+
import (
20+
"context"
21+
"flag"
22+
23+
"github.com/vmware/govmomi/govc/cli"
24+
"github.com/vmware/govmomi/govc/flags"
25+
"github.com/vmware/govmomi/govc/sso"
26+
"github.com/vmware/govmomi/ssoadmin"
27+
"github.com/vmware/govmomi/ssoadmin/types"
28+
)
29+
30+
type policyDetails struct {
31+
*flags.ClientFlag
32+
33+
pol types.AdminPasswordPolicy
34+
MinLength *int32
35+
MinAlphabeticCount *int32
36+
MinUppercaseCount *int32
37+
MinLowercaseCount *int32
38+
MinNumericCount *int32
39+
MinSpecialCharCount *int32
40+
PasswordLifetimeDays *int32
41+
}
42+
43+
func (cmd *policyDetails) Usage() string {
44+
return "NAME"
45+
}
46+
47+
func (cmd *policyDetails) Register(ctx context.Context, f *flag.FlagSet) {
48+
cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
49+
cmd.ClientFlag.Register(ctx, f)
50+
51+
f.StringVar(&cmd.pol.Description, "Description", "", "Description")
52+
f.Var(flags.NewOptionalInt32(&cmd.MinLength), "MinLength", "Minimim length")
53+
f.Var(flags.NewInt32(&cmd.pol.PasswordFormat.LengthRestriction.MaxLength), "MaxLength", "Maximum length")
54+
f.Var(flags.NewOptionalInt32(&cmd.MinAlphabeticCount), "MinAlphabeticCount", "Minimum alphabetic count")
55+
f.Var(flags.NewOptionalInt32(&cmd.MinUppercaseCount), "MinUppercaseCount", "Minimum uppercase count")
56+
f.Var(flags.NewOptionalInt32(&cmd.MinLowercaseCount), "MinLowercaseCount", "Minimum lowercase count")
57+
f.Var(flags.NewOptionalInt32(&cmd.MinNumericCount), "MinNumericCount", "Minimum numeric count")
58+
f.Var(flags.NewOptionalInt32(&cmd.MinSpecialCharCount), "MinSpecialCharCount", "Minimum special characters count")
59+
f.Var(flags.NewInt32(&cmd.pol.PasswordFormat.MaxIdenticalAdjacentCharacters), "MaxIdenticalAdjacentCharacters", "Maximum identical adjacent characters")
60+
f.Var(flags.NewInt32(&cmd.pol.ProhibitedPreviousPasswordsCount), "ProhibitedPreviousPasswordsCount", "Prohibited previous passwords count")
61+
f.Var(flags.NewOptionalInt32(&cmd.PasswordLifetimeDays), "PasswordLifetimeDays", "Password lifetime days")
62+
}
63+
64+
type update struct {
65+
policyDetails
66+
}
67+
68+
func init() {
69+
cli.Register("sso.lpp.update", &update{})
70+
}
71+
72+
func (cmd *update) Description() string {
73+
return `Update SSO local password policy.
74+
75+
Examples:
76+
govc sso.lpp.update -PasswordLifetimeDays 0`
77+
}
78+
79+
func smerge(src *string, current string) {
80+
if *src == "" {
81+
*src = current
82+
}
83+
}
84+
85+
func imerge(src *int32, current int32) {
86+
if *src == 0 {
87+
*src = current
88+
}
89+
}
90+
91+
func oimerge(src *int32, flag *int32, current int32) {
92+
if flag == nil {
93+
*src = current
94+
} else {
95+
*src = *flag
96+
}
97+
}
98+
99+
func (cmd *update) Run(ctx context.Context, f *flag.FlagSet) error {
100+
return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
101+
current, err := c.GetLocalPasswordPolicy(ctx)
102+
if err != nil {
103+
return err
104+
}
105+
106+
smerge(&cmd.pol.Description, current.Description)
107+
oimerge(&cmd.pol.PasswordFormat.LengthRestriction.MinLength, cmd.MinLength, current.PasswordFormat.LengthRestriction.MinLength)
108+
imerge(&cmd.pol.PasswordFormat.LengthRestriction.MaxLength, current.PasswordFormat.LengthRestriction.MaxLength)
109+
oimerge(&cmd.pol.PasswordFormat.AlphabeticRestriction.MinAlphabeticCount, cmd.MinAlphabeticCount, current.PasswordFormat.AlphabeticRestriction.MinAlphabeticCount)
110+
oimerge(&cmd.pol.PasswordFormat.AlphabeticRestriction.MinUppercaseCount, cmd.MinUppercaseCount, current.PasswordFormat.AlphabeticRestriction.MinUppercaseCount)
111+
oimerge(&cmd.pol.PasswordFormat.AlphabeticRestriction.MinLowercaseCount, cmd.MinLowercaseCount, current.PasswordFormat.AlphabeticRestriction.MinLowercaseCount)
112+
oimerge(&cmd.pol.PasswordFormat.MinNumericCount, cmd.MinNumericCount, current.PasswordFormat.MinNumericCount)
113+
oimerge(&cmd.pol.PasswordFormat.MinSpecialCharCount, cmd.MinSpecialCharCount, current.PasswordFormat.MinSpecialCharCount)
114+
imerge(&cmd.pol.PasswordFormat.MaxIdenticalAdjacentCharacters, current.PasswordFormat.MaxIdenticalAdjacentCharacters)
115+
imerge(&cmd.pol.ProhibitedPreviousPasswordsCount, current.ProhibitedPreviousPasswordsCount)
116+
oimerge(&cmd.pol.PasswordLifetimeDays, cmd.PasswordLifetimeDays, current.PasswordLifetimeDays)
117+
118+
return c.UpdateLocalPasswordPolicy(ctx, cmd.pol)
119+
})
120+
}

ssoadmin/client.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ func (c *Client) CreateSolutionUser(ctx context.Context, name string, details ty
146146
return err
147147
}
148148

149+
func (c *Client) UpdateLocalPasswordPolicy(ctx context.Context, policy types.AdminPasswordPolicy) error {
150+
req := types.UpdateLocalPasswordPolicy{
151+
This: c.ServiceContent.PasswordPolicyService,
152+
Policy: policy,
153+
}
154+
155+
_, err := methods.UpdateLocalPasswordPolicy(ctx, c, &req)
156+
return err
157+
}
158+
149159
func (c *Client) UpdateSolutionUser(ctx context.Context, name string, details types.AdminSolutionDetails) error {
150160
req := types.UpdateLocalSolutionUserDetails{
151161
This: c.ServiceContent.PrincipalManagementService,
@@ -411,6 +421,19 @@ func (c *Client) FindParentGroups(ctx context.Context, id types.PrincipalId, gro
411421
return nil, nil
412422
}
413423

424+
func (c *Client) GetLocalPasswordPolicy(ctx context.Context) (*types.AdminPasswordPolicy, error) {
425+
req := types.GetLocalPasswordPolicy{
426+
This: c.ServiceContent.PasswordPolicyService,
427+
}
428+
429+
res, err := methods.GetLocalPasswordPolicy(ctx, c, &req)
430+
if err != nil {
431+
return nil, err
432+
}
433+
434+
return &res.Returnval, nil
435+
}
436+
414437
func (c *Client) Login(ctx context.Context) error {
415438
req := types.Login{
416439
This: c.ServiceContent.SessionManager,

0 commit comments

Comments
 (0)