Skip to content

Commit

Permalink
linting and semgrep addressed
Browse files Browse the repository at this point in the history
  • Loading branch information
wiremanb committed Dec 6, 2023
1 parent 1487a40 commit a217c8d
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 67 deletions.
11 changes: 6 additions & 5 deletions page_shield.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
"net/http"

"github.com/goccy/go-json"
)

// PageShield represents the page shield object minus any timestamps

Check failure on line 11 in page_shield.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 11 in page_shield.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
type PageShield struct {
Enabled bool `json:"enabled"`
UseCloudflareReportingEndpoint bool `json:"use_cloudflare_reporting_endpoint"`
UseConnectionURLPath bool `json:"use_connection_url_path"`
Enabled *bool `json:"enabled,omitempty"`
UseCloudflareReportingEndpoint *bool `json:"use_cloudflare_reporting_endpoint,omitempty"`
UseConnectionURLPath *bool `json:"use_connection_url_path,omitempty"`
}

// PageShieldSettings represents the page shield settings for a zone

Check failure on line 18 in page_shield.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 18 in page_shield.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
Expand All @@ -20,7 +21,7 @@ type PageShieldSettings struct {
UpdatedAt string `json:"updated_at"`
}

// PageShieldSettingsResponse represents the response from the page shield settings endpoint
// PageShieldSettingsResponse represents the response from the page shield settings endpoint.
type PageShieldSettingsResponse struct {
PageShield PageShieldSettings `json:"result"`
Response
Expand Down
11 changes: 6 additions & 5 deletions page_shield_connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,39 @@ package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
"net/http"

"github.com/goccy/go-json"
)

// ListPageShieldConnectionsParams represents parameters for a page shield connection request

Check failure on line 11 in page_shield_connections.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)

Check failure on line 11 in page_shield_connections.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
type ListPageShieldConnectionsParams struct {
Direction string `json:"direction"`
ExcludeCdnCgi bool `json:"exclude_cdn_cgi"`
ExcludeCdnCgi *bool `json:"exclude_cdn_cgi,omitempty"`
ExcludeUrls string `json:"exclude_urls"`
Export string `json:"export"`
Hosts string `json:"hosts"`
OrderBy string `json:"order_by"`
Page string `json:"page"`
PageURL string `json:"page_url"`
PerPage int `json:"per_page"`
PrioritizeMalicious bool `json:"prioritize_malicious"`
PrioritizeMalicious *bool `json:"prioritize_malicious,omitempty"`
Status string `json:"status"`
URLs string `json:"urls"`
}

// PageShieldConnection represents a page shield connection
type PageShieldConnection struct {
AddedAt string `json:"added_at"`
DomainReportedMalicious bool `json:"domain_reported_malicious"`
DomainReportedMalicious *bool `json:"domain_reported_malicious,omitempty"`
FirstPageURL string `json:"first_page_url"`
FirstSeenAt string `json:"first_seen_at"`
Host string `json:"host"`
ID string `json:"id"`
LastSeenAt string `json:"last_seen_at"`
PageURLs []string `json:"page_urls"`
URL string `json:"url"`
URLContainsCdnCgiPath bool `json:"url_contains_cdn_cgi_path"`
URLContainsCdnCgiPath *bool `json:"url_contains_cdn_cgi_path,omitempty"`
}

// ListPageShieldConnectionsResponse represents the response from the list page shield connections endpoint
Expand Down
28 changes: 16 additions & 12 deletions page_shield_connections_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,50 @@ package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
"net/http"
"testing"

"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)

// Mock data for PageShieldConnections
var mockPageShieldConnections = []PageShieldConnection{
{
AddedAt: "2021-08-18T10:51:10.09615Z",
DomainReportedMalicious: false,
DomainReportedMalicious: BoolPtr(false),
FirstPageURL: "blog.cloudflare.com/page",
FirstSeenAt: "2021-08-18T10:51:08Z",
Host: "blog.cloudflare.com",
ID: "c9ef84a6bf5e47138c75d95e2f933e8f",
LastSeenAt: "2021-09-02T09:57:54Z",
PageURLs: []string{"blog.cloudflare.com/page1", "blog.cloudflare.com/page2"},
URL: "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js",
URLContainsCdnCgiPath: false,
URLContainsCdnCgiPath: BoolPtr(false),
},
{
AddedAt: "2021-09-18T10:51:10.09615Z",
DomainReportedMalicious: false,
DomainReportedMalicious: BoolPtr(false),
FirstPageURL: "blog.cloudflare.com/page02",
FirstSeenAt: "2021-08-18T10:51:08Z",
Host: "blog.cloudflare.com",
ID: "c9ef84a6bf5e47138c75d95e2f933e8f",
LastSeenAt: "2021-09-02T09:57:54Z",
PageURLs: []string{"blog.cloudflare.com/page1", "blog.cloudflare.com/page2"},
URL: "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js",
URLContainsCdnCgiPath: false,
URLContainsCdnCgiPath: BoolPtr(false),
},
{
AddedAt: "2021-10-18T10:51:10.09615Z",
DomainReportedMalicious: false,
DomainReportedMalicious: BoolPtr(false),
FirstPageURL: "blog.cloudflare.com/page03",
FirstSeenAt: "2021-08-18T10:51:08Z",
Host: "blog.cloudflare.com",
ID: "c9ef84a6bf5e47138c75d95e2f933e8f",
LastSeenAt: "2021-09-02T09:57:54Z",
PageURLs: []string{"blog.cloudflare.com/page1", "blog.cloudflare.com/page2"},
URL: "https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.min.js",
URLContainsCdnCgiPath: false,
URLContainsCdnCgiPath: BoolPtr(false),
},
}

Expand All @@ -60,9 +60,11 @@ func TestListPageShieldConnections(t *testing.T) {
response := ListPageShieldConnectionsResponse{
Result: mockPageShieldConnections,
}
json.NewEncoder(w).Encode(response)
err := json.NewEncoder(w).Encode(response)
if err != nil {
t.Fatal(err)
}
})

result, _, err := client.ListPageShieldConnections(context.Background(), &ResourceContainer{Identifier: "testzone"}, ListPageShieldConnectionsParams{})
assert.NoError(t, err)
assert.Equal(t, mockPageShieldConnections, result)
Expand All @@ -72,14 +74,16 @@ func TestGetPageShieldConnection(t *testing.T) {
setup()
defer teardown()

connectionID := "c9ef84a6bf5e47138c75d95e2f933e8f"
connectionID := "c9ef84a6bf5e47138c75d95e2f933e8f" //nolint
mux.HandleFunc(fmt.Sprintf("/zones/testzone/page_shield/connections/%s", connectionID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
response := mockPageShieldConnections[0] // Assuming it's the first mock connection
json.NewEncoder(w).Encode(response)
err := json.NewEncoder(w).Encode(response)
if err != nil {
t.Fatal(err)
}
})

result, err := client.GetPageShieldConnection(context.Background(), &ResourceContainer{Identifier: "testzone"}, connectionID)
assert.NoError(t, err)
assert.Equal(t, &mockPageShieldConnections[0], result)
Expand Down
5 changes: 3 additions & 2 deletions page_shield_policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
"net/http"

"github.com/goccy/go-json"
)

// PageShieldPolicy represents a page shield policy
type PageShieldPolicy struct {
Action string `json:"action"`
Description string `json:"description"`
Enabled bool `json:"enabled"`
Enabled *bool `json:"enabled,omitempty"`
Expression string `json:"expression"`
ID string `json:"id"`
Value string `json:"value"`
Expand Down
37 changes: 22 additions & 15 deletions page_shield_policies_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,34 @@ package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
"net/http"
"testing"

"github.com/goccy/go-json"
"github.com/stretchr/testify/assert"
)

var mockPageShieldPolicies = []PageShieldPolicy{
{
Action: "allow",
Description: "Checkout page CSP policy",
Enabled: true,
Enabled: BoolPtr(true),
Expression: "ends_with(http.request.uri.path, \"/checkout\")",
ID: "c9ef84a6bf5e47138c75d95e2f933e8f",
Value: "script-src 'none';",
},
{
Action: "allow",
Description: "Checkout page CSP policy",
Enabled: true,
Enabled: BoolPtr(true),
Expression: "ends_with(http.request.uri.path, \"/login\")",
ID: "c9ef84a6bf5e47138c75d95e2f933e82",
Value: "script-src 'none';",
},
{
Action: "allow",
Description: "Checkout page CSP policy",
Enabled: true,
Enabled: BoolPtr(true),
Expression: "ends_with(http.request.uri.path, \"/logout\")",
ID: "c9ef84a6bf5e47138c75d95e2f933e83",
Value: "script-src 'none';",
Expand All @@ -47,9 +47,11 @@ func TestListPageShieldPolicies(t *testing.T) {
response := ListPageShieldPoliciesResponse{
Result: mockPageShieldPolicies,
}
json.NewEncoder(w).Encode(response)
err := json.NewEncoder(w).Encode(response)
if err != nil {
t.Fatal(err)
}
})

result, _, err := client.ListPageShieldPolicies(context.Background(), &ResourceContainer{Identifier: "testzone"})
assert.NoError(t, err)
assert.Equal(t, mockPageShieldPolicies, result)
Expand All @@ -66,17 +68,19 @@ func TestCreatePageShieldPolicy(t *testing.T) {
err := json.NewDecoder(r.Body).Decode(&params)
assert.NoError(t, err)
params.ID = "newPolicyID"
json.NewEncoder(w).Encode(params)
err = json.NewEncoder(w).Encode(params)
if err != nil {
t.Fatal(err)
}
})

newPolicy := PageShieldPolicy{
Action: "block",
Description: "New policy",
Enabled: true,
Enabled: BoolPtr(true),
Expression: "ends_with(http.request.uri.path, \"/new\")",
Value: "script-src 'self';",
}

result, err := client.CreatePageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, newPolicy)
assert.NoError(t, err)
assert.Equal(t, "newPolicyID", result.ID)
Expand All @@ -91,7 +95,6 @@ func TestDeletePageShieldPolicy(t *testing.T) {
assert.Equal(t, http.MethodDelete, r.Method, "Expected method 'DELETE', got %s", r.Method)
w.WriteHeader(http.StatusOK) // Assuming successful deletion returns 200 OK
})

err := client.DeletePageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, policyID)
assert.NoError(t, err)
}
Expand All @@ -104,9 +107,11 @@ func TestGetPageShieldPolicy(t *testing.T) {
mux.HandleFunc(fmt.Sprintf("/zones/testzone/page_shield/policies/%s", policyID), func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method, "Expected method 'GET', got %s", r.Method)
w.Header().Set("content-type", "application/json")
json.NewEncoder(w).Encode(mockPageShieldPolicies[0]) // Assuming the first mock policy
err := json.NewEncoder(w).Encode(mockPageShieldPolicies[0]) // Assuming the first mock policy
if err != nil {
t.Fatal(err)
}
})

result, err := client.GetPageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, policyID)
assert.NoError(t, err)
assert.Equal(t, &mockPageShieldPolicies[0], result)
Expand All @@ -125,17 +130,19 @@ func TestUpdatePageShieldPolicy(t *testing.T) {
err := json.NewDecoder(r.Body).Decode(&params)
assert.NoError(t, err)
params.ID = policyID
json.NewEncoder(w).Encode(params)
err = json.NewEncoder(w).Encode(params)
if err != nil {
t.Fatal(err)
}
})

updatedPolicy := PageShieldPolicy{
Action: "block",
Description: "Updated policy",
Enabled: false,
Enabled: BoolPtr(false),
Expression: "ends_with(http.request.uri.path, \"/updated\")",
Value: "script-src 'self';",
}

result, err := client.UpdatePageShieldPolicy(context.Background(), &ResourceContainer{Identifier: "testzone"}, policyID, updatedPolicy)
assert.NoError(t, err)
assert.Equal(t, policyID, result.ID)
Expand Down
15 changes: 8 additions & 7 deletions page_shield_scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package cloudflare
import (
"context"
"fmt"
"github.com/goccy/go-json"
"net/http"

"github.com/goccy/go-json"
)

// PageShieldScript represents a Page Shield script.
type PageShieldScript struct {
AddedAt string `json:"added_at"`
DomainReportedMalicious bool `json:"domain_reported_malicious"`
DomainReportedMalicious *bool `json:"domain_reported_malicious,omitempty"`
FetchedAt string `json:"fetched_at"`
FirstPageURL string `json:"first_page_url"`
FirstSeenAt string `json:"first_seen_at"`
Expand All @@ -21,29 +22,29 @@ type PageShieldScript struct {
LastSeenAt string `json:"last_seen_at"`
PageURLs []string `json:"page_urls"`
URL string `json:"url"`
URLContainsCdnCgiPath bool `json:"url_contains_cdn_cgi_path"`
URLContainsCdnCgiPath *bool `json:"url_contains_cdn_cgi_path,omitempty"`
}

// ListPageShieldScriptsParams represents a PageShield Script request parameters
//
// API reference: https://developers.cloudflare.com/api/operations/page-shield-list-page-shield-scripts#Query-Parameters
type ListPageShieldScriptsParams struct {
Direction string `json:"direction"`
ExcludeCdnCgi bool `json:"exclude_cdn_cgi"`
ExcludeDuplicates bool `json:"exclude_duplicates"`
ExcludeCdnCgi *bool `json:"exclude_cdn_cgi,omitempty"`
ExcludeDuplicates *bool `json:"exclude_duplicates,omitempty"`
ExcludeUrls string `json:"exclude_urls"`
Export string `json:"export"`
Hosts string `json:"hosts"`
OrderBy string `json:"order_by"`
Page string `json:"page"`
PageURL string `json:"page_url"`
PerPage int `json:"per_page"`
PrioritizeMalicious bool `json:"prioritize_malicious"`
PrioritizeMalicious *bool `json:"prioritize_malicious,omitempty"`
Status string `json:"status"`
URLs string `json:"urls"`
}

// PageShieldScriptResponse represents the response from the PageShield Script API
// PageShieldScriptsResponse represents the response from the PageShield Script API
type PageShieldScriptsResponse struct {
Results []PageShieldScript `json:"result"`
Response
Expand Down

0 comments on commit a217c8d

Please sign in to comment.