-
Notifications
You must be signed in to change notification settings - Fork 24.8k
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
Watcher: Store username on watch execution #31873
Changes from 1 commit
a5874e0
45ad8b7
560111e
db6d207
0edc33e
c2c49d8
dcd734d
5a467d0
a1ea27d
b49b2b5
d54bcbe
0dc862f
cf3d82e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,12 +43,14 @@ public abstract class WatchRecord implements ToXContentObject { | |
private static final ParseField METADATA = new ParseField("metadata"); | ||
private static final ParseField EXECUTION_RESULT = new ParseField("result"); | ||
private static final ParseField EXCEPTION = new ParseField("exception"); | ||
private static final ParseField EXECUTED_BY = new ParseField("executed_by"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about simply naming this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. one simple change to affect the whole PR ;) |
||
|
||
protected final Wid id; | ||
protected final Watch watch; | ||
private final String nodeId; | ||
protected final TriggerEvent triggerEvent; | ||
protected final ExecutionState state; | ||
private final String executedBy; | ||
|
||
// only emitted to xcontent in "debug" mode | ||
protected final Map<String, Object> vars; | ||
|
@@ -60,7 +62,7 @@ public abstract class WatchRecord implements ToXContentObject { | |
|
||
private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, Map<String, Object> vars, ExecutableInput input, | ||
ExecutableCondition condition, Map<String, Object> metadata, Watch watch, WatchExecutionResult executionResult, | ||
String nodeId) { | ||
String nodeId, String executedBy) { | ||
this.id = id; | ||
this.triggerEvent = triggerEvent; | ||
this.state = state; | ||
|
@@ -71,15 +73,16 @@ private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, Map | |
this.executionResult = executionResult; | ||
this.watch = watch; | ||
this.nodeId = nodeId; | ||
this.executedBy = executedBy; | ||
} | ||
|
||
private WatchRecord(Wid id, TriggerEvent triggerEvent, ExecutionState state, String nodeId) { | ||
this(id, triggerEvent, state, Collections.emptyMap(), null, null, null, null, null, nodeId); | ||
this(id, triggerEvent, state, Collections.emptyMap(), null, null, null, null, null, nodeId, null); | ||
} | ||
|
||
private WatchRecord(WatchRecord record, ExecutionState state) { | ||
this(record.id, record.triggerEvent, state, record.vars, record.input, record.condition, record.metadata, record.watch, | ||
record.executionResult, record.nodeId); | ||
record.executionResult, record.nodeId, record.executedBy); | ||
} | ||
|
||
private WatchRecord(WatchExecutionContext context, ExecutionState state) { | ||
|
@@ -88,12 +91,13 @@ private WatchRecord(WatchExecutionContext context, ExecutionState state) { | |
context.watch() != null ? context.watch().condition() : null, | ||
context.watch() != null ? context.watch().metadata() : null, | ||
context.watch(), | ||
null, context.getNodeId()); | ||
null, context.getNodeId(), context.getExecutedBy()); | ||
} | ||
|
||
private WatchRecord(WatchExecutionContext context, WatchExecutionResult executionResult) { | ||
this(context.id(), context.triggerEvent(), getState(executionResult), context.vars(), context.watch().input(), | ||
context.watch().condition(), context.watch().metadata(), context.watch(), executionResult, context.getNodeId()); | ||
context.watch().condition(), context.watch().metadata(), context.watch(), executionResult, context.getNodeId(), | ||
context.getExecutedBy()); | ||
} | ||
|
||
public static ExecutionState getState(WatchExecutionResult executionResult) { | ||
|
@@ -179,6 +183,9 @@ public final XContentBuilder toXContent(XContentBuilder builder, Params params) | |
if (executionResult != null) { | ||
builder.field(EXECUTION_RESULT.getPreferredName(), executionResult, params); | ||
} | ||
if (executedBy != null) { | ||
builder.field(EXECUTED_BY.getPreferredName(), executedBy); | ||
} | ||
innerToXContent(builder, params); | ||
builder.endObject(); | ||
return builder; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,10 +74,63 @@ teardown: | |
id: "my_watch" | ||
- match: { watch_record.watch_id: "my_watch" } | ||
- match: { watch_record.state: "executed" } | ||
- match: { watch_record.executed_by: "watcher_manager" } | ||
|
||
|
||
|
||
|
||
--- | ||
"Test watch is runas user properly recorded": | ||
- do: | ||
xpack.watcher.put_watch: | ||
id: "my_watch" | ||
body: > | ||
{ | ||
"trigger": { | ||
"schedule" : { "cron" : "0 0 0 1 * ? 2099" } | ||
}, | ||
"input": { | ||
"search" : { | ||
"request" : { | ||
"indices" : [ "my_test_index" ], | ||
"body" :{ | ||
"query" : { "match_all": {} } | ||
} | ||
} | ||
} | ||
}, | ||
"condition" : { | ||
"compare" : { | ||
"ctx.payload.hits.total" : { | ||
"gte" : 1 | ||
} | ||
} | ||
}, | ||
"actions": { | ||
"logging": { | ||
"logging": { | ||
"text": "Successfully ran my_watch to test for search input" | ||
} | ||
} | ||
} | ||
} | ||
- match: { _id: "my_watch" } | ||
|
||
- do: | ||
xpack.watcher.get_watch: | ||
id: "my_watch" | ||
- match: { _id: "my_watch" } | ||
- is_false: watch.status.headers | ||
|
||
- do: | ||
headers: { es-security-runas-user: x_pack_rest_user } | ||
xpack.watcher.execute_watch: | ||
id: "my_watch" | ||
- match: { watch_record.watch_id: "my_watch" } | ||
- match: { watch_record.state: "executed" } | ||
- match: { watch_record.executed_by: "x_pack_rest_user" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
|
||
--- | ||
"Test watch search input does not work against index user is not allowed to read": | ||
|
||
|
@@ -130,6 +183,7 @@ teardown: | |
- match: { watch_record.watch_id: "my_watch" } | ||
# because we are not allowed to read the index, there wont be any data | ||
- match: { watch_record.state: "execution_not_needed" } | ||
- match: { watch_record.executed_by: "watcher_manager" } | ||
|
||
|
||
--- | ||
|
@@ -272,6 +326,7 @@ teardown: | |
id: "my_watch" | ||
- match: { watch_record.watch_id: "my_watch" } | ||
- match: { watch_record.state: "executed" } | ||
- match: { watch_record.executed_by: "watcher_manager" } | ||
|
||
- do: | ||
get: | ||
|
@@ -320,6 +375,7 @@ teardown: | |
id: "my_watch" | ||
- match: { watch_record.watch_id: "my_watch" } | ||
- match: { watch_record.state: "executed" } | ||
- match: { watch_record.executed_by: "watcher_manager" } | ||
|
||
- do: | ||
get: | ||
|
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.
this can be refactored into a small static method, so you could write also some tests (so many ifs/null checks to check). This could also be done lazily in the getter (not sure yet if that is a good idea though).
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.
This method could also be overwritten by sub classes (both happen to call
super.ensureExists()
though) - might be a good reason to load it lazilyThere 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.
There looks to be no need to override this method, so ill make it final (and still move that bit to a static helper for testing)
edit: disregard.
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.
after some snooping I noticed we can and should do this #31926