Repository: cloudflare/cloudflare-go
Pull request #2729 (2024-07-09 21:13:22)

Update Hyperdrive Config Origin model to include new fields
## Description

This PR updates the Hyperdrive API model to include new forthcoming fields. Some downstream changes in https://github.com/cloudflare/terraform-provider-cloudflare will depend on these changes.

## Has your change been tested?

Tests have been updated to account for the new fields.

## Types of changes

What sort of change does your code introduce/modify?

- [ ] Bug fix (non-breaking change which fixes an issue)
- [x] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Checklist:

- [x] My code follows the code style of this project.
- [x] My change requires a change to the documentation.
- [x] I have updated the documentation accordingly.
- [x] I have added tests to cover my changes.
- [x] All new and existing tests passed.
- [x] This change is using publicly documented in [cloudflare/api-schemas](https://github.com/cloudflare/api-schemas) 
      and relies on stable APIs.

[1]: https://help.github.com/articles/closing-issues-using-keywords/


github-actions[bot]: Oops! It looks like no changelog entry is attached to this PR. Please include a release note as described in https://github.com/cloudflare/cloudflare-go/blob/master/docs/changelog-process.md.

Example: 

~~~
```release-note:TYPE
Release note
```
~~~

If you do not require a release note to be included, please add the `workflow/skip-changelog-entry` label.
gitguardian[bot]: #### ⚠️ GitGuardian has uncovered 1 secret following the scan of your pull request.

Please consider investigating the findings and remediating the incidents. Failure to do so may lead to compromising the associated services or software components.

Since your pull request originates from a forked repository, GitGuardian is not able to associate the secrets uncovered with secret incidents on your GitGuardian dashboard.
Skipping this check run and merging your pull request will create secret incidents on your GitGuardian dashboard.

<details>
<summary>🔎 Detected hardcoded secret in your pull request</summary>
<br>

| GitGuardian id | GitGuardian status | Secret                         | Commit           | Filename        |                      |
| -------------- | ------------------ | ------------------------------ | ---------------- | --------------- | -------------------- |
| [-](https://dashboard.gitguardian.com/workspace/249495/incidents/secrets) | - | Generic High Entropy Secret | 23191ffece9a7cba5d5d127f7a6fcace3832e6b0 | hyperdrive_test.go | [View secret](https://github.com/cloudflare/cloudflare-go/commit/23191ffece9a7cba5d5d127f7a6fcace3832e6b0#diff-b28b452525eeb13f8f9d5a62a699d436a0b8a20dc90978abb01f18b188ccc323L239) |



</details>

<details>
<summary>🛠 Guidelines to remediate hardcoded secrets</summary>
<br>

1. Understand the implications of revoking this secret by investigating where it is used in your code.
2. Replace and store your secret safely. [Learn here](https://blog.gitguardian.com/secrets-api-management?utm_source=product&amp;utm_medium=GitHub_checks&amp;utm_campaign=check_run_comment) the best practices.
3. Revoke and [rotate this secret](https://docs.gitguardian.com/secrets-detection/secrets-detection-engine/detectors/generics/generic_high_entropy_secret#revoke-the-secret?utm_source=product&amp;utm_medium=GitHub_checks&amp;utm_campaign=check_run_comment).
4. If possible, [rewrite git history](https://blog.gitguardian.com/rewriting-git-history-cheatsheet?utm_source=product&amp;utm_medium=GitHub_checks&amp;utm_campaign=check_run_comment). Rewriting git history is not a trivial act. You might completely break other contributing developers' workflow and you risk accidentally deleting legitimate data.

To avoid such incidents in the future consider

-   following these [best practices](https://blog.gitguardian.com/secrets-api-management/?utm_source=product&amp;utm_medium=GitHub_checks&amp;utm_campaign=check_run_comment) for managing and storing secrets including API keys and other credentials
-   install [secret detection on pre-commit](https://docs.gitguardian.com/ggshield-docs/integrations/git-hooks/pre-commit?utm_source=product&amp;utm_medium=GitHub_checks&amp;utm_campaign=check_run_comment) to catch secret before it leaves your machine and ease remediation.


</details>

---

<sup>🦉 [GitGuardian](https://dashboard.gitguardian.com/auth/login/?utm_medium=checkruns&amp;utm_source=github&amp;utm_campaign=cr1) detects secrets in your source code to help developers and security teams secure the modern development process. You are seeing this because you or someone else with access to this repository has authorized GitGuardian to scan your pull request.<br/></sup>

Patch:
diff --git a/.changelog/2729.txt b/.changelog/2729.txt
new file mode 100644
index 000000000..f309e1824
--- /dev/null
+++ b/.changelog/2729.txt
@@ -0,0 +1,3 @@
+```release-notes:enhancement
+hyperdrive: Add support for Hyperdrive Over Access attributes
+```
\ No newline at end of file
diff --git a/hyperdrive.go b/hyperdrive.go
index c1ddbc9a5..bd1912efa 100644
--- a/hyperdrive.go
+++ b/hyperdrive.go
@@ -26,13 +26,21 @@ type HyperdriveConfig struct {
 	Caching HyperdriveConfigCaching `json:"caching,omitempty"`
 }
 
+type HyperdriveOriginType string
+
 type HyperdriveConfigOrigin struct {
-	Database string `json:"database,omitempty"`
-	Password string `json:"password"`
-	Host     string `json:"host,omitempty"`
-	Port     int    `json:"port,omitempty"`
-	Scheme   string `json:"scheme,omitempty"`
-	User     string `json:"user,omitempty"`
+	Database       string `json:"database,omitempty"`
+	Host           string `json:"host,omitempty"`
+	Port           int    `json:"port,omitempty"`
+	Scheme         string `json:"scheme,omitempty"`
+	User           string `json:"user,omitempty"`
+	AccessClientID string `json:"access_client_id,omitempty"`
+}
+
+type HyperdriveConfigOriginWithSecrets struct {
+	HyperdriveConfigOrigin
+	Password           string `json:"password"`
+	AccessClientSecret string `json:"access_client_secret,omitempty"`
 }
 
 type HyperdriveConfigCaching struct {
@@ -47,9 +55,9 @@ type HyperdriveConfigListResponse struct {
 }
 
 type CreateHyperdriveConfigParams struct {
-	Name    string                  `json:"name"`
-	Origin  HyperdriveConfigOrigin  `json:"origin"`
-	Caching HyperdriveConfigCaching `json:"caching,omitempty"`
+	Name    string                            `json:"name"`
+	Origin  HyperdriveConfigOriginWithSecrets `json:"origin"`
+	Caching HyperdriveConfigCaching           `json:"caching,omitempty"`
 }
 
 type HyperdriveConfigResponse struct {
@@ -58,10 +66,10 @@ type HyperdriveConfigResponse struct {
 }
 
 type UpdateHyperdriveConfigParams struct {
-	HyperdriveID string                  `json:"-"`
-	Name         string                  `json:"name"`
-	Origin       HyperdriveConfigOrigin  `json:"origin"`
-	Caching      HyperdriveConfigCaching `json:"caching,omitempty"`
+	HyperdriveID string                            `json:"-"`
+	Name         string                            `json:"name"`
+	Origin       HyperdriveConfigOriginWithSecrets `json:"origin"`
+	Caching      HyperdriveConfigCaching           `json:"caching,omitempty"`
 }
 
 type ListHyperdriveConfigParams struct{}


Test patch:
diff --git a/hyperdrive_test.go b/hyperdrive_test.go
index c19378f14..8ff44ebc3 100644
--- a/hyperdrive_test.go
+++ b/hyperdrive_test.go
@@ -12,9 +12,11 @@ import (
 const (
 	testHyperdriveConfigId   = "6b7efc370ea34ded8327fa20698dfe3a"
 	testHyperdriveConfigName = "example-hyperdrive"
+	testAccessClientID       = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.access"
+	testAccessClientSecret   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 )
 
-func testHyperdriveConfig() HyperdriveConfig {
+func testHyperdriveHostAndPortConfig() HyperdriveConfig {
 	return HyperdriveConfig{
 		ID:   testHyperdriveConfigId,
 		Name: testHyperdriveConfigName,
@@ -33,6 +35,25 @@ func testHyperdriveConfig() HyperdriveConfig {
 	}
 }
 
+func testHyperdriveCFAccessConfig() HyperdriveConfig {
+	return HyperdriveConfig{
+		ID:   testHyperdriveConfigId,
+		Name: testHyperdriveConfigName,
+		Origin: HyperdriveConfigOrigin{
+			Database:       "postgres",
+			Host:           "database.example.com",
+			Scheme:         "postgres",
+			User:           "postgres",
+			AccessClientID: testAccessClientID,
+		},
+		Caching: HyperdriveConfigCaching{
+			Disabled:             BoolPtr(false),
+			MaxAge:               30,
+			StaleWhileRevalidate: 15,
+		},
+	}
+}
+
 func TestHyperdriveConfig_List(t *testing.T) {
 	setup()
 	defer teardown()
@@ -72,7 +93,7 @@ func TestHyperdriveConfig_List(t *testing.T) {
 	result, err := client.ListHyperdriveConfigs(context.Background(), AccountIdentifier(testAccountID), ListHyperdriveConfigParams{})
 	if assert.NoError(t, err) {
 		assert.Equal(t, 1, len(result))
-		assert.Equal(t, testHyperdriveConfig(), result[0])
+		assert.Equal(t, testHyperdriveHostAndPortConfig(), result[0])
 	}
 }
 
@@ -119,11 +140,11 @@ func TestHyperdriveConfig_Get(t *testing.T) {
 
 	result, err := client.GetHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), testHyperdriveConfigId)
 	if assert.NoError(t, err) {
-		assert.Equal(t, testHyperdriveConfig(), result)
+		assert.Equal(t, testHyperdriveHostAndPortConfig(), result)
 	}
 }
 
-func TestHyperdriveConfig_Create(t *testing.T) {
+func TestHyperdriveConfig_CreateHostAndPort(t *testing.T) {
 	setup()
 	defer teardown()
 
@@ -166,13 +187,15 @@ func TestHyperdriveConfig_Create(t *testing.T) {
 
 	result, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{
 		Name: "example-hyperdrive",
-		Origin: HyperdriveConfigOrigin{
-			Database: "postgres",
+		Origin: HyperdriveConfigOriginWithSecrets{
+			HyperdriveConfigOrigin: HyperdriveConfigOrigin{
+				Database: "postgres",
+				Host:     "database.example.com",
+				Port:     5432,
+				Scheme:   "postgres",
+				User:     "postgres",
+			},
 			Password: "password",
-			Host:     "database.example.com",
-			Port:     5432,
-			Scheme:   "postgres",
-			User:     "postgres",
 		},
 		Caching: HyperdriveConfigCaching{
 			Disabled:             BoolPtr(false),
@@ -182,7 +205,74 @@ func TestHyperdriveConfig_Create(t *testing.T) {
 	})
 
 	if assert.NoError(t, err) {
-		assert.Equal(t, testHyperdriveConfig(), result)
+		assert.Equal(t, testHyperdriveHostAndPortConfig(), result)
+	}
+}
+
+func TestHyperdriveConfig_CreateCFAccess(t *testing.T) {
+	setup()
+	defer teardown()
+
+	mux.HandleFunc(fmt.Sprintf("/accounts/%s/hyperdrive/configs", testAccountID), func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodPost, r.Method, "Expected method 'POST', got %s", r.Method)
+
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprintf(w, `{
+			"success": true,
+			"errors": [],
+			"messages": [],
+			"result": {
+				"id": "6b7efc370ea34ded8327fa20698dfe3a",
+				"caching": {
+					"disabled": false,
+					"max_age": 30,
+					"stale_while_revalidate": 15
+				},
+				"name": "example-hyperdrive",
+				"origin": {
+					"database": "postgres",
+					"host": "database.example.com",
+					"scheme": "postgres",
+					"user": "postgres",
+					"access_client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.access",
+					"access_client_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
+				}
+			}
+		}`)
+	})
+
+	_, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(""), CreateHyperdriveConfigParams{})
+	if assert.Error(t, err) {
+		assert.Equal(t, ErrMissingAccountID, err)
+	}
+
+	_, err = client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{})
+	if assert.Error(t, err) {
+		assert.Equal(t, ErrMissingHyperdriveConfigName, err)
+	}
+
+	result, err := client.CreateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), CreateHyperdriveConfigParams{
+		Name: "example-hyperdrive",
+		Origin: HyperdriveConfigOriginWithSecrets{
+			HyperdriveConfigOrigin: HyperdriveConfigOrigin{
+				Database:       "postgres",
+				Host:           "database.example.com",
+				Scheme:         "postgres",
+				User:           "postgres",
+				AccessClientID: testAccessClientID,
+			},
+			Password:           "password",
+			AccessClientSecret: testAccessClientSecret,
+		},
+		Caching: HyperdriveConfigCaching{
+			Disabled:             BoolPtr(false),
+			MaxAge:               30,
+			StaleWhileRevalidate: 15,
+		},
+	})
+
+	if assert.NoError(t, err) {
+		assert.Equal(t, testHyperdriveCFAccessConfig(), result)
 	}
 }
 
@@ -259,13 +349,15 @@ func TestHyperdriveConfig_Update(t *testing.T) {
 	result, err := client.UpdateHyperdriveConfig(context.Background(), AccountIdentifier(testAccountID), UpdateHyperdriveConfigParams{
 		HyperdriveID: "6b7efc370ea34ded8327fa20698dfe3a",
 		Name:         "example-hyperdrive",
-		Origin: HyperdriveConfigOrigin{
-			Database: "postgres",
+		Origin: HyperdriveConfigOriginWithSecrets{
+			HyperdriveConfigOrigin: HyperdriveConfigOrigin{
+				Database: "postgres",
+				Host:     "database.example.com",
+				Port:     5432,
+				Scheme:   "postgres",
+				User:     "postgres",
+			},
 			Password: "password",
-			Host:     "database.example.com",
-			Port:     5432,
-			Scheme:   "postgres",
-			User:     "postgres",
 		},
 		Caching: HyperdriveConfigCaching{
 			Disabled:             BoolPtr(false),
@@ -275,6 +367,6 @@ func TestHyperdriveConfig_Update(t *testing.T) {
 	})
 
 	if assert.NoError(t, err) {
-		assert.Equal(t, testHyperdriveConfig(), result)
+		assert.Equal(t, testHyperdriveHostAndPortConfig(), result)
 	}
 }