[Improve](auditlog) audit log print real sql in prepared statement#43038
[Improve](auditlog) audit log print real sql in prepared statement#43038eldenmoon merged 2 commits intoapache:masterfrom
Conversation
|
Thank you for your contribution to Apache Doris. Since 2024-03-18, the Document has been moved to doris-website. |
| context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer()); | ||
| } | ||
| context.getMysqlChannel().flush(); | ||
| context.getState().setNoop(); |
There was a problem hiding this comment.
what happend if setOK? you should distingwish diffent behavior bettwen NOOP and OK
There was a problem hiding this comment.
After verification, I found that changing it to OK would cause issues. Therefore, when logging, I added a validation: if the status is com_stmt_prepare and there are no errors, it is recorded as OK.
There was a problem hiding this comment.
I saw the issue and this setting, and I thought this setNoop was a bug, so I didn’t think it through carefully. Thank you for the reminder
|
|
||
| public AuditEventBuilder getAuditEventBuilder() { | ||
| return auditEventBuilder; | ||
| return new AuditEventBuilder(); |
There was a problem hiding this comment.
do not new AuditEventBuilder() here, new AuditEventBuilder for prepared statement EXECUTE
There was a problem hiding this comment.
Thank you for your correction, I have modified this part by performing a deep copy of the auditevent object to prevent the statement recorded in the audit log from being overwritten.
| if (!expr.isNullLiteral()) { | ||
| value = expr.toString(); | ||
| } | ||
| origStmt = origStmt.replaceFirst("\\?", value); |
There was a problem hiding this comment.
what if literal contains "?" for example , select * from tbl where key = "123?"
There was a problem hiding this comment.
Thank you for your correction.I will modify it to skip ? enclosed in quotes.
| for (Map.Entry<PlaceholderId, Expression> entry : sortedEntries) { | ||
| Expression expr = entry.getValue(); | ||
| String value = ""; | ||
| if (!expr.isNullLiteral()) { |
There was a problem hiding this comment.
If expr is a NullLiteral, toString() will return "NULL".
| public AuditEvent build() { | ||
| return this.auditEvent; | ||
| AuditEvent copy = new AuditEvent(); | ||
| copy.type = auditEvent.type; |
There was a problem hiding this comment.
do not modify this. you should call reset some where when handle EXEUCTE command
There was a problem hiding this comment.
Thank you for your correction
| } | ||
|
|
||
| private String parseRealSql(String origStmt, Map<PlaceholderId, Expression> idExpressionMap) { | ||
| if (idExpressionMap.isEmpty()) { |
There was a problem hiding this comment.
I think it's tricky to rewrite the sql, and i think maybe another way, we could print EXETUE xxx USING XXX , and add another filed in audit log to print the prepared sql like select * from tbl where a = ?
ba98233 to
db0b8b5
Compare
656edf6 to
2ef96e3
Compare
2ef96e3 to
e57e441
Compare
|
run buildall |
|
PR approved by at least one committer and no changes requested. |
|
PR approved by anyone and no changes requested. |
|
run buildall |
|
PR approved by at least one committer and no changes requested. |
| executor.execute(); | ||
| if (ctx.getSessionVariable().isEnablePreparedStmtAuditLog()) { | ||
| stmtStr = executeStmt.toSql(); | ||
| stmtStr = stmtStr + "/*originalSql = " + prepareCommand.getOriginalStmt().originStmt + "*/"; |
There was a problem hiding this comment.
add blank before /*originalSql for user friendly.
| public String ctl = ""; | ||
| @AuditField(value = "Db") | ||
| public String db = ""; | ||
| @AuditField(value = "CommandType") |
There was a problem hiding this comment.
@eldenmoon Will it cause sede compatibility problem ?
There was a problem hiding this comment.
no, adding fields will not cause compatibility problem
d7585e7 to
6c99373
Compare
|
PR approved by at least one committer and no changes requested. |
|
run buildall |
…43038) 1. Use the "execute *** using *** /*original sql = */" in the audit log instead of "execute *** using ***". 2. Add a CommandType parameter to the audit log. 3. When the prepared statement is ready, it should log OK instead of NOOP
…statement (apache#43321) Cherry-picked from apache#43038 Co-authored-by: He xueyu <112704062+HexyinUESTC@users.noreply.github.com>
…pache#43038) 1. Use the "execute *** using *** /*original sql = */" in the audit log instead of "execute *** using ***". 2. Add a CommandType parameter to the audit log. 3. When the prepared statement is ready, it should log OK instead of NOOP
What problem does this PR solve?
Issue Number: close #42553
Problem Summary:
Check List (For Committer)
Test
Behavior changed:
Does this need documentation?
Release note
None
Check List (For Reviewer who merge this PR)