Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: skip audit logging for validate_only requests (#18025)

Danny Xu9b14ed518182
diff --git a/backend/api/v1/audit.go b/backend/api/v1/audit.go
index 3ceb61d648..4555d47b03 100644
--- a/backend/api/v1/audit.go
+++ b/backend/api/v1/audit.go
@@ -135,6 +135,11 @@ func (c *auditConnectStreamingConn) Send(resp any) error {
}
func createAuditLogConnect(ctx context.Context, request, response any, method string, storage *store.Store, secret string, profile *config.Profile, serviceData *anypb.Any, rerr error, headers http.Header, peerAddr string, latency time.Duration) error {
+ // Skip audit logging for validate-only requests.
+ if isValidateOnlyRequest(request) {
+ return nil
+ }
+
requestString, err := getRequestString(request)
if err != nil {
return errors.Wrapf(err, "failed to get request string")
@@ -574,6 +579,36 @@ func getRequestMetadataFromHeaders(headers http.Header, peerAddr string) *storep
}
}
+// isValidateOnlyRequest checks if a request has validate_only field set to true
+// using protoreflect to generically detect the field.
+func isValidateOnlyRequest(request any) bool {
+ if request == nil {
+ return false
+ }
+
+ // Check if the value is nil (for pointer types).
+ val := reflect.ValueOf(request)
+ if val.Kind() == reflect.Ptr && val.IsNil() {
+ return false
+ }
+
+ protoMsg, ok := request.(proto.Message)
+ if !ok {
+ return false
+ }
+
+ // Use protoreflect to check for validate_only field.
+ msg := protoMsg.ProtoReflect()
+ fields := msg.Descriptor().Fields()
+ validateOnlyField := fields.ByName("validate_only")
+ if validateOnlyField == nil {
+ return false
+ }
+
+ // Check if the field is set and is true.
+ return msg.Get(validateOnlyField).Bool()
+}
+
// expect
// 1. connect.Error
// 2. other unknown errors
diff --git a/backend/api/v1/common_test.go b/backend/api/v1/common_test.go
index 529fdc465f..5d2f7bff79 100644
--- a/backend/api/v1/common_test.go
+++ b/backend/api/v1/common_test.go
@@ -4,6 +4,8 @@ import (
"testing"
"github.com/stretchr/testify/require"
+
+ v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
)
func TestIsValidResourceID(t *testing.T) {
@@ -191,3 +193,80 @@ func TestParseOrderBy(t *testing.T) {
}
}
}
+
+func TestIsValidateOnlyRequest(t *testing.T) {
+ tests := []struct {
+ name string
+ request any
+ want bool
+ }{
+ {
+ name: "nil request",
+ request: nil,
+ want: false,
+ },
+ {
+ name: "non-proto request",
+ request: "not a proto",
+ want: false,
+ },
+ {
+ name: "request without validate_only field",
+ request: &v1pb.GetInstanceRequest{
+ Name: "instances/test",
+ },
+ want: false,
+ },
+ {
+ name: "CreateInstanceRequest with validate_only=false",
+ request: &v1pb.CreateInstanceRequest{
+ InstanceId: "test",
+ Instance: &v1pb.Instance{},
+ ValidateOnly: false,
+ },
+ want: false,
+ },
+ {
+ name: "CreateInstanceRequest with validate_only=true",
+ request: &v1pb.CreateInstanceRequest{
+ InstanceId: "test",
+ Instance: &v1pb.Instance{},
+ ValidateOnly: true,
+ },
+ want: true,
+ },
+ {
+ name: "UpdateSettingRequest with validate_only=true",
+ request: &v1pb.UpdateSettingRequest{
+ Setting: &v1pb.Setting{},
+ ValidateOnly: true,
+ },
+ want: true,
+ },
+ {
+ name: "AddDataSourceRequest with validate_only=true",
+ request: &v1pb.AddDataSourceRequest{
+ Name: "instances/test",
+ DataSource: &v1pb.DataSource{},
+ ValidateOnly: true,
+ },
+ want: true,
+ },
+ {
+ name: "CreateRolloutRequest with validate_only=false",
+ request: &v1pb.CreateRolloutRequest{
+ Parent: "projects/test",
+ Rollout: &v1pb.Rollout{},
+ ValidateOnly: false,
+ },
+ want: false,
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.name, func(t *testing.T) {
+ got := isValidateOnlyRequest(test.request)
+ require.Equal(t, test.want, got)
+ })
+ }
+}