Skip to content
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

Api token authc/z implementation with Cache #4992

Open
wants to merge 11 commits into
base: feature/api-tokens
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@
import org.opensearch.search.internal.SearchContext;
import org.opensearch.search.query.QuerySearchResult;
import org.opensearch.security.action.apitokens.ApiTokenAction;
import org.opensearch.security.action.apitokens.ApiTokenIndexListenerCache;
import org.opensearch.security.action.configupdate.ConfigUpdateAction;
import org.opensearch.security.action.configupdate.TransportConfigUpdateAction;
import org.opensearch.security.action.onbehalf.CreateOnBehalfOfTokenAction;
Expand Down Expand Up @@ -717,6 +718,15 @@
dlsFlsBaseContext
)
);

// TODO: Is there a higher level approach that makes more sense here? Does this cover unsuccessful index ops?
if (ConfigConstants.OPENSEARCH_API_TOKENS_INDEX.equals(indexModule.getIndex().getName())) {
ApiTokenIndexListenerCache apiTokenIndexListenerCacher = ApiTokenIndexListenerCache.getInstance();
apiTokenIndexListenerCacher.initialize();
indexModule.addIndexOperationListener(apiTokenIndexListenerCacher);
log.warn("Security plugin started listening to operations on index {}", ConfigConstants.OPENSEARCH_API_TOKENS_INDEX);

Check warning on line 727 in src/main/java/org/opensearch/security/OpenSearchSecurityPlugin.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/OpenSearchSecurityPlugin.java#L724-L727

Added lines #L724 - L727 were not covered by tests
}

indexModule.forceQueryCacheProvider((indexSettings, nodeCache) -> new QueryCache() {

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* 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.
*/

package org.opensearch.security.action.apitokens;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import org.opensearch.common.xcontent.LoggingDeprecationHandler;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.index.engine.Engine;
import org.opensearch.index.shard.IndexingOperationListener;

/**
* This class implements an index operation listener for operations performed on api tokens
* These indices are defined on bootstrap and configured to listen in OpenSearchSecurityPlugin.java
*/
public class ApiTokenIndexListenerCache implements IndexingOperationListener {

private final static Logger log = LogManager.getLogger(ApiTokenIndexListenerCache.class);

private static final ApiTokenIndexListenerCache INSTANCE = new ApiTokenIndexListenerCache();
private final ConcurrentHashMap<String, String> idToJtiMap = new ConcurrentHashMap<>();

private Map<String, Permissions> jtis = new ConcurrentHashMap<>();

private boolean initialized;

private ApiTokenIndexListenerCache() {}

public static ApiTokenIndexListenerCache getInstance() {
return ApiTokenIndexListenerCache.INSTANCE;
}

/**
* Initializes the ApiTokenIndexListenerCache.
* This method is called during the plugin's initialization process.
*
*/
public void initialize() {

if (initialized) {
return;

Check warning on line 56 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L56

Added line #L56 was not covered by tests
}

initialized = true;

Check warning on line 59 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L59

Added line #L59 was not covered by tests

}

Check warning on line 61 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L61

Added line #L61 was not covered by tests

public boolean isInitialized() {
return initialized;

Check warning on line 64 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L64

Added line #L64 was not covered by tests
}

/**
* This method is called after an index operation is performed.
* It adds the JTI of the indexed document to the cache and maps the document ID to the JTI (for deletion handling).
* @param shardId The shard ID of the index where the operation was performed.
* @param index The index where the operation was performed.
* @param result The result of the index operation.
*/
@Override
public void postIndex(ShardId shardId, Engine.Index index, Engine.IndexResult result) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious, why use this approach w/ IndexOperationListener opposed to the same approach as the security index vs in-memory data-structures that back the security index?

i.e. With the security index, if I call the API to create a new user. The node that receives the request will fulfill the request and add the new user to the security index. After updating the security index, it will use an internal transport action (TransportConfigUpdateAction) to instruct all nodes of the cluster to re-read from the security index. On node bootstrap, each node of the cluster reads from the security index and populates their in-memory cache of the security index.

BytesReference sourceRef = index.source();

Check warning on line 76 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L76

Added line #L76 was not covered by tests

try {
XContentParser parser = XContentType.JSON.xContent()
.createParser(NamedXContentRegistry.EMPTY, LoggingDeprecationHandler.INSTANCE, sourceRef.streamInput());

Check warning on line 80 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L79-L80

Added lines #L79 - L80 were not covered by tests

ApiToken token = ApiToken.fromXContent(parser);
jtis.put(token.getJti(), new Permissions(token.getClusterPermissions(), token.getIndexPermissions()));
idToJtiMap.put(index.id(), token.getJti());

Check warning on line 84 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L82-L84

Added lines #L82 - L84 were not covered by tests

} catch (IOException e) {
log.error("Failed to parse indexed document", e);
}
}

Check warning on line 89 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L86-L89

Added lines #L86 - L89 were not covered by tests

/**
* This method is called after a delete operation is performed.
* It deletes the corresponding document id in the map and the corresponding jti from the cache.
* @param shardId The shard ID of the index where the delete operation was performed.
* @param delete The delete operation that was performed.
* @param result The result of the delete operation.
*/
@Override
public void postDelete(ShardId shardId, Engine.Delete delete, Engine.DeleteResult result) {
String docId = delete.id();
String jti = idToJtiMap.remove(docId);

Check warning on line 101 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L100-L101

Added lines #L100 - L101 were not covered by tests
if (jti != null) {
jtis.remove(jti);
log.debug("Removed token with ID {} and JTI {} from cache", docId, jti);

Check warning on line 104 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L103-L104

Added lines #L103 - L104 were not covered by tests
}
}

Check warning on line 106 in src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/ApiTokenIndexListenerCache.java#L106

Added line #L106 was not covered by tests

public Map<String, Permissions> getJtis() {
return jtis;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String createApiToken(
) {
apiTokenIndexHandler.createApiTokenIndexIfAbsent();
// TODO: Add validation on whether user is creating a token with a subset of their permissions
ExpiringBearerAuthToken token = securityTokenManager.issueApiToken(name, expiration, clusterPermissions, indexPermissions);
ExpiringBearerAuthToken token = securityTokenManager.issueApiToken(name, expiration);
ApiToken apiToken = new ApiToken(
name,
securityTokenManager.encryptToken(token.getCompleteToken()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

package org.opensearch.security.action.apitokens;

import java.util.List;

public class Permissions {
private List<String> clusterPerm;
private List<ApiToken.IndexPermission> indexPermission;

// Constructor
public Permissions(List<String> clusterPerm, List<ApiToken.IndexPermission> indexPermission) {
this.clusterPerm = clusterPerm;
this.indexPermission = indexPermission;
}

// Getters and setters
public List<String> getClusterPerm() {
return clusterPerm;

Check warning on line 25 in src/main/java/org/opensearch/security/action/apitokens/Permissions.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/Permissions.java#L25

Added line #L25 was not covered by tests
}

public void setClusterPerm(List<String> clusterPerm) {
this.clusterPerm = clusterPerm;
}

Check warning on line 30 in src/main/java/org/opensearch/security/action/apitokens/Permissions.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/Permissions.java#L29-L30

Added lines #L29 - L30 were not covered by tests

public List<ApiToken.IndexPermission> getIndexPermission() {
return indexPermission;

Check warning on line 33 in src/main/java/org/opensearch/security/action/apitokens/Permissions.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/Permissions.java#L33

Added line #L33 was not covered by tests
}

public void setIndexPermission(List<ApiToken.IndexPermission> indexPermission) {
this.indexPermission = indexPermission;
}

Check warning on line 38 in src/main/java/org/opensearch/security/action/apitokens/Permissions.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/action/apitokens/Permissions.java#L37-L38

Added lines #L37 - L38 were not covered by tests

}
Original file line number Diff line number Diff line change
Expand Up @@ -584,22 +584,24 @@
originalSource = "{}";
}
if (securityIndicesMatcher.test(shardId.getIndexName())) {
try (
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
THROW_UNSUPPORTED_OPERATION,
originalResult.internalSourceRef(),
XContentType.JSON
)
) {
Object base64 = parser.map().values().iterator().next();
if (base64 instanceof String) {
originalSource = (new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8));
} else {
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);
if (originalSource == null) {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes in this file are to correct a mis-merge that happened in prior PRs to this feature branch.

try (
XContentParser parser = XContentHelper.createParser(
NamedXContentRegistry.EMPTY,
THROW_UNSUPPORTED_OPERATION,
originalResult.internalSourceRef(),
XContentType.JSON
)
) {
Object base64 = parser.map().values().iterator().next();
if (base64 instanceof String) {
originalSource = (new String(BaseEncoding.base64().decode((String) base64), StandardCharsets.UTF_8));
} else {
originalSource = XContentHelper.convertToJson(originalResult.internalSourceRef(), false, XContentType.JSON);

Check warning on line 600 in src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java#L600

Added line #L600 was not covered by tests
}
} catch (Exception e) {
log.error(e.toString());

Check warning on line 603 in src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/security/auditlog/impl/AbstractAuditLog.java#L602-L603

Added lines #L602 - L603 were not covered by tests
}
} catch (Exception e) {
log.error(e.toString());
}

try (
Expand Down Expand Up @@ -640,7 +642,7 @@
}
}

if (!complianceConfig.shouldLogWriteMetadataOnly()) {
if (!complianceConfig.shouldLogWriteMetadataOnly() && !complianceConfig.shouldLogDiffsForWrite()) {
if (securityIndicesMatcher.test(shardId.getIndexName())) {
// current source, normally not null or empty
try (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@

package org.opensearch.security.authtoken.jwt;

import java.io.IOException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.List;
Expand All @@ -28,9 +26,6 @@
import org.opensearch.OpenSearchException;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.security.action.apitokens.ApiToken;

import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWSAlgorithm;
Expand Down Expand Up @@ -157,14 +152,8 @@ public ExpiringBearerAuthToken createJwt(
}

@SuppressWarnings("removal")
public ExpiringBearerAuthToken createJwt(
final String issuer,
final String subject,
final String audience,
final long expiration,
final List<String> clusterPermissions,
final List<ApiToken.IndexPermission> indexPermissions
) throws JOSEException, ParseException, IOException {
public ExpiringBearerAuthToken createJwt(final String issuer, final String subject, final String audience, final long expiration)
throws JOSEException, ParseException {
final long currentTimeMs = timeProvider.getAsLong();
final Date now = new Date(currentTimeMs);

Expand All @@ -178,20 +167,6 @@ public ExpiringBearerAuthToken createJwt(
final Date expiryTime = new Date(expiration);
claimsBuilder.expirationTime(expiryTime);

if (clusterPermissions != null) {
final String listOfClusterPermissions = String.join(",", clusterPermissions);
claimsBuilder.claim("cp", encryptString(listOfClusterPermissions));
}

if (indexPermissions != null) {
List<String> permissionStrings = new ArrayList<>();
for (ApiToken.IndexPermission permission : indexPermissions) {
permissionStrings.add(permission.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS).toString());
}
final String listOfIndexPermissions = String.join(",", permissionStrings);
claimsBuilder.claim("ip", encryptString(listOfIndexPermissions));
}

final JWSHeader header = new JWSHeader.Builder(JWSAlgorithm.parse(signingKey.getAlgorithm().getName())).build();

final SignedJWT signedJwt = AccessController.doPrivileged(
Expand Down
Loading
Loading