Skip to content
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

Cb 5584 provide more information in the debug log for auditing #3061

Open
wants to merge 8 commits into
base: devel
Choose a base branch
from
8 changes: 8 additions & 0 deletions server/bundles/io.cloudbeaver.server/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@
</eventHandler>
</extension>

<extension point="org.jkiss.dbeaver.settings">
<propertyGroup id="log" label="Logger">
<property id="log.debug" label="Show all variables in log"
type="boolean" scopes="global"
description="Show in logs all variables in graphql endpoints"/>
</propertyGroup>
</extension>

</plugin>
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public class GraphQLEndpoint extends HttpServlet {
Expand All @@ -68,7 +65,6 @@
private static final String HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS = "Access-Control-Allow-Credentials";

private static final String CORE_SCHEMA_FILE_NAME = "schema/schema.graphqls";

private final GraphQL graphQL;

private static final Gson gson = new GsonBuilder()
Expand Down Expand Up @@ -252,11 +248,14 @@
// if (variables != null) {
// apiCall += " (" + variables + ")";
// }
// }

Check warning on line 251 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Comment has incorrect indentation level 0, expected is 12, indentation should be the same level as line 252. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLEndpoint.java:251:1: warning: Comment has incorrect indentation level 0, expected is 12, indentation should be the same level as line 252. (com.puppycrawl.tools.checkstyle.checks.indentation.CommentsIndentationCheck)
String sessionId = request.getSession().getId();
String userId = GraphQLLoggerUtil.getUserId(request);
String loggerMessage = GraphQLLoggerUtil.buildLoggerMessage(sessionId, userId, variables);
if (apiCall != null) {
log.debug("API > " + apiCall);
log.debug("API > " + apiCall + loggerMessage);
} else if (DEBUG) {
log.debug("API > " + query);
log.debug("API > " + query + loggerMessage);
}
}
ExecutionInput executionInput = contextBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* 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.cloudbeaver.server.graphql;

import io.cloudbeaver.model.session.WebSession;
import io.cloudbeaver.server.CBApplication;
import io.cloudbeaver.server.CBPlatform;
import jakarta.servlet.http.HttpServletRequest;
import org.jkiss.utils.CommonUtils;

import java.util.Map;
import java.util.Set;

public class GraphQLLoggerUtil {

private static final Set<String> PROHIBITED_VARIABLES = GraphQLProhibitedVariables.getAllProhibitedVariables();

public static String getUserId(HttpServletRequest request) {

Check warning on line 32 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java:32:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)

Check warning on line 32 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java#L32

Missing a Javadoc comment.
String userId = null;
WebSession session = (WebSession) CBApplication.getInstance().getSessionManager().getSession(request.getSession().getId());
if (session != null) {
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
userId = session.getUserContext().getUserId();
if (userId == null && session.getUserContext().isAuthorizedInSecurityManager()) {
userId = "anonymous";
}
}
return userId;
}

public static String buildLoggerMessage(String sessionId, String userId, Map<String, Object> variables) {

Check warning on line 44 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java

View workflow job for this annotation

GitHub Actions / Server / Lint

[checkstyle] reported by reviewdog 🐶 Missing a Javadoc comment. Raw Output: /github/workspace/./server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java:44:5: warning: Missing a Javadoc comment. (com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck)

Check warning on line 44 in server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java

View check run for this annotation

Jenkins-CI-integration / CheckStyle Java Report

server/bundles/io.cloudbeaver.server/src/io/cloudbeaver/server/graphql/GraphQLLoggerUtil.java#L44

Missing a Javadoc comment.
StringBuilder loggerMessage = new StringBuilder(" [user: ").append(userId)
.append(", sessionId: ").append(sessionId).append("]");

if (CBPlatform.getInstance().getPreferenceStore().getBoolean("log.debug") && variables != null) {
DenisSinelnikov marked this conversation as resolved.
Show resolved Hide resolved
loggerMessage.append(" [variables] ");
String parsedVariables = parseVarialbes(variables);
if (CommonUtils.isNotEmpty(parsedVariables)) {
loggerMessage.append(parseVarialbes(variables));
}
}
return loggerMessage.toString();
}

private static String parseVarialbes(Map<String, Object> map) {
StringBuilder result = new StringBuilder();

for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();

if (PROHIBITED_VARIABLES.contains(key)) {
continue;
}

if (value instanceof Map) {
result.append(parseVarialbes((Map<String, Object>) value));
} else {
result.append(key).append(": ").append(value).append(" ");
}
}
return result.toString().trim();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* DBeaver - Universal Database Manager
* Copyright (C) 2010-2024 DBeaver Corp and others
*
* 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.cloudbeaver.server.graphql;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public enum GraphQLProhibitedVariables {


PASSWORD("password"),
Copy link
Member

Choose a reason for hiding this comment

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

It don't think that global enum will work. We need some explicit annotations for parameters which we do not want to log (for example as comments).
However we need to move log invocation into WebExecutionStrategy to get information about GQL endpoint.

If this is too complicated then we can keep it as is for now. In this case add "credentials" and "username" too.
However I don't understand why we need enum but simple Set

Copy link
Contributor Author

Choose a reason for hiding this comment

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

let's leave Set for now, added additional parameters, let's start a task on improve with annotations, need more time for realization

CONFIG("config"),
PARAMETERS("parameters"),
SETTINGS("settings"),
LICENSE_TEXT("licenseText");

private final String name;

GraphQLProhibitedVariables(String name) {
this.name = name;
}

public String getName() {
return name;
}

public static Set<String> getAllProhibitedVariables() {
return Arrays.stream(GraphQLProhibitedVariables.values())
.map(GraphQLProhibitedVariables::getName)
.collect(Collectors.toSet());
}
}