From bd029a523d39abb30d09691b2e91d457f9fce48f Mon Sep 17 00:00:00 2001 From: Eric Bissonnette Date: Tue, 17 Dec 2024 09:01:46 -0500 Subject: [PATCH 1/3] bugfix: certificate_authorities: add mtls_certificate_id to query params --- certificate_authorities.go | 24 ++++++++++++++---------- certificate_authorities_test.go | 13 +++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/certificate_authorities.go b/certificate_authorities.go index b9cffb2ab61..8e88e03046a 100644 --- a/certificate_authorities.go +++ b/certificate_authorities.go @@ -17,6 +17,11 @@ type UpdateCertificateAuthoritiesHostnameAssociationsParams struct { MTLSCertificateID string `json:"mtls_certificate_id,omitempty"` } +type HostnameAssociationsUpdateRequest struct { + Hostnames []HostnameAssociation `json:"hostnames,omitempty"` + MTLSCertificateID string `json:"mtls_certificate_id,omitempty"` +} + type HostnameAssociationsResponse struct { Response Result []HostnameAssociation `json:"result"` @@ -28,12 +33,11 @@ type HostnameAssociation = string // // API Reference: https://developers.cloudflare.com/api/resources/certificate_authorities/subresources/hostname_associations/methods/get/ func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Context, rc *ResourceContainer, params ListCertificateAuthoritiesHostnameAssociationsParams) ([]HostnameAssociation, error) { + if rc.Level != ZoneRouteLevel { + return []HostnameAssociation{}, ErrRequiredZoneLevelResourceContainer + } - uri := fmt.Sprintf( - "/%s/%s/certificate_authorities/hostname_associations", - rc.Level, - rc.Identifier, - ) + uri := buildURI(fmt.Sprintf("/zones/%s/certificate_authorities/hostname_associations", rc.Identifier), params) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { @@ -53,11 +57,11 @@ func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Conte // // API Reference: https://developers.cloudflare.com/api/resources/certificate_authorities/subresources/hostname_associations/methods/update/ func (api *API) UpdateCertificateAuthoritiesHostnameAssociations(ctx context.Context, rc *ResourceContainer, params UpdateCertificateAuthoritiesHostnameAssociationsParams) ([]HostnameAssociation, error) { - uri := fmt.Sprintf( - "/%s/%s/certificate_authorities/hostname_associations", - rc.Level, - rc.Identifier, - ) + if rc.Level != ZoneRouteLevel { + return []HostnameAssociation{}, ErrRequiredZoneLevelResourceContainer + } + + uri := fmt.Sprintf("/zones/%s/certificate_authorities/hostname_associations", rc.Identifier) res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params) if err != nil { diff --git a/certificate_authorities_test.go b/certificate_authorities_test.go index 940524a5b7b..4cb88477075 100644 --- a/certificate_authorities_test.go +++ b/certificate_authorities_test.go @@ -6,6 +6,7 @@ import ( "net/http" "testing" + "github.com/goccy/go-json" "github.com/stretchr/testify/assert" ) @@ -15,6 +16,7 @@ func TestListCertificateAuthoritiesHostnameAssociations(t *testing.T) { handler := func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method) + assert.Equal(t, "72ef4d06-4752-4493-a60a-7421470fd585", r.URL.Query().Get("mtls_certificate_id")) w.Header().Set("content-type", "application/json") fmt.Fprintf(w, `{ "success": true, @@ -51,6 +53,16 @@ func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) { handler := func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method) + + wantReqHostnames := []HostnameAssociation{ + "admin.example.com", + "foobar.example.com", + } + var req HostnameAssociationsUpdateRequest + assert.NoError(t, json.NewDecoder(r.Body).Decode(&req)) + assert.Equal(t, "72ef4d06-4752-4493-a60a-7421470fd585", req.MTLSCertificateID) + assert.Equal(t, wantReqHostnames, req.Hostnames) + w.Header().Set("content-type", "application/json") fmt.Fprintf(w, `{ "success": true, @@ -64,6 +76,7 @@ func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) { } hostnameAssociations := UpdateCertificateAuthoritiesHostnameAssociationsParams{ + MTLSCertificateID: "72ef4d06-4752-4493-a60a-7421470fd585", Hostnames: []HostnameAssociation{ "admin.example.com", "foobar.example.com", From c7be137e3b8cf5fde61961a67df4469c90da3e53 Mon Sep 17 00:00:00 2001 From: Eric Bissonnette Date: Tue, 17 Dec 2024 10:18:30 -0500 Subject: [PATCH 2/3] bugfix: certificate_authorities: fix wrong result structure --- certificate_authorities.go | 10 +++++++--- certificate_authorities_test.go | 20 ++++++++++++-------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/certificate_authorities.go b/certificate_authorities.go index 8e88e03046a..80ba39c877f 100644 --- a/certificate_authorities.go +++ b/certificate_authorities.go @@ -24,7 +24,11 @@ type HostnameAssociationsUpdateRequest struct { type HostnameAssociationsResponse struct { Response - Result []HostnameAssociation `json:"result"` + Result HostnameAssociations `json:"result"` +} + +type HostnameAssociations struct { + Hostnames []HostnameAssociation `json:"hostnames"` } type HostnameAssociation = string @@ -50,7 +54,7 @@ func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Conte return []HostnameAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } - return hostnameAssociationsResponse.Result, nil + return hostnameAssociationsResponse.Result.Hostnames, nil } // Replace Hostname Associations @@ -74,5 +78,5 @@ func (api *API) UpdateCertificateAuthoritiesHostnameAssociations(ctx context.Con return []HostnameAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err) } - return hostnameAssociationsResponse.Result, nil + return hostnameAssociationsResponse.Result.Hostnames, nil } diff --git a/certificate_authorities_test.go b/certificate_authorities_test.go index 4cb88477075..cba5c57f7a2 100644 --- a/certificate_authorities_test.go +++ b/certificate_authorities_test.go @@ -22,10 +22,12 @@ func TestListCertificateAuthoritiesHostnameAssociations(t *testing.T) { "success": true, "errors": [], "messages": [], - "result": [ - "admin.example.com", - "foobar.example.com" - ] + "result": { + "hostnames": [ + "admin.example.com", + "foobar.example.com" + ] + } }`) } @@ -68,10 +70,12 @@ func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) { "success": true, "errors": [], "messages": [], - "result": [ - "admin.example.com", - "foobar.example.com" - ] + "result": { + "hostnames": [ + "admin.example.com", + "foobar.example.com" + ] + } }`) } From e830fd45387895ae8af5a511b7959486b3e75d76 Mon Sep 17 00:00:00 2001 From: Eric Bissonnette Date: Tue, 17 Dec 2024 10:33:31 -0500 Subject: [PATCH 3/3] add changelog for #3742 --- .changelog/3742.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/3742.txt diff --git a/.changelog/3742.txt b/.changelog/3742.txt new file mode 100644 index 00000000000..0395042d07d --- /dev/null +++ b/.changelog/3742.txt @@ -0,0 +1,3 @@ +```release-note:bug +certificate_authorities: fixes for methods to interact with Certificate Authorities Hostname Associations API +``` \ No newline at end of file