github.com/vmware/go-vcloud-director/v2@v2.24.0/govcd/adminorg_ldap_test.go (about)

     1  //go:build user || functional || ALL
     2  
     3  /*
     4   * Copyright 2022 VMware, Inc.  All rights reserved.  Licensed under the Apache v2 License.
     5   */
     6  
     7  package govcd
     8  
     9  import (
    10  	"fmt"
    11  
    12  	"github.com/vmware/go-vcloud-director/v2/types/v56"
    13  	. "gopkg.in/check.v1"
    14  )
    15  
    16  // Test_LDAP serves as a "subtest" framework for tests requiring LDAP configuration. It sets up LDAP
    17  // configuration for Org and cleans up this test run.
    18  //
    19  // Prerequisites:
    20  // * LDAP server already installed
    21  // * LDAP server IP set in TestConfig.VCD.LdapServer
    22  func (vcd *TestVCD) Test_LDAP(check *C) {
    23  	if vcd.skipAdminTests {
    24  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    25  	}
    26  	vcd.checkSkipWhenApiToken(check)
    27  
    28  	ldapHostIp := vcd.config.VCD.LdapServer
    29  	if ldapHostIp == "" {
    30  		check.Skip("[" + check.TestName() + "] LDAP server IP not provided in configuration")
    31  	}
    32  	// Due to a bug in VCD, when configuring LDAP service, Org publishing catalog settings `Publish external catalogs` and
    33  	// `Subscribe to external catalogs ` gets disabled. For that reason we are getting the current values from those vars
    34  	// to set them at the end of the test, to avoid interference with other tests.
    35  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
    36  	check.Assert(err, IsNil)
    37  	check.Assert(adminOrg, NotNil)
    38  
    39  	publishExternalCatalogs := adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally
    40  	subscribeToExternalCatalogs := adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanSubscribe
    41  
    42  	fmt.Printf("Setting up LDAP (IP: %s)\n", ldapHostIp)
    43  	err = configureLdapForOrg(vcd, adminOrg, ldapHostIp, check.TestName())
    44  	check.Assert(err, IsNil)
    45  	defer func() {
    46  		fmt.Println("Unconfiguring LDAP")
    47  		// Clear LDAP configuration
    48  		err = adminOrg.LdapDisable()
    49  		check.Assert(err, IsNil)
    50  
    51  		// Due to the VCD bug mentioned above, we need to set the previous state from the publishing settings vars
    52  		check.Assert(adminOrg.Refresh(), IsNil)
    53  
    54  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally = publishExternalCatalogs
    55  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanSubscribe = subscribeToExternalCatalogs
    56  
    57  		task, err := adminOrg.Update()
    58  		check.Assert(err, IsNil)
    59  
    60  		err = task.WaitTaskCompletion()
    61  		check.Assert(err, IsNil)
    62  	}()
    63  
    64  	// Run tests requiring LDAP from here.
    65  	vcd.test_GroupCRUD(check)
    66  	vcd.test_GroupFinderGetGenericEntity(check)
    67  	vcd.test_GroupUserListIsPopulated(check)
    68  }
    69  
    70  func (vcd *TestVCD) Test_LDAPSystem(check *C) {
    71  	if vcd.skipAdminTests {
    72  		check.Skip(fmt.Sprintf(TestRequiresSysAdminPrivileges, check.TestName()))
    73  	}
    74  	vcd.checkSkipWhenApiToken(check)
    75  
    76  	// Due to a bug in VCD, when configuring LDAP service, Org publishing catalog settings `Publish external catalogs` and
    77  	// `Subscribe to external catalogs ` gets disabled. For that reason we are getting the current values from those vars
    78  	// to set them at the end of the test, to avoid interference with other tests.
    79  	adminOrg, err := vcd.client.GetAdminOrgByName(vcd.org.Org.Name)
    80  	check.Assert(err, IsNil)
    81  	check.Assert(adminOrg, NotNil)
    82  
    83  	publishExternalCatalogs := adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally
    84  	subscribeToExternalCatalogs := adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanSubscribe
    85  	ldapSettings := types.OrgLdapSettingsType{
    86  		OrgLdapMode:   "SYSTEM",
    87  		CustomUsersOu: "ou=Foo,dc=domain,dc=local base DN",
    88  	}
    89  
    90  	_, err = adminOrg.LdapConfigure(&ldapSettings)
    91  	check.Assert(err, IsNil)
    92  	defer func() {
    93  		fmt.Println("Unconfiguring LDAP")
    94  		// Clear LDAP configuration
    95  		err = adminOrg.LdapDisable()
    96  		check.Assert(err, IsNil)
    97  
    98  		// Due to the VCD bug mentioned above, we need to set the previous state from the publishing settings vars
    99  		check.Assert(adminOrg.Refresh(), IsNil)
   100  
   101  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanPublishExternally = publishExternalCatalogs
   102  		adminOrg.AdminOrg.OrgSettings.OrgGeneralSettings.CanSubscribe = subscribeToExternalCatalogs
   103  
   104  		task, err := adminOrg.Update()
   105  		check.Assert(err, IsNil)
   106  
   107  		err = task.WaitTaskCompletion()
   108  		check.Assert(err, IsNil)
   109  	}()
   110  }
   111  
   112  // configureLdapForOrg sets up LDAP configuration in vCD org
   113  func configureLdapForOrg(vcd *TestVCD, adminOrg *AdminOrg, ldapHostIp, testName string) error {
   114  	fmt.Printf("# Configuring LDAP settings for Org '%s'", vcd.config.VCD.Org)
   115  
   116  	// The below settings are tailored for LDAP docker testing image
   117  	// https://github.com/rroemhild/docker-test-openldap
   118  	ldapSettings := &types.OrgLdapSettingsType{
   119  		OrgLdapMode: types.LdapModeCustom,
   120  		CustomOrgLdapSettings: &types.CustomOrgLdapSettings{
   121  			HostName:                ldapHostIp,
   122  			Port:                    389,
   123  			SearchBase:              "dc=planetexpress,dc=com",
   124  			AuthenticationMechanism: "SIMPLE",
   125  			ConnectorType:           "OPEN_LDAP",
   126  			Username:                "cn=admin,dc=planetexpress,dc=com",
   127  			Password:                "GoodNewsEveryone",
   128  			UserAttributes: &types.OrgLdapUserAttributes{
   129  				ObjectClass:               "inetOrgPerson",
   130  				ObjectIdentifier:          "uid",
   131  				Username:                  "uid",
   132  				Email:                     "mail",
   133  				FullName:                  "cn",
   134  				GivenName:                 "givenName",
   135  				Surname:                   "sn",
   136  				Telephone:                 "telephoneNumber",
   137  				GroupMembershipIdentifier: "dn",
   138  			},
   139  			GroupAttributes: &types.OrgLdapGroupAttributes{
   140  				ObjectClass:          "group",
   141  				ObjectIdentifier:     "cn",
   142  				GroupName:            "cn",
   143  				Membership:           "member",
   144  				MembershipIdentifier: "dn",
   145  			},
   146  		},
   147  	}
   148  
   149  	_, err := adminOrg.LdapConfigure(ldapSettings)
   150  	if err != nil {
   151  		return err
   152  	}
   153  	fmt.Println(" Done")
   154  	AddToCleanupList("LDAP-configuration", "orgLdapSettings", adminOrg.AdminOrg.Name, testName)
   155  	return nil
   156  }