github.com/vmware/govmomi@v0.37.2/govc/sso/idp/ldap_update.go (about)

     1  /*
     2  Copyright (c) 2023-2023 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 idp
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"reflect"
    23  
    24  	"github.com/vmware/govmomi/govc/cli"
    25  	"github.com/vmware/govmomi/govc/flags"
    26  	"github.com/vmware/govmomi/govc/sso"
    27  	"github.com/vmware/govmomi/ssoadmin"
    28  	"github.com/vmware/govmomi/ssoadmin/types"
    29  )
    30  
    31  type ldapUpdate struct {
    32  	*flags.ClientFlag
    33  	serverType string
    34  	alias      string
    35  	idpDetails types.LdapIdentitySourceDetails
    36  	auth       types.SsoAdminIdentitySourceManagementServiceAuthenticationCredentails
    37  }
    38  
    39  func (cmd *ldapUpdate) Usage() string {
    40  	return "NAME"
    41  }
    42  
    43  func (cmd *ldapUpdate) Register(ctx context.Context, f *flag.FlagSet) {
    44  	cmd.ClientFlag, ctx = flags.NewClientFlag(ctx)
    45  	cmd.ClientFlag.Register(ctx, f)
    46  
    47  	f.StringVar(&cmd.serverType, "ServerType", "ActiveDirectory", "ServerType")
    48  	f.StringVar(&cmd.alias, "DomainAlias", "", "DomainAlias")
    49  	f.StringVar(&cmd.idpDetails.FriendlyName, "FriendlyName", "", "FriendlyName")
    50  	f.StringVar(&cmd.idpDetails.UserBaseDn, "UserBaseDn", "", "UserBaseDn")
    51  	f.StringVar(&cmd.idpDetails.GroupBaseDn, "GroupBaseDn", "", "GroupBaseDn")
    52  	f.StringVar(&cmd.idpDetails.PrimaryURL, "PrimaryUrl", "", "PrimaryUrl")
    53  	f.StringVar(&cmd.idpDetails.FailoverURL, "FailoverUrl", "", "FailoverUrl")
    54  	f.StringVar(&cmd.auth.Username, "AuthUsername", "", "Username")
    55  	f.StringVar(&cmd.auth.Password, "AuthPassword", "", "Password")
    56  }
    57  
    58  type lidpupd struct {
    59  	ldapUpdate
    60  }
    61  
    62  func init() {
    63  	cli.Register("sso.idp.ldap.update", &lidpupd{})
    64  }
    65  
    66  func (cmd *lidpupd) Description() string {
    67  	return `Update SSO ldap identity provider source.
    68  
    69  Examples:
    70    govc sso.idp.ldap.update  -FriendlyName CORPLOCAL corp.local`
    71  }
    72  
    73  func smerge(src *string, current string) {
    74  	if *src == "" {
    75  		*src = current
    76  	}
    77  }
    78  
    79  func (cmd *lidpupd) Run(ctx context.Context, f *flag.FlagSet) error {
    80  	if f.NArg() != 1 {
    81  		return flag.ErrHelp
    82  	}
    83  	idpname := f.Arg(0)
    84  	return sso.WithClient(ctx, cmd.ClientFlag, func(c *ssoadmin.Client) error {
    85  		sources, err := c.IdentitySources(ctx)
    86  		if err != nil {
    87  			return err
    88  		}
    89  
    90  		GetLdapIdentitySourceByName := func(i []types.LdapIdentitySource, name string) *types.LdapIdentitySource {
    91  			var n []types.LdapIdentitySource
    92  			for _, e := range i {
    93  				if e.Name == name {
    94  					n = append(n, e)
    95  				}
    96  			}
    97  			if len(n) != 1 {
    98  				return nil
    99  			}
   100  			return &n[0]
   101  		}
   102  
   103  		currentidp := GetLdapIdentitySourceByName(sources.LDAPS, idpname)
   104  		if currentidp == nil {
   105  			return c.RegisterLdap(ctx, cmd.serverType, idpname, cmd.alias, cmd.idpDetails, cmd.auth)
   106  		}
   107  
   108  		if cmd.auth.Username != "" && cmd.auth.Password != "" {
   109  			updateLdapAuthnErr := c.UpdateLdapAuthnType(ctx, idpname, cmd.auth)
   110  			if updateLdapAuthnErr != nil {
   111  				return updateLdapAuthnErr
   112  			}
   113  		}
   114  
   115  		IsAnyIdpDetails := func(d types.LdapIdentitySourceDetails) bool {
   116  			values := reflect.ValueOf(cmd.idpDetails)
   117  			for i := 0; i < values.NumField(); i++ {
   118  				if values.Field(i).Interface() != "" {
   119  					return true
   120  				}
   121  			}
   122  			return false
   123  		}
   124  		if IsAnyIdpDetails(cmd.idpDetails) {
   125  			smerge(&cmd.idpDetails.FriendlyName, currentidp.Details.FriendlyName)
   126  			smerge(&cmd.idpDetails.UserBaseDn, currentidp.Details.UserBaseDn)
   127  			smerge(&cmd.idpDetails.GroupBaseDn, currentidp.Details.GroupBaseDn)
   128  			smerge(&cmd.idpDetails.PrimaryURL, currentidp.Details.PrimaryURL)
   129  			smerge(&cmd.idpDetails.FailoverURL, currentidp.Details.FailoverURL)
   130  			updateLdapErr := c.UpdateLdap(ctx, idpname, cmd.idpDetails)
   131  			if updateLdapErr != nil {
   132  				return updateLdapErr
   133  			}
   134  		}
   135  		return nil
   136  	})
   137  }