Skip to content

Commit

Permalink
opensearch-project#2595 request payload changes
Browse files Browse the repository at this point in the history
Signed-off-by: scosta <[email protected]>
  • Loading branch information
samuelcostae committed May 8, 2023
1 parent d53251e commit 9ccbe9e
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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;
Expand Down Expand Up @@ -58,8 +59,8 @@ public class ExtensionRegistrationApiAction extends AbstractApiAction {
// "description": "Extension that greets the user",
// "developer": "messages",
// "indices": "messages",
// "protected_indices": {},
// "endpoints": "/hello, /goodbye",
// "protected_indices": null,
// "endpoints": "hello, goodbye",
// "protected_endpoints": "/update/{name}"
//}

Expand Down Expand Up @@ -102,42 +103,30 @@ protected void handleGet(final RestChannel channel, RestRequest request, Client
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("unique_id");
final String developer = 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 String username = request.param("name");
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().source(contentAsNode));

if(save(request)){
if("allGood" == "allGood"){
generateAuthToken();
createdResponse(channel, "'" + uniqueId + "' updated");
createdResponse(channel, "Extension " + uniqueId + " was Created");
}

final SecurityDynamicConfiguration<?> internalUsersConfiguration = load(getConfigName(), false);

final ObjectNode contentAsNode = (ObjectNode) content;
final SecurityJsonNode securityJsonNode = new SecurityJsonNode(contentAsNode);

// if password is set, it takes precedence over hash
final String plainTextPassword = securityJsonNode.get("password").asString();
final String origHash = securityJsonNode.get("hash").asString();
// if (plainTextPassword != null && plainTextPassword.length() > 0) {
// contentAsNode.remove("password");
// contentAsNode.put("hash", hash(plainTextPassword.toCharArray()));
// } else if (origHash != null && origHash.length() > 0) {
// contentAsNode.remove("password");
// } else if (plainTextPassword != null && plainTextPassword.isEmpty() && origHash == null) {
// contentAsNode.remove("password");
// }


// checks complete, create or update the user
internalUsersConfiguration.putCObject(username, DefaultObjectMapper.readTree(contentAsNode, internalUsersConfiguration.getImplementingClass()));

}

private boolean save(RestRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@ public class ExtensionRegistrationApiActionTest extends SystemIndicesTests {
//Sample Request
// {
// "unique_id": "hello_world",
// "description": "Extension that greets the user",
// "developer": "messages",
// "indices": "messages",
// "protected_indices": {},
// "endpoints": "/hello, /goodbye",
// "protected_indices": null,
// "endpoints": "hello, goodbye",
// "protected_endpoints": "/update/{name}"
//}
private final String correctExtRequest = " {\n" + " \"unique_id\": \"hello_world\",\n" + " \"indices\": \"messages\",\n" + " \"protected_indices\": {},\n" + " \"endpoints\": \"/hello, /goodbye\",\n" + " \"protected_endpoints\": \"/update/{name}\"\n" + " }";

private final String correctExtRequest = "{\"unique_id\":\"hello_world\",\"indices\":\"messages\",\"protected_indices\":\"null\",\"endpoints\":\"hello, goodbye\",\"protected_endpoints\":\"/update/{name}\"}\n";

private final String wrongExtRequest = " {\n" + " \"indices\": \"messages\",\n" + " \"protected_indices\": {},\n" + " \"endpoints\": \"/hello, /goodbye\",\n" + " \"protected_endpoints\": \"/update/{name}\"\n" + " }";

Expand Down Expand Up @@ -74,23 +77,23 @@ private RestHelper sslRestHelper() {
return restHelper;
}
@Test
public void tempTestForExtensionRegistrationAPiActionRemoveAfter() throws Exception {
public void CorrectExtensionRegistrationShouldReturnCreatedTest() throws Exception {
setupSettingsWithSsl();

RestHelper keyStoreRestHelper = keyStoreRestHelper();
RestHelper sslRestHelper = sslRestHelper();

String indexSettings = "{\n" +
" \"index\" : {\n" +
" \"unique_id\" : {\n" +
" \"refresh_interval\" : null\n" +
" }\n" +
"}";

//as Superadmin
RestHelper.HttpResponse responsea = keyStoreRestHelper.executeGetRequest( ENDPOINT, indexSettings);
assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode());
// RestHelper.HttpResponse responsea = keyStoreRestHelper.executeGetRequest( ENDPOINT, correctExtRequest);
// assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode());

responsea = keyStoreRestHelper.executePutRequest( ENDPOINT, indexSettings);
RestHelper.HttpResponse responsea = keyStoreRestHelper.executePutRequest( ENDPOINT, correctExtRequest);
assertEquals(RestStatus.CREATED.getStatus(), responsea.getStatusCode());

//as admin
Expand Down

0 comments on commit 9ccbe9e

Please sign in to comment.