|
10 | 10 | import org.elasticsearch.ElasticsearchSecurityException; |
11 | 11 | import org.elasticsearch.Version; |
12 | 12 | import org.elasticsearch.common.Nullable; |
| 13 | +import org.elasticsearch.common.bytes.BytesReference; |
13 | 14 | import org.elasticsearch.common.settings.Settings; |
14 | 15 | import org.elasticsearch.common.util.concurrent.ThreadContext; |
15 | 16 | import org.elasticsearch.common.util.concurrent.ThreadContext.StoredContext; |
| 17 | +import org.elasticsearch.common.xcontent.XContentHelper; |
| 18 | +import org.elasticsearch.common.xcontent.XContentType; |
16 | 19 | import org.elasticsearch.node.Node; |
17 | 20 | import org.elasticsearch.xpack.core.security.authc.Authentication; |
18 | 21 | import org.elasticsearch.xpack.core.security.authc.Authentication.AuthenticationType; |
|
23 | 26 | import java.io.IOException; |
24 | 27 | import java.io.UncheckedIOException; |
25 | 28 | import java.util.Collections; |
| 29 | +import java.util.HashMap; |
| 30 | +import java.util.Map; |
26 | 31 | import java.util.Objects; |
27 | 32 | import java.util.function.Consumer; |
28 | 33 | import java.util.function.Function; |
29 | 34 |
|
| 35 | +import static org.elasticsearch.xpack.core.security.authc.Authentication.VERSION_API_KEY_ROLES_AS_BYTES; |
| 36 | +import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY; |
| 37 | +import static org.elasticsearch.xpack.core.security.authc.AuthenticationField.API_KEY_ROLE_DESCRIPTORS_KEY; |
| 38 | + |
30 | 39 | /** |
31 | 40 | * A lightweight utility that can find the current user and authentication information for the local thread. |
32 | 41 | */ |
33 | 42 | public class SecurityContext { |
| 43 | + |
34 | 44 | private final Logger logger = LogManager.getLogger(SecurityContext.class); |
35 | 45 |
|
36 | 46 | private final ThreadContext threadContext; |
@@ -149,8 +159,27 @@ public void executeAfterRewritingAuthentication(Consumer<StoredContext> consumer |
149 | 159 | final Authentication authentication = getAuthentication(); |
150 | 160 | try (ThreadContext.StoredContext ignore = threadContext.stashContext()) { |
151 | 161 | setAuthentication(new Authentication(authentication.getUser(), authentication.getAuthenticatedBy(), |
152 | | - authentication.getLookedUpBy(), version, authentication.getAuthenticationType(), authentication.getMetadata())); |
| 162 | + authentication.getLookedUpBy(), version, authentication.getAuthenticationType(), |
| 163 | + rewriteMetadataForApiKeyRoleDescriptors(version, authentication))); |
153 | 164 | consumer.accept(original); |
154 | 165 | } |
155 | 166 | } |
| 167 | + |
| 168 | + private Map<String, Object> rewriteMetadataForApiKeyRoleDescriptors(Version streamVersion, Authentication authentication) { |
| 169 | + Map<String, Object> metadata = authentication.getMetadata(); |
| 170 | + if (authentication.getAuthenticationType() == AuthenticationType.API_KEY |
| 171 | + && authentication.getVersion().onOrAfter(VERSION_API_KEY_ROLES_AS_BYTES) |
| 172 | + && streamVersion.before(VERSION_API_KEY_ROLES_AS_BYTES)) { |
| 173 | + metadata = new HashMap<>(metadata); |
| 174 | + metadata.put( |
| 175 | + API_KEY_ROLE_DESCRIPTORS_KEY, |
| 176 | + XContentHelper.convertToMap( |
| 177 | + (BytesReference) metadata.get(API_KEY_ROLE_DESCRIPTORS_KEY), false, XContentType.JSON).v2()); |
| 178 | + metadata.put( |
| 179 | + API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY, |
| 180 | + XContentHelper.convertToMap( |
| 181 | + (BytesReference) metadata.get(API_KEY_LIMITED_ROLE_DESCRIPTORS_KEY), false, XContentType.JSON).v2()); |
| 182 | + } |
| 183 | + return metadata; |
| 184 | + } |
156 | 185 | } |
0 commit comments