Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add per-hostname tls settings api and tests #1356

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions per_hostname_tls_settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package cloudflare

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)

// HostnameTLSSetting represents the metadata for a user-created tls setting.
type HostnameTLSSetting struct {
Hostname string `json:"hostname"`
Value interface{} `json:"value"`
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
Status string `json:"status"`
CreatedAt time.Time `json:"created_at"`
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
UpdatedAt time.Time `json:"updated_at"`
}

// HostnameTLSSettingResponse represents the response from the PUT and DELETE endpoints for per-hostname tls settings.
type HostnameTLSSettingResponse struct {
Response
Result HostnameTLSSetting `json:"result"`
}

// HostnameTLSSettingsResponse represents the response from the retrieval endpoint for per-hostname tls settings.
type HostnameTLSSettingsResponse struct {
Response
Result []HostnameTLSSetting `json:"result"`
ResultInfo `json:"result_info"`
}

// EditHostnameTLSSettingParams represents the data related to the per-hostname tls setting being edited.
type EditHostnameTLSSettingParams struct {
Value interface{} `json:"value"`
}

type ListHostnameTLSSettingsParams struct {
PaginationOptions
Limit int `url:"limit,omitempty"`
Offset int `url:"offset,omitempty"`
Hostname []string `url:"hostname,omitempty"`
}

var (
ErrMissingHostnameTLSSettingName = errors.New("tls setting name required but missing")
)

// ListHostnameTLSSettings returns a list of all user-created tls setting values for the specified setting and hostnames.
//
// API reference: https://developers.cloudflare.com/api/operations/per-hostname-tls-settings-list
func (api *API) ListHostnameTLSSettings(ctx context.Context, rc *ResourceContainer, setting string, params ListHostnameTLSSettingsParams) ([]HostnameTLSSetting, ResultInfo, error) {
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
if rc.Identifier == "" {
return []HostnameTLSSetting{}, ResultInfo{}, ErrMissingZoneID
}
if setting == "" {
return []HostnameTLSSetting{}, ResultInfo{}, ErrMissingHostnameTLSSettingName
}

uri := fmt.Sprintf("/zones/%s/hostnames/settings/%s", rc.Identifier, setting)
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, params)
if err != nil {
return []HostnameTLSSetting{}, ResultInfo{}, err
}
var r HostnameTLSSettingsResponse
if err := json.Unmarshal(res, &r); err != nil {
return []HostnameTLSSetting{}, ResultInfo{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, r.ResultInfo, err
}

// EditHostnameTLSSetting will update the per-hostname tls setting for the specified hostname.
//
// API reference: https://developers.cloudflare.com/api/operations/per-hostname-tls-settings-put
func (api *API) EditHostnameTLSSetting(ctx context.Context, rc *ResourceContainer, setting, hostname string, params EditHostnameTLSSettingParams) (HostnameTLSSetting, error) {
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
if rc.Identifier == "" {
return HostnameTLSSetting{}, ErrMissingZoneID
}
if setting == "" {
return HostnameTLSSetting{}, ErrMissingHostnameTLSSettingName
}

uri := fmt.Sprintf("/zones/%s/hostnames/settings/%s/%s", rc.Identifier, setting, hostname)
res, err := api.makeRequestContext(ctx, http.MethodPut, uri, params)
if err != nil {
return HostnameTLSSetting{}, err
}
var r HostnameTLSSettingResponse
if err := json.Unmarshal(res, &r); err != nil {
return HostnameTLSSetting{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}

// DeleteHostnameTLSSetting will delete the specified per-hostname tls setting.
//
// API reference: https://developers.cloudflare.com/api/operations/per-hostname-tls-settings-delete
func (api *API) DeleteHostnameTLSSetting(ctx context.Context, rc *ResourceContainer, setting, hostname string) (HostnameTLSSetting, error) {
jacobbednarz marked this conversation as resolved.
Show resolved Hide resolved
if rc.Identifier == "" {
return HostnameTLSSetting{}, ErrMissingZoneID
}
if setting == "" {
return HostnameTLSSetting{}, ErrMissingHostnameTLSSettingName
}

uri := fmt.Sprintf("/zones/%s/hostnames/settings/%s/%s", rc.Identifier, setting, hostname)
res, err := api.makeRequestContext(ctx, http.MethodDelete, uri, nil)
if err != nil {
return HostnameTLSSetting{}, err
}
var r HostnameTLSSettingResponse
if err := json.Unmarshal(res, &r); err != nil {
return HostnameTLSSetting{}, fmt.Errorf("%s: %w", errUnmarshalError, err)
}
return r.Result, nil
}
202 changes: 202 additions & 0 deletions per_hostname_tls_settings_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
package cloudflare

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

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

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

handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"hostname": "app.example.com",
"value": "1.2",
"status": "active",
"created_at": "2023-07-26T21:12:55.56942Z",
"updated_at": "2023-07-31T22:06:44.739794Z"
}
],
"result_info": {
"page": 1,
"per_page": 50,
"count": 1,
"total_count": 1,
"total_pages": 1
}
}`)
}

mux.HandleFunc("/zones/"+testZoneID+"/hostnames/settings/min_tls_version", handler)
createdAt, _ := time.Parse(time.RFC3339, "2023-07-26T21:12:55.56942Z")
updatedAt, _ := time.Parse(time.RFC3339, "2023-07-31T22:06:44.739794Z")
want := []HostnameTLSSetting{
{
Hostname: "app.example.com",
Value: "1.2",
Status: "active",
CreatedAt: createdAt,
UpdatedAt: updatedAt,
},
}

actual, _, err := client.ListHostnameTLSSettings(context.Background(), ZoneIdentifier(testZoneID), "min_tls_version", ListHostnameTLSSettingsParams{})
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

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

handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
{
"hostname": "app.example.com",
"value": [
"AES128-GCM-SHA256",
"ECDHE-RSA-AES128-GCM-SHA256"
],
"status": "active",
"created_at": "2023-07-26T21:12:55.56942Z",
"updated_at": "2023-07-31T22:06:44.739794Z"
}
],
"result_info": {
"page": 1,
"per_page": 50,
"count": 1,
"total_count": 1,
"total_pages": 1
}
}`)
}

mux.HandleFunc("/zones/"+testZoneID+"/hostnames/settings/ciphers", handler)
createdAt, _ := time.Parse(time.RFC3339, "2023-07-26T21:12:55.56942Z")
updatedAt, _ := time.Parse(time.RFC3339, "2023-07-31T22:06:44.739794Z")

strs := []string{"AES128-GCM-SHA256", "ECDHE-RSA-AES128-GCM-SHA256"}
cipherssuites := make([]interface{}, len(strs))
for i, s := range strs {
cipherssuites[i] = s
}
want := []HostnameTLSSetting{
{
Hostname: "app.example.com",
Value: cipherssuites,
Status: "active",
CreatedAt: createdAt,
UpdatedAt: updatedAt,
},
}

actual, _, err := client.ListHostnameTLSSettings(context.Background(), ZoneIdentifier(testZoneID), "ciphers", ListHostnameTLSSettingsParams{})
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

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

handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"hostname": "app.example.com",
"value": [
"AES128-GCM-SHA256",
"ECDHE-RSA-AES128-GCM-SHA256"
],
"status": "active",
"created_at": "2023-07-26T21:12:55.56942Z",
"updated_at": "2023-07-31T22:06:44.739794Z"
}
}`)
}

mux.HandleFunc("/zones/"+testZoneID+"/hostnames/settings/ciphers/app.example.com", handler)
createdAt, _ := time.Parse(time.RFC3339, "2023-07-26T21:12:55.56942Z")
updatedAt, _ := time.Parse(time.RFC3339, "2023-07-31T22:06:44.739794Z")

strs := []string{"AES128-GCM-SHA256", "ECDHE-RSA-AES128-GCM-SHA256"}
cipherssuites := make([]interface{}, len(strs))
for i, s := range strs {
cipherssuites[i] = s
}
want := HostnameTLSSetting{
Hostname: "app.example.com",
Value: cipherssuites,
Status: "active",
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}

params := EditHostnameTLSSettingParams{
Value: []string{"AES128-GCM-SHA256", "ECDHE-RSA-AES128-GCM-SHA256"},
}
actual, err := client.EditHostnameTLSSetting(context.Background(), ZoneIdentifier(testZoneID), "ciphers", "app.example.com", params)
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}

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

handler := func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("content-type", "application/json")
fmt.Fprint(w, `{
"success": true,
"errors": [],
"messages": [],
"result": {
"hostname": "app.example.com",
"value": "",
"status": "active",
"created_at": "2023-07-26T21:12:55.56942Z",
"updated_at": "2023-07-31T22:06:44.739794Z"
}
}`)
}

mux.HandleFunc("/zones/"+testZoneID+"/hostnames/settings/ciphers/app.example.com", handler)
createdAt, _ := time.Parse(time.RFC3339, "2023-07-26T21:12:55.56942Z")
updatedAt, _ := time.Parse(time.RFC3339, "2023-07-31T22:06:44.739794Z")
want := HostnameTLSSetting{
Hostname: "app.example.com",
Value: "",
Status: "active",
CreatedAt: createdAt,
UpdatedAt: updatedAt,
}

actual, err := client.DeleteHostnameTLSSetting(context.Background(), ZoneIdentifier(testZoneID), "ciphers", "app.example.com")
if assert.NoError(t, err) {
assert.Equal(t, want, actual)
}
}
Loading