-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Expose API key name to the ingest pipeline #51305
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
Changes from 1 commit
88ff001
d1592f8
620554a
f2ac3f6
d481a56
29f33ce
b81c6e3
deb7ca9
4d508a3
919edb1
9f7ca68
ac2efff
d488471
cc63be7
e7af393
1e1d801
44a77d3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -76,6 +76,10 @@ public RealmRef getLookedUpBy() { | |||||
| return lookedUpBy; | ||||||
| } | ||||||
|
|
||||||
| public RealmRef getNominalRealm() { | ||||||
|
||||||
| public RealmRef getNominalRealm() { | |
| public RealmRef getRealm() { |
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 think getRealm is a bit too general - I worry about someone just deciding to call getRealm() without thinking about the fact that there's 2 possible realms that might be in play and they need to be intentional about which one to use.
Maybe getSourceRealm()?
The better long term fix would actually be to make getLookedUpBy() always return a realm (that is, is uses this implementation) and then have a isUserLookup() if you need to know whether there was a specific (separate) lookup realm.
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 agree getRealm is too general. I am OK with getSourceRealm and will go with it.
Is User#isRunAs functionally equivelent to the isUserLookup method you propose?
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.
Logically equivalent yes, but it's implemented by looking at different fields, so I'd rather have both implementations so that the one in Authentication is specifically about whether lookupRealm is populated.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /* | ||
| * | ||
| * * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * * or more contributor license agreements. Licensed under the Elastic License; | ||
| * * you may not use this file except in compliance with the Elastic License. | ||
| * | ||
| */ | ||
|
|
||
| package org.elasticsearch.xpack.core.security.authc; | ||
|
|
||
| import org.elasticsearch.test.ESTestCase; | ||
| import org.elasticsearch.xpack.core.security.user.User; | ||
|
|
||
| public class AuthenticationTests extends ESTestCase { | ||
|
|
||
| public void testWillGetLookupByWhenItExists() { | ||
| final Authentication.RealmRef authenticatedBy = new Authentication.RealmRef("auth_by", "auth_by_type", "node"); | ||
| final Authentication.RealmRef lookedUpBy = new Authentication.RealmRef("lookup_by", "lookup_by_type", "node"); | ||
| final Authentication authentication = new Authentication( | ||
| new User("user"), authenticatedBy, lookedUpBy); | ||
|
|
||
| assertEquals(lookedUpBy, authentication.getNominalRealm()); | ||
| } | ||
|
|
||
| public void testWillGetAuthenticateByWhenLookupIsNull() { | ||
| final Authentication.RealmRef authenticatedBy = new Authentication.RealmRef("auth_by", "auth_by_type", "node"); | ||
| final Authentication authentication = new Authentication( | ||
| new User("user"), authenticatedBy, null); | ||
|
|
||
| assertEquals(authenticatedBy, authentication.getNominalRealm()); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -273,10 +273,8 @@ XContentBuilder newDocument(SecureString apiKey, String name, Authentication aut | |
| .startObject("creator") | ||
| .field("principal", authentication.getUser().principal()) | ||
| .field("metadata", authentication.getUser().metadata()) | ||
| .field("realm", authentication.getLookedUpBy() == null ? | ||
| authentication.getAuthenticatedBy().getName() : authentication.getLookedUpBy().getName()) | ||
| .field("realm_type", authentication.getLookedUpBy() == null ? | ||
| authentication.getAuthenticatedBy().getType() : authentication.getLookedUpBy().getType()) | ||
| .field("realm", authentication.getNominalRealm().getName()) | ||
| .field("realm_type", authentication.getNominalRealm().getType()) | ||
| .endObject() | ||
| .endObject(); | ||
|
|
||
|
|
@@ -886,7 +884,7 @@ public static String getCreatorRealmName(final Authentication authentication) { | |
| if (authentication.getAuthenticatedBy().getType().equals(API_KEY_REALM_TYPE)) { | ||
| return (String) authentication.getMetadata().get(API_KEY_CREATOR_REALM_NAME); | ||
| } else { | ||
| return authentication.getAuthenticatedBy().getName(); | ||
| return authentication.getNominalRealm().getName(); | ||
|
||
| } | ||
| } | ||
|
|
||
|
|
@@ -901,7 +899,7 @@ public static String getCreatorRealmType(final Authentication authentication) { | |
| if (authentication.getAuthenticatedBy().getType().equals(API_KEY_REALM_TYPE)) { | ||
| return (String) authentication.getMetadata().get(API_KEY_CREATOR_REALM_TYPE); | ||
| } else { | ||
| return authentication.getAuthenticatedBy().getType(); | ||
| return authentication.getNominalRealm().getType(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -87,31 +87,39 @@ public IngestDocument execute(IngestDocument ingestDocument) throws Exception { | |
| } | ||
| break; | ||
| case API_KEY: | ||
| final HashMap<String, Object> apiKey = new HashMap<>(); | ||
| final String apiKey = "api_key"; | ||
| final Object existingApiKeyField = userObject.get(apiKey); | ||
| @SuppressWarnings("unchecked") | ||
| final Map<String, Object> apiKeyField = | ||
| existingApiKeyField instanceof Map ? (Map<String, Object>) existingApiKeyField : new HashMap<>(); | ||
| Object apiKeyName = authentication.getMetadata().get(ApiKeyService.API_KEY_NAME_KEY); | ||
| if (apiKeyName != null) { | ||
| apiKey.put("name", apiKeyName); | ||
| apiKeyField.put("name", apiKeyName); | ||
| } | ||
| Object apiKeyId = authentication.getMetadata().get(ApiKeyService.API_KEY_ID_KEY); | ||
| if (apiKeyId != null) { | ||
| apiKey.put("id", apiKeyId); | ||
| apiKeyField.put("id", apiKeyId); | ||
| } | ||
| if (false == apiKey.isEmpty()) { | ||
| userObject.put("api_key", apiKey); | ||
| if (false == apiKeyField.isEmpty()) { | ||
| userObject.put(apiKey, apiKeyField); | ||
| } | ||
| break; | ||
| case REALM: | ||
| final String realmKey = "realm"; | ||
| final Object existingRealmField = userObject.get(realmKey); | ||
| @SuppressWarnings("unchecked") | ||
| final Map<String, Object> realmField = | ||
| existingRealmField instanceof Map ? (Map<String, Object>) existingRealmField : new HashMap<>(); | ||
| final Object realmName = ApiKeyService.getCreatorRealmName(authentication); | ||
|
||
| final Object realmType = ApiKeyService.getCreatorRealmType(authentication); | ||
| final HashMap<String, Object> realm = new HashMap<>(); | ||
| if (realmName != null) { | ||
| realm.put("name", realmName); | ||
| realmField.put("name", realmName); | ||
| } | ||
| final Object realmType = ApiKeyService.getCreatorRealmType(authentication); | ||
| if (realmType != null) { | ||
| realm.put("type", realmType); | ||
| realmField.put("type", realmType); | ||
| } | ||
| if (false == realm.isEmpty()) { | ||
| userObject.put("realm", realm); | ||
| if (false == realmField.isEmpty()) { | ||
| userObject.put(realmKey, realmField); | ||
| } | ||
| break; | ||
| case AUTHENTICATION_TYPE: | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.