ssl.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package cloudflare
  2. import (
  3. "encoding/json"
  4. "time"
  5. "github.com/pkg/errors"
  6. )
  7. // ZoneCustomSSL represents custom SSL certificate metadata.
  8. type ZoneCustomSSL struct {
  9. ID string `json:"id"`
  10. Hosts []string `json:"hosts"`
  11. Issuer string `json:"issuer"`
  12. Signature string `json:"signature"`
  13. Status string `json:"status"`
  14. BundleMethod string `json:"bundle_method"`
  15. GeoRestrictions ZoneCustomSSLGeoRestrictions `json:"geo_restrictions"`
  16. ZoneID string `json:"zone_id"`
  17. UploadedOn time.Time `json:"uploaded_on"`
  18. ModifiedOn time.Time `json:"modified_on"`
  19. ExpiresOn time.Time `json:"expires_on"`
  20. Priority int `json:"priority"`
  21. KeylessServer KeylessSSL `json:"keyless_server"`
  22. }
  23. // ZoneCustomSSLGeoRestrictions represents the parameter to create or update
  24. // geographic restrictions on a custom ssl certificate.
  25. type ZoneCustomSSLGeoRestrictions struct {
  26. Label string `json:"label"`
  27. }
  28. // zoneCustomSSLResponse represents the response from the zone SSL details endpoint.
  29. type zoneCustomSSLResponse struct {
  30. Response
  31. Result ZoneCustomSSL `json:"result"`
  32. }
  33. // zoneCustomSSLsResponse represents the response from the zone SSL list endpoint.
  34. type zoneCustomSSLsResponse struct {
  35. Response
  36. Result []ZoneCustomSSL `json:"result"`
  37. }
  38. // ZoneCustomSSLOptions represents the parameters to create or update an existing
  39. // custom SSL configuration.
  40. type ZoneCustomSSLOptions struct {
  41. Certificate string `json:"certificate"`
  42. PrivateKey string `json:"private_key"`
  43. BundleMethod string `json:"bundle_method,omitempty"`
  44. GeoRestrictions ZoneCustomSSLGeoRestrictions `json:"geo_restrictions,omitempty"`
  45. Type string `json:"type,omitempty"`
  46. }
  47. // ZoneCustomSSLPriority represents a certificate's ID and priority. It is a
  48. // subset of ZoneCustomSSL used for patch requests.
  49. type ZoneCustomSSLPriority struct {
  50. ID string `json:"ID"`
  51. Priority int `json:"priority"`
  52. }
  53. // CreateSSL allows you to add a custom SSL certificate to the given zone.
  54. //
  55. // API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-create-ssl-configuration
  56. func (api *API) CreateSSL(zoneID string, options ZoneCustomSSLOptions) (ZoneCustomSSL, error) {
  57. uri := "/zones/" + zoneID + "/custom_certificates"
  58. res, err := api.makeRequest("POST", uri, options)
  59. if err != nil {
  60. return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError)
  61. }
  62. var r zoneCustomSSLResponse
  63. if err := json.Unmarshal(res, &r); err != nil {
  64. return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError)
  65. }
  66. return r.Result, nil
  67. }
  68. // ListSSL lists the custom certificates for the given zone.
  69. //
  70. // API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-list-ssl-configurations
  71. func (api *API) ListSSL(zoneID string) ([]ZoneCustomSSL, error) {
  72. uri := "/zones/" + zoneID + "/custom_certificates"
  73. res, err := api.makeRequest("GET", uri, nil)
  74. if err != nil {
  75. return nil, errors.Wrap(err, errMakeRequestError)
  76. }
  77. var r zoneCustomSSLsResponse
  78. if err := json.Unmarshal(res, &r); err != nil {
  79. return nil, errors.Wrap(err, errUnmarshalError)
  80. }
  81. return r.Result, nil
  82. }
  83. // SSLDetails returns the configuration details for a custom SSL certificate.
  84. //
  85. // API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-ssl-configuration-details
  86. func (api *API) SSLDetails(zoneID, certificateID string) (ZoneCustomSSL, error) {
  87. uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID
  88. res, err := api.makeRequest("GET", uri, nil)
  89. if err != nil {
  90. return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError)
  91. }
  92. var r zoneCustomSSLResponse
  93. if err := json.Unmarshal(res, &r); err != nil {
  94. return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError)
  95. }
  96. return r.Result, nil
  97. }
  98. // UpdateSSL updates (replaces) a custom SSL certificate.
  99. //
  100. // API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-update-ssl-configuration
  101. func (api *API) UpdateSSL(zoneID, certificateID string, options ZoneCustomSSLOptions) (ZoneCustomSSL, error) {
  102. uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID
  103. res, err := api.makeRequest("PATCH", uri, options)
  104. if err != nil {
  105. return ZoneCustomSSL{}, errors.Wrap(err, errMakeRequestError)
  106. }
  107. var r zoneCustomSSLResponse
  108. if err := json.Unmarshal(res, &r); err != nil {
  109. return ZoneCustomSSL{}, errors.Wrap(err, errUnmarshalError)
  110. }
  111. return r.Result, nil
  112. }
  113. // ReprioritizeSSL allows you to change the priority (which is served for a given
  114. // request) of custom SSL certificates associated with the given zone.
  115. //
  116. // API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-re-prioritize-ssl-certificates
  117. func (api *API) ReprioritizeSSL(zoneID string, p []ZoneCustomSSLPriority) ([]ZoneCustomSSL, error) {
  118. uri := "/zones/" + zoneID + "/custom_certificates/prioritize"
  119. params := struct {
  120. Certificates []ZoneCustomSSLPriority `json:"certificates"`
  121. }{
  122. Certificates: p,
  123. }
  124. res, err := api.makeRequest("PUT", uri, params)
  125. if err != nil {
  126. return nil, errors.Wrap(err, errMakeRequestError)
  127. }
  128. var r zoneCustomSSLsResponse
  129. if err := json.Unmarshal(res, &r); err != nil {
  130. return nil, errors.Wrap(err, errUnmarshalError)
  131. }
  132. return r.Result, nil
  133. }
  134. // DeleteSSL deletes a custom SSL certificate from the given zone.
  135. //
  136. // API reference: https://api.cloudflare.com/#custom-ssl-for-a-zone-delete-an-ssl-certificate
  137. func (api *API) DeleteSSL(zoneID, certificateID string) error {
  138. uri := "/zones/" + zoneID + "/custom_certificates/" + certificateID
  139. if _, err := api.makeRequest("DELETE", uri, nil); err != nil {
  140. return errors.Wrap(err, errMakeRequestError)
  141. }
  142. return nil
  143. }