-
Notifications
You must be signed in to change notification settings - Fork 11
Read-only Toggle #60
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
base: main
Are you sure you want to change the base?
Read-only Toggle #60
Conversation
Greptile Summary
Confidence Score: 2/5
Important Files Changed
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
8 files reviewed, 5 comments
Edit Code Review Agent Settings | Greptile
React with 👍 or 👎 to share your feedback on this new summary format
| if r.config.ReadOnlyMode { | ||
| isReadOnly, err := r.handleReadOnlyCheck(query) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !isReadOnly { | ||
| return nil, mysql.NewError(mysql.ER_OPTION_PREVENTS_STATEMENT, "Operation not allowed by policy in read-only mode.") | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like repeating, extracted into some thing like
err = checkReadOnly(...)
if err != nil {
return ...
}instead?
| dialect := sqi.DialectMySQL | ||
| strict := false | ||
| options := sqi.IdentifyOptions{ | ||
| Dialect: &dialect, | ||
| Strict: &strict, | ||
| } | ||
|
|
||
| identifiedQueries, err := sqi.Identify(query, options) | ||
| if err != nil { | ||
| log.Error(). | ||
| Str("sessionID", r.config.SessionID). | ||
| Str("query", query). | ||
| Err(err). | ||
| Msg("Failed to identify query; blocking in read-only mode.") | ||
| return false, err | ||
| } | ||
|
|
||
| // verify that every statement in the query is read-only | ||
| for _, identifiedQuery := range identifiedQueries { | ||
| if identifiedQuery.ExecutionType != sqi.ExecutionListing && identifiedQuery.ExecutionType != sqi.ExecutionInformation { | ||
| log.Warn(). | ||
| Str("sessionID", r.config.SessionID). | ||
| Str("query", query). | ||
| Str("executionType", string(identifiedQuery.ExecutionType)). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be automatic unit test tests covering different SQL statements trying to see if it can parse them correctly and detect write syntax. One test case for mysql and another for postgresql.
| for _, identifiedQuery := range identifiedQueries { | ||
| if identifiedQuery.ExecutionType != sqi.ExecutionListing && identifiedQuery.ExecutionType != sqi.ExecutionInformation { | ||
| log.Warn(). | ||
| Str("sessionID", p.config.SessionID). | ||
| Str("query", queryContent). | ||
| Str("executionType", string(identifiedQuery.ExecutionType)). | ||
| Msg("Write query blocked in read-only mode.") | ||
|
|
||
| errorResponse := &pgproto3.ErrorResponse{ | ||
| Severity: "ERROR", | ||
| Code: "42803", // insufficient_privilege | ||
| Message: "Operation not allowed by policy in read-only mode.", | ||
| } | ||
| clientBackend.Send(errorResponse) | ||
| _ = clientBackend.Flush() | ||
| errChan <- fmt.Errorf("write query blocked: %s (type: %s)", queryContent, identifiedQuery.ExecutionType) | ||
| return false | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unit test cases
| if r.config.ReadOnlyMode { | ||
| isReadOnly, err := r.handleReadOnlyCheck(query) | ||
| if err != nil { | ||
| return 0, 0, nil, err | ||
| } | ||
| if !isReadOnly { | ||
| return 0, 0, nil, mysql.NewError(mysql.ER_OPTION_PREVENTS_STATEMENT, "Operation not allowed by policy in read-only mode.") | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DRY
| if r.config.ReadOnlyMode { | ||
| isReadOnly, err := r.handleReadOnlyCheck(query) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if !isReadOnly { | ||
| return nil, mysql.NewError(mysql.ER_OPTION_PREVENTS_STATEMENT, "Operation not allowed by policy in read-only mode.") | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DRY
Read-only toggle for PAM DB