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

Add user information to slowlog #105621

Merged
merged 5 commits into from
Mar 11, 2024
Merged

Add user information to slowlog #105621

merged 5 commits into from
Mar 11, 2024

Conversation

jfreden
Copy link
Contributor

@jfreden jfreden commented Feb 19, 2024

This PR adds two new index settings:

 index.search.slowlog.include.user: true | false
 index.indexing.slowlog.include.user: true | false

When enabled, depending on the authenticated user, the slow log will have a subset of these additional fields:

user.name - the user principal 
user.effective.name - the effective user (usually same as user.name but different for run-as)
user.realm - realm for authenticated user
user.effective.realm - realm for effective user (usually same as user.name but different for run-as)
auth.type - one of TOKEN | REALM | API_KEY
apikey.id - id of apikey if applicable
apikey.name - name of apikey if applicable

Example

{
  "@timestamp": "2024-02-21T12:42:37.255Z",
  "log.level": "WARN",
  "auth.type": "REALM",
  "elasticsearch.slowlog.id": null,
  "elasticsearch.slowlog.message": "[index6][0]",
  "elasticsearch.slowlog.search_type": "QUERY_THEN_FETCH",
  "elasticsearch.slowlog.source": "{\"query\":{\"match_all\":{\"boost\":1.0}}}",
  "elasticsearch.slowlog.stats": "[]",
  "elasticsearch.slowlog.took": "747.3micros",
  "elasticsearch.slowlog.took_millis": 0,
  "elasticsearch.slowlog.total_hits": "1 hits",
  "elasticsearch.slowlog.total_shards": 1,
  "user.effective.name": "elastic",
  "user.effective.realm": "reserved",
  "user.name": "elastic",
  "user.realm": "reserved",
  "ecs.version": "1.2.0",
  "service.name": "ES_ECS",
  "event.dataset": "elasticsearch.index_search_slowlog",
  "process.thread.name": "elasticsearch[runTask-0][search][T#5]",
  "log.logger": "index.search.slowlog.query",
  "elasticsearch.cluster.uuid": "Ui23kfF1SHKJwu_hI1iPPQ",
  "elasticsearch.node.id": "JK-jn-XpQ3OsDUsq5ZtfGg",
  "elasticsearch.node.name": "node-0",
  "elasticsearch.cluster.name": "distribution_run"
}

TODO

  • TBD if this should be behind gold license or not.
  • Add a PR or issue to update beats slow log integration to consume the new fields

@jfreden jfreden force-pushed the user_in_slowlog branch 2 times, most recently from a4ca9ea to caa59af Compare February 20, 2024 13:06
authContext.put("apikey.id", authenticatingSubject.getMetadata().get(AuthenticationField.API_KEY_ID_KEY).toString());
authContext.put("apikey.name", authenticatingSubject.getMetadata().get(AuthenticationField.API_KEY_NAME_KEY).toString());
}
// TODO: do we want token name here for service accounts?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we want token name here for service accounts?

@jfreden jfreden marked this pull request as ready for review February 22, 2024 08:23
@jfreden jfreden requested a review from a team as a code owner February 22, 2024 08:23
@elasticsearchmachine elasticsearchmachine added the needs:triage Requires assignment of a team area label label Feb 22, 2024
@jfreden jfreden added :Security/Security Security issues without another label and removed needs:triage Requires assignment of a team area label labels Feb 22, 2024
@elasticsearchmachine elasticsearchmachine added the Team:Security Meta label for security team label Feb 22, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-security (Team:Security)

@jfreden jfreden added >enhancement >docs General docs changes and removed Team:Security Meta label for security team labels Feb 22, 2024
@jfreden jfreden requested a review from jakelandis February 22, 2024 08:24
@elasticsearchmachine elasticsearchmachine added Team:Docs Meta label for docs team Team:Security Meta label for security team labels Feb 22, 2024
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-docs (Team:Docs)

@jfreden jfreden changed the title Add user and auth information to slowlog Add user information to slowlog Feb 22, 2024
Copy link
Contributor

@jakelandis jakelandis left a comment

Choose a reason for hiding this comment

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

looking good. a couple comments

Copy link
Contributor

@jakelandis jakelandis left a comment

Choose a reason for hiding this comment

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

Looking really good, just a couple last comments w.r.t. to exactly what we log.

@@ -0,0 +1,10 @@
apply plugin: 'elasticsearch.internal-java-rest-test'
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to create another QA project here? It seems we could just move SecuritySlowLogIT into one of the existing security QA projects.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for reviewing!

I created a new one because the audit log had a separate one and it felt like they're a little similar. Thinking some more about it this could also fit in with the security-basic tests, since it's part of the basic licence. I'll move it there.

Copy link
Contributor

@pgomulka pgomulka left a comment

Choose a reason for hiding this comment

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

it looks great, but please change the example to not use guid
also left one question

docs/reference/index-modules/slowlog.asciidoc Outdated Show resolved Hide resolved
private boolean includeUserInIndexing = false;
private boolean includeUserInSearch = false;

public SecuritySlowLogFieldProvider(Security plugin) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Security is a Plugin subclass. Are you sure we want to depend on such a high level entry point class? When do we use this constructor?
We are 'providing' this instance via SPI, so the other noarg constructor is used , right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for calling this out.

The one arg constructor is called from here, where the parameter has to be a Plugin and the auth context that is needed is only available in the Security plugin.

To pass "SPI validation" the provided implementation needs to be declared in module-info and one of the validation requirements is that it has a no arg constructor, but we never actually call it. I'll add an IllegalStateException to the no arg constructor like we do here to make sure we don't end up in that state.

@jfreden jfreden requested a review from jakelandis March 4, 2024 13:22
Copy link
Contributor

@jakelandis jakelandis left a comment

Choose a reason for hiding this comment

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

LGTM

Nice work ! This will really help with troubleshooting.
Also, don't forget to update (or log the request) to update the mappings for beats integration. (i think here)

@jfreden
Copy link
Contributor Author

jfreden commented Mar 11, 2024

@elasticmachine update branch

@jfreden jfreden merged commit 2063fab into elastic:main Mar 11, 2024
18 checks passed
@syepes
Copy link

syepes commented Apr 26, 2024

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
>docs General docs changes >enhancement :Security/Security Security issues without another label Team:Docs Meta label for docs team Team:Security Meta label for security team v8.14.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants