forked from opensearch-project/flow-framework
-
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.
Extract tenant id from REST header into RestAction
Signed-off-by: Daniel Widdis <[email protected]>
- Loading branch information
Showing
14 changed files
with
200 additions
and
2 deletions.
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
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
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
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
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
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
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
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
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
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
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
54 changes: 54 additions & 0 deletions
54
src/main/java/org/opensearch/flowframework/util/RestActionUtils.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,54 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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.flowframework.util; | ||
|
||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.flowframework.common.CommonValue; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.rest.RestRequest; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* Utility methods for Rest Handlers | ||
*/ | ||
public class RestActionUtils { | ||
|
||
private RestActionUtils() {} | ||
|
||
/** | ||
* Finds the tenant id in the REST Headers | ||
* @param isMultiTenancyEnabled whether multitenancy is enabled | ||
* @param restRequest the RestRequest | ||
* @return The tenant ID from the headers or null if multitenancy is not enabled | ||
*/ | ||
public static String getTenantID(Boolean isMultiTenancyEnabled, RestRequest restRequest) { | ||
if (isMultiTenancyEnabled) { | ||
Map<String, List<String>> headers = restRequest.getHeaders(); | ||
if (headers.containsKey(CommonValue.TENANT_ID_HEADER)) { | ||
List<String> tenantIdList = headers.get(CommonValue.TENANT_ID_HEADER); | ||
if (tenantIdList != null && !tenantIdList.isEmpty()) { | ||
String tenantId = tenantIdList.get(0); | ||
if (tenantId != null) { | ||
return tenantId; | ||
} else { | ||
throw new FlowFrameworkException("Tenant ID can't be null", RestStatus.FORBIDDEN); | ||
} | ||
} else { | ||
throw new FlowFrameworkException("Tenant ID header is present but has no value", RestStatus.FORBIDDEN); | ||
} | ||
} else { | ||
throw new FlowFrameworkException("Tenant ID header is missing", RestStatus.FORBIDDEN); | ||
} | ||
} else { | ||
return null; | ||
} | ||
} | ||
} |
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
110 changes: 110 additions & 0 deletions
110
src/test/java/org/opensearch/flowframework/util/RestActionUtilsTests.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,110 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* 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.flowframework.util; | ||
|
||
import org.opensearch.core.rest.RestStatus; | ||
import org.opensearch.flowframework.common.CommonValue; | ||
import org.opensearch.flowframework.exception.FlowFrameworkException; | ||
import org.opensearch.rest.RestRequest; | ||
import org.opensearch.test.OpenSearchTestCase; | ||
import org.opensearch.test.rest.FakeRestRequest; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
public class RestActionUtilsTests extends OpenSearchTestCase { | ||
@Test | ||
public void testGetTenantID() { | ||
String tenantId = "test-tenant"; | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
headers.put(CommonValue.TENANT_ID_HEADER, Collections.singletonList(tenantId)); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
String actualTenantID = RestActionUtils.getTenantID(Boolean.TRUE, restRequest); | ||
Assert.assertEquals(tenantId, actualTenantID); | ||
} | ||
|
||
@Test | ||
public void testGetTenantID_NullTenantID() { | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
headers.put(CommonValue.TENANT_ID_HEADER, Collections.singletonList(null)); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
try { | ||
RestActionUtils.getTenantID(Boolean.TRUE, restRequest); | ||
Assert.fail("Expected FlowFrameworkException"); | ||
} catch (Exception e) { | ||
Assert.assertTrue(e instanceof FlowFrameworkException); | ||
Assert.assertEquals(RestStatus.FORBIDDEN, ((FlowFrameworkException) e).status()); | ||
Assert.assertEquals("Tenant ID can't be null", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetTenantID_NoMultiTenancy() { | ||
String tenantId = "test-tenant"; | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
headers.put(CommonValue.TENANT_ID_HEADER, Collections.singletonList(tenantId)); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
String tenantID = RestActionUtils.getTenantID(Boolean.FALSE, restRequest); | ||
Assert.assertNull(tenantID); | ||
} | ||
|
||
@Test | ||
public void testGetTenantID_EmptyTenantIDList() { | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
headers.put(CommonValue.TENANT_ID_HEADER, Collections.emptyList()); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
FlowFrameworkException exception = expectThrows( | ||
FlowFrameworkException.class, | ||
() -> RestActionUtils.getTenantID(Boolean.TRUE, restRequest) | ||
); | ||
assertEquals(RestStatus.FORBIDDEN, exception.status()); | ||
assertEquals("Tenant ID header is present but has no value", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testGetTenantID_MissingTenantIDHeader() { | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
FlowFrameworkException exception = expectThrows( | ||
FlowFrameworkException.class, | ||
() -> RestActionUtils.getTenantID(Boolean.TRUE, restRequest) | ||
); | ||
assertEquals(RestStatus.FORBIDDEN, exception.status()); | ||
assertEquals("Tenant ID header is missing", exception.getMessage()); | ||
} | ||
|
||
@Test | ||
public void testGetTenantID_MultipleValues() { | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
headers.put(CommonValue.TENANT_ID_HEADER, List.of("tenant1", "tenant2")); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
String actualTenantID = RestActionUtils.getTenantID(Boolean.TRUE, restRequest); | ||
assertEquals("tenant1", actualTenantID); | ||
} | ||
|
||
@Test | ||
public void testGetTenantID_EmptyStringTenantID() { | ||
Map<String, List<String>> headers = new HashMap<>(); | ||
headers.put(CommonValue.TENANT_ID_HEADER, Collections.singletonList("")); | ||
RestRequest restRequest = new FakeRestRequest.Builder(xContentRegistry()).withHeaders(headers).build(); | ||
|
||
String actualTenantID = RestActionUtils.getTenantID(Boolean.TRUE, restRequest); | ||
assertEquals("", actualTenantID); | ||
} | ||
} |