github.com/vmware/govmomi@v0.37.2/govc/sso/lpp/update.go (about)

     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  }