Skip to content

CreateSSL geo_restrictions is not omitted when empty #479

Description

@trjstewart

When using the China Network Enterprise feature and creating a custom SSL Certificate the Geo Key Manager feature is not supported.

Expected behavior

According to the API documentation the geo_restrictions property on the custom_certificates endpoint should be optional when creating a certificate via a POST request. I would expect that the below code has the output also shown below;

var opts cloudflare.ZoneCustomSSLOptions
opts.Certificate = "<certificate>"
opts.PrivateKey = "<private key>"
res, _ := json.Marshal(&opts)
log.Print(string(res))

// {
//   "certificate": "<certificate>",
//   "private_key": "<private key>",
// }

Current behavior

When not providing the geo_restrictions property the output of the object marshal includes it as shown below;

var opts cloudflare.ZoneCustomSSLOptions
opts.Certificate = "<certificate>"
opts.PrivateKey = "<private key>"
res, _ := json.Marshal(&opts)
log.Print(string(res))

// {
//   "certificate": "<certificate>",
//   "private_key": "<private key>",
//   "geo_restrictions": { "label": ""}
// }

This happens because the ZoneCustomSSLGeoRestrictions property of the ZoneCustomSSLOptions struct is itself a struct and therefore never actually empty, causing the omitempty flag in the marshaler to never act as intended.

Possible solution

I believe there are two solutions to this issue, however the more correct solution is a breaking change so I'm going to propose (and also submit PRs for) both.

  1. change the ZoneCustomSSLGeoRestrictions to be a pointer within the ZoneCustomSSLOptions struct. This would cause a breaking change, however is the much cleaner and more forwards compatible way to implement the fix.
  2. implement a custom marshaler for the ZoneCustomSSLOptions struct to handle omiting the ZoneCustomSSLGeoRestrictions property if it's not set. This would be a non-breaking change but also require the maintenance of this custom marshaler if the properties of ZoneCustomSSLGeoRestrictions change.

Steps to reproduce (for bugs)

  1. Setup a Cloudflare Zone with the China Network enabled.
  2. Fill in the placeholders and run the below code snippet;
func main() {
  apiKey := "<api key>"
  apiEmail := "<api email>"
  api, err := cloudflare.New(apiKey, apiEmail)
  if err != nil {
    log.Fatal(err)
  }
  var opts cloudflare.ZoneCustomSSLOptions
  opts.Certificate = "<certificate>"
  opts.PrivateKey = "<private key>"
  api.CreateSSL("<zone id>", opts)
}
  1. you will receive the below error;
error from makeRequest: HTTP status 400: content "{\"success\":false,\"errors\":[{\"code\":1232,\"message\":\"Geo Key Manager is not supported while using the China network\"}],\"messages\":[],\"result\":null}"

Context

As an enterprise customer this is preventing us from managing our Cloudflare infrastructure via Terraform as this library is used within the Cloudflare Terraform Provider.

Your Environment

  • v0.11.7 - still present on master

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions