PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/action/apikey/TransportCreateApiKeyAction.java

https://github.com/elasticsearch/elasticsearch
Java | 72 lines | 56 code | 7 blank | 9 comment | 9 complexity | 959d9841c27668fb7e819388d458e096 MD5 | raw file
Possible License(s): AGPL-3.0
  1. /*
  2. * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
  3. * or more contributor license agreements. Licensed under the Elastic License
  4. * 2.0; you may not use this file except in compliance with the Elastic License
  5. * 2.0.
  6. */
  7. package org.elasticsearch.xpack.security.action.apikey;
  8. import org.elasticsearch.action.ActionListener;
  9. import org.elasticsearch.action.support.ActionFilters;
  10. import org.elasticsearch.action.support.HandledTransportAction;
  11. import org.elasticsearch.common.inject.Inject;
  12. import org.elasticsearch.tasks.Task;
  13. import org.elasticsearch.transport.TransportService;
  14. import org.elasticsearch.xcontent.NamedXContentRegistry;
  15. import org.elasticsearch.xpack.core.security.SecurityContext;
  16. import org.elasticsearch.xpack.core.security.action.apikey.CreateApiKeyAction;
  17. import org.elasticsearch.xpack.core.security.action.apikey.CreateApiKeyRequest;
  18. import org.elasticsearch.xpack.core.security.action.apikey.CreateApiKeyResponse;
  19. import org.elasticsearch.xpack.core.security.authc.Authentication;
  20. import org.elasticsearch.xpack.core.security.authz.RoleDescriptor;
  21. import org.elasticsearch.xpack.security.authc.ApiKeyService;
  22. import org.elasticsearch.xpack.security.authc.support.ApiKeyGenerator;
  23. import org.elasticsearch.xpack.security.authz.store.CompositeRolesStore;
  24. /**
  25. * Implementation of the action needed to create an API key
  26. */
  27. public final class TransportCreateApiKeyAction extends HandledTransportAction<CreateApiKeyRequest, CreateApiKeyResponse> {
  28. private final ApiKeyGenerator generator;
  29. private final SecurityContext securityContext;
  30. @Inject
  31. public TransportCreateApiKeyAction(
  32. TransportService transportService,
  33. ActionFilters actionFilters,
  34. ApiKeyService apiKeyService,
  35. SecurityContext context,
  36. CompositeRolesStore rolesStore,
  37. NamedXContentRegistry xContentRegistry
  38. ) {
  39. super(CreateApiKeyAction.NAME, transportService, actionFilters, CreateApiKeyRequest::new);
  40. this.generator = new ApiKeyGenerator(apiKeyService, rolesStore, xContentRegistry);
  41. this.securityContext = context;
  42. }
  43. @Override
  44. protected void doExecute(Task task, CreateApiKeyRequest request, ActionListener<CreateApiKeyResponse> listener) {
  45. final Authentication authentication = securityContext.getAuthentication();
  46. if (authentication == null) {
  47. listener.onFailure(new IllegalStateException("authentication is required"));
  48. } else {
  49. if (authentication.isApiKey() && grantsAnyPrivileges(request)) {
  50. listener.onFailure(
  51. new IllegalArgumentException(
  52. "creating derived api keys requires an explicit role descriptor that is empty (has no privileges)"
  53. )
  54. );
  55. return;
  56. }
  57. generator.generateApiKey(authentication, request, listener);
  58. }
  59. }
  60. private static boolean grantsAnyPrivileges(CreateApiKeyRequest request) {
  61. return request.getRoleDescriptors() == null
  62. || request.getRoleDescriptors().isEmpty()
  63. || false == request.getRoleDescriptors().stream().allMatch(RoleDescriptor::isEmpty);
  64. }
  65. }