Skip to content

[Improve](auditlog) audit log print real sql in prepared statement#43038

Merged
eldenmoon merged 2 commits intoapache:masterfrom
HexyinUESTC:issue_fix
Nov 6, 2024
Merged

[Improve](auditlog) audit log print real sql in prepared statement#43038
eldenmoon merged 2 commits intoapache:masterfrom
HexyinUESTC:issue_fix

Conversation

@HexyinUESTC
Copy link
Copy Markdown
Contributor

@HexyinUESTC HexyinUESTC commented Oct 31, 2024

What problem does this PR solve?

  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.
  4. Fixed a bug where only the last statement in multiple executions was logged multiple times, while previous statements were not logged.
image image

Issue Number: close #42553

Problem Summary:

Check List (For Committer)

  • Test

    • Regression test
    • Unit Test
    • Manual test (add detailed scripts or steps below)
    • No need to test or manual test. Explain why:
      • This is a refactor/code format and no logic has been changed.
      • Previous test can cover this change.
      • No colde files have been changed.
      • Other reason
  • Behavior changed:

    • No.
    • Yes.
  • Does this need documentation?

    • No.
    • Yes.
  • Release note

    None

Check List (For Reviewer who merge this PR)

  • Confirm the release note
  • Confirm test cases
  • Confirm document
  • Add branch pick label

@doris-robot
Copy link
Copy Markdown

Thank you for your contribution to Apache Doris.
Don't know what should be done next? See How to process your PR

Since 2024-03-18, the Document has been moved to doris-website.
See Doris Document.

context.getMysqlChannel().sendOnePacket(serializer.toByteBuffer());
}
context.getMysqlChannel().flush();
context.getState().setNoop();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

what happend if setOK? you should distingwish diffent behavior bettwen NOOP and OK

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do not new AuditEventBuilder() here, new AuditEventBuilder for prepared statement EXECUTE

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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);
Copy link
Copy Markdown
Member

@eldenmoon eldenmoon Nov 1, 2024

Choose a reason for hiding this comment

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

what if literal contains "?" for example , select * from tbl where key = "123?"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why special treat null value?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

If expr is a NullLiteral, toString() will return "NULL".

@HexyinUESTC HexyinUESTC closed this Nov 1, 2024
@HexyinUESTC HexyinUESTC deleted the issue_fix branch November 1, 2024 06:00
@HexyinUESTC HexyinUESTC reopened this Nov 1, 2024
public AuditEvent build() {
return this.auditEvent;
AuditEvent copy = new AuditEvent();
copy.type = auditEvent.type;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

do not modify this. you should call reset some where when handle EXEUCTE command

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you for your correction

}

private String parseRealSql(String origStmt, Map<PlaceholderId, Expression> idExpressionMap) {
if (idExpressionMap.isEmpty()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 = ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Modified

@HexyinUESTC HexyinUESTC changed the title audit log print real sql in prepared statement [Improve](auditlog) audit log print real sql in prepared statement Nov 1, 2024
@HexyinUESTC HexyinUESTC force-pushed the issue_fix branch 2 times, most recently from 656edf6 to 2ef96e3 Compare November 1, 2024 07:35
eldenmoon
eldenmoon previously approved these changes Nov 1, 2024
Copy link
Copy Markdown
Member

@eldenmoon eldenmoon left a comment

Choose a reason for hiding this comment

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

LGTM

@eldenmoon
Copy link
Copy Markdown
Member

run buildall

eldenmoon
eldenmoon previously approved these changes Nov 1, 2024
Copy link
Copy Markdown
Member

@eldenmoon eldenmoon left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 1, 2024

PR approved by at least one committer and no changes requested.

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Nov 1, 2024
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 1, 2024

PR approved by anyone and no changes requested.

xiaokang
xiaokang previously approved these changes Nov 1, 2024
Copy link
Copy Markdown
Contributor

@xiaokang xiaokang left a comment

Choose a reason for hiding this comment

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

LGTM

@HexyinUESTC HexyinUESTC dismissed stale reviews from xiaokang and eldenmoon via d7585e7 November 2, 2024 03:58
@github-actions github-actions bot removed the approved Indicates a PR has been approved by one committer. label Nov 2, 2024
@eldenmoon
Copy link
Copy Markdown
Member

run buildall

eldenmoon
eldenmoon previously approved these changes Nov 4, 2024
Copy link
Copy Markdown
Member

@eldenmoon eldenmoon left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Nov 4, 2024
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 4, 2024

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 + "*/";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

add blank before /*originalSql for user friendly.

public String ctl = "";
@AuditField(value = "Db")
public String db = "";
@AuditField(value = "CommandType")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@eldenmoon Will it cause sede compatibility problem ?

Copy link
Copy Markdown
Member

@eldenmoon eldenmoon Nov 4, 2024

Choose a reason for hiding this comment

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

no, adding fields will not cause compatibility problem

@github-actions github-actions bot removed the approved Indicates a PR has been approved by one committer. label Nov 4, 2024
Copy link
Copy Markdown
Member

@eldenmoon eldenmoon left a comment

Choose a reason for hiding this comment

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

LGTM

@github-actions github-actions bot added the approved Indicates a PR has been approved by one committer. label Nov 4, 2024
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Nov 4, 2024

PR approved by at least one committer and no changes requested.

@eldenmoon
Copy link
Copy Markdown
Member

run buildall

Copy link
Copy Markdown
Contributor

@xiaokang xiaokang left a comment

Choose a reason for hiding this comment

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

LGTM

@eldenmoon eldenmoon merged commit 8c55f2b into apache:master Nov 6, 2024
github-actions bot pushed a commit that referenced this pull request Nov 6, 2024
…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
dataroaring pushed a commit that referenced this pull request Nov 7, 2024
…statement (#43321)

Cherry-picked from #43038

Co-authored-by: He xueyu <112704062+HexyinUESTC@users.noreply.github.com>
eldenmoon pushed a commit to eldenmoon/incubator-doris that referenced this pull request Nov 12, 2024
…statement (apache#43321)

Cherry-picked from apache#43038

Co-authored-by: He xueyu <112704062+HexyinUESTC@users.noreply.github.com>
eldenmoon pushed a commit to eldenmoon/incubator-doris that referenced this pull request Nov 26, 2024
…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
yiguolei pushed a commit that referenced this pull request Nov 26, 2024
…44588)

cherry-pick from #43038

Co-authored-by: He xueyu <112704062+HexyinUESTC@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] audit log print real sql in prepared statement

6 participants