vmware / go-vcloud-director

Golang SDK for VMware Cloud Director
Other
80 stars 76 forks source link

Fix Certificate retrieval when its alias has special characters #589

Closed adambarreiro closed 1 year ago

adambarreiro commented 1 year ago

Bug description

When a Certificate inside the library contained a : in the alias (name), the functions Client.GetCertificateFromLibraryByName and AdminOrg.GetCertificateFromLibraryByName are not able to find it, returning an ENF (Entity Not Found) error.

How to reproduce

Given any VCD version, it is as simple as changing the certificate name in the tests, for example:

    alias := "Test_CertificateInLibrary:1"

Fix

The function shouldDoSlowSearch documentation made the following statements:

shouldDoSlowSearch (...) the encoding is rejected by the API in VCD 10.2 version.
(...) Yet, this not needed anymore in VCD 10.3 version.
(...)

However, if one uses Postman to send the following query in VCD 10.4.x:

https://{{vcdHost}}/cloudapi/1.0.0/ssl/certificateLibrary/?filter=alias==Test%3BCertificate%3BIn%3BLibrary%26filterEncoded=true%26pageSize=128&filterEncoded=true&pageSize=128

It returns the error:

"minorErrorCode": "BAD_REQUEST",
    "message": "Cannot parse the supplied filter: alias==Test;Certificate;In;Library&filterEncoded=true&pageSize=128",
    "stackTrace": "com.vmware.vcloud.api.rest.parser.QueryParseException: Cannot parse the supplied filter: alias==Test;Certificate;In;Library&filterEncoded=true&pageSize=128\n\tat com.vmware.vcloud.api.rest.providers.filters.openapi.QueryValidationFilter.enforceFilteringRestric

So, the statements are false. For this reason, this PR removes the VCD version check in shouldDoSlowSearch, as this issue happens always and independent of the VCD version.

This PR also removes the url.QueryEscape inside shouldDoSlowSearch:

params.Set("filter", fmt.Sprintf(filterKey+"==%s", url.QueryEscape(name)))

This operation is wrong, as the OpenAPI generic calls to retrieve all items also escape the parameters:

func (client *Client) newOpenApiRequest(...) {
...
    // Add the params to our URL
    reqUrlCopy.RawQuery += params.Encode()

So in result, the alias==%s filter was encoded twice, causing the VCD filter processor to fail when the alias has special characters like :, as they're encoded twice.

Tests

Tested in 10.4.2

GOVCD_SKIP_VAPP_CREATION=1 go test -tags functional -check.f 'Test_CertificateInLibrary|Test_GetCertificateFromLibrary.*|Test_NsxtVdcGroup' -check.vv -timeout=0