Skip to content
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

Adding Max HTTP Response Size IT #901

Merged
merged 4 commits into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ internal object PluginSettings {
private const val DEFAULT_SOCKET_TIMEOUT_MILLISECONDS = 50000

/**
* Default maximum HTTP response string length
* Default maximum HTTP response size
*/
private val DEFAULT_MAX_HTTP_RESPONSE_SIZE = SETTING_HTTP_MAX_CONTENT_LENGTH.getDefault(Settings.EMPTY).getBytes().toInt()
private val DEFAULT_MAX_HTTP_RESPONSE_SIZE = SETTING_HTTP_MAX_CONTENT_LENGTH.getDefault(Settings.EMPTY).bytes.toInt()

/**
* Default email header length. minimum value from 100 reference emails
Expand Down Expand Up @@ -235,7 +235,7 @@ internal object PluginSettings {
var socketTimeout: Int

/**
* Maximum HTTP response string length
* Maximum HTTP response size
*/
@Volatile
var maxHttpResponseSize: Int
Expand Down Expand Up @@ -513,7 +513,7 @@ internal object PluginSettings {
val clusterMaxHttpResponseSize = clusterService.clusterSettings.get(MAX_HTTP_RESPONSE_SIZE)
if (clusterMaxHttpResponseSize != null) {
log.debug("$LOG_PREFIX:$MAX_HTTP_RESPONSE_SIZE_KEY -autoUpdatedTo-> $clusterMaxHttpResponseSize")
socketTimeout = clusterSocketTimeout
maxHttpResponseSize = clusterMaxHttpResponseSize
}
val clusterAllowedConfigTypes = clusterService.clusterSettings.get(ALLOWED_CONFIG_TYPES)
if (clusterAllowedConfigTypes != null) {
Expand Down
1 change: 1 addition & 0 deletions notifications/notifications/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ configurations {
}

dependencies {
testImplementation project(path: ':opensearch-notifications-core')
toepkerd marked this conversation as resolved.
Show resolved Hide resolved
opensearchPlugin "org.opensearch.plugin:opensearch-notifications-core:${bwcPluginVersion}@zip"
opensearchPlugin "org.opensearch.plugin:notifications:${bwcPluginVersion}@zip"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

package org.opensearch.integtest

import com.sun.net.httpserver.HttpServer
import org.junit.AfterClass
import org.junit.Assert
import org.junit.BeforeClass
import org.opensearch.core.rest.RestStatus
import org.opensearch.notifications.NotificationPlugin
import org.opensearch.rest.RestRequest
import java.net.InetAddress
import java.net.InetSocketAddress

internal class MaxHTTPResponseSizeIT : PluginRestTestCase() {
toepkerd marked this conversation as resolved.
Show resolved Hide resolved
fun `test HTTP response has truncated size`() {
// update max http response size setting
val updateSettingJsonString = """
{
"transient": {
"opensearch.notifications.core.max_http_response_size": "8"
}
}
""".trimIndent()

val updateSettingsResponse = executeRequest(
RestRequest.Method.PUT.name,
"/_cluster/settings",
updateSettingJsonString,
RestStatus.OK.status
)
Assert.assertNotNull(updateSettingsResponse)
logger.info("update settings response: $updateSettingsResponse")
Thread.sleep(1000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any reason to wait for 1s

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


val url = "http://${server.address.hostString}:${server.address.port}/webhook"

val createRequestJsonString = """
{
"config":{
"name":"this is a sample config name",
"description":"this is a sample config description",
"config_type":"webhook",
"is_enabled":true,
"webhook":{
"url":"$url",
"header_params": {
"Content-type": "text/plain"
}
}
}
}
""".trimIndent()
val configId = createConfigWithRequestJsonString(createRequestJsonString)
Assert.assertNotNull(configId)
Thread.sleep(1000)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here


// send test message
val sendResponse = executeRequest(
RestRequest.Method.POST.name,
"${NotificationPlugin.PLUGIN_BASE_URI}/feature/test/$configId",
"",
RestStatus.OK.status
)

logger.info("response: $sendResponse")

val statusText = sendResponse.getAsJsonArray("status_list")[0].asJsonObject["delivery_status"].asJsonObject["status_text"].asString

// we set the max HTTP response size to 8 bytes, which means the expected string length of the response is 8 / 2 (bytes per Java char) = 4
Assert.assertEquals(4, statusText.length)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about we set max http response size to 9? how the result will look like?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it will do an int division truncation and limit the string to 4 chars

}

companion object {
private lateinit var server: HttpServer

@JvmStatic
@BeforeClass
fun setupWebhook() {
server = HttpServer.create(InetSocketAddress(InetAddress.getLoopbackAddress(), 0), 0)

server.createContext("/webhook") {
val response = "This is a longer than usual response that should be truncated"
it.sendResponseHeaders(200, response.length.toLong())
val os = it.responseBody
os.write(response.toByteArray())
os.close()
}

server.start()
}

@JvmStatic
@AfterClass
fun stopMockServer() {
server.stop(1)
}
}
}
Loading