-
Notifications
You must be signed in to change notification settings - Fork 130
Add table for Gateway audit logs #816
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
Open
kjsbot
wants to merge
15
commits into
trinodb:main
Choose a base branch
from
kjsbot:feature/audit-table
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 13 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f86f7e3
Add gateway_audit_logs table to DB schema
kjsbot 7929e66
Update postgres table
kjsbot 1b2eb36
Remove DB field
kjsbot 3e31ffd
Add helpers for writing to DB
kjsbot f2d9c20
Update DB schema
kjsbot c2622b1
Remove line
kjsbot b39a335
Update test case
kjsbot 3b0f520
Update test case
kjsbot 7471e34
Make AuditLogger interface, update docs
kjsbot 0604aea
Update table columns
kjsbot 2e071e6
Add comment fields to UI for auditing
kjsbot d7c9058
Resolve merge conflicts
kjsbot ffeace6
Merge branch 'main' into feature/audit-table
kjsbot 4366e8f
Update AuditLogger design
kjsbot 9e465fb
Add test
kjsbot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
23 changes: 23 additions & 0 deletions
23
gateway-ha/src/main/java/io/trino/gateway/ha/audit/AuditAction.java
This file contains hidden or 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,23 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.audit; | ||
|
|
||
| public enum AuditAction { | ||
| CREATE, | ||
| UPDATE, | ||
| DELETE, | ||
| ACTIVATE, | ||
| DEACTIVATE, | ||
| UNKNOWN | ||
| } |
19 changes: 19 additions & 0 deletions
19
gateway-ha/src/main/java/io/trino/gateway/ha/audit/AuditContext.java
This file contains hidden or 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,19 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.audit; | ||
|
|
||
| public enum AuditContext { | ||
| TRINO_GW_UI, | ||
| TRINO_GW_API | ||
| } |
27 changes: 27 additions & 0 deletions
27
gateway-ha/src/main/java/io/trino/gateway/ha/audit/AuditLogger.java
This file contains hidden or 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,27 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.audit; | ||
|
|
||
| import static java.util.Objects.requireNonNullElse; | ||
|
|
||
| public interface AuditLogger | ||
| { | ||
| void logAudit(String user, String ip, String backendName, AuditAction action, AuditContext context, boolean success, String userComment); | ||
|
|
||
| static String sanitizeComment(String comment) | ||
| { | ||
| String c = requireNonNullElse(comment, ""); | ||
| return c.replaceAll("\\s+", " ").trim(); | ||
| } | ||
| } | ||
49 changes: 49 additions & 0 deletions
49
gateway-ha/src/main/java/io/trino/gateway/ha/audit/CompositeAuditLogger.java
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.audit; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import io.airlift.log.Logger; | ||
|
|
||
| import java.util.Collections; | ||
| import java.util.Set; | ||
|
|
||
| import static java.util.Objects.requireNonNullElse; | ||
|
|
||
| public class CompositeAuditLogger | ||
| implements AuditLogger | ||
| { | ||
| private static final Logger log = Logger.get(CompositeAuditLogger.class); | ||
| private final Set<AuditLogger> loggers; | ||
|
|
||
| @Inject | ||
| public CompositeAuditLogger(Set<AuditLogger> loggers) | ||
| { | ||
| this.loggers = requireNonNullElse(loggers, Collections.emptySet()); | ||
| } | ||
|
|
||
| @Override | ||
| public void logAudit(String user, String ip, String backend, AuditAction action, AuditContext context, boolean success, String userComment) | ||
| { | ||
| String sanitizedComment = AuditLogger.sanitizeComment(userComment); | ||
| for (AuditLogger logger : loggers) { | ||
| try { | ||
| logger.logAudit(user, ip, backend, action, context, success, sanitizedComment); | ||
| } | ||
| catch (Exception e) { | ||
| log.error(e, "Audit sink %s failed", logger.getClass().getSimpleName()); | ||
| } | ||
| } | ||
| } | ||
| } |
49 changes: 49 additions & 0 deletions
49
gateway-ha/src/main/java/io/trino/gateway/ha/audit/DatabaseAuditLogger.java
This file contains hidden or 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,49 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.audit; | ||
|
|
||
| import com.google.inject.Inject; | ||
| import io.airlift.log.Logger; | ||
| import io.trino.gateway.ha.persistence.dao.AuditLogDao; | ||
| import org.jdbi.v3.core.Jdbi; | ||
|
|
||
| import java.sql.Timestamp; | ||
| import java.time.Instant; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
|
|
||
| public class DatabaseAuditLogger | ||
| implements AuditLogger | ||
| { | ||
| private static final Logger log = Logger.get(DatabaseAuditLogger.class); | ||
| private final AuditLogDao dao; | ||
|
|
||
| @Inject | ||
| public DatabaseAuditLogger(Jdbi jdbi) | ||
| { | ||
| dao = requireNonNull(jdbi, "jdbi is null").onDemand(AuditLogDao.class); | ||
| } | ||
|
|
||
| @Override | ||
| public void logAudit(String user, String ip, String backendName, AuditAction action, AuditContext context, boolean success, String userComment) | ||
| { | ||
| try { | ||
| dao.log(user, ip, backendName, action.toString(), context.toString(), success, | ||
| AuditLogger.sanitizeComment(userComment), Timestamp.from(Instant.now())); | ||
| } | ||
| catch (Exception e) { | ||
| log.error("Failed to write audit log to database: %s", e.getMessage()); | ||
| } | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
gateway-ha/src/main/java/io/trino/gateway/ha/audit/LogAuditLogger.java
This file contains hidden or 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,30 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.audit; | ||
|
|
||
| import io.airlift.log.Logger; | ||
|
|
||
| public class LogAuditLogger | ||
| implements AuditLogger | ||
| { | ||
| private static final Logger log = Logger.get(LogAuditLogger.class); | ||
|
|
||
| @Override | ||
| public void logAudit(String user, String ip, String backendName, AuditAction action, AuditContext context, boolean success, String userComment) | ||
| { | ||
| String comment = AuditLogger.sanitizeComment(userComment); | ||
| log.info("GW_AUDIT_LOG: user=%s, ipAddress=%s, backend=%s, action=%s, context=%s, success=%s, userComment=%s", | ||
| user, ip, backendName, action, context, success, comment); | ||
| } | ||
| } |
128 changes: 128 additions & 0 deletions
128
gateway-ha/src/main/java/io/trino/gateway/ha/config/AdminProxyBackendConfiguration.java
This file contains hidden or 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,128 @@ | ||
| /* | ||
| * 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 io.trino.gateway.ha.config; | ||
|
|
||
| import com.fasterxml.jackson.annotation.JsonProperty; | ||
| import com.fasterxml.jackson.annotation.JsonSetter; | ||
| import io.trino.gateway.ha.audit.AuditAction; | ||
| import io.trino.gateway.ha.audit.AuditContext; | ||
|
|
||
| public class AdminProxyBackendConfiguration | ||
| { | ||
| private final ProxyBackendConfiguration proxyBackendConfiguration = new ProxyBackendConfiguration(); | ||
| private AuditAction action; | ||
| private String comment = ""; | ||
| private AuditContext context; | ||
|
|
||
| @JsonProperty | ||
| public String getName() | ||
| { | ||
| return proxyBackendConfiguration.getName(); | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setName(String name) | ||
| { | ||
| proxyBackendConfiguration.setName(name); | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getProxyTo() | ||
| { | ||
| return proxyBackendConfiguration.getProxyTo(); | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setProxyTo(String proxyTo) | ||
| { | ||
| proxyBackendConfiguration.setProxyTo(proxyTo); | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getExternalUrl() | ||
| { | ||
| return proxyBackendConfiguration.getExternalUrl(); | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setExternalUrl(String externalUrl) | ||
| { | ||
| proxyBackendConfiguration.setExternalUrl(externalUrl); | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public boolean isActive() | ||
| { | ||
| return proxyBackendConfiguration.isActive(); | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setActive(boolean active) | ||
| { | ||
| proxyBackendConfiguration.setActive(active); | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getRoutingGroup() | ||
| { | ||
| return proxyBackendConfiguration.getRoutingGroup(); | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setRoutingGroup(String routingGroup) | ||
| { | ||
| proxyBackendConfiguration.setRoutingGroup(routingGroup); | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public AuditAction getAction() | ||
| { | ||
| return this.action; | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setAction(AuditAction action) | ||
| { | ||
| this.action = action; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public String getComment() | ||
| { | ||
| return this.comment; | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setComment(String comment) | ||
| { | ||
| this.comment = comment; | ||
| } | ||
|
|
||
| @JsonProperty | ||
| public AuditContext getContext() | ||
| { | ||
| return this.context; | ||
| } | ||
|
|
||
| @JsonSetter | ||
| public void setContext(AuditContext context) | ||
| { | ||
| this.context = context; | ||
| } | ||
|
|
||
| public ProxyBackendConfiguration getProxyBackendConfiguration() | ||
| { | ||
| return proxyBackendConfiguration; | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I don’t like the design where
CompositeAuditLoggerimplementsAuditLogger.There are a few problems with this design:
AuditLogger), which makes the configuration hard to understand:static String sanitizeComment).CompositeAuditLoggerand the other loggers are different, so they should not be combined under the same interface.I would recommend using the following approach instead: