From 58e019a23dbc150652c656275a194ccc4f0a705d Mon Sep 17 00:00:00 2001 From: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com> Date: Thu, 13 Jul 2023 16:40:46 +0100 Subject: [PATCH] =?UTF-8?q?Added=20the=20addition=20of=20the=20server=20ve?= =?UTF-8?q?rsion=20and=20distribution=20as=20a=20respon=E2=80=A6=20(#8084)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Added server distribution and version into response header Signed-off-by: Sam Hobbs * Removed extra lines Signed-off-by: Sam Hobbs * Update server/src/main/java/org/opensearch/http/DefaultRestChannel.java Co-authored-by: Owais Kazi Signed-off-by: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com> * Moved defining of server header string Signed-off-by: Sam Hobbs * Removed wildcard import Signed-off-by: Sam Hobbs * Applied spotless to files Signed-off-by: Sam Hobbs * Added to testHeadersSet test within the default rest channel test file to test that server version is returned Signed-off-by: Sam Hobbs * Ran spotless Signed-off-by: Sam Hobbs * Added comment Signed-off-by: Sam Hobbs * Created static string for reused server header title and name of application strings, also refactored server header to server version Signed-off-by: Sam Hobbs * Refactored hash map from server header to server version and updated test Signed-off-by: Sam Hobbs * Ran spotless Signed-off-by: Sam Hobbs * Moved server version field from global variable to local, defined sever string as static value Signed-off-by: Sam Hobbs * Added prefix to header title Signed-off-by: Sam Hobbs * Removed redundant serverVersion field and instead inlined creation of map passed into addCustomHeader method Signed-off-by: Sam Hobbs * Ran spotless Signed-off-by: Sam Hobbs * Removed redundant OPEN_SEARCH_NAME_VALUE Signed-off-by: Sam Hobbs * Update server/src/test/java/org/opensearch/http/DefaultRestChannelTests.java Co-authored-by: Andriy Redko Signed-off-by: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com> * Moved change log addition to 2.x unreleased and wrote message in imperative Signed-off-by: Sam Hobbs * Corrected name of fetched header in header test Signed-off-by: Sam Hobbs * Added server version header value as constant Signed-off-by: Sam Hobbs --------- Signed-off-by: Sam Hobbs Signed-off-by: Sam Hobbs <118170270+Sam-ScottLogic@users.noreply.github.com> Co-authored-by: Owais Kazi Co-authored-by: Andriy Redko Signed-off-by: Shivansh Arora --- CHANGELOG.md | 1 + .../java/org/opensearch/http/DefaultRestChannel.java | 11 ++++++++++- .../org/opensearch/http/DefaultRestChannelTests.java | 5 +++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ef26caadc48bf..d5c54a5c695a8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -105,6 +105,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Create concept of persistent ThreadContext headers that are unstashable ([#8291]()https://github.com/opensearch-project/OpenSearch/pull/8291) - [Search pipelines] Add Global Ignore_failure options for Processors ([#8373](https://github.com/opensearch-project/OpenSearch/pull/8373)) - Enable Partial Flat Object ([#7997](https://github.com/opensearch-project/OpenSearch/pull/7997)) +- Add server version as REST response header [#6583](https://github.com/opensearch-project/OpenSearch/issues/6583) - Add jdk.incubator.vector module support for JDK 20+ ([#8601](https://github.com/opensearch-project/OpenSearch/pull/8601)) - Introduce full support for Search Pipeline ([#8613](https://github.com/opensearch-project/OpenSearch/pull/8613)) diff --git a/server/src/main/java/org/opensearch/http/DefaultRestChannel.java b/server/src/main/java/org/opensearch/http/DefaultRestChannel.java index 75a2a68dd78fe..523a89e05caa3 100644 --- a/server/src/main/java/org/opensearch/http/DefaultRestChannel.java +++ b/server/src/main/java/org/opensearch/http/DefaultRestChannel.java @@ -32,6 +32,7 @@ package org.opensearch.http; +import org.opensearch.Build; import org.opensearch.action.ActionListener; import org.opensearch.common.Nullable; import org.opensearch.core.common.bytes.BytesArray; @@ -48,7 +49,6 @@ import org.opensearch.rest.RestRequest; import org.opensearch.rest.RestResponse; import org.opensearch.core.rest.RestStatus; - import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -69,6 +69,12 @@ public class DefaultRestChannel extends AbstractRestChannel implements RestChann static final String CONTENT_TYPE = "content-type"; static final String CONTENT_LENGTH = "content-length"; static final String SET_COOKIE = "set-cookie"; + static final String SERVER_VERSION = "X-OpenSearch-Version"; + static final String SERVER_VERSION_VALUE = "OpenSearch/" + + Build.CURRENT.getQualifiedVersion() + + " (" + + Build.CURRENT.getDistribution() + + ")"; private final HttpRequest httpRequest; private final BigArrays bigArrays; @@ -76,6 +82,7 @@ public class DefaultRestChannel extends AbstractRestChannel implements RestChann private final ThreadContext threadContext; private final HttpChannel httpChannel; private final CorsHandler corsHandler; + private final Map> SERVER_VERSION_HEADER = Map.of(SERVER_VERSION, List.of(SERVER_VERSION_VALUE)); @Nullable private final HttpTracer tracerLog; @@ -147,6 +154,8 @@ public void sendResponse(RestResponse restResponse) { addCustomHeaders(httpResponse, restResponse.getHeaders()); addCustomHeaders(httpResponse, threadContext.getResponseHeaders()); + addCustomHeaders(httpResponse, SERVER_VERSION_HEADER); + // If our response doesn't specify a content-type header, set one setHeaderField(httpResponse, CONTENT_TYPE, restResponse.contentType(), false); // If our response has no content-length, calculate and set one diff --git a/server/src/test/java/org/opensearch/http/DefaultRestChannelTests.java b/server/src/test/java/org/opensearch/http/DefaultRestChannelTests.java index 0338c9009ab86..1ffe0538edaad 100644 --- a/server/src/test/java/org/opensearch/http/DefaultRestChannelTests.java +++ b/server/src/test/java/org/opensearch/http/DefaultRestChannelTests.java @@ -32,6 +32,7 @@ package org.opensearch.http; +import org.opensearch.Build; import org.opensearch.action.ActionListener; import org.opensearch.core.common.bytes.BytesArray; import org.opensearch.core.common.bytes.BytesReference; @@ -189,6 +190,10 @@ public void testHeadersSet() { assertEquals("abc", headers.get(Task.X_OPAQUE_ID).get(0)); assertEquals(Integer.toString(resp.content().length()), headers.get(DefaultRestChannel.CONTENT_LENGTH).get(0)); assertEquals(resp.contentType(), headers.get(DefaultRestChannel.CONTENT_TYPE).get(0)); + assertEquals( + "OpenSearch/" + Build.CURRENT.getQualifiedVersion() + " (" + Build.CURRENT.getDistribution() + ")", + headers.get("X-OpenSearch-Version").get(0) + ); } public void testCookiesSet() {