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

Refactored and tested SerializeForLogging #6456

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
23 changes: 23 additions & 0 deletions common/authorization/authorizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"strings"

clientworker "go.uber.org/cadence/worker"
Expand Down Expand Up @@ -115,6 +117,27 @@
SerializeForLogging() (string, error)
}

type simpleRequestLogWrapper struct {
request interface{}
}

func (f *simpleRequestLogWrapper) SerializeForLogging() (string, error) {
if f.request == nil || reflect.ValueOf(f.request).IsNil() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit inefficient, but this is a minor thing in comparison to the next json.Marshal, so should be fine to use

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, I added a comment outlining this

return "", nil
}

res, err := json.Marshal(f.request)
if err != nil {
return "", err
}

Check warning on line 132 in common/authorization/authorizer.go

View check run for this annotation

Codecov / codecov/patch

common/authorization/authorizer.go#L131-L132

Added lines #L131 - L132 were not covered by tests

return string(res), nil
}

func NewFilteredRequestBody(request interface{}) FilteredRequestBody {
return &simpleRequestLogWrapper{request}
}

func validatePermission(claims *JWTClaims, attributes *Attributes, data domainData) error {
if (attributes.Permission < PermissionRead) || (attributes.Permission > PermissionAdmin) {
return fmt.Errorf("permission %v is not supported", attributes.Permission)
Expand Down
87 changes: 87 additions & 0 deletions common/authorization/authorizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/stretchr/testify/assert"

"github.com/uber/cadence/common"
"github.com/uber/cadence/common/types"
)

func Test_validatePermission(t *testing.T) {
Expand Down Expand Up @@ -95,3 +96,89 @@ func Test_validatePermission(t *testing.T) {
})
}
}

func TestSignalWithStartWorkflowExecutionRequestSerializeForLogging(t *testing.T) {
tests := map[string]struct {
input *types.SignalWithStartWorkflowExecutionRequest
expectedOutput string
expectedErrorOutput error
}{
"complete request without error": {
input: createNewSignalWithStartWorkflowExecutionRequest(),
expectedOutput: "{\"domain\":\"testDomain\",\"workflowId\":\"testWorkflowID\",\"workflowType\":{\"name\":\"testWorkflowType\"},\"taskList\":{\"name\":\"testTaskList\",\"kind\":\"STICKY\"},\"executionStartToCloseTimeoutSeconds\":1,\"taskStartToCloseTimeoutSeconds\":1,\"identity\":\"testIdentity\",\"requestId\":\"DF66E35D-A5B0-425D-8731-6AAC4A4B6368\",\"workflowIdReusePolicy\":\"AllowDuplicate\",\"signalName\":\"testRequest\",\"control\":\"dGVzdENvbnRyb2w=\",\"retryPolicy\":{\"initialIntervalInSeconds\":1,\"backoffCoefficient\":1,\"maximumIntervalInSeconds\":1,\"maximumAttempts\":1,\"nonRetriableErrorReasons\":[\"testArray\"],\"expirationIntervalInSeconds\":1},\"cronSchedule\":\"testSchedule\",\"header\":{},\"delayStartSeconds\":1,\"jitterStartSeconds\":1,\"firstRunAtTimestamp\":1}",
expectedErrorOutput: nil,
},

"empty request without error": {
input: &types.SignalWithStartWorkflowExecutionRequest{},
expectedOutput: "{}",
expectedErrorOutput: nil,
},

"nil request without error": {
input: nil,
expectedOutput: "",
expectedErrorOutput: nil,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.NotPanics(t, func() {
wrappedInput := NewFilteredRequestBody(test.input)
output, err := wrappedInput.SerializeForLogging()
assert.Equal(t, test.expectedOutput, output)
assert.Equal(t, test.expectedErrorOutput, err)
assert.NotContains(t, output, "PII")
})
})
}
}

func createNewSignalWithStartWorkflowExecutionRequest() *types.SignalWithStartWorkflowExecutionRequest {
testTasklistKind := types.TaskListKind(1)
testExecutionStartToCloseTimeoutSeconds := int32(1)
testTaskStartToCloseTimeoutSeconds := int32(1)
testWorkflowIDReusePolicy := types.WorkflowIDReusePolicy(1)
testDelayStartSeconds := int32(1)
testJitterStartSeconds := int32(1)
testFirstRunAtTimestamp := int64(1)
piiTestArray := []byte("testInputPII")
piiTestMap := make(map[string][]byte)
piiTestMap["PII"] = piiTestArray

testReq := &types.SignalWithStartWorkflowExecutionRequest{
Domain: "testDomain",
WorkflowID: "testWorkflowID",
WorkflowType: &types.WorkflowType{Name: "testWorkflowType"},
TaskList: &types.TaskList{
Name: "testTaskList",
Kind: &testTasklistKind,
},
Input: piiTestArray,
ExecutionStartToCloseTimeoutSeconds: &testExecutionStartToCloseTimeoutSeconds,
TaskStartToCloseTimeoutSeconds: &testTaskStartToCloseTimeoutSeconds,
Identity: "testIdentity",
RequestID: "DF66E35D-A5B0-425D-8731-6AAC4A4B6368",
WorkflowIDReusePolicy: &testWorkflowIDReusePolicy,
SignalName: "testRequest",
SignalInput: piiTestArray,
Control: []byte("testControl"),
RetryPolicy: &types.RetryPolicy{
InitialIntervalInSeconds: 1,
BackoffCoefficient: 1,
MaximumIntervalInSeconds: 1,
MaximumAttempts: 1,
NonRetriableErrorReasons: []string{"testArray"},
ExpirationIntervalInSeconds: 1,
},
CronSchedule: "testSchedule",
Memo: &types.Memo{Fields: piiTestMap},
SearchAttributes: &types.SearchAttributes{IndexedFields: piiTestMap},
Header: &types.Header{Fields: map[string][]byte{}},
DelayStartSeconds: &testDelayStartSeconds,
JitterStartSeconds: &testJitterStartSeconds,
FirstRunAtTimestamp: &testFirstRunAtTimestamp,
}
return testReq
}
jakobht marked this conversation as resolved.
Show resolved Hide resolved
105 changes: 0 additions & 105 deletions common/types/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,6 @@ type AddSearchAttributeRequest struct {
SecurityToken string `json:"securityToken,omitempty"`
}

func (v *AddSearchAttributeRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

// GetSearchAttribute is an internal getter (TBD...)
func (v *AddSearchAttributeRequest) GetSearchAttribute() (o map[string]IndexedValueType) {
if v != nil && v.SearchAttribute != nil {
Expand All @@ -56,13 +49,6 @@ type AdminDescribeWorkflowExecutionRequest struct {
Execution *WorkflowExecution `json:"execution,omitempty"`
}

func (v *AdminDescribeWorkflowExecutionRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

// GetDomain is an internal getter (TBD...)
func (v *AdminDescribeWorkflowExecutionRequest) GetDomain() (o string) {
if v != nil {
Expand Down Expand Up @@ -107,13 +93,6 @@ type GetWorkflowExecutionRawHistoryV2Request struct {
NextPageToken []byte `json:"nextPageToken,omitempty"`
}

func (v *GetWorkflowExecutionRawHistoryV2Request) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

// GetDomain is an internal getter (TBD...)
func (v *GetWorkflowExecutionRawHistoryV2Request) GetDomain() (o string) {
if v != nil {
Expand Down Expand Up @@ -229,13 +208,6 @@ type ResendReplicationTasksRequest struct {
EndVersion *int64 `json:"endVersion,omitempty"`
}

func (v *ResendReplicationTasksRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

// GetWorkflowID is an internal getter (TBD...)
func (v *ResendReplicationTasksRequest) GetWorkflowID() (o string) {
if v != nil {
Expand Down Expand Up @@ -272,13 +244,6 @@ type GetDynamicConfigRequest struct {
Filters []*DynamicConfigFilter `json:"filters,omitempty"`
}

func (v *GetDynamicConfigRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type GetDynamicConfigResponse struct {
Value *DataBlob `json:"value,omitempty"`
}
Expand All @@ -288,39 +253,18 @@ type UpdateDynamicConfigRequest struct {
ConfigValues []*DynamicConfigValue `json:"configValues,omitempty"`
}

func (v *UpdateDynamicConfigRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type RestoreDynamicConfigRequest struct {
ConfigName string `json:"configName,omitempty"`
Filters []*DynamicConfigFilter `json:"filters,omitempty"`
}

func (v *RestoreDynamicConfigRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

// AdminDeleteWorkflowRequest is an internal type (TBD...)
type AdminDeleteWorkflowRequest struct {
Domain string `json:"domain,omitempty"`
Execution *WorkflowExecution `json:"execution,omitempty"`
SkipErrors bool `json:"skipErrors,omitempty"`
}

func (v *AdminDeleteWorkflowRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

func (v *AdminDeleteWorkflowRequest) GetDomain() (o string) {
if v != nil {
return v.Domain
Expand Down Expand Up @@ -356,13 +300,6 @@ type ListDynamicConfigRequest struct {
ConfigName string `json:"configName,omitempty"`
}

func (v *ListDynamicConfigRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type ListDynamicConfigResponse struct {
Entries []*DynamicConfigEntry `json:"entries,omitempty"`
}
Expand Down Expand Up @@ -434,13 +371,6 @@ func FromIsolationGroupPartitionList(in []IsolationGroupPartition) IsolationGrou

type GetGlobalIsolationGroupsRequest struct{}

func (v *GetGlobalIsolationGroupsRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type GetGlobalIsolationGroupsResponse struct {
IsolationGroups IsolationGroupConfiguration
}
Expand All @@ -449,26 +379,12 @@ type UpdateGlobalIsolationGroupsRequest struct {
IsolationGroups IsolationGroupConfiguration
}

func (v *UpdateGlobalIsolationGroupsRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type UpdateGlobalIsolationGroupsResponse struct{}

type GetDomainIsolationGroupsRequest struct {
Domain string
}

func (v *GetDomainIsolationGroupsRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type GetDomainIsolationGroupsResponse struct {
IsolationGroups IsolationGroupConfiguration
}
Expand All @@ -478,26 +394,12 @@ type UpdateDomainIsolationGroupsRequest struct {
IsolationGroups IsolationGroupConfiguration
}

func (v *UpdateDomainIsolationGroupsRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type UpdateDomainIsolationGroupsResponse struct{}

type GetDomainAsyncWorkflowConfiguratonRequest struct {
Domain string
}

func (v *GetDomainAsyncWorkflowConfiguratonRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type GetDomainAsyncWorkflowConfiguratonResponse struct {
Configuration *AsyncWorkflowConfiguration
}
Expand Down Expand Up @@ -525,12 +427,5 @@ type UpdateDomainAsyncWorkflowConfiguratonRequest struct {
Configuration *AsyncWorkflowConfiguration
}

func (v *UpdateDomainAsyncWorkflowConfiguratonRequest) SerializeForLogging() (string, error) {
if v == nil {
return "", nil
}
return SerializeRequest(v)
}

type UpdateDomainAsyncWorkflowConfiguratonResponse struct {
}
Loading
Loading