github.com/vmware/govmomi@v0.37.2/govc/sso/lpp/info.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  	"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  }