logstash-integration-elastic_enterprise_search 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: fe83963cbb7d00dffdf8b52106d2db2e34f8b5389c891a7a1db7f939657c6ed2
4
+ data.tar.gz: 21be61a90719d99c958a9d4eed07a67a4d5ea22d6fbcf5cb8531a3af5e036c01
5
+ SHA512:
6
+ metadata.gz: f4fece8fe3bec522750956526422d44da6b0ea1b2bcf42d01ca992f5835f67ab5e173e9543442da453f97dc68c78cf87fab8a7370925d7696e68069eeb20f16d
7
+ data.tar.gz: 88ba3212c15e0f84a2023472a80b4acbb81663db628d6ef209cba3295ec4310a78b06e21c1c3a31a552ea08e249faf04a6fc8efa5f3f52998dddd1557a8b2eb6
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ ## 2.0.0
2
+
3
+ - Initial release of the Elastic EnterpriseSearch Integration Plugin, which carries the
4
+ previous AppSearch Output plugin codebase;
5
+ independent changelogs for previous versions can be found:
6
+ - [AppSearch Output Plugin @1.2.0](https://github.com/logstash-plugins/logstash-output-elastic_app_search/blob/v1.2.0/CHANGELOG.md)
7
+
data/CONTRIBUTORS ADDED
@@ -0,0 +1,12 @@
1
+ The following is a list of people who have contributed ideas, code, bug
2
+ reports, or in general have helped logstash along its way.
3
+
4
+ Contributors:
5
+ * Joao Duarte - jsvduarte@gmail.com
6
+ * Peter Kim - petedogg@gmail.com
7
+ * Andrea Selva (andsel)
8
+
9
+ Note: If you've sent us patches, bug reports, or otherwise contributed to
10
+ Logstash, and you aren't on the list above and want to be, please let us know
11
+ and we'll make sure you're here. Contributions from folks like you are what make
12
+ open source awesome.
data/DEVELOPER.md ADDED
@@ -0,0 +1,5 @@
1
+ # logstash-integration-elastic_enterprise_search
2
+ Elastic Enterprise Search integration for Logstash, including AppSearch and WorkplaceSearch output plugins
3
+
4
+ # Dependencies
5
+ * elastic-app-search
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ logstash_path = ENV["LOGSTASH_PATH"] || "../../logstash"
6
+ use_logstash_source = ENV["LOGSTASH_SOURCE"] && ENV["LOGSTASH_SOURCE"].to_s == "1"
7
+
8
+ if Dir.exist?(logstash_path) && use_logstash_source
9
+ gem 'logstash-core', :path => "#{logstash_path}/logstash-core"
10
+ gem 'logstash-core-plugin-api', :path => "#{logstash_path}/logstash-core-plugin-api"
11
+ end
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright 2020 Elastic and contributors
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
data/NOTICE.TXT ADDED
@@ -0,0 +1,5 @@
1
+ Elasticsearch
2
+ Copyright 2012-2015 Elasticsearch
3
+
4
+ This product includes software developed by The Apache Software
5
+ Foundation (http://www.apache.org/).
data/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # logstash-integration-elastic_enterprise_search
2
+
3
+
4
+ [![Travis Build Status](https://travis-ci.com/logstash-plugins/logstash-output-example.svg)](https://travis-ci.com/logstash-plugins/logstash-output-example)
5
+
6
+ This is a plugin for [Logstash](https://github.com/elastic/logstash).
7
+
8
+ It is fully free and fully open source. The license is Apache 2.0, meaning you are pretty much free to use it however you want in whatever way.
9
+
10
+ ## Documentation
11
+
12
+ Logstash provides infrastructure to automatically build documentation for this plugin. We provide a template file, index.asciidoc, where you can add documentation. The contents of this file will be converted into html and then placed with other plugin documentation in a [central location](http://www.elastic.co/guide/en/logstash/current/).
13
+
14
+ - For formatting config examples, you can use the asciidoc `[source,json]` directive
15
+ - For more asciidoc formatting tips, see the excellent reference here https://github.com/elastic/docs#asciidoc-guide
16
+
17
+ ## Need Help?
18
+
19
+ Need help? Try #logstash on freenode IRC or the https://discuss.elastic.co/c/logstash discussion forum.
20
+
21
+ ## Developing
22
+
23
+ ### 1. Plugin Developement and Testing
24
+
25
+ #### Code
26
+ - To get started, you'll need JRuby with the Bundler gem installed.
27
+
28
+ - Create a new plugin or clone and existing from the GitHub [logstash-plugins](https://github.com/logstash-plugins) organization. We also provide [example plugins](https://github.com/logstash-plugins?query=example).
29
+
30
+ - Install dependencies
31
+ ```sh
32
+ bundle install
33
+ ```
34
+
35
+ #### Test
36
+
37
+ - Update your dependencies
38
+
39
+ ```sh
40
+ bundle install
41
+ ```
42
+
43
+ - Run tests
44
+
45
+ ```sh
46
+ bundle exec rspec
47
+ ```
48
+
49
+ ### 2. Running your unpublished Plugin in Logstash
50
+
51
+ #### 2.1 Run in a local Logstash clone
52
+
53
+ - Edit Logstash `Gemfile` and add the local plugin path, for example:
54
+ ```ruby
55
+ gem "logstash-filter-awesome", :path => "/your/local/logstash-filter-awesome"
56
+ ```
57
+ - Install plugin
58
+ ```sh
59
+ # Logstash 2.3 and higher
60
+ bin/logstash-plugin install --no-verify
61
+
62
+ # Prior to Logstash 2.3
63
+ bin/plugin install --no-verify
64
+
65
+ ```
66
+ - Run Logstash with your plugin
67
+ ```sh
68
+ bin/logstash -e 'filter {awesome {}}'
69
+ ```
70
+ At this point any modifications to the plugin code will be applied to this local Logstash setup. After modifying the plugin, simply rerun Logstash.
71
+
72
+ #### 2.2 Run in an installed Logstash
73
+
74
+ You can use the same **2.1** method to run your plugin in an installed Logstash by editing its `Gemfile` and pointing the `:path` to your local plugin development directory or you can build the gem and install it using:
75
+
76
+ - Build your plugin gem
77
+ ```sh
78
+ gem build logstash-filter-awesome.gemspec
79
+ ```
80
+ - Install the plugin from the Logstash home
81
+ ```sh
82
+ # Logstash 2.3 and higher
83
+ bin/logstash-plugin install --no-verify
84
+
85
+ # Prior to Logstash 2.3
86
+ bin/plugin install --no-verify
87
+
88
+ ```
89
+ - Start Logstash and proceed to test the plugin
90
+
91
+ ## Contributing
92
+
93
+ All contributions are welcome: ideas, patches, documentation, bug reports, complaints, and even something you drew up on a napkin.
94
+
95
+ Programming is not a required skill. Whatever you've seen about open source and maintainers or community members saying "send patches or die" - you will not see that here.
96
+
97
+ It is more important to the community that you are able to contribute.
98
+
99
+ For more information about contributing, see the [CONTRIBUTING](https://github.com/elastic/logstash/blob/master/CONTRIBUTING.md) file.
100
+ >>>>>>> 418bfff... Rought output plugin creation
@@ -0,0 +1,117 @@
1
+ # encoding: utf-8
2
+ require "logstash/outputs/base"
3
+ require "elastic-app-search"
4
+
5
+ class LogStash::Outputs::ElasticAppSearch < LogStash::Outputs::Base
6
+ config_name "elastic_app_search"
7
+
8
+ config :engine, :validate => :string, :required => true
9
+ config :host, :validate => :string
10
+ config :url, :validate => :string
11
+ config :api_key, :validate => :password, :required => true
12
+ config :timestamp_destination, :validate => :string
13
+ config :document_id, :validate => :string
14
+ config :path, :validate => :string, :default => "/api/as/v1/"
15
+
16
+ ENGINE_WITH_SPRINTF_REGEX = /^.*%\{.+\}.*$/
17
+
18
+ public
19
+ def register
20
+ if @host.nil? && @url.nil?
21
+ raise ::LogStash::ConfigurationError.new("Please specify either \"url\" (for self-managed) or \"host\" (for SaaS).")
22
+ elsif @host && @url
23
+ raise ::LogStash::ConfigurationError.new("Both \"url\" or \"host\" can't be set simultaneously. Please specify either \"url\" (for self-managed) or \"host\" (for SaaS).")
24
+ elsif @host && path_is_set? # because path has a default value we need extra work to if the user set it
25
+ raise ::LogStash::ConfigurationError.new("The setting \"path\" is not compatible with \"host\". Use \"path\" only with \"url\".")
26
+ elsif @host
27
+ @client = Elastic::AppSearch::Client.new(:host_identifier => @host, :api_key => @api_key.value)
28
+ elsif @url
29
+ @client = Elastic::AppSearch::Client.new(:api_endpoint => @url + @path, :api_key => @api_key.value)
30
+ end
31
+ check_connection! unless @engine =~ ENGINE_WITH_SPRINTF_REGEX
32
+ rescue => e
33
+ if e.message =~ /401/
34
+ raise ::LogStash::ConfigurationError.new("Failed to connect to App Search. Error: 401. Please check your credentials")
35
+ elsif e.message =~ /404/
36
+ raise ::LogStash::ConfigurationError.new("Failed to connect to App Search. Error: 404. Please check if host '#{@host}' is correct and you've created an engine with name '#{@engine}'")
37
+ else
38
+ raise ::LogStash::ConfigurationError.new("Failed to connect to App Search. #{e.message}")
39
+ end
40
+ end
41
+
42
+ public
43
+ def multi_receive(events)
44
+ # because App Search has a limit of 100 documents per bulk
45
+ events.each_slice(100) do |events|
46
+ batch = format_batch(events)
47
+ if @logger.trace?
48
+ @logger.trace("Sending bulk to AppSearch", :size => batch.size, :data => batch.inspect)
49
+ end
50
+ index(batch)
51
+ end
52
+ end
53
+
54
+ private
55
+ def format_batch(events)
56
+ docs_for_engine = {}
57
+ events.each do |event|
58
+ doc = event.to_hash
59
+ # we need to remove default fields that start with "@"
60
+ # since Elastic App Search doesn't accept them
61
+ if @timestamp_destination
62
+ doc[@timestamp_destination] = doc.delete("@timestamp")
63
+ else # delete it
64
+ doc.delete("@timestamp")
65
+ end
66
+ if @document_id
67
+ doc["id"] = event.sprintf(@document_id)
68
+ end
69
+ doc.delete("@version")
70
+ resolved_engine = event.sprintf(@engine)
71
+ unless docs_for_engine[resolved_engine]
72
+ if @logger.debug?
73
+ @logger.debug("Creating new engine segment in batch to send", :resolved_engine => resolved_engine)
74
+ end
75
+ docs_for_engine[resolved_engine] = []
76
+ end
77
+ docs_for_engine[resolved_engine] << doc
78
+ end
79
+ docs_for_engine
80
+ end
81
+
82
+ def index(batch)
83
+ batch.each do |resolved_engine, documents|
84
+ begin
85
+ if resolved_engine =~ ENGINE_WITH_SPRINTF_REGEX || resolved_engine =~ /^\s*$/
86
+ raise "Cannot resolve engine field name #{@engine} from event"
87
+ end
88
+ response = @client.index_documents(resolved_engine, documents)
89
+ report(documents, response)
90
+ rescue => e
91
+ @logger.error("Failed to execute index operation. Retrying..", :exception => e.class, :reason => e.message,
92
+ :resolved_engine => resolved_engine)
93
+ sleep(1)
94
+ retry
95
+ end
96
+ end
97
+ end
98
+
99
+ def report(documents, response)
100
+ documents.each_with_index do |document, i|
101
+ errors = response[i]["errors"]
102
+ if errors.empty?
103
+ @logger.trace? && @logger.trace("Document was indexed with no errors", :document => document)
104
+ else
105
+ @logger.warn("Document failed to index. Dropping..", :document => document, :errors => errors.to_a)
106
+ end
107
+ end
108
+ end
109
+
110
+ def check_connection!
111
+ @client.get_engine(@engine)
112
+ end
113
+
114
+ def path_is_set?
115
+ original_params.key?("path")
116
+ end
117
+ end
@@ -0,0 +1,31 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'logstash-integration-elastic_enterprise_search'
3
+ s.version = '2.0.0'
4
+ s.licenses = ['Apache-2.0']
5
+ s.summary = "Integration with Elastic Enterprise Search - output plugins"
6
+ s.description = "This gem is a Logstash plugin required to be installed on top of the Logstash core pipeline "+
7
+ "using $LS_HOME/bin/logstash-plugin install gemname. This gem is not a stand-alone program."
8
+ s.authors = ["Elastic"]
9
+ s.email = 'info@elastic.co'
10
+ s.homepage = "http://www.elastic.co/guide/en/logstash/current/index.html"
11
+ s.require_paths = ['lib', 'vendor/jar-dependencies']
12
+
13
+ # Files
14
+ s.files = Dir['lib/**/*','spec/**/*','vendor/**/*','*.gemspec','*.md','CONTRIBUTORS','Gemfile','LICENSE','NOTICE.TXT']
15
+ # Tests
16
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
17
+
18
+ # Special flag to let us know this is actually a logstash plugin
19
+ # Special flag to let us know this is actually a logstash plugin
20
+ s.metadata = {
21
+ "logstash_plugin" => "true",
22
+ "logstash_group" => "integration",
23
+ "integration_plugins" => "logstash-output-elastic_app_search"
24
+ }
25
+
26
+ # Gem dependencies
27
+ s.add_runtime_dependency "logstash-core-plugin-api", "~> 2.0"
28
+ s.add_runtime_dependency "logstash-codec-plain"
29
+ s.add_runtime_dependency "elastic-app-search", '~>7.8.0'
30
+ s.add_development_dependency "logstash-devutils"
31
+ end
@@ -0,0 +1,180 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/elastic_app_search"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+ require "json"
7
+
8
+ describe "indexing against running AppSearch", :integration => true do
9
+
10
+ let(:engine_name) do
11
+ (0...10).map { ('a'..'z').to_a[rand(26)] }.join
12
+ end
13
+
14
+ let(:config) do
15
+ {
16
+ "api_key" => ENV['APPSEARCH_PRIVATE_KEY'],
17
+ "engine" => engine_name,
18
+ "url" => "http://appsearch:3002"
19
+ }
20
+ end
21
+
22
+ subject(:app_search_output) { LogStash::Outputs::ElasticAppSearch.new(config) }
23
+
24
+ before(:each) do
25
+ create_engine(engine_name, "http://appsearch:3002", ENV['APPSEARCH_PRIVATE_KEY'])
26
+ end
27
+
28
+ private
29
+ def create_engine(engine_name, host, api_key)
30
+ url = host + "/api/as/v1/engines"
31
+ resp = Faraday.post(url, "{\"name\": \"#{engine_name}\"}",
32
+ "Content-Type" => "application/json",
33
+ "Authorization" => "Bearer " + api_key)
34
+ expect(resp.status).to eq(200)
35
+ end
36
+
37
+ describe "search and private keys are configured" do
38
+ let(:api_key_settings) do
39
+ {
40
+ :private => ENV['APPSEARCH_PRIVATE_KEY'],
41
+ :search => ENV['APPSEARCH_SEARCH_KEY']
42
+ }
43
+ end
44
+
45
+ it "setup apikeys" do
46
+ expect(api_key_settings[:private]).to start_with("private-")
47
+ expect(api_key_settings[:search]).to start_with("search-")
48
+ end
49
+ end
50
+
51
+ describe "register" do
52
+ let(:config) do
53
+ {
54
+ "api_key" => ENV['APPSEARCH_PRIVATE_KEY'],
55
+ "engine" => "%{engine_name_field}",
56
+ "url" => "http://appsearch:3002"
57
+ }
58
+ end
59
+
60
+ context "when engine is defined in sprintf format" do
61
+ it "does not raise an error" do
62
+ expect { subject.register }.to_not raise_error
63
+ end
64
+ end
65
+ end
66
+
67
+ describe "indexing" do
68
+
69
+ before do
70
+ app_search_output.register
71
+ end
72
+
73
+ describe "single event" do
74
+ let(:event) { LogStash::Event.new("message" => "an event to index") }
75
+
76
+ it "should be indexed" do
77
+ app_search_output.multi_receive([event])
78
+
79
+ results = Stud.try(20.times, RSpec::Expectations::ExpectationNotMetError) do
80
+ attempt_response = execute_search_call(engine_name)
81
+ expect(attempt_response.status).to eq(200)
82
+ parsed_resp = JSON.parse(attempt_response.body)
83
+ expect(parsed_resp.dig("meta", "page", "total_pages")).to eq(1)
84
+ parsed_resp["results"]
85
+ end
86
+ expect(results.first.dig("message", "raw")).to eq "an event to index"
87
+ end
88
+
89
+ context "using sprintf-ed engine" do
90
+ let(:config) do
91
+ {
92
+ "api_key" => ENV['APPSEARCH_PRIVATE_KEY'],
93
+ "engine" => "%{engine_name_field}",
94
+ "url" => "http://appsearch:3002"
95
+ }
96
+ end
97
+
98
+ let(:event) { LogStash::Event.new("message" => "an event to index", "engine_name_field" => engine_name) }
99
+
100
+ it "should be indexed" do
101
+ app_search_output.multi_receive([event])
102
+
103
+ results = Stud.try(20.times, RSpec::Expectations::ExpectationNotMetError) do
104
+ attempt_response = execute_search_call(engine_name)
105
+ expect(attempt_response.status).to eq(200)
106
+ parsed_resp = JSON.parse(attempt_response.body)
107
+ expect(parsed_resp.dig("meta", "page", "total_pages")).to eq(1)
108
+ parsed_resp["results"]
109
+ end
110
+ expect(results.first.dig("message", "raw")).to eq "an event to index"
111
+ end
112
+ end
113
+ end
114
+
115
+ private
116
+ def execute_search_call(engine_name)
117
+ url = config["url"] + "/api/as/v1/engines/#{engine_name}/search"
118
+ resp = Faraday.post(url, '{"query": "event"}',
119
+ "Content-Type" => "application/json",
120
+ "Authorization" => "Bearer " + config["api_key"])
121
+ end
122
+
123
+ describe "multiple events" do
124
+ context "single static engine" do
125
+ let(:events) { generate_events(200) } #2 times the slice size used to batch
126
+
127
+ it "all should be indexed" do
128
+ app_search_output.multi_receive(events)
129
+
130
+ expect_indexed(engine_name, 200)
131
+ end
132
+ end
133
+
134
+ context "multiple sprintf engines" do
135
+ let(:config) do
136
+ {
137
+ "api_key" => ENV['APPSEARCH_PRIVATE_KEY'],
138
+ "engine" => "%{engine_name_field}",
139
+ "url" => "http://appsearch:3002"
140
+ }
141
+ end
142
+
143
+ it "all should be indexed" do
144
+ create_engine('testengin1', "http://appsearch:3002", ENV['APPSEARCH_PRIVATE_KEY'])
145
+ create_engine('testengin2', "http://appsearch:3002", ENV['APPSEARCH_PRIVATE_KEY'])
146
+ events = generate_events(100, 'testengin1')
147
+ events += generate_events(100, 'testengin2')
148
+ events.shuffle!
149
+
150
+ app_search_output.multi_receive(events)
151
+
152
+ expect_indexed('testengin1', 100)
153
+ expect_indexed('testengin2', 100)
154
+ end
155
+ end
156
+ end
157
+
158
+ private
159
+ def expect_indexed(engine_name, expected_docs_count)
160
+ results = Stud.try(20.times, RSpec::Expectations::ExpectationNotMetError) do
161
+ attempt_response = execute_search_call(engine_name)
162
+ expect(attempt_response.status).to eq(200)
163
+ parsed_resp = JSON.parse(attempt_response.body)
164
+ expect(parsed_resp.dig("meta", "page", "total_results")).to eq(expected_docs_count)
165
+ parsed_resp["results"]
166
+ end
167
+ expect(results.first.dig("message", "raw")).to start_with("an event to index")
168
+ end
169
+
170
+ def generate_events(num_events, engine_name = nil)
171
+ (1..num_events).map do |i|
172
+ if engine_name
173
+ LogStash::Event.new("message" => "an event to index #{i}", "engine_name_field" => engine_name)
174
+ else
175
+ LogStash::Event.new("message" => "an event to index #{i}")
176
+ end
177
+ end
178
+ end
179
+ end
180
+ end
@@ -0,0 +1,50 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/outputs/elastic_app_search"
4
+ require "logstash/codecs/plain"
5
+ require "logstash/event"
6
+
7
+ describe LogStash::Outputs::ElasticAppSearch do
8
+ let(:sample_event) { LogStash::Event.new }
9
+ let(:host) { "test-host" }
10
+ let(:api_key) { "my_key" }
11
+ let(:engine) { "test-engine" }
12
+ subject { described_class.new(config) }
13
+
14
+ describe "#register" do
15
+ before(:each) do
16
+ allow(subject).to receive(:check_connection!)
17
+ end
18
+ context "when host is configured" do
19
+ let(:config) { { "host" => host, "api_key" => api_key, "engine" => engine } }
20
+ it "does not raise an error" do
21
+ expect { subject.register }.to_not raise_error
22
+ end
23
+ end
24
+ context "when host and path is configured" do
25
+ let(:config) { { "host" => host, "api_key" => api_key, "engine" => engine, "path" => "/v1" } }
26
+ it "raises an error" do
27
+ expect { subject.register }.to raise_error(LogStash::ConfigurationError)
28
+ end
29
+ end
30
+ context "when host and url is configured" do
31
+ let(:config) { { "host" => host, "api_key" => api_key, "engine" => engine, "url" => "http://localhost:9300" } }
32
+ it "raises an error" do
33
+ expect { subject.register }.to raise_error(LogStash::ConfigurationError)
34
+ end
35
+ end
36
+ context "when neither host nor url is configured" do
37
+ let(:config) { { "api_key" => api_key, "engine" => engine } }
38
+ it "raises an error" do
39
+ expect { subject.register }.to raise_error(LogStash::ConfigurationError)
40
+ end
41
+ end
42
+ context "when engine is in sprintf format" do
43
+ let(:config) { { "host" => host, "api_key" => api_key, "engine" => "%{type}" } }
44
+ it "connection is not checked" do
45
+ expect { subject.register }.to_not raise_error
46
+ expect(subject).not_to receive(:check_connection!)
47
+ end
48
+ end
49
+ end
50
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-integration-elastic_enterprise_search
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Elastic
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '2.0'
19
+ name: logstash-core-plugin-api
20
+ prerelease: false
21
+ type: :runtime
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ requirement: !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ name: logstash-codec-plain
34
+ prerelease: false
35
+ type: :runtime
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 7.8.0
47
+ name: elastic-app-search
48
+ prerelease: false
49
+ type: :runtime
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 7.8.0
55
+ - !ruby/object:Gem::Dependency
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ name: logstash-devutils
62
+ prerelease: false
63
+ type: :development
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: This gem is a Logstash plugin required to be installed on top of the
70
+ Logstash core pipeline using $LS_HOME/bin/logstash-plugin install gemname. This
71
+ gem is not a stand-alone program.
72
+ email: info@elastic.co
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - CHANGELOG.md
78
+ - CONTRIBUTORS
79
+ - DEVELOPER.md
80
+ - Gemfile
81
+ - LICENSE
82
+ - NOTICE.TXT
83
+ - README.md
84
+ - lib/logstash/outputs/elastic_app_search.rb
85
+ - logstash-integration-elastic_enterprise_search.gemspec
86
+ - spec/integration/outputs/index_spec.rb
87
+ - spec/unit/outputs/appsearch_spec.rb
88
+ homepage: http://www.elastic.co/guide/en/logstash/current/index.html
89
+ licenses:
90
+ - Apache-2.0
91
+ metadata:
92
+ logstash_plugin: 'true'
93
+ logstash_group: integration
94
+ integration_plugins: logstash-output-elastic_app_search
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ - vendor/jar-dependencies
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.13
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Integration with Elastic Enterprise Search - output plugins
116
+ test_files:
117
+ - spec/integration/outputs/index_spec.rb
118
+ - spec/unit/outputs/appsearch_spec.rb