logstash-codec-cloudfront 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1acb89596fe3f204d4c0ee4f57a5df136adaba53
4
+ data.tar.gz: 6a46f6f11903cb279301941b0fd84b729121ff15
5
+ SHA512:
6
+ metadata.gz: ab1fc16e7e90aa297b450e4567e32c6ec7b89091d8fc688df0b5ba6dd29786fd6fc0a0996c8987cb674e618914c9d85de5f52253e901bedb3f7dd64583cb55e8
7
+ data.tar.gz: c7d3d3fc1aa056dbb3cb241738ae0a0f2225d774398b74d6927600aaf82ce84bf8ef22068ae597fd802b9787ead78d026dc7d28b42fe66c8317306b28a154933
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+ gem "logstash", :github => "elasticsearch/logstash", :branch => "1.5"
data/LICENSE ADDED
@@ -0,0 +1,13 @@
1
+ Copyright (c) 2012-2014 Elasticsearch <http://www.elasticsearch.org>
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ @files=[]
2
+
3
+ task :default do
4
+ system("rake -T")
5
+ end
6
+
7
+ require "logstash/devutils/rake"
@@ -0,0 +1,85 @@
1
+ # encoding: utf-8
2
+ require "logstash/codecs/base"
3
+ require "logstash/codecs/plain"
4
+ require "logstash/json"
5
+
6
+ # This codec will read cloudfront encoded content
7
+ class LogStash::Codecs::Cloudfront < LogStash::Codecs::Base
8
+ config_name "cloudfront"
9
+
10
+ milestone 3
11
+
12
+ # The character encoding used in this codec. Examples include "UTF-8" and
13
+ # "CP1252"
14
+ #
15
+ # JSON requires valid UTF-8 strings, but in some cases, software that
16
+ # emits JSON does so in another encoding (nxlog, for example). In
17
+ # weird cases like this, you can set the charset setting to the
18
+ # actual encoding of the text and logstash will convert it for you.
19
+ #
20
+ # For nxlog users, you'll want to set this to "CP1252"
21
+ config :charset, :validate => ::Encoding.name_list, :default => "UTF-8"
22
+
23
+ public
24
+ def initialize(params={})
25
+ super(params)
26
+ @converter = LogStash::Util::Charset.new(@charset)
27
+ @converter.logger = @logger
28
+ end
29
+
30
+ public
31
+ def decode(data)
32
+ begin
33
+ @gzip = Zlib::GzipReader.new(data)
34
+
35
+ metadata = extract_metadata(@gzip)
36
+
37
+ @logger.debug("Cloudfront: Extracting metadata", :metadata => metadata)
38
+
39
+ @gzip.each_line do |line|
40
+ yield create_event(line, metadata)
41
+ end
42
+
43
+ rescue Zlib::Error, Zlib::GzipFile::Error=> e
44
+ file = data.is_a?(String) ? data : data.class
45
+
46
+ @logger.error("Cloudfront codec: We cannot uncompress the gzip file", :filename => file)
47
+ raise e
48
+ end
49
+ end # def decode
50
+
51
+ public
52
+ def create_event(line, metadata)
53
+ event = LogStash::Event.new("message" => @converter.convert(line))
54
+ event["cloudfront_version"] = metadata["cloudfront_version"]
55
+ event["cloudfront_fields"] = metadata["cloudfront_fields"]
56
+ event
57
+ end
58
+
59
+
60
+ def extract_metadata(io)
61
+ version = extract_version(io.gets)
62
+ fields = extract_fields(io.gets)
63
+
64
+ return {
65
+ "cloudfront_version" => version,
66
+ "cloudfront_fields" => fields,
67
+ }
68
+ end
69
+
70
+
71
+ def extract_version(line)
72
+ if /^#Version: .+/.match(line)
73
+ junk, version = line.strip().split(/#Version: (.+)/)
74
+ version unless version.nil?
75
+ end
76
+ end
77
+
78
+
79
+ def extract_fields(line)
80
+ if /^#Fields: .+/.match(line)
81
+ junk, format = line.strip().split(/#Fields: (.+)/)
82
+ format unless format.nil?
83
+ end
84
+ end
85
+ end # class LogStash::Codecs::Cloudfront
@@ -0,0 +1,28 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ s.name = 'logstash-codec-cloudfront'
4
+ s.version = '0.1.1'
5
+ s.licenses = ['Apache License (2.0)']
6
+ s.summary = "This codec may be used to decode (via inputs) cloudfront gziped files"
7
+ s.description = "This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program"
8
+ s.authors = ["Elasticsearch"]
9
+ s.email = 'richard.pijnenburg@elasticsearch.com'
10
+ s.homepage = "http://www.elasticsearch.org/guide/en/logstash/current/index.html"
11
+ s.require_paths = ["lib"]
12
+
13
+ # Files
14
+ s.files = `git ls-files`.split($\)
15
+
16
+ # Tests
17
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
18
+
19
+ # Special flag to let us know this is actually a logstash plugin
20
+ s.metadata = { "logstash_plugin" => "true", "logstash_group" => "codec" }
21
+
22
+ # Gem dependencies
23
+ s.add_runtime_dependency 'logstash', '>= 1.4.0', '< 2.0.0'
24
+
25
+ s.add_development_dependency 'logstash-devutils'
26
+ s.add_development_dependency 'logstash-codec-plain'
27
+ end
28
+
@@ -0,0 +1,92 @@
1
+ # encoding: utf-8
2
+ require "logstash/devutils/rspec/spec_helper"
3
+ require "logstash/codecs/cloudfront"
4
+ require "logstash/errors"
5
+ require "stringio"
6
+ require "zlib"
7
+
8
+ def compress_with_gzip(io)
9
+ compressed = StringIO.new('', 'r+b')
10
+
11
+ gzip = Zlib::GzipWriter.new(compressed)
12
+ gzip.write(io.read)
13
+ gzip.finish
14
+
15
+ compressed.rewind
16
+
17
+ compressed
18
+ end
19
+
20
+ describe LogStash::Codecs::Cloudfront do
21
+ let!(:uncompressed_cloudfront_log) do
22
+ # Using format from
23
+ # http://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/AccessLogs.html
24
+ str = StringIO.new
25
+
26
+ str << "#Version: 1.0\n"
27
+ str << "#Fields: date time x-edge-location c-ip x-event sc-bytes x-cf-status x-cf-client-id cs-uri-stem cs-uri-query c-referrer x-page-url​ c-user-agent x-sname x-sname-query x-file-ext x-sid\n"
28
+ str << "2010-03-12 23:51:20 SEA4 192.0.2.147 connect 2014 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=204 LNX%2010,0,32,18 - - - -\n"
29
+ str << "2010-03-12 23:51:21 SEA4 192.0.2.222 play 3914 OK bfd8a98bee0840d9b871b7f6ade9908f rtmp://shqshne4jdp4b6.cloudfront.net/cfx/st​ key=value http://player.longtailvideo.com/player.swf http://www.longtailvideo.com/support/jw-player-setup-wizard?example=204 LNX%2010,0,32,18 myvideo p=2&q=4 flv 1\n"
30
+
31
+ str.rewind
32
+ str
33
+ end
34
+
35
+ describe "#decode" do
36
+ it "should create events from a gzip file" do
37
+ events = []
38
+
39
+ subject.decode(compress_with_gzip(uncompressed_cloudfront_log)) do |event|
40
+ events << event
41
+ end
42
+
43
+ expect(events.size).to eq(2)
44
+ end
45
+
46
+ it 'should extract the metadata of the file' do
47
+ events = []
48
+
49
+ subject.decode(compress_with_gzip(uncompressed_cloudfront_log)) do |event|
50
+ events << event
51
+ end
52
+
53
+ expect(events.first["cloudfront_version"]).to eq("1.0")
54
+ expect(events.first["cloudfront_fields"]).to eq("date time x-edge-location c-ip x-event sc-bytes x-cf-status x-cf-client-id cs-uri-stem cs-uri-query c-referrer x-page-url​ c-user-agent x-sname x-sname-query x-file-ext x-sid")
55
+ end
56
+ end
57
+
58
+ describe "#extract_version" do
59
+ it "returns the version from a matched string" do
60
+ line = "#Version: 1.0"
61
+
62
+ expect(subject.extract_version(line)).to eq("1.0")
63
+ end
64
+
65
+ it "doesn't return anything if version isnt matched" do
66
+ line = "Bleh my string"
67
+ expect(subject.extract_version(line)).to eq(nil)
68
+ end
69
+
70
+ it "doesn't match if #Version is not at the beginning of the string" do
71
+ line = "2010-03-12 23:53:44 SEA4 192.0.2.4 stop 323914 OK bfd8a98bee0840d9b871b7f6ade9908f #Version: 1.0 Bleh blah"
72
+ expect(subject.extract_version(line)).to eq(nil)
73
+ end
74
+ end
75
+
76
+ describe "#extract_fields" do
77
+ it "return a string with all the fields" do
78
+ line = "#Fields: date time x-edge-location c-ip x-event sc-bytes x-cf-status x-cf-client-id cs-uri-stem cs-uri-query c-referrer x-page-url​ c-user-agent x-sname x-sname-query x-file-ext x-sid"
79
+ expect(subject.extract_fields(line)).to eq("date time x-edge-location c-ip x-event sc-bytes x-cf-status x-cf-client-id cs-uri-stem cs-uri-query c-referrer x-page-url​ c-user-agent x-sname x-sname-query x-file-ext x-sid")
80
+ end
81
+
82
+ it "doesn't return anything if we can the fields list" do
83
+ line = "Bleh my string"
84
+ expect(subject.extract_fields(line)).to eq(nil)
85
+ end
86
+
87
+ it "doesnt match if #Fields: is not at the beginning of the string" do
88
+ line = "2010-03-12 23:53:44 SEA4 192.0.2.4 stop 323914 OK bfd8a98bee0840d9b871b7f6ade9908f #Fields: 1.0 Bleh blah"
89
+ expect(subject.extract_fields(line)).to eq(nil)
90
+ end
91
+ end
92
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logstash-codec-cloudfront
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Elasticsearch
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logstash
15
+ version_requirements: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ - - <
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.0
23
+ requirement: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - '>='
26
+ - !ruby/object:Gem::Version
27
+ version: 1.4.0
28
+ - - <
29
+ - !ruby/object:Gem::Version
30
+ version: 2.0.0
31
+ prerelease: false
32
+ type: :runtime
33
+ - !ruby/object:Gem::Dependency
34
+ name: logstash-devutils
35
+ version_requirements: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - '>='
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirement: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ prerelease: false
46
+ type: :development
47
+ - !ruby/object:Gem::Dependency
48
+ name: logstash-codec-plain
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirement: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ prerelease: false
60
+ type: :development
61
+ description: This gem is a logstash plugin required to be installed on top of the Logstash core pipeline using $LS_HOME/bin/plugin install gemname. This gem is not a stand-alone program
62
+ email: richard.pijnenburg@elasticsearch.com
63
+ executables: []
64
+ extensions: []
65
+ extra_rdoc_files: []
66
+ files:
67
+ - .gitignore
68
+ - Gemfile
69
+ - LICENSE
70
+ - Rakefile
71
+ - lib/logstash/codecs/cloudfront.rb
72
+ - logstash-codec-cloudfront.gemspec
73
+ - spec/codecs/cloudfront_spec.rb
74
+ homepage: http://www.elasticsearch.org/guide/en/logstash/current/index.html
75
+ licenses:
76
+ - Apache License (2.0)
77
+ metadata:
78
+ logstash_plugin: 'true'
79
+ logstash_group: codec
80
+ post_install_message:
81
+ rdoc_options: []
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - '>='
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ requirements: []
95
+ rubyforge_project:
96
+ rubygems_version: 2.4.4
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: This codec may be used to decode (via inputs) cloudfront gziped files
100
+ test_files:
101
+ - spec/codecs/cloudfront_spec.rb