whowas 0.1.4 → 0.2.0
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 +4 -4
- data/lib/{apis → adapters}/splunk.rb +3 -3
- data/lib/generators/whowas/adapter_generator.rb +13 -0
- data/lib/generators/whowas/recipe_generator.rb +3 -3
- data/lib/generators/whowas/search_method_generator.rb +3 -3
- data/lib/generators/whowas/templates/{api.rb → adapter.rb} +10 -9
- data/lib/generators/whowas/templates/initializer.rb +8 -8
- data/lib/generators/whowas/templates/recipe.rb +2 -2
- data/lib/generators/whowas/templates/search_method.rb +10 -8
- data/lib/whowas.rb +4 -4
- data/lib/whowas/{api.rb → adapter.rb} +3 -3
- data/lib/whowas/formattable.rb +2 -6
- data/lib/whowas/searchable.rb +5 -5
- data/lib/whowas/validatable.rb +3 -2
- data/lib/whowas/version.rb +1 -1
- metadata +6 -6
- data/lib/generators/whowas/api_generator.rb +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a92b53b7bef30819cd2501c2435b13b10be15708
|
4
|
+
data.tar.gz: 4f038b70ab9fd3bd78a3132a8d82f012737a4c48
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ca98edc334537e9ead70ad1b847a7457d417535aa31179e71764e4f4414df0bb46b51a1388a3f161162a24b668f977078924e0371b0d346bbebf87bd90f4008
|
7
|
+
data.tar.gz: 35a057c905f6d486312239c80449473071138ebe17144845b3f47cfe0d7439563df2289d933b4d5aa4564990b781cb76e6fa54996dea555947d3f4b6b0f53806
|
@@ -2,7 +2,7 @@ require "splunk-sdk-ruby"
|
|
2
2
|
|
3
3
|
module Whowas
|
4
4
|
class Splunk
|
5
|
-
include Whowas::
|
5
|
+
include Whowas::Adapter
|
6
6
|
|
7
7
|
@@connection = nil
|
8
8
|
|
@@ -13,7 +13,7 @@ module Whowas
|
|
13
13
|
def self.connection(config: Whowas.splunk_config)
|
14
14
|
@@connection ||= ::Splunk::connect(config)
|
15
15
|
rescue => e
|
16
|
-
raise Whowas::Errors::ServiceUnavailable, e
|
16
|
+
raise Whowas::Errors::ServiceUnavailable, "#{self.class.name}: #{e}"
|
17
17
|
end
|
18
18
|
|
19
19
|
def validate(input)
|
@@ -22,7 +22,7 @@ module Whowas
|
|
22
22
|
input[:offset].is_a?(Integer) &&
|
23
23
|
DateTime.parse(input[:timestamp]) &&
|
24
24
|
true) ||
|
25
|
-
(raise Whowas::Errors::InvalidInput, "Invalid input for Splunk
|
25
|
+
(raise Whowas::Errors::InvalidInput, "Invalid input for Splunk")
|
26
26
|
end
|
27
27
|
|
28
28
|
def format(input)
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module Whowas
|
2
|
+
class AdapterGenerator < Rails::Generators::NamedBase
|
3
|
+
source_root File.expand_path("../templates", __FILE__)
|
4
|
+
|
5
|
+
def create
|
6
|
+
copy_file "adapter.rb", "app/whowas/adapters/#{file_name}.rb"
|
7
|
+
end
|
8
|
+
|
9
|
+
def rename_class
|
10
|
+
gsub_file "app/whowas/adapters/#{file_name}.rb", /MyAdapter/, name.camelize
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -3,11 +3,11 @@ module Whowas
|
|
3
3
|
source_root File.expand_path("../templates", __FILE__)
|
4
4
|
|
5
5
|
def create
|
6
|
-
copy_file "recipe.rb", "app/recipes/#{file_name}.rb"
|
6
|
+
copy_file "recipe.rb", "app/whowas/recipes/#{file_name}.rb"
|
7
7
|
end
|
8
8
|
|
9
9
|
def rename_method
|
10
|
-
gsub_file "app/recipes/#{file_name}.rb", /name_this_recipe/, name.underscore
|
10
|
+
gsub_file "app/whowas/recipes/#{file_name}.rb", /name_this_recipe/, name.underscore
|
11
11
|
end
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
@@ -3,11 +3,11 @@ module Whowas
|
|
3
3
|
source_root File.expand_path("../templates", __FILE__)
|
4
4
|
|
5
5
|
def create
|
6
|
-
copy_file "search_method.rb", "app/search_methods/#{file_name}.rb"
|
6
|
+
copy_file "search_method.rb", "app/whowas/search_methods/#{file_name}.rb"
|
7
7
|
end
|
8
8
|
|
9
9
|
def rename_class
|
10
|
-
gsub_file "app/search_methods/#{file_name}.rb", /MySearchMethod/, name.camelize
|
10
|
+
gsub_file "app/whowas/search_methods/#{file_name}.rb", /MySearchMethod/, name.camelize
|
11
11
|
end
|
12
12
|
end
|
13
|
-
end
|
13
|
+
end
|
@@ -1,17 +1,18 @@
|
|
1
1
|
module Whowas
|
2
|
-
class
|
3
|
-
# Whowas::
|
4
|
-
# "search" instance method.
|
5
|
-
include Whowas::
|
2
|
+
class MyAdapter
|
3
|
+
# Whowas::Adapter provides the public interface to your adapter, accessed
|
4
|
+
# through the "search" instance method.
|
5
|
+
include Whowas::Adapter
|
6
6
|
|
7
|
-
# All custom
|
8
|
-
# you can add private methods as needed for connecting to the
|
7
|
+
# All custom adapter code is defined in the private methods below.
|
8
|
+
# Naturally, you can add private methods as needed for connecting to the
|
9
|
+
# adapter, etc.
|
9
10
|
private
|
10
11
|
|
11
12
|
## Required
|
12
13
|
|
13
|
-
# Sends a search query with provided input to your API and
|
14
|
-
# as a string.
|
14
|
+
# Sends a search query with provided input to your third-party API and
|
15
|
+
# returns results as a string.
|
15
16
|
def search_api(input)
|
16
17
|
""
|
17
18
|
end
|
@@ -27,7 +28,7 @@ module Whowas
|
|
27
28
|
end
|
28
29
|
|
29
30
|
# Transforms input one last time before API call.
|
30
|
-
# Will be called on input for all search_methods using this
|
31
|
+
# Will be called on input for all search_methods using this adapter.
|
31
32
|
# For search_method-specific transformations, use the format_input method
|
32
33
|
# in your search_method.
|
33
34
|
def format(input)
|
@@ -3,16 +3,16 @@ Whowas.configuration do |config|
|
|
3
3
|
## Recipe Table
|
4
4
|
# You MUST define your default recipe and any other recipes you may use
|
5
5
|
# here. Viable keys include "default", "ip_default", "mac_default", and
|
6
|
-
# valid IP addresses or CIDR blocks.
|
6
|
+
# valid IP addresses or CIDR blocks. Recipes are messages sent to Whowas.
|
7
7
|
#
|
8
8
|
# An example of a full recipe table:
|
9
9
|
#
|
10
10
|
# config.recipe_table = {
|
11
|
-
# "192.168.1.0/24":
|
12
|
-
# "10.0.0.0/8":
|
13
|
-
# ip_default:
|
14
|
-
# mac_default:
|
15
|
-
# default:
|
11
|
+
# "192.168.1.0/24": "home_wireless",
|
12
|
+
# "10.0.0.0/8": "internal_wired",
|
13
|
+
# ip_default: "other_ips",
|
14
|
+
# mac_default: "search_by_mac",
|
15
|
+
# default: "other_ips"
|
16
16
|
# }
|
17
17
|
config.recipe_table = {}
|
18
18
|
|
@@ -22,7 +22,7 @@ Whowas.configuration do |config|
|
|
22
22
|
config.recipe_class = Whowas::Recipes
|
23
23
|
|
24
24
|
## API configuration and credentials
|
25
|
-
# If you are not using a bundled
|
25
|
+
# If you are not using a bundled adapter, you can safely ignore this section.
|
26
26
|
|
27
27
|
# Splunk API configuration
|
28
28
|
# The recommended method is to store your Splunk credentials in environment
|
@@ -37,4 +37,4 @@ Whowas.configuration do |config|
|
|
37
37
|
# }
|
38
38
|
#
|
39
39
|
config.splunk_config = {}
|
40
|
-
end
|
40
|
+
end
|
@@ -5,10 +5,10 @@ module Whowas
|
|
5
5
|
# All you have to do is specify the search method classes in the order
|
6
6
|
# they should be called. The output for each search method should match
|
7
7
|
# the input of the next.
|
8
|
-
Middleware::Builder.new do
|
8
|
+
::Middleware::Builder.new do
|
9
9
|
# use MySearchMethod1
|
10
10
|
# use MySearchMethod2
|
11
11
|
# use MySearchMethod3
|
12
12
|
end
|
13
13
|
end
|
14
|
-
end
|
14
|
+
end
|
@@ -1,8 +1,9 @@
|
|
1
1
|
module Whowas
|
2
2
|
class MySearchMethod
|
3
3
|
# All required public methods are contained in the Middleware package. It
|
4
|
-
# initializes the search method, calls search on the
|
5
|
-
# input, and returns the output to the next search method or the
|
4
|
+
# initializes the search method, calls search on the adapter with the
|
5
|
+
# provided input, and returns the output to the next search method or the
|
6
|
+
# caller.
|
6
7
|
#
|
7
8
|
# The Searchable modules (Validatable, Formattable, and Parsable) are
|
8
9
|
# technically optional but in practice necessary to ensure usable input and
|
@@ -10,9 +11,9 @@ module Whowas
|
|
10
11
|
include Whowas::Middleware
|
11
12
|
include Whowas::Searchable
|
12
13
|
|
13
|
-
##
|
14
|
-
# You MUST set this to the name of a bundled or custom
|
15
|
-
@@
|
14
|
+
## ADAPTER
|
15
|
+
# You MUST set this to the name of a bundled or custom adapter class.
|
16
|
+
@@adapter = ADAPTER_CLASS_HERE
|
16
17
|
|
17
18
|
private
|
18
19
|
|
@@ -44,15 +45,16 @@ module Whowas
|
|
44
45
|
# mac addresses given as input to this search method should use colons as
|
45
46
|
# separators, perform that transformation here.
|
46
47
|
#
|
47
|
-
#
|
48
|
+
# Adapter-wide transformations to the input can be made in the adapter
|
49
|
+
# format method.
|
48
50
|
def format_input(input)
|
49
51
|
input
|
50
52
|
end
|
51
53
|
|
52
54
|
## Parsable
|
53
55
|
|
54
|
-
# Extract pieces of the results string from the
|
55
|
-
# input hash for the next search method or the final result.
|
56
|
+
# Extract pieces of the results string from the adapter using regex to form
|
57
|
+
# the input hash for the next search method or the final result.
|
56
58
|
def output_formats
|
57
59
|
{
|
58
60
|
# username: /User <\K\w*/
|
data/lib/whowas.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require "middleware"
|
2
2
|
|
3
|
-
require "whowas/
|
3
|
+
require "whowas/adapter"
|
4
4
|
require "whowas/configuration"
|
5
5
|
require "whowas/errors"
|
6
6
|
require "whowas/formattable"
|
@@ -11,8 +11,8 @@ require "whowas/searchable"
|
|
11
11
|
require "whowas/validatable"
|
12
12
|
require "whowas/version"
|
13
13
|
|
14
|
-
# bundled
|
15
|
-
require "
|
14
|
+
# bundled adapters
|
15
|
+
require "adapters/splunk"
|
16
16
|
|
17
17
|
|
18
18
|
module Whowas
|
@@ -45,7 +45,7 @@ module Whowas
|
|
45
45
|
def self.search(input)
|
46
46
|
recipe = recipe_class.select(input) || Whowas.recipe_table[:default]
|
47
47
|
env = {input: input, results: []}
|
48
|
-
recipe.call(env)
|
48
|
+
self.send(recipe).call(env)
|
49
49
|
env
|
50
50
|
end
|
51
51
|
|
@@ -1,6 +1,6 @@
|
|
1
1
|
module Whowas
|
2
|
-
module
|
3
|
-
# All
|
2
|
+
module Adapter
|
3
|
+
# All adapters use (adapter_instance).search as the only public method
|
4
4
|
# and follow the pattern below.
|
5
5
|
# Validation and formatting are optional.
|
6
6
|
# search_api must contain the core api search code and return the results.
|
@@ -12,7 +12,7 @@ module Whowas
|
|
12
12
|
|
13
13
|
private
|
14
14
|
|
15
|
-
# default methods for
|
15
|
+
# default methods for adapters to use or override
|
16
16
|
#:nocov:
|
17
17
|
def validate(input)
|
18
18
|
true
|
data/lib/whowas/formattable.rb
CHANGED
@@ -7,15 +7,11 @@ module Whowas
|
|
7
7
|
# :nocov:
|
8
8
|
private
|
9
9
|
# This is a hook for the including class to use to modify the input before
|
10
|
-
# the
|
11
|
-
#
|
12
|
-
# For example, when searching in Splunk, the Firewall class (which includes
|
13
|
-
# Searchable) must create the query string from the ip and port, plus any
|
14
|
-
# other parameters like the Splunk index it wants to search.
|
10
|
+
# the adapter gets it.
|
15
11
|
#
|
16
12
|
# By default, this method just returns the arguments given, but note that
|
17
13
|
# most classes _will have_ to modify the input to make it usable by the
|
18
|
-
#
|
14
|
+
# adapter.
|
19
15
|
def format_input(input)
|
20
16
|
input
|
21
17
|
end
|
data/lib/whowas/searchable.rb
CHANGED
@@ -1,10 +1,10 @@
|
|
1
1
|
module Whowas
|
2
2
|
module Searchable
|
3
|
-
@@
|
3
|
+
@@adapter = nil
|
4
4
|
|
5
|
-
# The including class *must* set the
|
6
|
-
def
|
7
|
-
@@
|
5
|
+
# The including class *must* set the adapter class in a class constant.
|
6
|
+
def adapter
|
7
|
+
@@adapter || (raise Errors::SubclassResponsibility)
|
8
8
|
end
|
9
9
|
|
10
10
|
# extend the including class with the searchable sub-modules
|
@@ -19,7 +19,7 @@ module Whowas
|
|
19
19
|
def search(input)
|
20
20
|
validate(input)
|
21
21
|
input = format(input)
|
22
|
-
result =
|
22
|
+
result = adapter.new.search(input)
|
23
23
|
parse(result)
|
24
24
|
end
|
25
25
|
end
|
data/lib/whowas/validatable.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Whowas
|
2
2
|
module Validatable
|
3
3
|
# Checks for required inputs and input formats
|
4
|
-
# that the
|
4
|
+
# that the adapter will need to process the search.
|
5
5
|
#
|
6
6
|
# It does *not* matter if there are other, non-required parameters
|
7
7
|
# in the input hash; they will be ignored later.
|
@@ -32,7 +32,8 @@ module Whowas
|
|
32
32
|
end
|
33
33
|
end
|
34
34
|
|
35
|
-
# Format is a lambda defined in the
|
35
|
+
# Format is a lambda defined in the search method class which returns
|
36
|
+
# true or false.
|
36
37
|
# This allows for flexible format checking:
|
37
38
|
# e.g. regex for ip addresses and DateTime.parse calls for timestamps
|
38
39
|
def check_format(formats, input)
|
data/lib/whowas/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: whowas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jess Frisch
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: middleware
|
@@ -138,17 +138,17 @@ files:
|
|
138
138
|
- Rakefile
|
139
139
|
- bin/console
|
140
140
|
- bin/setup
|
141
|
-
- lib/
|
142
|
-
- lib/generators/whowas/
|
141
|
+
- lib/adapters/splunk.rb
|
142
|
+
- lib/generators/whowas/adapter_generator.rb
|
143
143
|
- lib/generators/whowas/install_generator.rb
|
144
144
|
- lib/generators/whowas/recipe_generator.rb
|
145
145
|
- lib/generators/whowas/search_method_generator.rb
|
146
|
-
- lib/generators/whowas/templates/
|
146
|
+
- lib/generators/whowas/templates/adapter.rb
|
147
147
|
- lib/generators/whowas/templates/initializer.rb
|
148
148
|
- lib/generators/whowas/templates/recipe.rb
|
149
149
|
- lib/generators/whowas/templates/search_method.rb
|
150
150
|
- lib/whowas.rb
|
151
|
-
- lib/whowas/
|
151
|
+
- lib/whowas/adapter.rb
|
152
152
|
- lib/whowas/configuration.rb
|
153
153
|
- lib/whowas/errors.rb
|
154
154
|
- lib/whowas/formattable.rb
|
@@ -1,13 +0,0 @@
|
|
1
|
-
module Whowas
|
2
|
-
class ApiGenerator < Rails::Generators::NamedBase
|
3
|
-
source_root File.expand_path("../templates", __FILE__)
|
4
|
-
|
5
|
-
def create
|
6
|
-
copy_file "api.rb", "app/apis/#{file_name}.rb"
|
7
|
-
end
|
8
|
-
|
9
|
-
def rename_class
|
10
|
-
gsub_file "app/apis/#{file_name}.rb", /MyApi/, name.camelize
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|