-
Notifications
You must be signed in to change notification settings - Fork 276
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 api token action to handle creation of api token index #4912
base: feature/api-tokens
Are you sure you want to change the base?
Changes from 1 commit
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
package org.opensearch.security.dlic.rest.api; | ||
|
||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.common.collect.ImmutableMap; | ||
import org.opensearch.action.admin.indices.create.CreateIndexRequest; | ||
import org.opensearch.action.index.IndexResponse; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.core.xcontent.ToXContent; | ||
import org.opensearch.security.DefaultObjectMapper; | ||
import org.opensearch.security.dlic.rest.validation.EndpointValidator; | ||
import org.opensearch.security.dlic.rest.validation.RequestContentValidator; | ||
import org.opensearch.security.dlic.rest.validation.RequestContentValidator.DataType; | ||
import org.opensearch.security.dlic.rest.validation.ValidationResult; | ||
import org.opensearch.security.securityconf.impl.CType; | ||
import org.opensearch.security.securityconf.impl.v7.ConfigV7; | ||
import org.opensearch.security.support.ConfigConstants; | ||
import org.opensearch.security.support.SecurityJsonNode; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static org.opensearch.rest.RestRequest.Method.*; | ||
import static org.opensearch.security.dlic.rest.api.RateLimitersApiAction.NAME_JSON_PROPERTY; | ||
import static org.opensearch.security.dlic.rest.api.Responses.*; | ||
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix; | ||
import static org.opensearch.security.securityconf.impl.v7.ConfigV7.*; | ||
|
||
public class ApiTokenApiAction extends AbstractApiAction { | ||
|
||
public static final String NAME_JSON_PROPERTY = "ip"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like this is copied from elsewhere? Can this be removed? |
||
|
||
|
||
private static final List<Route> ROUTES = addRoutesPrefix( | ||
ImmutableList.of( | ||
new Route(GET, "/apitokens"), | ||
new Route(PUT, "/apitokens/{name}") | ||
// new Route(DELETE, "/apitokens/{name}"), | ||
) | ||
); | ||
|
||
protected ApiTokenApiAction(ClusterService clusterService, ThreadPool threadPool, SecurityApiDependencies securityApiDependencies) { | ||
super(Endpoint.APITOKENS, clusterService, threadPool, securityApiDependencies); | ||
this.requestHandlersBuilder.configureRequestHandlers(this::authFailureConfigApiRequestHandlers); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: the naming also looks copied here. Can this be renamed? |
||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "API Token actions to retrieve / update configs."; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return ROUTES; | ||
} | ||
|
||
@Override | ||
protected CType<ConfigV7> getConfigType() { | ||
return CType.CONFIG; | ||
} | ||
|
||
private void authFailureConfigApiRequestHandlers(RequestHandler.RequestHandlersBuilder requestHandlersBuilder) { | ||
|
||
requestHandlersBuilder.override( | ||
GET, | ||
(channel, request, client) -> loadConfiguration(getConfigType(), false, false).valid(configuration -> { | ||
if (!apiTokenIndexExists()) { | ||
ok(channel, "empty list"); | ||
} else { | ||
ok(channel, "non-empty list"); | ||
} | ||
}).error((status, toXContent) -> response(channel, status, toXContent))) | ||
.override(PUT, (channel, request, client) -> loadConfiguration(getConfigType(), false, false).valid(configuration -> { | ||
String token = createApiToken(request.param(NAME_JSON_PROPERTY), client); | ||
ok(channel, token + " created successfully"); | ||
}).error((status, toXContent) -> response(channel, status, toXContent))); | ||
|
||
} | ||
|
||
public String createApiToken(String name, Client client) { | ||
createApiTokenIndexIfAbsent(client); | ||
|
||
return "test-token"; | ||
} | ||
|
||
public Boolean apiTokenIndexExists() { | ||
return clusterService.state().metadata().hasConcreteIndex(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX); | ||
} | ||
|
||
public void createApiTokenIndexIfAbsent(Client client) { | ||
if (!apiTokenIndexExists()) { | ||
final Map<String, Object> indexSettings = ImmutableMap.of("index.number_of_shards", 1, "index.auto_expand_replicas", "0-all"); | ||
final CreateIndexRequest createIndexRequest = new CreateIndexRequest(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX).settings(indexSettings); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI By subclassing AbstractApiAction this would be authorized like other security APIs. Like other API handlers, I think we will want to go to the transport layer here and execute a transport action and create the index from within there. Any calls to a system index should be wrapped into threadContext.stashContext to assure that the plugin can perform the action regardless of the authenticated user's permissions. |
||
logger.info(client.admin().indices().create(createIndexRequest).actionGet().isAcknowledged()); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ public enum Endpoint { | |
AUTHTOKEN, | ||
TENANTS, | ||
RATELIMITERS, | ||
APITOKENS, | ||
MIGRATE, | ||
VALIDATE, | ||
WHITELIST, | ||
|
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.
remove wildcard import