Skip to content

Commit aa56d26

Browse files
Extract route paths prefixes into constants (#4173)
Signed-off-by: Andrey Pleskach <ples@aiven.io>
1 parent 864d8be commit aa56d26

File tree

7 files changed

+46
-20
lines changed

7 files changed

+46
-20
lines changed

src/main/java/com/amazon/dlic/auth/http/saml/AuthTokenProcessorHandler.java

+14-9
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ private AuthTokenProcessorAction.Response handleImpl(
137137
String samlResponseBase64,
138138
String samlRequestId,
139139
String acsEndpoint,
140-
Saml2Settings saml2Settings
140+
Saml2Settings saml2Settings,
141+
String requestPath // the parameter will be removed in the future as soon as we will read of legacy paths aka
142+
// /_opendistro/_security/...
141143
) {
142144
if (token_log.isDebugEnabled()) {
143145
try {
@@ -156,7 +158,7 @@ private AuthTokenProcessorAction.Response handleImpl(
156158
final SamlResponse samlResponse = new SamlResponse(saml2Settings, acsEndpoint, samlResponseBase64);
157159

158160
if (!samlResponse.isValid(samlRequestId)) {
159-
log.warn("Error while validating SAML response in /_opendistro/_security/api/authtoken");
161+
log.warn("Error while validating SAML response in {}", requestPath);
160162
return null;
161163
}
162164

@@ -178,17 +180,14 @@ private Optional<SecurityResponse> handleLowLevel(RestRequest restRequest) throw
178180

179181
if (restRequest.getMediaType() != XContentType.JSON) {
180182
throw new OpenSearchSecurityException(
181-
"/_opendistro/_security/api/authtoken expects content with type application/json",
183+
restRequest.path() + " expects content with type application/json",
182184
RestStatus.UNSUPPORTED_MEDIA_TYPE
183185
);
184186

185187
}
186188

187189
if (restRequest.method() != Method.POST) {
188-
throw new OpenSearchSecurityException(
189-
"/_opendistro/_security/api/authtoken expects POST requests",
190-
RestStatus.METHOD_NOT_ALLOWED
191-
);
190+
throw new OpenSearchSecurityException(restRequest.path() + " expects POST requests", RestStatus.METHOD_NOT_ALLOWED);
192191
}
193192

194193
Saml2Settings saml2Settings = this.saml2SettingsProvider.getCached();
@@ -218,7 +217,13 @@ private Optional<SecurityResponse> handleLowLevel(RestRequest restRequest) throw
218217
acsEndpoint = getAbsoluteAcsEndpoint(((ObjectNode) jsonRoot).get("acsEndpoint").textValue());
219218
}
220219

221-
AuthTokenProcessorAction.Response responseBody = this.handleImpl(samlResponseBase64, samlRequestId, acsEndpoint, saml2Settings);
220+
AuthTokenProcessorAction.Response responseBody = this.handleImpl(
221+
samlResponseBase64,
222+
samlRequestId,
223+
acsEndpoint,
224+
saml2Settings,
225+
restRequest.path()
226+
);
222227

223228
if (responseBody == null) {
224229
return Optional.empty();
@@ -228,7 +233,7 @@ private Optional<SecurityResponse> handleLowLevel(RestRequest restRequest) throw
228233

229234
return Optional.of(new SecurityResponse(HttpStatus.SC_OK, null, responseBodyString, XContentType.JSON.mediaType()));
230235
} catch (JsonProcessingException e) {
231-
log.warn("Error while parsing JSON for /_opendistro/_security/api/authtoken", e);
236+
log.warn("Error while parsing JSON for {}", restRequest.path(), e);
232237
return Optional.of(new SecurityResponse(HttpStatus.SC_BAD_REQUEST, "JSON could not be parsed"));
233238
}
234239
}

src/main/java/org/opensearch/security/action/onbehalf/CreateOnBehalfOfTokenAction.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@
3232
import org.opensearch.security.identity.SecurityTokenManager;
3333

3434
import static org.opensearch.rest.RestRequest.Method.POST;
35+
import static org.opensearch.security.dlic.rest.support.Utils.PLUGIN_API_ROUTE_PREFIX;
3536
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix;
3637

3738
public class CreateOnBehalfOfTokenAction extends BaseRestHandler {
3839

3940
private static final List<Route> routes = addRoutesPrefix(
4041
ImmutableList.of(new NamedRoute.Builder().method(POST).path("/generateonbehalfoftoken").uniqueName("security:obo/create").build()),
41-
"/_plugins/_security/api"
42+
PLUGIN_API_ROUTE_PREFIX
4243
);
4344

4445
public static final long OBO_DEFAULT_EXPIRY_SECONDS = 5 * 60;

src/main/java/org/opensearch/security/dlic/rest/support/Utils.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,19 @@
5555
import org.opensearch.security.user.User;
5656

5757
import static org.opensearch.core.xcontent.DeprecationHandler.THROW_UNSUPPORTED_OPERATION;
58+
import static org.opensearch.security.OpenSearchSecurityPlugin.LEGACY_OPENDISTRO_PREFIX;
59+
import static org.opensearch.security.OpenSearchSecurityPlugin.PLUGINS_PREFIX;
5860

5961
public class Utils {
6062

63+
public final static String PLUGIN_ROUTE_PREFIX = "/" + PLUGINS_PREFIX;
64+
65+
public final static String LEGACY_PLUGIN_ROUTE_PREFIX = "/" + LEGACY_OPENDISTRO_PREFIX;
66+
67+
public final static String PLUGIN_API_ROUTE_PREFIX = PLUGIN_ROUTE_PREFIX + "/api";
68+
69+
public final static String LEGACY_PLUGIN_API_ROUTE_PREFIX = LEGACY_PLUGIN_ROUTE_PREFIX + "/api";
70+
6171
private static final ObjectMapper internalMapper = new ObjectMapper();
6272

6373
public static Map<String, Object> convertJsonToxToStructuredMap(ToXContent jsonContent) {
@@ -217,7 +227,7 @@ public static Set<String> generateFieldResourcePaths(final Set<String> fields, f
217227
*Total number of routes is expanded as twice as the number of routes passed in
218228
*/
219229
public static List<Route> addRoutesPrefix(List<Route> routes) {
220-
return addRoutesPrefix(routes, "/_opendistro/_security/api", "/_plugins/_security/api");
230+
return addRoutesPrefix(routes, LEGACY_PLUGIN_API_ROUTE_PREFIX, PLUGIN_API_ROUTE_PREFIX);
221231
}
222232

223233
/**
@@ -248,7 +258,7 @@ public static List<Route> addRoutesPrefix(List<Route> routes, final String... pr
248258
*Total number of routes is expanded as twice as the number of routes passed in
249259
*/
250260
public static List<DeprecatedRoute> addDeprecatedRoutesPrefix(List<DeprecatedRoute> deprecatedRoutes) {
251-
return addDeprecatedRoutesPrefix(deprecatedRoutes, "/_opendistro/_security/api", "/_plugins/_security/api");
261+
return addDeprecatedRoutesPrefix(deprecatedRoutes, LEGACY_PLUGIN_API_ROUTE_PREFIX, PLUGIN_API_ROUTE_PREFIX);
252262
}
253263

254264
/**

src/main/java/org/opensearch/security/rest/DashboardsInfoAction.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,19 @@
5050

5151
import static org.opensearch.rest.RestRequest.Method.GET;
5252
import static org.opensearch.rest.RestRequest.Method.POST;
53+
import static org.opensearch.security.dlic.rest.support.Utils.LEGACY_PLUGIN_ROUTE_PREFIX;
54+
import static org.opensearch.security.dlic.rest.support.Utils.PLUGIN_ROUTE_PREFIX;
5355
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix;
5456

5557
public class DashboardsInfoAction extends BaseRestHandler {
5658

5759
private static final List<Route> routes = ImmutableList.<Route>builder()
5860
.addAll(
59-
addRoutesPrefix(ImmutableList.of(new Route(GET, "/dashboardsinfo"), new Route(POST, "/dashboardsinfo")), "/_plugins/_security")
61+
addRoutesPrefix(ImmutableList.of(new Route(GET, "/dashboardsinfo"), new Route(POST, "/dashboardsinfo")), PLUGIN_ROUTE_PREFIX)
62+
)
63+
.addAll(
64+
addRoutesPrefix(ImmutableList.of(new Route(GET, "/kibanainfo"), new Route(POST, "/kibanainfo")), LEGACY_PLUGIN_ROUTE_PREFIX)
6065
)
61-
.addAll(addRoutesPrefix(ImmutableList.of(new Route(GET, "/kibanainfo"), new Route(POST, "/kibanainfo")), "/_opendistro/_security"))
6266
.build();
6367

6468
private final Logger log = LogManager.getLogger(this.getClass());

src/main/java/org/opensearch/security/rest/SecurityHealthAction.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@
4444

4545
import static org.opensearch.rest.RestRequest.Method.GET;
4646
import static org.opensearch.rest.RestRequest.Method.POST;
47+
import static org.opensearch.security.dlic.rest.support.Utils.LEGACY_PLUGIN_ROUTE_PREFIX;
48+
import static org.opensearch.security.dlic.rest.support.Utils.PLUGIN_ROUTE_PREFIX;
4749
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix;
4850

4951
public class SecurityHealthAction extends BaseRestHandler {
5052
private static final List<Route> routes = addRoutesPrefix(
5153
ImmutableList.of(new Route(GET, "/health"), new Route(POST, "/health")),
52-
"/_opendistro/_security",
53-
"/_plugins/_security"
54+
LEGACY_PLUGIN_ROUTE_PREFIX,
55+
PLUGIN_ROUTE_PREFIX
5456
);
5557

5658
private final BackendRegistry registry;

src/main/java/org/opensearch/security/rest/SecurityInfoAction.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,15 @@
5757

5858
import static org.opensearch.rest.RestRequest.Method.GET;
5959
import static org.opensearch.rest.RestRequest.Method.POST;
60+
import static org.opensearch.security.dlic.rest.support.Utils.LEGACY_PLUGIN_ROUTE_PREFIX;
61+
import static org.opensearch.security.dlic.rest.support.Utils.PLUGIN_ROUTE_PREFIX;
6062
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix;
6163

6264
public class SecurityInfoAction extends BaseRestHandler {
6365
private static final List<Route> routes = addRoutesPrefix(
6466
ImmutableList.of(new Route(GET, "/authinfo"), new Route(POST, "/authinfo")),
65-
"/_opendistro/_security",
66-
"/_plugins/_security"
67+
LEGACY_PLUGIN_ROUTE_PREFIX,
68+
PLUGIN_ROUTE_PREFIX
6769
);
6870

6971
private final Logger log = LogManager.getLogger(this.getClass());

src/main/java/org/opensearch/security/rest/TenantInfoAction.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,15 @@
6161

6262
import static org.opensearch.rest.RestRequest.Method.GET;
6363
import static org.opensearch.rest.RestRequest.Method.POST;
64+
import static org.opensearch.security.dlic.rest.support.Utils.LEGACY_PLUGIN_ROUTE_PREFIX;
65+
import static org.opensearch.security.dlic.rest.support.Utils.PLUGIN_ROUTE_PREFIX;
6466
import static org.opensearch.security.dlic.rest.support.Utils.addRoutesPrefix;
6567

6668
public class TenantInfoAction extends BaseRestHandler {
6769
private static final List<Route> routes = addRoutesPrefix(
6870
ImmutableList.of(new Route(GET, "/tenantinfo"), new Route(POST, "/tenantinfo")),
69-
"/_opendistro/_security",
70-
"/_plugins/_security"
71+
LEGACY_PLUGIN_ROUTE_PREFIX,
72+
PLUGIN_ROUTE_PREFIX
7173
);
7274

7375
private final Logger log = LogManager.getLogger(this.getClass());

0 commit comments

Comments
 (0)