forked from opensearch-project/security
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a indexmanager to handle document creation
Signed-off-by: Derek Ho <dxho@amazon.com>
- Loading branch information
Showing
3 changed files
with
138 additions
and
1 deletion.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/main/java/org/opensearch/security/dlic/rest/api/ApiToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
src/main/java/org/opensearch/security/dlic/rest/api/ApiTokenIndexManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
|
||
} | ||
} | ||
|
||
} |