Skip to content

Commit

Permalink
Add a indexmanager to handle document creation
Browse files Browse the repository at this point in the history
Signed-off-by: Derek Ho <dxho@amazon.com>
  • Loading branch information
derek-ho committed Nov 19, 2024
1 parent a56163d commit 72f2838
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 1 deletion.
78 changes: 78 additions & 0 deletions src/main/java/org/opensearch/security/dlic/rest/api/ApiToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* 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 java.io.IOException;
import java.time.Instant;
import java.util.List;

import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;

public class ApiToken implements ToXContent {
private String description;
private String jti;

private Instant creationTime;
private List<String> roles;

public ApiToken(String description, String jti, List<String> roles) {
this.creationTime = Instant.now();
this.description = description;
this.jti = jti;
this.roles = roles;

}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getJti() {
return jti;
}

public void setJti(String jti) {
this.jti = jti;
}

public Instant getCreationTime() {
return creationTime;
}

public void setCreationTime(Instant creationTime) {
this.creationTime = creationTime;
}

public List<String> getRoles() {
return roles;
}

public void setRoles(List<String> roles) {
this.roles = roles;
}

@Override
public XContentBuilder toXContent(XContentBuilder xContentBuilder, ToXContent.Params params) throws IOException {
xContentBuilder.startObject();
xContentBuilder.field("description", description);
xContentBuilder.field("jti", jti);
xContentBuilder.field("roles", roles);
xContentBuilder.field("creation_time", creationTime.toEpochMilli());
xContentBuilder.endObject();
return xContentBuilder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ private void apiTokenApiRequestHandlers(RequestHandler.RequestHandlersBuilder re

public String createApiToken(String name, Client client) {
createApiTokenIndexIfAbsent(client);
/* TODO: Create document representing the API token */
new ApiTokenIndexManager(client).indexToken(new ApiToken(name, "test-token", List.of()));
return "test-token";
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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 java.io.IOException;

import org.opensearch.action.get.GetRequest;
import org.opensearch.action.index.IndexRequest;
import org.opensearch.client.Client;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.security.support.ConfigConstants;

public class ApiTokenIndexManager {

private Client client;

public ApiTokenIndexManager(Client client) {
this.client = client;
}

public void indexToken(ApiToken token) {
try (final ThreadContext.StoredContext ctx = client.threadPool().getThreadContext().stashContext()) {

XContentBuilder builder = XContentFactory.jsonBuilder();
String jsonString = token.toXContent(builder, ToXContent.EMPTY_PARAMS).toString();

IndexRequest request = new IndexRequest(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX) // Index name
.source(jsonString, XContentType.JSON); // Set JSON source

client.index(request);

} catch (IOException e) {
throw new RuntimeException(e);
}

}

public Object getTokens() {
try (final ThreadContext.StoredContext ctx = client.threadPool().getThreadContext().stashContext()) {

return client.get(new GetRequest(ConfigConstants.OPENSEARCH_API_TOKENS_INDEX));

}
}

}

0 comments on commit 72f2838

Please sign in to comment.