This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 80
Local uri input monitor support #369
Open
AWSHurneyt
wants to merge
14
commits into
opendistro-for-elasticsearch:local-uri-input-monitor-support
Choose a base branch
from
AWSHurneyt:local-uri-input-monitor-support
base: local-uri-input-monitor-support
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
dc0c449
Implemented LocalUriInput and extension class
AWSHurneyt f33f9a4
Implemented SupportedApiSettings and extensions. Implemented logic fo…
AWSHurneyt 546b384
Implemented SupportedApiSettings and extensions. Implemented logic fo…
AWSHurneyt ab030dc
Merge branch 'local-uri-input-monitor-support' of github.com:AWSHurne…
AWSHurneyt c9a73fd
Implemented unit tests and integration tests for LocalUriInput
AWSHurneyt 708d00b
Refactored SupportedApiSettings and SupportedApiSettingsExtensions ba…
AWSHurneyt 3ec3d71
Implemented unit tests and integration tests for LocalUriInput
AWSHurneyt aa789dd
Refactored LocalUriInput and tests to confirm host is always localhos…
AWSHurneyt e819bfb
Refactored SupportedApiSettings and SupportedApiSettingsExtensions ba…
AWSHurneyt c50ab98
Merge branch 'local-uri-input-monitor-support' of github.com:AWSHurne…
AWSHurneyt 6111184
Refactored LocalUriInput to support receiving only a path as input, a…
AWSHurneyt 28d3fba
Implemented support for configuring SupportedApiSettings::supportedAp…
AWSHurneyt c05884a
Refactored resolveToActionRequest to remove redundant call to validat…
AWSHurneyt 1d03c61
Refactored an erroneous value assignment in the SupportedApiSettingsE…
AWSHurneyt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
91 changes: 91 additions & 0 deletions
91
...in/kotlin/com/amazon/opendistroforelasticsearch/alerting/settings/SupportedApiSettings.kt
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,91 @@ | ||
package com.amazon.opendistroforelasticsearch.alerting.settings | ||
|
||
import com.amazon.opendistroforelasticsearch.alerting.core.model.LocalUriInput | ||
import org.elasticsearch.action.ActionRequest | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest | ||
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequest | ||
import org.elasticsearch.common.xcontent.XContentHelper | ||
import org.elasticsearch.common.xcontent.json.JsonXContent | ||
|
||
/** | ||
* A class that supports storing a unique set of API paths that can be accessed by general users. | ||
*/ | ||
class SupportedApiSettings { | ||
companion object { | ||
const val CLUSTER_HEALTH_PATH = "/_cluster/health" | ||
const val CLUSTER_STATS_PATH = "/_cluster/stats" | ||
|
||
private const val RESOURCE_FILE = "supported_json_payloads.json" | ||
|
||
/** | ||
* The key in this map represents the path to call an API. | ||
* | ||
* NOTE: Paths should conform to the following pattern: | ||
* "/_cluster/stats" | ||
* | ||
* The value in these maps represents a path root mapped to a list of paths to field values. | ||
* If the value mapped to an API is an empty map, no fields will be redacted from the API response. | ||
* | ||
* NOTE: Keys in this map should consist of root components of the response body; e.g.,: | ||
* "indices" | ||
* | ||
* Values in these maps should consist of the remaining fields in the path | ||
* to the supported value separated by periods; e.g.,: | ||
* "shards.total", | ||
* "shards.index.shards.min" | ||
* | ||
* In this example for ClusterStats, the response will only include | ||
* the values at the end of these two paths: | ||
* "/_cluster/stats": { | ||
* "indices": [ | ||
* "shards.total", | ||
* "shards.index.shards.min" | ||
* ] | ||
* } | ||
*/ | ||
private var supportedApiList = HashMap<String, Map<String, ArrayList<String>>>() | ||
|
||
init { | ||
val supportedJsonPayloads = SupportedApiSettings::class.java.getResource(RESOURCE_FILE) | ||
@Suppress("UNCHECKED_CAST") | ||
if (supportedJsonPayloads != null) supportedApiList = | ||
XContentHelper.convertToMap( | ||
JsonXContent.jsonXContent, supportedJsonPayloads.readText(), false) as HashMap<String, Map<String, ArrayList<String>>> | ||
} | ||
|
||
/** | ||
* Returns the map of all supported json payload associated with the provided path from supportedApiList. | ||
* @param path The path for the requested API. | ||
* @return The map of all supported json payload for the requested API. | ||
* @throws IllegalArgumentException When supportedApiList does not contain a value for the provided key. | ||
*/ | ||
fun getSupportedJsonPayload(path: String): Map<String, ArrayList<String>> { | ||
return supportedApiList[path] ?: throw IllegalArgumentException("API path not in supportedApiList: $path") | ||
} | ||
|
||
/** | ||
* Will then return an [ActionRequest] for the API associated with that path. | ||
* Will otherwise throw an exception. | ||
* @param localUriInput The [LocalUriInput] to resolve. | ||
* @throws IllegalArgumentException when the requested API is not supported. | ||
* @return The [ActionRequest] for the API associated with the provided [LocalUriInput]. | ||
*/ | ||
fun resolveToActionRequest(localUriInput: LocalUriInput): ActionRequest { | ||
return when (val path = localUriInput.toConstructedUri().path) { | ||
CLUSTER_HEALTH_PATH -> ClusterHealthRequest() | ||
CLUSTER_STATS_PATH -> ClusterStatsRequest() | ||
else -> throw IllegalArgumentException("Unsupported API: $path") | ||
} | ||
} | ||
|
||
/** | ||
* Confirms whether the provided path is in [supportedApiList]. | ||
* Throws an exception if the provided path is not on the list; otherwise performs no action. | ||
* @param path The path to validate. | ||
* @throws IllegalArgumentException when supportedApiList does not contain the provided path. | ||
*/ | ||
fun validatePath(path: String) { | ||
if (!supportedApiList.contains(path)) throw IllegalArgumentException("API path not in supportedApiList: $path") | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
...lin/com/amazon/opendistroforelasticsearch/alerting/util/SupportedApiSettingsExtensions.kt
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,64 @@ | ||
package com.amazon.opendistroforelasticsearch.alerting.util | ||
|
||
import com.amazon.opendistroforelasticsearch.alerting.core.model.LocalUriInput | ||
import com.amazon.opendistroforelasticsearch.alerting.elasticapi.convertToMap | ||
import com.amazon.opendistroforelasticsearch.alerting.settings.SupportedApiSettings | ||
import com.amazon.opendistroforelasticsearch.alerting.settings.SupportedApiSettings.Companion.resolveToActionRequest | ||
import org.elasticsearch.action.ActionResponse | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest | ||
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse | ||
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsRequest | ||
import org.elasticsearch.action.admin.cluster.stats.ClusterStatsResponse | ||
import org.elasticsearch.client.Client | ||
import org.elasticsearch.common.xcontent.support.XContentMapValues | ||
|
||
/** | ||
* Calls the appropriate transport action for the API requested in the [localUriInput]. | ||
* @param localUriInput The [LocalUriInput] to resolve. | ||
* @param client The [Client] used to call the respective transport action. | ||
* @throws IllegalArgumentException When the requested API is not supported by this feature. | ||
*/ | ||
fun executeTransportAction(localUriInput: LocalUriInput, client: Client): ActionResponse { | ||
return when (val request = resolveToActionRequest(localUriInput)) { | ||
is ClusterHealthRequest -> client.admin().cluster().health(request).get() | ||
is ClusterStatsRequest -> client.admin().cluster().clusterStats(request).get() | ||
else -> throw IllegalArgumentException("Unsupported API request: ${request.javaClass.name}") | ||
} | ||
} | ||
|
||
/** | ||
* Populates a [HashMap] with the values in the [ActionResponse]. | ||
* @throws IllegalArgumentException when the [ActionResponse] is not supported by this feature. | ||
*/ | ||
fun ActionResponse.toMap(): Map<String, Any> { | ||
return when (this) { | ||
is ClusterHealthResponse -> redactFieldsFromResponse(this.convertToMap(), | ||
SupportedApiSettings.getSupportedJsonPayload(SupportedApiSettings.CLUSTER_HEALTH_PATH)) | ||
is ClusterStatsResponse -> redactFieldsFromResponse(this.convertToMap(), | ||
SupportedApiSettings.getSupportedJsonPayload(SupportedApiSettings.CLUSTER_STATS_PATH)) | ||
else -> throw IllegalArgumentException("Unsupported ActionResponse type: ${this.javaClass.name}") | ||
} | ||
} | ||
|
||
/** | ||
* Populates a [HashMap] with only the values that support being exposed to users. | ||
*/ | ||
@Suppress("UNCHECKED_CAST") | ||
fun redactFieldsFromResponse(mappedActionResponse: Map<String, Any>, supportedJsonPayload: Map<String, ArrayList<String>>): Map<String, Any> { | ||
return when { | ||
supportedJsonPayload.isEmpty() -> mappedActionResponse | ||
else -> { | ||
val output = hashMapOf<String, Any>() | ||
for ((key, value) in supportedJsonPayload) { | ||
when (val mappedValue = mappedActionResponse[key]) { | ||
is Map<*, *> -> output[key] = XContentMapValues.filter( | ||
mappedActionResponse[key] as MutableMap<String, *>?, | ||
value.toTypedArray(), arrayOf() | ||
) | ||
else -> output[key] = mappedValue ?: hashMapOf<String, Any>() | ||
} | ||
} | ||
output | ||
} | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...rces/com/amazon/opendistroforelasticsearch/alerting/settings/supported_json_payloads.json
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,4 @@ | ||
{ | ||
"/_cluster/health": {}, | ||
"/_cluster/stats": {} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we not want to make this a configurable setting for the customers? What is in the
supported_json_payloads.json
can be the default values.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For P0, the
supportedApiList
is configured using the JSON; users will need to make changes to the resource file in order to update that list. As the plan was to only supportClusterHealth
andClusterStats
for P0, an API to configure the list was not a high priority for P0.