Skip to content

Commit

Permalink
Merge pull request #3740 from ebisso/feat/certificate-authorities
Browse files Browse the repository at this point in the history
Feat/certificate authorities
  • Loading branch information
jacobbednarz authored Dec 17, 2024
2 parents ac318ca + 6645437 commit 19c0f6a
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/3740.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
certificate_authorities: add new methods to interact with Certificate Authorities Hostname Associations API
```
74 changes: 74 additions & 0 deletions certificate_authorities.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package cloudflare

import (
"context"
"fmt"
"net/http"

"github.com/goccy/go-json"
)

type ListCertificateAuthoritiesHostnameAssociationsParams struct {
MTLSCertificateID string `url:"mtls_certificate_id,omitempty"`
}

type UpdateCertificateAuthoritiesHostnameAssociationsParams struct {
Hostnames []HostnameAssociation `json:"hostnames,omitempty"`
MTLSCertificateID string `json:"mtls_certificate_id,omitempty"`
}

type HostnameAssociationsResponse struct {
Response
Result []HostnameAssociation `json:"result"`
}

type HostnameAssociation = string

// List Hostname Associations
//
// API Reference: https://developers.cloudflare.com/api/operations/client-certificate-for-a-zone-list-hostname-associations
func (api *API) ListCertificateAuthoritiesHostnameAssociations(ctx context.Context, rc *ResourceContainer, params ListCertificateAuthoritiesHostnameAssociationsParams) ([]HostnameAssociation, error) {

uri := fmt.Sprintf(
"/%s/%s/certificate_authorities/hostname_associations",
rc.Level,
rc.Identifier,
)

res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

var hostnameAssociationsResponse HostnameAssociationsResponse
err = json.Unmarshal(res, &hostnameAssociationsResponse)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return hostnameAssociationsResponse.Result, nil
}

// Replace Hostname Associations
//
// API Reference: https://developers.cloudflare.com/api/operations/client-certificate-for-a-zone-put-hostname-associations
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,
)

res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errMakeRequestError, err)
}

var hostnameAssociationsResponse HostnameAssociationsResponse
err = json.Unmarshal(res, &hostnameAssociationsResponse)
if err != nil {
return []HostnameAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}

return hostnameAssociationsResponse.Result, nil
}
85 changes: 85 additions & 0 deletions certificate_authorities_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package cloudflare

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/stretchr/testify/assert"
)

func TestListCertificateAuthoritiesHostnameAssociations(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
"admin.example.com",
"foobar.example.com"
]
}`)
}

hostnameAssociations := ListCertificateAuthoritiesHostnameAssociationsParams{
MTLSCertificateID: "72ef4d06-4752-4493-a60a-7421470fd585",
}

want := []HostnameAssociation{
"admin.example.com",
"foobar.example.com",
}

mux.HandleFunc("/zones/"+testZoneID+"/certificate_authorities/hostname_associations", handler)

actual, err := client.ListCertificateAuthoritiesHostnameAssociations(context.Background(), testZoneRC, hostnameAssociations)

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

func TestUpdateCertificateAuthoritiesHostnameAssociations(t *testing.T) {
setup()
defer teardown()

handler := func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
w.Header().Set("content-type", "application/json")
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
"admin.example.com",
"foobar.example.com"
]
}`)
}

hostnameAssociations := UpdateCertificateAuthoritiesHostnameAssociationsParams{
Hostnames: []HostnameAssociation{
"admin.example.com",
"foobar.example.com",
},
}

want := []HostnameAssociation{
"admin.example.com",
"foobar.example.com",
}

mux.HandleFunc("/zones/"+testZoneID+"/certificate_authorities/hostname_associations", handler)

actual, err := client.UpdateCertificateAuthoritiesHostnameAssociations(context.Background(), testZoneRC, hostnameAssociations)

if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

0 comments on commit 19c0f6a

Please sign in to comment.