-
Notifications
You must be signed in to change notification settings - Fork 286
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
Extension registration #2750
Closed
samuelcostae
wants to merge
9
commits into
opensearch-project:main
from
samuelcostae:extension-registration
Closed
Extension registration #2750
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a9adf2
#2595 Initial Draft, Creating endpoint to register the extension (/ex…
samuelcostae fbed0a0
#2595 Initial Draft test class
samuelcostae 53bd02b
#2595 further tries
samuelcostae d507dfc
#2595 further tries
samuelcostae d53251e
#2595 adding dummy validator to reach class
samuelcostae 9ccbe9e
#2595 request payload changes
samuelcostae 4979451
Merge branch 'opensearch-project:main' into extension-registration
samuelcostae e7a8d50
#2595 spotless apply
samuelcostae 514064c
#2595 some refactors
samuelcostae File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
164 changes: 164 additions & 0 deletions
164
src/main/java/org/opensearch/security/dlic/rest/api/ExtensionRegistrationApiAction.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,164 @@ | ||
/* | ||
* 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.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import com.google.common.collect.ImmutableList; | ||
|
||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.client.Client; | ||
import org.opensearch.cluster.service.ClusterService; | ||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.inject.Inject; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.rest.RestChannel; | ||
import org.opensearch.rest.RestController; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.rest.RestRequest.Method; | ||
import org.opensearch.security.DefaultObjectMapper; | ||
import org.opensearch.security.auditlog.AuditLog; | ||
import org.opensearch.security.configuration.AdminDNs; | ||
import org.opensearch.security.configuration.ConfigurationRepository; | ||
import org.opensearch.security.dlic.rest.validation.AbstractConfigurationValidator; | ||
import org.opensearch.security.dlic.rest.validation.ExtensionRegistrationValidator; | ||
import org.opensearch.security.privileges.PrivilegesEvaluator; | ||
import org.opensearch.security.securityconf.impl.CType; | ||
import org.opensearch.security.securityconf.impl.SecurityDynamicConfiguration; | ||
import org.opensearch.security.ssl.transport.PrincipalExtractor; | ||
import org.opensearch.security.support.SecurityJsonNode; | ||
import org.opensearch.threadpool.ThreadPool; | ||
|
||
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix; | ||
|
||
public class ExtensionRegistrationApiAction extends AbstractApiAction { | ||
private static final List<Route> routes = addRoutesPrefix(ImmutableList.of( | ||
new Route(Method.GET, "/extensions/register"), | ||
new Route(Method.DELETE, "/extensions/register"), | ||
new Route(Method.PUT, "/extensions/register"), | ||
new Route(Method.PATCH, "/extension/register") | ||
)); | ||
|
||
//Sample Request | ||
// { | ||
// "unique_id": "hello_world", | ||
// "description": "Extension that greets the user", | ||
// "developer": "messages", | ||
// "indices": "messages", | ||
// "protected_indices": null, | ||
// "endpoints": "hello, goodbye", | ||
// "protected_endpoints": "/update/{name}" | ||
//} | ||
|
||
@Inject | ||
public ExtensionRegistrationApiAction(final Settings settings, final Path configPath, final RestController controller, | ||
final Client client, final AdminDNs adminDNs, final ConfigurationRepository cl, | ||
final ClusterService cs, final PrincipalExtractor principalExtractor, final PrivilegesEvaluator evaluator, | ||
ThreadPool threadPool, AuditLog auditLog) { | ||
super(settings, configPath, controller, client, adminDNs, cl, cs, principalExtractor, evaluator, threadPool, | ||
auditLog); | ||
} | ||
|
||
@Override | ||
protected boolean hasPermissionsToCreate(final SecurityDynamicConfiguration<?> dynamicConfigFactory, | ||
final Object content, | ||
final String resourceName) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public List<Route> routes() { | ||
return routes; | ||
} | ||
|
||
@Override | ||
protected Endpoint getEndpoint() { | ||
return Endpoint.EXTENSIONREGISTRATION; | ||
} | ||
|
||
@Override | ||
protected void handleGet(final RestChannel channel, RestRequest request, Client client, final JsonNode content) | ||
throws IOException{ | ||
|
||
createdResponse(channel, " updated"); | ||
|
||
; | ||
} | ||
|
||
@Override | ||
protected void handlePut(RestChannel channel, final RestRequest request, final Client client, final JsonNode content) throws IOException { | ||
|
||
final String uniqueId = request.param("unique_id"); | ||
final String description = request.param("description"); | ||
final String developer = request.param("developer"); | ||
final List<String> indices = Arrays.asList(request.param("indices")); | ||
final List<String> protected_indices = Arrays.asList(request.param("protected_indices")); | ||
final List<String> endpoints = Arrays.asList(request.param("endpoints")); | ||
final List<String> protected_endpoints = Arrays.asList(request.param("protected_endpoints")); | ||
|
||
final ObjectNode contentAsNode = (ObjectNode) content; | ||
|
||
contentAsNode.put("uniqueId", uniqueId); | ||
contentAsNode.put("description", description); | ||
contentAsNode.put("developer", developer); | ||
contentAsNode.put("indices", indices.toString()); | ||
contentAsNode.put("protected_indices", protected_indices.toString()); | ||
contentAsNode.put("endpoints", endpoints.toString()); | ||
contentAsNode.put("protected_endpoints", protected_endpoints.toString()); | ||
|
||
client.index(new IndexRequest("registered_extensions").source(contentAsNode)); | ||
|
||
if("allGood" == "allGood"){ | ||
generateAuthToken(); | ||
createdResponse(channel, "Extension " + uniqueId + " was Created"); | ||
} | ||
|
||
} | ||
|
||
private boolean save(RestRequest request) { | ||
|
||
return true; | ||
} | ||
|
||
@Override | ||
protected void filter(SecurityDynamicConfiguration<?> builder) { | ||
super.filter(builder); | ||
// replace password hashes in addition. We must not remove them from the | ||
// Builder since this would remove users completely if they | ||
// do not have any addition properties like roles or attributes | ||
builder.clearHashes(); | ||
} | ||
|
||
private String generateAuthToken(){ | ||
return "bearer: 9999999999999999"; | ||
} | ||
|
||
@Override | ||
protected String getResourceName() { | ||
return "extensionsregistry"; | ||
} | ||
|
||
@Override | ||
protected CType getConfigName() { | ||
return CType.CONFIG; | ||
} | ||
|
||
@Override | ||
protected AbstractConfigurationValidator getValidator(RestRequest request, BytesReference ref, Object... params) { | ||
return new ExtensionRegistrationValidator(request, isSuperAdmin(), ref, this.settings, params); | ||
} | ||
} |
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
42 changes: 42 additions & 0 deletions
42
...ain/java/org/opensearch/security/dlic/rest/validation/ExtensionRegistrationValidator.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,42 @@ | ||
/* | ||
* 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.validation; | ||
|
||
import org.opensearch.common.bytes.BytesReference; | ||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
/** | ||
* Validator for Internal Users Api Action. | ||
*/ | ||
public class ExtensionRegistrationValidator extends AbstractConfigurationValidator { | ||
|
||
public ExtensionRegistrationValidator(final RestRequest request, boolean isSuperAdmin, BytesReference ref, final Settings opensearchSettings, | ||
Object... param) { | ||
super(request, ref, opensearchSettings, param); | ||
allowedKeys.put("unique_id", DataType.STRING); | ||
allowedKeys.put("description", DataType.STRING); | ||
allowedKeys.put("developer", DataType.STRING); | ||
allowedKeys.put("indices", DataType.STRING); | ||
allowedKeys.put("protected_indices", DataType.STRING); | ||
allowedKeys.put("endpoints", DataType.STRING); | ||
allowedKeys.put("protected_endpoints", DataType.STRING); | ||
} | ||
|
||
@Override | ||
public boolean validate() { | ||
//TODO | ||
return true; | ||
|
||
|
||
} | ||
} |
101 changes: 101 additions & 0 deletions
101
src/test/java/org/opensearch/security/dlic/rest/api/ExtensionRegistrationApiActionTest.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,101 @@ | ||
/* | ||
* 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 org.apache.hc.core5.http.HttpStatus; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import org.opensearch.common.settings.Settings; | ||
import org.opensearch.rest.RestStatus; | ||
import org.opensearch.security.support.ConfigConstants; | ||
import org.opensearch.security.system_indices.SystemIndicesTests; | ||
import org.opensearch.security.test.DynamicSecurityConfig; | ||
import org.opensearch.security.test.helper.file.FileHelper; | ||
import org.opensearch.security.test.helper.rest.RestHelper; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX; | ||
|
||
public class ExtensionRegistrationApiActionTest extends SystemIndicesTests { | ||
private final String ENDPOINT = PLUGINS_PREFIX + "/api/extensions/register"; | ||
|
||
//Sample Request | ||
// { | ||
// "unique_id": "hello_world", | ||
// "description": "Extension that greets the user", | ||
// "developer": "messages", | ||
// "indices": "messages", | ||
// "protected_indices": null, | ||
// "endpoints": "hello, goodbye", | ||
// "protected_endpoints": "/update/{name}" | ||
//} | ||
|
||
private final String correctExtensionRequest = "{\"unique_id\":\"hello_world\",\"indices\":\"messages\",\"protected_indices\":\"null\",\"endpoints\":\"hello, goodbye\",\"protected_endpoints\":\"/update/{name}\"}\n"; | ||
|
||
private final String wrongExtensionRequest = " {\n" + " \"indices\": \"messages\",\n" + " \"protected_indices\": {},\n" + " \"endpoints\": \"/hello, /goodbye\",\n" + " \"protected_endpoints\": \"/update/{name}\"\n" + " }"; | ||
|
||
private void setupSettingsWithSsl() throws Exception { | ||
|
||
Settings systemIndexSettings = Settings.builder() | ||
.put(ConfigConstants.SECURITY_SYSTEM_INDICES_ENABLED_KEY, false) | ||
.put("plugins.security.ssl.http.enabled",true) | ||
.put("plugins.security.ssl.http.keystore_filepath", FileHelper.getAbsoluteFilePathFromClassPath("node-0-keystore.jks")) | ||
.put("plugins.security.ssl.http.truststore_filepath", FileHelper.getAbsoluteFilePathFromClassPath("truststore.jks")) | ||
.put("path.repo", repositoryPath.getRoot().getAbsolutePath()) | ||
.build(); | ||
setup(Settings.EMPTY, | ||
new DynamicSecurityConfig() | ||
.setConfig("config_system_indices.yml") | ||
.setSecurityRoles("roles_system_indices.yml") | ||
.setSecurityInternalUsers("internal_users_system_indices.yml") | ||
.setSecurityRolesMapping("roles_mapping_system_indices.yml"), | ||
systemIndexSettings, | ||
true); | ||
} | ||
private RestHelper keyStoreRestHelper() { | ||
RestHelper restHelper = restHelper(); | ||
restHelper.keystore = "kirk-keystore.jks"; | ||
restHelper.enableHTTPClientSSL = true; | ||
restHelper.trustHTTPServerCertificate = true; | ||
restHelper.sendAdminCertificate = true; | ||
return restHelper; | ||
} | ||
|
||
private RestHelper sslRestHelper() { | ||
RestHelper restHelper = restHelper(); | ||
restHelper.enableHTTPClientSSL = true; | ||
return restHelper; | ||
} | ||
@Test | ||
public void CorrectExtensionRegistrationShouldReturnCreatedTest() throws Exception { | ||
setupSettingsWithSsl(); | ||
|
||
RestHelper keyStoreRestHelper = keyStoreRestHelper(); | ||
RestHelper sslRestHelper = sslRestHelper(); | ||
|
||
//as Superadmin | ||
// RestHelper.HttpResponse responsea = keyStoreRestHelper.executeGetRequest( ENDPOINT, correctExtensionRequest); | ||
// assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode()); | ||
|
||
RestHelper.HttpResponse responsea = keyStoreRestHelper.executePutRequest( ENDPOINT, correctExtensionRequest); | ||
assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode()); | ||
|
||
//as admin | ||
// responsea = sslRestHelper.executeGetRequest( ENDPOINT, indexSettings, allAccessUserHeader); | ||
// assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode()); | ||
// | ||
// responsea = sslRestHelper.executePutRequest( ENDPOINT, indexSettings, allAccessUserHeader); | ||
// assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode()); | ||
} | ||
|
||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The request body arrives here empty .
An im missing something obvious?