Repository: cloudflare/cloudflare-go
Pull request #3577 (2024-11-07 11:29:23)

Fix disabling redact PII on logging settings
## Description

Trying to disable redact PII on the logging
settings would have no effect because the logging
settings object would be marshaled without the
`redact_pii` field which result in the default
value being used, which is `true` in this case.

To fix this, the `redact_pii` is now included
even set to `false` when the logging settings
object is marshaled.

## Has your change been tested?

Besides the automated test, the change was
manually validated by using the lib to call
the API to make the change that was causing
problems.

## Types of changes

What sort of change does your code introduce/modify?

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] 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.
- [ ] My change requires a change to the documentation.
- [ ] 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/



Patch:
diff --git a/.changelog/3578.txt b/.changelog/3578.txt
deleted file mode 100644
index 763293814..000000000
--- a/.changelog/3578.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-```release-note:dependency
-deps: bumps goreleaser/goreleaser-action from 6.0.0 to 6.1.0
-```
diff --git a/.changelog/3584.txt b/.changelog/3584.txt
deleted file mode 100644
index 0072c2bb0..000000000
--- a/.changelog/3584.txt
+++ /dev/null
@@ -1,3 +0,0 @@
-```release-note:dependency
-deps: bumps golang.org/x/time from 0.7.0 to 0.8.0
-```
diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml
index c123c38b9..7abfac5e1 100644
--- a/.github/workflows/release.yml
+++ b/.github/workflows/release.yml
@@ -17,7 +17,7 @@ jobs:
         with:
           go-version-file: 'go.mod'
       - name: Run GoReleaser
-        uses: goreleaser/goreleaser-action@v6.1.0
+        uses: goreleaser/goreleaser-action@v6.0.0
         with:
           version: latest
           args: release --clean
diff --git a/go.mod b/go.mod
index fb381dd2d..1b94004ad 100644
--- a/go.mod
+++ b/go.mod
@@ -9,7 +9,7 @@ require (
 	github.com/stretchr/testify v1.9.0
 	github.com/urfave/cli/v2 v2.27.5
 	golang.org/x/net v0.30.0
-	golang.org/x/time v0.8.0
+	golang.org/x/time v0.7.0
 )
 
 require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
diff --git a/go.sum b/go.sum
index 027fa4e10..3fc52d4be 100644
--- a/go.sum
+++ b/go.sum
@@ -43,8 +43,8 @@ golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4=
 golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU=
 golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
 golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
-golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
-golang.org/x/time v0.8.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
+golang.org/x/time v0.7.0 h1:ntUhktv3OPE6TgYxXWv9vKvUSJyIFJlyohwbkEwPrKQ=
+golang.org/x/time v0.7.0/go.mod h1:3BpzKBy/shNhVucY/MWOyx10tF3SFh9QdLuxbVysPQM=
 golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
diff --git a/teams_accounts.go b/teams_accounts.go
index 91e1e6a6c..036605588 100644
--- a/teams_accounts.go
+++ b/teams_accounts.go
@@ -132,7 +132,7 @@ type TeamsAccountLoggingConfiguration struct {
 
 type TeamsLoggingSettings struct {
 	LoggingSettingsByRuleType map[TeamsRuleType]TeamsAccountLoggingConfiguration `json:"settings_by_rule_type"`
-	RedactPii                 bool                                               `json:"redact_pii,omitempty"`
+	RedactPii                 *bool                                              `json:"redact_pii,omitempty"`
 }
 
 type TeamsDeviceSettings struct {


Test patch:
diff --git a/teams_accounts_test.go b/teams_accounts_test.go
index c681e9fef..6c58caac4 100644
--- a/teams_accounts_test.go
+++ b/teams_accounts_test.go
@@ -2,11 +2,13 @@ package cloudflare
 
 import (
 	"context"
+	"encoding/json"
 	"fmt"
 	"net/http"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
+	"github.com/stretchr/testify/require"
 )
 
 func TestTeamsAccount(t *testing.T) {
@@ -112,7 +114,7 @@ func TestTeamsAccountConfiguration(t *testing.T) {
 			Antivirus: &TeamsAntivirus{
 				EnabledDownloadPhase: true,
 				NotificationSettings: &TeamsNotificationSettings{
-					Enabled:    &trueValue,
+					Enabled:    BoolPtr(true),
 					Message:    "msg",
 					SupportURL: "https://hi.com",
 				},
@@ -231,7 +233,7 @@ func TestTeamsAccountGetLoggingConfiguration(t *testing.T) {
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, actual, TeamsLoggingSettings{
-			RedactPii: true,
+			RedactPii: BoolPtr(true),
 			LoggingSettingsByRuleType: map[TeamsRuleType]TeamsAccountLoggingConfiguration{
 				TeamsDnsRuleType: {LogAll: false, LogBlocks: true},
 			},
@@ -257,7 +259,7 @@ func TestTeamsAccountUpdateLoggingConfiguration(t *testing.T) {
 	mux.HandleFunc("/accounts/"+testAccountID+"/gateway/logging", handler)
 
 	actual, err := client.TeamsAccountUpdateLoggingConfiguration(context.Background(), testAccountID, TeamsLoggingSettings{
-		RedactPii: true,
+		RedactPii: BoolPtr(true),
 		LoggingSettingsByRuleType: map[TeamsRuleType]TeamsAccountLoggingConfiguration{
 			TeamsDnsRuleType: {
 				LogAll:    false,
@@ -274,7 +276,7 @@ func TestTeamsAccountUpdateLoggingConfiguration(t *testing.T) {
 
 	if assert.NoError(t, err) {
 		assert.Equal(t, actual, TeamsLoggingSettings{
-			RedactPii: true,
+			RedactPii: BoolPtr(true),
 			LoggingSettingsByRuleType: map[TeamsRuleType]TeamsAccountLoggingConfiguration{
 				TeamsDnsRuleType:  {LogAll: false, LogBlocks: true},
 				TeamsHttpRuleType: {LogAll: true, LogBlocks: false},
@@ -284,6 +286,54 @@ func TestTeamsAccountUpdateLoggingConfiguration(t *testing.T) {
 	}
 }
 
+func TestTeamsAccountDisableRedactPIILoggingConfiguration(t *testing.T) {
+	setup()
+	defer teardown()
+
+	handler := func(w http.ResponseWriter, r *http.Request) {
+		assert.Equal(t, http.MethodPut, r.Method, "Expected method 'PUT', got %s", r.Method)
+
+		request := readJson(t, r)
+		require.False(t, request["redact_pii"].(bool))
+
+		w.Header().Set("content-type", "application/json")
+		fmt.Fprintf(w, `{
+			"success": true,
+			"errors": [],
+			"messages": [],
+			"result": {"settings_by_rule_type":{"dns":{"log_all":false,"log_blocks":true}, "http":{"log_all":true,"log_blocks":false}, "l4": {"log_all": false, "log_blocks": true}},"redact_pii":true}
+		}`)
+	}
+
+	mux.HandleFunc("/accounts/"+testAccountID+"/gateway/logging", handler)
+
+	_, err := client.TeamsAccountUpdateLoggingConfiguration(context.Background(), testAccountID, TeamsLoggingSettings{
+		RedactPii: BoolPtr(false),
+		LoggingSettingsByRuleType: map[TeamsRuleType]TeamsAccountLoggingConfiguration{
+			TeamsDnsRuleType: {
+				LogAll:    false,
+				LogBlocks: true,
+			},
+			TeamsHttpRuleType: {
+				LogAll: true,
+			},
+			TeamsL4RuleType: {
+				LogBlocks: true,
+			},
+		},
+	})
+	require.NoError(t, err)
+}
+
+func readJson(t *testing.T, r *http.Request) map[string]interface{} {
+	var result map[string]interface{}
+	decoder := json.NewDecoder(r.Body)
+	defer r.Body.Close()
+	err := decoder.Decode(&result)
+	require.NoError(t, err)
+	return result
+}
+
 func TestTeamsAccountGetDeviceConfiguration(t *testing.T) {
 	setup()
 	defer teardown()