Skip to content

Commit

Permalink
mtls_certificate: update struct names
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz authored and ivan-section-io committed Jan 12, 2023
1 parent 16d712e commit 06e7d58
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 68 deletions.
146 changes: 85 additions & 61 deletions mtls_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,23 @@ import (
"time"
)

// MTLSAssociationDetails represents the metadata for an existing association between a user-uploaded mTLS certificate and a Cloudflare service.
type MTLSAssociationDetails struct {
// MTLSAssociation represents the metadata for an existing association
// between a user-uploaded mTLS certificate and a Cloudflare service.
type MTLSAssociation struct {
Service string `json:"service"`
Status string `json:"status"`
}

// MTLSAssociationResponse represents the response from the retrieval endpoint for mTLS certificate associations.
// MTLSAssociationResponse represents the response from the retrieval endpoint
// for mTLS certificate associations.
type MTLSAssociationResponse struct {
Response
Result []MTLSAssociationDetails `json:"result"`
Result []MTLSAssociation `json:"result"`
}

// MTLSCertificateDetails represents the metadata for a user-uploaded mTLS certificate.
type MTLSCertificateDetails struct {
// MTLSCertificate represents the metadata for a user-uploaded mTLS
// certificate.
type MTLSCertificate struct {
ID string `json:"id"`
Name string `json:"name"`
Issuer string `json:"issuer"`
Expand All @@ -35,20 +38,23 @@ type MTLSCertificateDetails struct {
ExpiresOn time.Time `json:"expires_on"`
}

// MTLSCertificateResponse represents the response from endpoints relating to retrieving, creating, and deleting an mTLS certificate.
// MTLSCertificateResponse represents the response from endpoints relating to
// retrieving, creating, and deleting an mTLS certificate.
type MTLSCertificateResponse struct {
Response
Result MTLSCertificateDetails `json:"result"`
Result MTLSCertificate `json:"result"`
}

// MTLSCertificatesResponse represents the response from the mTLS certificate list endpoint.
// MTLSCertificatesResponse represents the response from the mTLS certificate
// list endpoint.
type MTLSCertificatesResponse struct {
Response
Result []MTLSCertificateDetails `json:"result"`
Result []MTLSCertificate `json:"result"`
ResultInfo `json:"result_info"`
}

// MTLSCertificateParams represents the data related to the mTLS certificate being uploaded. Name is an optional field.
// MTLSCertificateParams represents the data related to the mTLS certificate
// being uploaded. Name is an optional field.
type CreateMTLSCertificateParams struct {
Name string `json:"name"`
Certificates string `json:"certificates"`
Expand All @@ -64,127 +70,145 @@ type ListMTLSCertificatesParams struct {
CA bool `url:"ca,omitempty"`
}

type ListMTLSCertificateAssociationsParams struct {
CertificateID string
}

var (
ErrMissingCertificateID = errors.New("missing required certificate ID")
)

// ListMTLSCertificates returns a list of all user-uploaded mTLS certificates.
//
// API reference: https://api.cloudflare.com/#mtls-certificate-management-list-mtls-certificates
func (api *API) ListMTLSCertificates(ctx context.Context, rc *ResourceContainer, params ListMTLSCertificatesParams) ([]MTLSCertificateDetails, ResultInfo, error) {
switch {
case rc.Level != AccountRouteLevel:
return []MTLSCertificateDetails{}, ResultInfo{}, ErrRequiredAccountLevelResourceContainer
case rc.Identifier == "":
return []MTLSCertificateDetails{}, ResultInfo{}, ErrMissingAccountID
func (api *API) ListMTLSCertificates(ctx context.Context, rc *ResourceContainer, params ListMTLSCertificatesParams) ([]MTLSCertificate, ResultInfo, error) {
if rc.Level != AccountRouteLevel {
return []MTLSCertificate{}, ResultInfo{}, ErrRequiredAccountLevelResourceContainer
}

if rc.Identifier == "" {
return []MTLSCertificate{}, ResultInfo{}, ErrMissingAccountID
}

uri := fmt.Sprintf("/accounts/%s/mtls_certificates", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, params)
if err != nil {
return []MTLSCertificateDetails{}, ResultInfo{}, err
return []MTLSCertificate{}, ResultInfo{}, err
}
var r MTLSCertificatesResponse
if err := json.Unmarshal(res, &r); err != nil {
return []MTLSCertificateDetails{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
return []MTLSCertificate{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, r.ResultInfo, err
}

// GetMTLSCertificateDetails returns the metadata associated with a user-uploaded mTLS certificate.
// GetMTLSCertificate returns the metadata associated with a user-uploaded mTLS
// certificate.
//
// API reference: https://api.cloudflare.com/#mtls-certificate-management-get-mtls-certificate
func (api *API) GetMTLSCertificateDetails(ctx context.Context, rc *ResourceContainer, certificateID string) (MTLSCertificateDetails, error) {
switch {
case rc.Level != AccountRouteLevel:
return MTLSCertificateDetails{}, ErrRequiredAccountLevelResourceContainer
case rc.Identifier == "":
return MTLSCertificateDetails{}, ErrMissingAccountID
case certificateID == "":
return MTLSCertificateDetails{}, ErrMissingCertificateID
func (api *API) GetMTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) (MTLSCertificate, error) {
if rc.Level != AccountRouteLevel {
return MTLSCertificate{}, ErrRequiredAccountLevelResourceContainer
}

if rc.Identifier == "" {
return MTLSCertificate{}, ErrMissingAccountID
}

if certificateID == "" {
return MTLSCertificate{}, ErrMissingCertificateID
}

uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s", rc.Identifier, certificateID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return MTLSCertificateDetails{}, err
return MTLSCertificate{}, err
}
var r MTLSCertificateResponse
if err := json.Unmarshal(res, &r); err != nil {
return MTLSCertificateDetails{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
return MTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}

// ListMTLSCertificateAssociations returns a list of all existing associations between the mTLS certificate and Cloudflare services.
// ListMTLSCertificateAssociations returns a list of all existing associations
// between the mTLS certificate and Cloudflare services.
//
// API reference: https://api.cloudflare.com/#mtls-certificate-management-list-mtls-certificate-associations
func (api *API) ListMTLSCertificateAssociations(ctx context.Context, rc *ResourceContainer, certificateID string) ([]MTLSAssociationDetails, error) {
switch {
case rc.Level != AccountRouteLevel:
return []MTLSAssociationDetails{}, ErrRequiredAccountLevelResourceContainer
case rc.Identifier == "":
return []MTLSAssociationDetails{}, ErrMissingAccountID
case certificateID == "":
return []MTLSAssociationDetails{}, ErrMissingCertificateID
func (api *API) ListMTLSCertificateAssociations(ctx context.Context, rc *ResourceContainer, params ListMTLSCertificateAssociationsParams) ([]MTLSAssociation, error) {
if rc.Level != AccountRouteLevel {
return []MTLSAssociation{}, ErrRequiredAccountLevelResourceContainer
}

if rc.Identifier == "" {
return []MTLSAssociation{}, ErrMissingAccountID
}

if params.CertificateID == "" {
return []MTLSAssociation{}, ErrMissingCertificateID
}

uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s/associations", rc.Identifier, certificateID)
uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s/associations", rc.Identifier, params.CertificateID)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return []MTLSAssociationDetails{}, err
return []MTLSAssociation{}, err
}
var r MTLSAssociationResponse
if err := json.Unmarshal(res, &r); err != nil {
return []MTLSAssociationDetails{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
return []MTLSAssociation{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}

// CreateMTLSCertificate will create the provided certificate for use with mTLS enabled Cloudflare services.
// CreateMTLSCertificate will create the provided certificate for use with mTLS
// enabled Cloudflare services.
//
// API reference: https://api.cloudflare.com/#mtls-certificate-management-upload-mtls-certificate
func (api *API) CreateMTLSCertificate(ctx context.Context, rc *ResourceContainer, params CreateMTLSCertificateParams) (MTLSCertificateDetails, error) {
switch {
case rc.Level != AccountRouteLevel:
return MTLSCertificateDetails{}, ErrRequiredAccountLevelResourceContainer
case rc.Identifier == "":
return MTLSCertificateDetails{}, ErrMissingAccountID
func (api *API) CreateMTLSCertificate(ctx context.Context, rc *ResourceContainer, params CreateMTLSCertificateParams) (MTLSCertificate, error) {
if rc.Level != AccountRouteLevel {
return MTLSCertificate{}, ErrRequiredAccountLevelResourceContainer
}

if rc.Identifier == "" {
return MTLSCertificate{}, ErrMissingAccountID
}

uri := fmt.Sprintf("/accounts/%s/mtls_certificates", rc.Identifier)
res, err := api.makeRequestContext(ctx, http.MethodPost, uri, params)
if err != nil {
return MTLSCertificateDetails{}, err
return MTLSCertificate{}, err
}
var r MTLSCertificateResponse
if err := json.Unmarshal(res, &r); err != nil {
return MTLSCertificateDetails{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
return MTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}

// DeleteMTLSCertificate will delete the specified mTLS certificate.
//
// API reference: https://api.cloudflare.com/#mtls-certificate-management-delete-mtls-certificate
func (api *API) DeleteMTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) (MTLSCertificateDetails, error) {
switch {
case rc.Level != AccountRouteLevel:
return MTLSCertificateDetails{}, ErrRequiredAccountLevelResourceContainer
case rc.Identifier == "":
return MTLSCertificateDetails{}, ErrMissingAccountID
case certificateID == "":
return MTLSCertificateDetails{}, ErrMissingCertificateID
func (api *API) DeleteMTLSCertificate(ctx context.Context, rc *ResourceContainer, certificateID string) (MTLSCertificate, error) {
if rc.Level != AccountRouteLevel {
return MTLSCertificate{}, ErrRequiredAccountLevelResourceContainer
}

if rc.Identifier == "" {
return MTLSCertificate{}, ErrMissingAccountID
}

if certificateID == "" {
return MTLSCertificate{}, ErrMissingCertificateID
}

uri := fmt.Sprintf("/accounts/%s/mtls_certificates/%s", rc.Identifier, certificateID)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return MTLSCertificateDetails{}, err
return MTLSCertificate{}, err
}
var r MTLSCertificateResponse
if err := json.Unmarshal(res, &r); err != nil {
return MTLSCertificateDetails{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
return MTLSCertificate{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
16 changes: 9 additions & 7 deletions mtls_certificates_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestGetMTLSCertificate(t *testing.T) {
mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/mtls_certificates/2458ce5a-0c35-4c7f-82c7-8e9487d3ff60", handler)
expiresOn, _ := time.Parse(time.RFC3339, "2122-10-29T16:59:47Z")
uploadedOn, _ := time.Parse(time.RFC3339, "2022-11-22T17:32:30.467938Z")
want := MTLSCertificateDetails{
want := MTLSCertificate{
ID: "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60",
Name: "example_ca_cert_5",
Issuer: "O=Example Inc.,L=California,ST=San Francisco,C=US",
Expand All @@ -49,7 +49,7 @@ func TestGetMTLSCertificate(t *testing.T) {
ExpiresOn: expiresOn,
}

actual, err := client.GetMTLSCertificateDetails(context.Background(), AccountIdentifier(testAccountID), "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60")
actual, err := client.GetMTLSCertificate(context.Background(), AccountIdentifier(testAccountID), "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60")
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestListMTLSCertificates(t *testing.T) {
mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/mtls_certificates", handler)
expiresOn, _ := time.Parse(time.RFC3339, "2122-10-29T16:59:47Z")
uploadedOn, _ := time.Parse(time.RFC3339, "2022-11-22T17:32:30.467938Z")
want := []MTLSCertificateDetails{
want := []MTLSCertificate{
{
ID: "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60",
Name: "example_ca_cert_5",
Expand Down Expand Up @@ -131,14 +131,16 @@ func TestListCertificateAssociations(t *testing.T) {
}

mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/mtls_certificates/2458ce5a-0c35-4c7f-82c7-8e9487d3ff60/associations", handler)
want := []MTLSAssociationDetails{
want := []MTLSAssociation{
{
Service: "gateway",
Status: "pending_deployment",
},
}

actual, err := client.ListMTLSCertificateAssociations(context.Background(), AccountIdentifier(testAccountID), "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60")
actual, err := client.ListMTLSCertificateAssociations(context.Background(), AccountIdentifier(testAccountID), ListMTLSCertificateAssociationsParams{
CertificateID: "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60",
})
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
Expand Down Expand Up @@ -172,7 +174,7 @@ func TestUploadMTLSCertificate(t *testing.T) {
mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/mtls_certificates", handler)
expiresOn, _ := time.Parse(time.RFC3339, "2122-10-29T16:59:47Z")
uploadedOn, _ := time.Parse(time.RFC3339, "2022-11-22T17:32:30.467938Z")
want := MTLSCertificateDetails{
want := MTLSCertificate{
ID: "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60",
Name: "example_ca_cert_5",
Issuer: "O=Example Inc.,L=California,ST=San Francisco,C=US",
Expand Down Expand Up @@ -224,7 +226,7 @@ func TestDeleteMTLSCertificate(t *testing.T) {
mux.HandleFunc("/accounts/01a7362d577a6c3019a474fd6f485823/mtls_certificates/2458ce5a-0c35-4c7f-82c7-8e9487d3ff60", handler)
expiresOn, _ := time.Parse(time.RFC3339, "2122-10-29T16:59:47Z")
uploadedOn, _ := time.Parse(time.RFC3339, "2022-11-22T17:32:30.467938Z")
want := MTLSCertificateDetails{
want := MTLSCertificate{
ID: "2458ce5a-0c35-4c7f-82c7-8e9487d3ff60",
Name: "example_ca_cert_5",
Issuer: "O=Example Inc.,L=California,ST=San Francisco,C=US",
Expand Down

0 comments on commit 06e7d58

Please sign in to comment.