Commit Graph

1456 Commits

Author SHA1 Message Date
Brian Murphy 41e42f0945 The DateTimes watcher uses for scheduled and trigger times should always be UTC
Before this change DateTimes were being constructed without providing a time zone,
this was causing some non-utc time leakage. In particular watch record ids were being created with non utc dates and
watch records were going to the wrong .watch_history index.
Add Clock.now(DateTimeZone) to allow callers to get UTC now.
Also construct the DateTimes with UTC timezones when we construct from millis.
Add all constuctors of DateTime that do not specify a time zone to the forbidden APIs.
This change makes constructing a `DateTime` object without providing a `DateTimeZone` forbidden.
This is much safer and makes time zone errors much more unlikely to occur.
Statically import DateTimeZone.UTC everywhere it was being used
Now all calls that use DateTimeZone.UTC just refrence UTC.

Fixes elastic/elasticsearch#150

Original commit: elastic/x-pack-elasticsearch@7f23ce605e
2015-04-23 15:17:23 -04:00
Brian Murphy 29c76b9c8a [LOG] Catch all and log all `WatcherException`s with watch id when parsing watches at start.
This will help debugging broken watches.

Fixes elastic/elasticsearch#251

Original commit: elastic/x-pack-elasticsearch@87e3e9a0dc
2015-04-23 15:13:19 -04:00
Martijn van Groningen 096e00caed index templates: use correct property to extract version from index template from cluster state
Original commit: elastic/x-pack-elasticsearch@ab86054c7f
2015-04-23 20:58:21 +02:00
uboness 345f610bdf Cleanup and Refactored Templates
Mainly how templates read/write themselves from/to xcontent. Instead of using `text`, use `template`

Original commit: elastic/x-pack-elasticsearch@0d6f317539
2015-04-23 15:15:46 +02:00
uboness ae1d4021c0 Cleanup and Refactoring
- Changed watch `name` to watch `id
- `TriggerEngine.Job#name` -> `TriggerEngine.Job#id`
- Removed `Trigger.SourceBuilder` in favor of `Trigger.Builder`
- Fixed compile warnings in `EmailTemplate`

Original commit: elastic/x-pack-elasticsearch@b7fb23712c
2015-04-23 13:06:53 +02:00
uboness 70209698fb Changed version to 1.0.0-Beta1-SNAPSHOT
Original commit: elastic/x-pack-elasticsearch@6bd62904cc
2015-04-23 10:35:33 +02:00
uboness 690af790b2 Cleanup and Refactoring of the transforms
* Split the transform into two constructs: `Transform` and `ExecutableTransform`. The former holds all the transform configuration, the latter can execute the transform based on that configuration (an executable transform holds a transform)
 - This makes the code clearer to understand and maintain.
 - This also enabled to pull some common implementation code into the `ExecutableTransform` and by that reduce the implementation details of each executable to the minimum required.

* Also, extracted the `Transform.Parser` to its own top level class, and renamed it to - `TransformFactory`. The main thing that the factory does is: 1) delegate to the parsing to the `Transform` class, 2) construct & wire up the `ExecutableTransform`.

* With the introduction of `Transform`, we no longer need the `SourceBuilder` for transforms. Instead, we have `Transform.Builder` that help you build a transform. This is much more intuitive from the client perspective.

Original commit: elastic/x-pack-elasticsearch@f6ee0d0c75
2015-04-23 10:23:08 +02:00
Martijn van Groningen 34c9d6af62 Removed the watcher executor thread pool as it is redundant now with the async indexing.
Original commit: elastic/x-pack-elasticsearch@6afaf0308b
2015-04-23 08:34:23 +02:00
Martijn van Groningen 7851ad6993 Removed quartz, timer and hashwheel schedule engines. Renames simple engine to ticket engine.
Original commit: elastic/x-pack-elasticsearch@f59be963ef
2015-04-22 23:36:53 +02:00
Martijn van Groningen a68db406e2 * Doubled the watcher executor thread pool size.
* Tune the index templates for higher index throughput.
* Trigger events should be processed and indexed in an async manner.
* Moved the job name into the TriggerEvent
* Added support for fire multiple events at the same time. If multiple events are fired at the same time then use async bulk to persist the watch record.
* Cut simple ticker and timer ticker over to fire multiple events at the same time
* Don't fire more than 1000 events at the time. (this also may result in large bulk requests)

Original commit: elastic/x-pack-elasticsearch@c7f6bd3812
2015-04-22 23:35:20 +02:00
Martijn van Groningen c8a0c27934 * Log EsRejectedExecutionException better than it is now. (letting it bubble up the the thread that is actually firing triggers, which then prints uncaught exception...)
* if we have EsRejectedExecutionException log on debug level.

Original commit: elastic/x-pack-elasticsearch@e8eb8fcf36
2015-04-22 23:30:11 +02:00
Martijn van Groningen a1b3d41822 * Added overall benchmark that tests all the scheduler engines
* Changed the isolated ScheduleEngineBenchmark to tests all engines and collect statistics during a test run.
* Updated the isolated schedule benchmark to just test the trigger part of the scheduler and capture some basic matrics.

Original commit: elastic/x-pack-elasticsearch@88bbf3688d
2015-04-22 23:30:10 +02:00
uboness 3f26a1b2e0 Adds initial schedule engine implementation
- `TimerScheduleTriggerEngine` - a single threaded Java `Timer`based scheduler. "Ticks" every second and checks all the registered schedules.

- `SimpleTickerScheduleTriggerEngine` - a single threaded scheduler. "Ticks" every second and checks all the registered schedules

- `SchedulerScheduleTriggerEngine` - a single threaded engine based on Java's schedule executor service. Here, every job is added as a scheduled task to the executor and each job is managing its own execution times.

- `HashWheelScheduleTriggerEngine` - a single threaded engine based on Netty's `HashWheelTimer`. Like with the `scheduler` above, every job is added as a scheduled task to the executor and each job is managing its own execution times.

Also:

- Added an undocumented feature to configure the schedule engine in the settings using `watcher.trigger.schedule.engine` (optional values right now are `quartz`, `simple`, `timer`, `hashwheel` and `scheduler`)
- `Cron` is a fork/copy of quartz `CronExpression`.. a bit cleaned up though.
- `Schedule` now exposes `nextScheduledTimeAfter` to return the next scheduled time after the given one.
- `CronnableSchedule` is now based on `Cron` (this exposed bugs in the schedule tests where we generated invalid cron expression. Now, since `Cronnable` creates the actual cron, validation is in place to make sure only valid expressions are created)
- While at it... refactored how the thread pool settings are set. Removed it from the plugin class, now each module is responsible for the settings of its own TPs. Also, if the thread pools are already configured in node settings we don't configure our default ones. This will enable users to configure the TPs in `elasticsearch.yml`
- Also updated `CronEvalTool` to work with `DateTime` construct (instead of java's `Date`)

Original commit: elastic/x-pack-elasticsearch@40d107c66e
2015-04-22 23:30:10 +02:00
Brian Murphy 3c7b42eb7b Support for Sanitized HTML in emails.
This change adds a shaded dependency on owasp (https://code.google.com/p/owasp-java-html-sanitizer/) to add support for HTML Sanitization.
Only images that reference an attachment are supported. This Sanitization may be customized for each email profile.
Other dangerous behavior is suppressed.

See elastic/elasticsearch#163
Fixes elastic/elasticsearch#163

Original commit: elastic/x-pack-elasticsearch@bc237d1beb
2015-04-22 16:57:03 -04:00
Martijn van Groningen 771b8824a5 Merge pull request elastic/elasticsearch#240 from martijnvg/improve_index_templates
Improve index templates

Original commit: elastic/x-pack-elasticsearch@8483e8872f
2015-04-22 16:05:51 +02:00
Martijn van Groningen f2fe639a3a Improve index templates:
* Disable the `_all` field, because it isn't used.
* Tune the history index for write throughput.

Original commit: elastic/x-pack-elasticsearch@db946a42b7
2015-04-22 15:51:55 +02:00
uboness ebda02438e Cleanup and Refactoring of the inputs
* Split the action into two constructs: `Input` and `ExecutableInput`. The former holds all the input configuration, the latter can execute the input based on that configuration (an executable input holds an input)
 - This the code clearer to understand and maintain.
 - This also enabled to pull some common implementation code into the `ExecutableInput` and by that reduce the implementation details of each executable to the minimum required.

* Also, extracted the `Input.Parser` to its own top level class, and renamed it to - `InputFactory`. The main thing that the factory does is: 1) delegate to the parsing to the `Input` class, 2) construct & wire up the `ExecutableInput`.

* With the introduction of `Input`, we no longer need the `SourceBuilder` for inputs. Instead, we have an `Input.Builder` that help you build an input. This is much more intuitive from the client perspective.

* Changed the `request` xcontent field in the http input result to `sent_request` for clarity
* Changed the `request` xcontent field in the search input result to `executed_request` for clarity

Original commit: elastic/x-pack-elasticsearch@63b93f9c7b
2015-04-21 23:12:43 +02:00
uboness 54fddac93f Add array access support for mustache templates
The default mustache template that is supported by elasticsearch doesn't support array/list access. This poses a real limitation for watcher as with `search` input, the hits are returned as an array/list. To bypass this limitation, an extra (tedious) step is required in order to transform the hits to a data structure that is supported by mustache.

This commit adds a new mustache script engine - `xmustache` to elasticsearch that supports array/list access in the form of `array.X` where `X` is the index into the array/list. This enables accessing the search results without using a transform. The following example will fetch the `"key"` field of the 3rd hit in the search result: `ctx.payload.hits.hits.3._source.key`.

This array/list support will be added to elasticsearch, but it'll only be available in later versions. For now, the default template in watcher will therefore be `xmustache`.

Added docs for templates

Fixes elastic/elasticsearch#230

Original commit: elastic/x-pack-elasticsearch@b09cad7f8b
2015-04-21 23:08:19 +02:00
uboness 02ba76fe21 Added the watch metadata to the template/script model
The watch metadata is now accessible to the templates and scripts (under `ctx.metadata`).

Also, changed the default email attachment to include the ctx model as a whole (not just the payload). This provides more context to the watch exectuion (e.g. watch id, execution time, metadata, etc...)

Original commit: elastic/x-pack-elasticsearch@c5dde855d2
2015-04-21 21:01:42 +02:00
Brian Murphy ca38fd6a89 [TEST] Add CountDownLatches to ensure order of operation.
This change adds countdownlatches to the `FairKeyedLock` tests on `WatchLockService`.

Original commit: elastic/x-pack-elasticsearch@2bf36e10d2
2015-04-21 14:54:11 -04:00
Brian Murphy 6c54251e61 Make lock service use fair locks.
The `WatchLockService` was not using fair locks. This could result in out of order execution of queue executions of the same watch.
Fork KeydLock tests from core and make global lock fair.

Fixes elastic/elasticsearch#225

Original commit: elastic/x-pack-elasticsearch@77382e09ca
2015-04-21 13:22:38 -04:00
Martijn van Groningen 49bbb0c801 Test: change `maxNumDataNodes` from 6 (which is the default) to 3
Original commit: elastic/x-pack-elasticsearch@1e13c7964b
2015-04-21 16:27:02 +02:00
Martijn van Groningen 901d2c0984 Test: if in starting state, wait until started state has been reached
Original commit: elastic/x-pack-elasticsearch@12e8b9da69
2015-04-21 16:24:00 +02:00
jaymode bff95d9ca1 Add CryptoService to support signing and encryption operations
This changes the SignatureService into a CryptoService that now supports encryption
operations in addition to the signing operations. The encryption leverages the existing
system key and uses it with AES.

Closes elastic/elasticsearch#805

Original commit: elastic/x-pack-elasticsearch@a792ed4a54
2015-04-21 08:38:40 -04:00
jaymode 39f587a497 [CLI] attempt to restore owner and group on new files
For the CLI tools in Shield we create a new temp file and replace the existing file
to prevent issues with reloading a half written file. This has a potential side effect
of changing the user and group that own the file. Many times the commands are
run with root privileges (sudo) and when run with root privileges we can actually
reset the owner and group correctly.

Closes elastic/elasticsearch#812

Original commit: elastic/x-pack-elasticsearch@1ee3715376
2015-04-21 07:32:44 -04:00
uboness 175423b984 Show a proper error when email password is not specified
Fixes elastic/elasticsearch#232

Original commit: elastic/x-pack-elasticsearch@2c0ab575d0
2015-04-20 21:36:16 +02:00
uboness f54c610c29 Cleanup and Refactoring of the conditions
* Split the action into two constructs: `Condition` and `ExecutableCondition`. The former holds all the condition configuration, the latter can execute the condition based on that configuration (an executable condition holds a condition)
 - This the code clearer to understand and maintain.
 - This also enabled to pull some common implementation code into the `ExecutableCondition` and by that reduce the implementation details of each executable to the minimum required.

* Also, extracted the `Condition.Parser` to its own top level class, and renamed it to - `ConditionFactory`. The main thing that the factory does is: 1) delegate to the parsing to the `Condition` class, 2) construct & wire up the `ExecutableCondition`.

* With the introduction of `Condition`, we no longer need the `SourceBuilder` for conditions. Instead, we have `Condiiton.Builder` that help you build condition. This is much more intuitive from the client perspective.

* Renamed `always_true` condition type to `always`
* Renamed `always_false` condition type to `never`

* Updated docs (also added docs for the `never` condition)

Original commit: elastic/x-pack-elasticsearch@0f8eb494ca
2015-04-17 15:07:23 -07:00
Brian Murphy cce3291b59 Register the `ScriptTransform.Parser` and `ChainTransform.Parser`.
We weren't properly registering the chain or script transforms so that they were accessable from the Watch.parse and PUT Watch APIs.

Original commit: elastic/x-pack-elasticsearch@2a236d62f3
2015-04-17 12:42:34 -04:00
Brian Murphy 90fa55d1eb Change default throttle period to 5s.
This change makes the default throttle period 5s.
This default can be controlled from config with the parameter `watcher.throttle.period.default_period`.
Added test and updated docs.

Original commit: elastic/x-pack-elasticsearch@cf8f5de724
2015-04-17 09:54:37 -04:00
Brian Murphy 0a7cf71152 Persist the Watch.Status if needed after execution
This change persists the Watch.Status if needed by marking the Status as `dirty` if the status mutates during watch execution.
If the status is dirty it will be persisted at the end of execution while the execution lock is held.
Change record_in_history to record_execution which also controls weither the status will be updated.

Fixes elastic/elasticsearch#222

Original commit: elastic/x-pack-elasticsearch@25869cabf0
2015-04-16 16:57:14 -04:00
Brian Murphy 44a3f600ab Update after review.
Change log level of message at shutdown.

Original commit: elastic/x-pack-elasticsearch@c6d80aaec9
2015-04-16 15:29:10 -04:00
Brian Murphy c6b5507436 Changes after review.
Add tests for stopped history store.

Original commit: elastic/x-pack-elasticsearch@e496891ed5
2015-04-16 15:29:10 -04:00
Brian Murphy 3e7b668406 Add started boolean and locks to `HistoryStore`
There were a few corner cases where a `HistoryStore.put` operation was underway while we were shutting down since we were not holding any lock during this time.
This change adds a ReadWriteLock and uses the ReadLock for updates and put operations and the WriteLock for shutdown.
Also add a lock around the manual execution of a watch since it could change the status.

Fixes elastic/elasticsearch#204

Original commit: elastic/x-pack-elasticsearch@60ef78eff5
2015-04-16 15:29:10 -04:00
Brian Murphy 7f0e4fab41 Fix REST execute API call with empty body.
Execute with an empty body will now just use the defaults.
Add REST test for empty body.
Fixes elastic/elasticsearch#217

Original commit: elastic/x-pack-elasticsearch@072cd47250
2015-04-16 15:27:08 -04:00
Brian Murphy 6e0cabac62 [LOGGING] Log an error message when we fail to parse a `Watch` or `WatchRecord`
If we failed to parse a `Watch` or `WatchRecord` at startup we currently get stuck as this behaviour is undefined.
This commit just adds some logging so we can tell that this is what happened.

Original commit: elastic/x-pack-elasticsearch@aa6d95c172
2015-04-16 14:38:33 -04:00
Martijn van Groningen 6309d2b5d3 changed the order in which the interval services are stopped when watcher stops.
Original commit: elastic/x-pack-elasticsearch@8092a76aa4
2015-04-15 09:05:06 +02:00
uboness 13573d6c6d Cleanup and Refactoring of the actions
* Split the action into two constructs: `Action` and `ExecutableAction`. The former holds all the action configuration, the latter can execute the action based on that configuration (an executable action holds an action)
 - This the code clearer to understand and maintain.
 - This also enabled to pull some common implementation code into the `ExecutableAction` and by that reduce the implementation details of each executable to the minimum required.

* Also, extracted teh `Action.Parser` to its own top level class, and renamed it to - `ActionFactory`. The main thing that the parser does is: 1) delegate to the parsing to the `Action` class, 2) construct & wire up the `ExecutableAction`.

* Cleaned up and aligned the log messages across all actions. Introduced
 - introduced parameterized exceptions (now the exception messages are constructed exactly like that log messages - using `{}` params)
 - Introduced the notion of `Wid` - Watch (execution) ID`. Now the `WatchExecutionContext` and the `WatchRecord` hold this construct as their ID (instead of an opaque string). The Wid has a well defined structure where it's prefixed with the watch id. This is very helpful to provide context to parsing the different constructs (actions in our case) as when parsing error occurs we can point directly to the place were we found the error (ie. watch id, record id and action id).

* With the introduction of `Action`, we no longer need the `SourceBuilder` for actions. Instead, we have `Action.Builder` that help you build actions. This is much more intuitive from the client perspective.

Original commit: elastic/x-pack-elasticsearch@df22da4e1f
2015-04-14 17:16:47 -07:00
Brian Murphy 8c87cb0728 Add test for invalid license.
This change adds a new test class that runs the REST tests with the license disabled.
It checks the error message to make sure that all api access is blocked from all the apis we normally test.

Fixes elastic/elasticsearch#193

Original commit: elastic/x-pack-elasticsearch@a70de9b7a7
2015-04-14 13:57:55 -04:00
Brian Murphy 9b1d8fd3e6 Fix NPE in EmailTemplate
EmailTemplate was passing replyTo to all calls to templatesToAddressList

Original commit: elastic/x-pack-elasticsearch@5ecc05e3f7
2015-04-13 12:30:57 -04:00
Brian Murphy 05dbbbdc48 Fix silent failure to parse valid script condition.
`
{
  "script" : "<script>"
}
`
Should be a valid script condition. This was silently failing to parse creating invalid watches in the system that could not be executed or interacted with except via DELETE.
Added a test for this as well.

Fixes elastic/elasticsearch#191

Original commit: elastic/x-pack-elasticsearch@25ed27bf2e
2015-04-13 11:59:41 -04:00
uboness 76ac647610 Cleanup of templates
- Introduced `TemplateEngine` and its only implementation `MustacheTemplateEngine`
- Change `Template` to only hold the template config.. the rendering is now done by the engine
- Introduced `EmailTemplate` that can render itself to an `Email` (given an engine and model)
- Renamed `TemplatedHttpRequest` to `HttpRequestTemplate` and it has a `render` method that given a model and an engine it creates an `HttpRequest` construct.
 - also fixed a bug where when the http request is build, the configured scheme wasn't set.
- cleaned up `HttpRequest` - made it immutable and now we use a builder to build it.
 - also fixed a bug in `HttpRequest` where the xcontent didn't render the scheme
- Got rid of all old template implementations... also, parsing a template is now simply `Template.parse(...)` (no need for a special `TemplateParser`.
- While at it, change the action builder to only deal with actions and not know anything about the action id. The id is now passed to the watch builder directly when adding an action

Original commit: elastic/x-pack-elasticsearch@1a7acc5868
2015-04-12 21:50:41 +02:00
Brian Murphy 5a8287e3db [TEST] Change from IntervalSchedule to CronSchedule.
Original commit: elastic/x-pack-elasticsearch@039a64477a
2015-04-10 20:08:11 -04:00
Brian Murphy 053abfa8fb Force timewarp mode on ManualExecutionTests
Original commit: elastic/x-pack-elasticsearch@cc3d33a53b
2015-04-10 19:47:13 -04:00
Brian Murphy e94f62b298 Make sure watcher is started before running the test.
Original commit: elastic/x-pack-elasticsearch@c1d081fd10
2015-04-10 19:23:32 -04:00
Brian Murphy b70bdb2b89 Fix error message when email fails to send.
Closes elastic/elasticsearch#195

Original commit: elastic/x-pack-elasticsearch@7256a32505
2015-04-10 19:05:18 -04:00
Brian Murphy 3e9c109079 This commit contains a major refactoring to some of the core constructs in watcher.
- What used to be the `HistoryService` is not named `ExecutionService`. In fact, it always was an execution context and was simply named wrong and place in the wrog place in the code.

- A new `execution` package was created, to contain all execution related contsructs which includes the new `ExecutionService`, `WatchExecutionContext`, `WatchExecutor`, `InternalWatchExecutor`.

- The `WatchExecutionContext` role in the execution process was extended to also enable it to "control" the execution. "control" in the sence that it can now provide default results to some of the contructs (for example, it can provide a default input and by that skip the execution of the input). Also, it enables simulating actions (more bellow).

- There are two implementations now of the `WatchExecutionContext`. A `TriggeredExecutionContext` which is used when the trigger service triggers the execution of a watch. And `ManualExecutionContext` enabling to manually execute a watch. The latter, can be configured such that different phases in the normal executions of the watch can be skipped. This is great for simulating a watch execution, that is, executing a watch without actually fully executing the actions (e.g. create the emails that the email action would have normally send, but without actually sending it). Both of these new context implementations also sit in the new `execution` package.

- With the changes described above, the actions now consult the execution context before actually performing their tasks. If the execution context indicates that the action should run in a "simulated" mode, the action will semi execute and return a `Simulated` result. Each action type defines what is "simulated mode" for it. For example, the email action creates the email and renders all its templats but doesn't actually send it, the returned `Email.Simulated` result will contain the email that would have been sent if it was running under normal conditions (non-simulated mode). The `index` action, doesn't actually indexes anything (just creating the index request that would have been executed), and the `webhook` action only creates the HTTP request but doesn't actually send it.

- A new `execute` API was introduced that utilizes all of the above and enables to manually execute registered watches. The API provides all the tools to control what actions are simulated and enables skipping the execution of the watch `input` and `condition`. By default it also skip the recording of the execution in the watch history (though this can be enabled if desired). This API is exposed both by the Java `WatcherClient` and via a dedicated REST endpoint. (documentation around this new API was added as well)

Closes elastic/elasticsearch#149

Original commit: elastic/x-pack-elasticsearch@ee8fe0490a
2015-04-10 18:02:48 -04:00
uboness 7676347bb4 Merge pull request elastic/elasticsearch#206 from mrsolo/fix/spelling
Fixed minor spelling errors in exception messages

Original commit: elastic/x-pack-elasticsearch@ba9be34f23
2015-04-10 21:09:15 +02:00
Bill Hwang 8242e25cde Fixed minor spelling errors in exception messages
Original commit: elastic/x-pack-elasticsearch@7b9294e320
2015-04-10 11:58:33 -07:00
jaymode 197817e900 use a time constant comparison function for security
For HMAC and password hash comparisons we need to use a time
constant comparison that always compares the whole value in order
to protect against timing attacks.

Original commit: elastic/x-pack-elasticsearch@f6082c76b9
2015-04-10 09:37:41 -04:00
uboness 303616b415 [temp-fix] disabled timewarp mode for WatchCrudTests
something seems to be broken... either in the integration test infra or somewhere else.. .investigation required

Original commit: elastic/x-pack-elasticsearch@c27bc41ef4
2015-04-10 15:17:24 +02:00