-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: add required field checks, fix: missing method_name check
- Loading branch information
Showing
8 changed files
with
263 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2023 The Authors (see AUTHORS file) | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// Package util provides untils for audit log payload validation. | ||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"google.golang.org/genproto/googleapis/cloud/audit" | ||
) | ||
|
||
// Validate validates the audit log payload for lumberjack. | ||
func Validate(payload *audit.AuditLog) error { | ||
if payload == nil { | ||
return fmt.Errorf("audit log payload cannot be nil") | ||
} | ||
|
||
if payload.MethodName == "" { | ||
return fmt.Errorf("MethodName cannot be empty") | ||
} | ||
|
||
if payload.ServiceName == "" { | ||
return fmt.Errorf("ServiceName cannot be empty") | ||
} | ||
|
||
if payload.ResourceName == "" { | ||
return fmt.Errorf("ResourceName cannot be empty") | ||
} | ||
|
||
if payload.AuthenticationInfo == nil { | ||
return fmt.Errorf("AuthenticationInfo cannot be nil") | ||
} | ||
|
||
email := payload.AuthenticationInfo.PrincipalEmail | ||
if err := validateEmail(email); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// This method is intended to validate that the email associated with the | ||
// authentication request has the correct format and in a valid domain. | ||
func validateEmail(email string) error { | ||
if email == "" { | ||
return fmt.Errorf("PrincipalEmail cannot be empty") | ||
} | ||
|
||
parts := strings.Split(email, "@") | ||
if len(parts) != 2 || parts[1] == "" { | ||
return fmt.Errorf("PrincipalEmail %q is malformed", email) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
// Copyright 2023 The Authors (see AUTHORS file) | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"google.golang.org/genproto/googleapis/cloud/audit" | ||
|
||
pkgtestutil "github.com/abcxyz/pkg/testutil" | ||
) | ||
|
||
func TestValidate(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
name string | ||
payload *audit.AuditLog | ||
wantErrSubstr string | ||
}{ | ||
{ | ||
name: "success", | ||
payload: &audit.AuditLog{ | ||
MethodName: "test-method", | ||
ServiceName: "test-service", | ||
ResourceName: "test-resource", | ||
AuthenticationInfo: &audit.AuthenticationInfo{ | ||
PrincipalEmail: "user@example.com", | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "payload_is_nil", | ||
wantErrSubstr: "audit log payload cannot be nil", | ||
}, | ||
{ | ||
name: "authInfo_is_nil", | ||
payload: &audit.AuditLog{ | ||
MethodName: "test-method", | ||
ServiceName: "test-service", | ||
ResourceName: "test-resource", | ||
}, | ||
wantErrSubstr: "AuthenticationInfo cannot be nil", | ||
}, | ||
{ | ||
name: "auth_email_is_nil", | ||
payload: &audit.AuditLog{ | ||
MethodName: "test-method", | ||
ServiceName: "test-service", | ||
ResourceName: "test-resource", | ||
AuthenticationInfo: &audit.AuthenticationInfo{}, | ||
}, | ||
wantErrSubstr: "PrincipalEmail cannot be empty", | ||
}, | ||
{ | ||
name: "auth_email_has_no_domain", | ||
payload: &audit.AuditLog{ | ||
MethodName: "test-method", | ||
ServiceName: "test-service", | ||
ResourceName: "test-resource", | ||
AuthenticationInfo: &audit.AuthenticationInfo{ | ||
PrincipalEmail: "user", | ||
}, | ||
}, | ||
wantErrSubstr: `PrincipalEmail "user" is malformed`, | ||
}, | ||
{ | ||
name: "serviceName_is_empty", | ||
payload: &audit.AuditLog{ | ||
MethodName: "test-method", | ||
ResourceName: "test-resource", | ||
AuthenticationInfo: &audit.AuthenticationInfo{ | ||
PrincipalEmail: "user@example.com", | ||
}, | ||
}, | ||
wantErrSubstr: "ServiceName cannot be empty", | ||
}, | ||
{ | ||
name: "resourceName_is_empty", | ||
payload: &audit.AuditLog{ | ||
MethodName: "test-method", | ||
ServiceName: "test-service", | ||
AuthenticationInfo: &audit.AuthenticationInfo{ | ||
PrincipalEmail: "user@example.com", | ||
}, | ||
}, | ||
wantErrSubstr: "ResourceName cannot be empty", | ||
}, | ||
{ | ||
name: "methodName_is_empty", | ||
payload: &audit.AuditLog{ | ||
ServiceName: "test-service", | ||
ResourceName: "test-resource", | ||
AuthenticationInfo: &audit.AuthenticationInfo{ | ||
PrincipalEmail: "user@example.com", | ||
}, | ||
}, | ||
wantErrSubstr: "MethodName cannot be empty", | ||
}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
err := Validate(tc.payload) | ||
if diff := pkgtestutil.DiffErrString(err, tc.wantErrSubstr); diff != "" { | ||
t.Errorf("Process(%+v) got unexpected error substring: %v", tc.name, diff) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.