Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Add logged-on User details to the Monitor and Destination #255

Merged
merged 8 commits into from
Sep 25, 2020

Conversation

skkosuri-amzn
Copy link
Contributor

Issue #, if available:
#6
#215

Description of changes:
Add logged-on User details to the Monitor and Destination
This one of PR's for adding security to alerting.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@codecov
Copy link

codecov bot commented Sep 23, 2020

Codecov Report

Merging #255 into master will increase coverage by 0.21%.
The diff coverage is 79.38%.

Impacted file tree graph

@@             Coverage Diff              @@
##             master     #255      +/-   ##
============================================
+ Coverage     77.31%   77.53%   +0.21%     
- Complexity      187      198      +11     
============================================
  Files           137      138       +1     
  Lines          4466     4522      +56     
  Branches        594      614      +20     
============================================
+ Hits           3453     3506      +53     
+ Misses          701      691      -10     
- Partials        312      325      +13     
Impacted Files Coverage Δ Complexity Δ
...relasticsearch/alerting/core/model/ScheduledJob.kt 86.66% <ø> (ø) 0.00 <0.00> (ø)
...ticsearch/alerting/elasticapi/ElasticExtensions.kt 56.81% <33.33%> (-1.72%) 0.00 <0.00> (ø)
...endistroforelasticsearch/alerting/model/Monitor.kt 88.70% <71.42%> (-2.52%) 0.00 <0.00> (ø)
...distroforelasticsearch/alerting/core/model/User.kt 81.39% <81.39%> (ø) 6.00 <6.00> (?)
...icsearch/alerting/model/destination/Destination.kt 62.93% <85.71%> (-2.32%) 0.00 <0.00> (ø)
...elasticsearch/alerting/action/GetMonitorRequest.kt 100.00% <100.00%> (+4.00%) 0.00 <0.00> (ø)
...e/action/node/ScheduledJobsStatsTransportAction.kt 75.00% <0.00%> (ø) 8.00% <0.00%> (ø%)
...asticsearch/alerting/core/schedule/JobScheduler.kt 75.71% <0.00%> (+4.28%) 15.00% <0.00%> (ø%)
...rch/alerting/core/action/node/ScheduledJobStats.kt 66.66% <0.00%> (+11.11%) 4.00% <0.00%> (+1.00%)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 48ba52a...8e1092f. Read the comment docs.

Comment on lines 63 to 68
if (srcContext != null) {
out.writeBoolean(true)
srcContext?.writeTo(out)
srcContext.writeTo(out)
} else {
out.writeBoolean(false)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Maybe cleaner?

out.writeBoolean(srcContext != null)
srcContext?.writeTo(out)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

like it 👍

@@ -117,8 +122,15 @@ data class Monitor(

override fun fromDocument(id: String, version: Long): Monitor = copy(id = id, version = version)

private fun XContentBuilder.optionalUserField(user: User?): XContentBuilder {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Move to ElasticExtensions

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1

@@ -131,6 +143,12 @@ data class Monitor(
schedule.writeTo(out)
out.writeInstant(lastUpdateTime)
out.writeOptionalInstant(enabledTime)
if (user != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Same as above

out.writeBoolean(user != null)
user?.writeTo(out)

@@ -0,0 +1,92 @@
package com.amazon.opendistroforelasticsearch.alerting.model
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing License


@Throws(IOException::class)
constructor(sin: StreamInput): this(
sin.readString(), // name
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I prefer using the named arguments instead of comments
i.e.

@Throws(IOException::class)
constructor(sin: StreamInput): this(
    name = sin.readString(),
    backendRoles = sin.readStringList(),
    roles = sin.readStringList(),
    customAttNames = sin.readStringList() 
)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

+1. Still kotlin newbie

}
}
}
return User(name, backendRoles, roles, customAttNames)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want any validation of the values here? Or is a User("", listOf(), listOf(), listOf()) a valid user?

@@ -164,6 +182,14 @@ fun Monitor.toJsonString(): String {
return this.toXContent(builder).string()
}

fun randomUser(): User {
return User("joe", listOf("ops", "backup"), listOf("all_access"), listOf("test_attr=test"))
Copy link
Contributor

Choose a reason for hiding this comment

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

These don't really seem random 😛

Copy link
Contributor Author

Choose a reason for hiding this comment

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

randomized

import org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken
import java.io.IOException

data class User(
Copy link
Contributor

Choose a reason for hiding this comment

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

I was under the impression we wanted to decouple the roles from the user when storing this on the Monitor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Having name, roles, backend_roles as user (sub) object is much clean. Checks are simpler. monitor.user

"type" : "text",
"fields" : {
"keyword" : {
"type" : "text"
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't this be type keyword? Same with below.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch. Yes it has to be keyword. Merged old version.

"type" : "text",
"fields" : {
"keyword" : {
"type" : "text"
Copy link
Contributor

Choose a reason for hiding this comment

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

Same comment as below.

val ranStrGen = RandomStringGenerator.Builder().build()
val randomUser = ranStrGen.generate(5)
val bckEndRole1 = ranStrGen.generate(10)
val bckEndRole2 = ranStrGen.generate(10)
Copy link
Contributor

Choose a reason for hiding this comment

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

There are already provided helper methods for generating random values, e.g. ESRestTestCase.randomAlphaOfLength(10)

lezzago
lezzago previously approved these changes Sep 25, 2020
} else {
out.writeBoolean(false)
}
out.writeBoolean(srcContext != null)

Choose a reason for hiding this comment

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

This is nice!

Copy link

@saratvemulapalli saratvemulapalli left a comment

Choose a reason for hiding this comment

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

Changes look good to me.

@skkosuri-amzn skkosuri-amzn merged commit 9697682 into master Sep 25, 2020
@qreshi qreshi deleted the add-user-pr branch December 19, 2020 00:45
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants