kenna 0.1.0 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 52fe805c8e85e972c6542f39aad60f8799ab90e4
4
- data.tar.gz: b9a9eeff68bd477f2b7b005c6ee2f3d72bec7e26
3
+ metadata.gz: 2d071e0a36ec4b38496dcb448521391ef0fc4e3a
4
+ data.tar.gz: 3bd66aedf05f757749b938ca79cf293c91e272a1
5
5
  SHA512:
6
- metadata.gz: 2a3b0a94a8151fcea25b057dac3881de8a206790443e7a51c609b09188e61a5b46800a230563ad2c107b9cca8785d425c20057b9b204504cc8b9285fa49cabe5
7
- data.tar.gz: 838a99326682989c8aa473e8f02c76326b22873d965e6fe2e5fc04c4874538f57c965fccd3448ff71a5be7661c5cf2fe95f6906de2d23924a45935715f27fce1
6
+ metadata.gz: 04b2188185f14649980dd172a73a5c96d3bdcd42131ee90c5514f54e5dee550f47c87c8a4d81d085c15dba9c39f6a759f9d642b072bfb2348de70a4ed60b8b18
7
+ data.tar.gz: c0eba01ca79c079de4313da5f86176b3926fc8aa20e8baa60955cab1af65345947c11d236a3d6da4d27bc808c041cf247303759d19910d8b505b8976e1b3c3cb
data/Gemfile CHANGED
@@ -5,4 +5,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
5
5
  # Specify your gem's dependencies in kenna.gemspec
6
6
  gemspec
7
7
  gem 'test-unit', '~> 3.2', '>= 3.2.7'
8
- gem 'pry', '~> 0.11.3'
8
+ gem 'pry', '~> 0.11.3'
9
+ gem 'rest-client', '~> 2.0', '>= 2.0.2'
10
+ gem 'faker', '~> 1.8', '>= 1.8.7'
data/Gemfile.lock CHANGED
@@ -1,19 +1,36 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kenna (0.1.0)
4
+ kenna (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
9
  coderay (1.1.2)
10
+ concurrent-ruby (1.0.5)
10
11
  diff-lcs (1.3)
12
+ domain_name (0.5.20170404)
13
+ unf (>= 0.0.5, < 1.0.0)
14
+ faker (1.8.7)
15
+ i18n (>= 0.7)
16
+ http-cookie (1.0.3)
17
+ domain_name (~> 0.5)
18
+ i18n (0.9.1)
19
+ concurrent-ruby (~> 1.0)
11
20
  method_source (0.9.0)
21
+ mime-types (3.1)
22
+ mime-types-data (~> 3.2015)
23
+ mime-types-data (3.2016.0521)
24
+ netrc (0.11.0)
12
25
  power_assert (0.4.1)
13
26
  pry (0.11.3)
14
27
  coderay (~> 1.1.0)
15
28
  method_source (~> 0.9.0)
16
29
  rake (10.5.0)
30
+ rest-client (2.0.2)
31
+ http-cookie (>= 1.0.2, < 2.0)
32
+ mime-types (>= 1.16, < 4.0)
33
+ netrc (~> 0.8)
17
34
  rspec (3.7.0)
18
35
  rspec-core (~> 3.7.0)
19
36
  rspec-expectations (~> 3.7.0)
@@ -29,15 +46,20 @@ GEM
29
46
  rspec-support (3.7.0)
30
47
  test-unit (3.2.7)
31
48
  power_assert
49
+ unf (0.1.4)
50
+ unf_ext
51
+ unf_ext (0.0.7.4)
32
52
 
33
53
  PLATFORMS
34
54
  ruby
35
55
 
36
56
  DEPENDENCIES
37
57
  bundler (~> 1.16)
58
+ faker (~> 1.8, >= 1.8.7)
38
59
  kenna!
39
60
  pry (~> 0.11.3)
40
61
  rake (~> 10.0)
62
+ rest-client (~> 2.0, >= 2.0.2)
41
63
  rspec (~> 3.0)
42
64
  test-unit (~> 3.2, >= 3.2.7)
43
65
 
data/README.md CHANGED
@@ -19,22 +19,42 @@ Or install it yourself as:
19
19
 
20
20
  ## Usage
21
21
 
22
- # Get an Api Instance
22
+ Get an Api Instance
23
23
 
24
- # Test token ty9hxcpmgdrvnuqe
25
- # @api = Kenna::Api.new("your_token")
24
+ Test token ty9hxcpmgdrvnuqe
25
+ @api = Kenna::Api.new("your_token")
26
26
 
27
- # Test token ty9hxcpmgdrvnuqe
27
+ Test token ty9hxcpmgdrvnuqe
28
28
  @api = Kenna::Api.new("ty9hxcpmgdrvnuqe")
29
29
 
30
- # Get All Users (only up to 500)
30
+ There are basic crud methods that return a Response Object,
31
+ use .body to get the JSON
32
+ use .code to get the response code
33
+ use .headers to get the headers
34
+ 'resource' is the root name of the endpoint, such as 'users'
35
+ TODO: Several methods are responding only with body and need to be refactored
36
+ TODO: Address throttling issues
37
+
38
+ @api.post(uri, body)
39
+ @api.postByResource(resource, body)
40
+ @api.get(uri)
41
+ @api.getById(resource, id)
42
+ @api.update(uri, body)
43
+ @api.updateById(resource, id, body)
44
+ @api.delete(uri)
45
+ @api.deleteById(resource, id)
46
+
47
+ The following are two examples of shortcut methods,
48
+ I intend to provide these for all routes
49
+
50
+ Get All Users (only up to 500 for now)
31
51
  @users = @api.getAllUsers()
32
52
  puts @users
33
53
 
34
- # Getting the first User ID (there should always be at least one)
54
+ Getting the first User ID (there should always be at least one)
35
55
  @user_id = @users[0]['id']
36
56
 
37
- # Get A Single User by ID
57
+ Get A Single User by ID
38
58
  @user = @api.getUserById(@user_id)
39
59
  puts @user
40
60
 
data/kenna.gemspec CHANGED
@@ -25,4 +25,6 @@ Gem::Specification.new do |spec|
25
25
  spec.add_development_dependency "rake", "~> 10.0"
26
26
  spec.add_development_dependency "rspec", "~> 3.0"
27
27
  spec.add_development_dependency "test-unit", "~> 3.2"
28
+ spec.add_development_dependency "rest-client", "~> 2.0"
29
+ spec.add_development_dependency "faker", "~> 1.8"
28
30
  end
data/lib/kenna/version.rb CHANGED
@@ -4,5 +4,5 @@
4
4
  # License:: Ruby license.
5
5
 
6
6
  module Kenna
7
- VERSION = "0.1.0"
7
+ VERSION = "0.1.1"
8
8
  end
data/lib/kenna.rb CHANGED
@@ -3,58 +3,180 @@
3
3
  # Copyright:: Copyright (c) 2018 Maynard Black. All rights reserved.
4
4
  # License:: Ruby license.
5
5
 
6
- require "kenna/version"
6
+ require 'json'
7
+ require 'kenna/version'
8
+ require 'rest-client'
9
+ require 'faker'
7
10
 
8
11
  module Kenna
9
- class Api
10
-
11
- def initialize(token)
12
- @token = token
13
- end
14
-
15
- def getAllUsers()
16
- callApi('GET', '/users', 'users', {})
17
- end
18
-
19
- def getUserById(id)
20
- path = '/users/' + id.to_s
21
- callApi('GET', path, 'user', {})
22
- end
23
-
24
- def callApi(meth, endpoint, base_key, body)
25
- require 'net/https'
26
- require 'uri'
27
- require 'json'
28
-
29
- url = URI.parse("https://api.kennasecurity.com")
30
- #base_url = 'https://api.kennasecurity.com'
31
- #params = ''
32
- #url = base_url + endpoint + params;
33
- data = nil
34
- data_str = nil
35
-
36
- http = Net::HTTP.new(url.host, 443)
37
- http.use_ssl = true
38
- http.start do |http|
39
- request =
40
- Net::HTTP::Get.new(endpoint, {'X-Risk-Token' => @token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
41
- response = http.request(request)
42
- #data_str = JSON.pretty_generate JSON.parse(response.body)[base_key]
43
- if base_key
44
- data = JSON.parse(response.body)[base_key]
45
- else
46
- data = JSON.parse(response.body)
47
- end
48
- end
49
- data
50
- end
12
+ class Api
13
+
14
+ # Must have a token to continue
15
+ def initialize(token)
16
+ $token = token
17
+ end
18
+
19
+ # TODO (Maynard Black): ask Kenna about API versioning
20
+ $base_url = "https://api.kennasecurity.com"
21
+
22
+ # Here are some generic CRUD calls to the API
23
+ # The URI is the full resource path without the base URL
24
+ # 'Resource' would be the same as base_key or root of the endpoint
25
+ # TODO (Maynard Black): Confirm with Kenna if this is standard and can be depended upon
26
+ # TODO (Maynard Black): Use config to set base URL and Token, find out how Kenna plans to version the API
27
+ # TODO (Maynard Black): Ask Kenna if ID's are unique accross all endpoints and do they plan on offering
28
+ # an endpoint to locate any resource by just the ID (they are not UUIDs so not likely)
29
+ # TODO (Important - Maynard Black): Refactor to return the whole response including headers
30
+ # This means updating the tests first! Then the API Browser App!
31
+ # I am currently only returning a JSON parsed body so there is no way to catch 404s and such
32
+ # UPDATE: the 2 get,delete and uppdate methods have been refactored as per above
33
+ # TODO (Maynard Black): Refactor the 2 post, put, getAllUsers, getUserById methods to return headers
34
+ # TODO (Maynard Black): Catch exceptions when hitting server, that is the only way to
35
+ # catch certain response codes (like 404) with the client I am using
36
+
37
+ # Create
38
+ def post(uri, body)
39
+ body = body.to_json
40
+ @url = $base_url + uri
41
+ @response = RestClient.post(@url, body, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
42
+ JSON.parse(@response.body)
43
+ end
44
+
45
+ def postByResource(resource, body)
46
+ body = body.to_json
47
+ @url = $base_url +'/'+ resource
48
+ @response = RestClient.post(@url, body, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
49
+ JSON.parse(@response.body)
50
+ end
51
+
52
+ # Read
53
+ def get(uri)
54
+ @url = $base_url + uri
55
+ @response = RestClient.get(@url, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
56
+ end
57
+
58
+ def getById(resource, id)
59
+ @url = $base_url +'/'+ resource +'/'+ id.to_s
60
+ @response = RestClient.get(@url, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
61
+ end
62
+
63
+ # Update
64
+ def update(uri, body)
65
+ body = body.to_json
66
+ @url = $base_url + uri
67
+ @response = RestClient.put(@url, body, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
68
+ end
69
+
70
+ def updateById(resource, id, body)
71
+ body = body.to_json
72
+ @url = $base_url +'/'+ resource +'/' + id.to_s
73
+ @response = RestClient.put(@url, body, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
74
+ end
75
+
76
+ # Delete
77
+ def delete(uri)
78
+ @url = $base_url + uri
79
+ @response = RestClient.delete(@url, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
80
+ end
81
+
82
+ def deleteById(resource, id)
83
+ @url = $base_url +'/'+ resource +'/' + id.to_s
84
+ @response = RestClient.delete(@url, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
51
85
  end
86
+
87
+ # User specific methods
88
+ # TODO (Maynard Black): Finish all User options
89
+ # TODO (Maynard Black): Do this for all other endpoints
90
+ # TODO (Maynard Black): Some endpoint calls will require an array of id's, the app can fill
91
+ # in the rest
92
+ def getAllUsers()
93
+ @url = $base_url + '/users'
94
+ @response = RestClient.get(@url, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
95
+ JSON.parse(@response.body)["users"]
96
+ end
97
+
98
+ def getUserById(id)
99
+ @url = $base_url + '/users/' + id.to_s
100
+ @response = RestClient.get(@url, headers={'X-Risk-Token' => $token, 'Content-Type' => 'application/json', 'Accept' => 'application/json'})
101
+ JSON.parse(@response.body)["user"]
102
+ end
103
+
104
+ # Generate a unique fake user for testing
105
+ # and for the API browser
106
+ def fakeUser
107
+ @roles = ['administrator', 'normal user', 'Linux Test Environment']
108
+ @role = @roles[rand(0..2)]
109
+ @fake_user = {
110
+ "user":
111
+ {
112
+ "firstname": Faker::Name.first_name ,
113
+ "lastname": Faker::Name.last_name ,
114
+ "email": Faker::Internet.email,
115
+ "role": @role
116
+ }
117
+ }
118
+ end
119
+
120
+ # Pretty version for API Browser
121
+ def fakeUserPretty
122
+ JSON.pretty_generate JSON.parse(fakeUser)
123
+ end
124
+
125
+ end
52
126
  end
53
127
 
54
- @api = Kenna::Api.new("ty9hxcpmgdrvnuqe")
55
- @users = @api.getAllUsers()
56
- @user_id = @users[0]['id']
57
- @user = @api.getUserById(@user_id)
128
+ # BASIC MANUAL TESTS
129
+ # There are actual tests for some things (Written first believe it or not!)
130
+ # But I wrote these local test to get a better idea of what was happening
131
+ # TODO (Maynard Black): Finish tests!
132
+ # TODO (Maynard Black): The sample account Token will need to change! This is my free temp one
133
+
134
+ # @api = Kenna::Api.new("ty9hxcpmgdrvnuqe")
135
+
136
+ # @new_user = @api.post('/users', @api.fakeUser)
137
+ # @id = @new_user["user"]["id"]
138
+ # puts @id
139
+
140
+ # @new_user = @api.postByResource('users', @api.fakeUser)
141
+ # @id2 = @new_user["user"]["id"]
142
+ # puts @id2
143
+
144
+ # TODO (Maynard Black): Address throttling issues
145
+
146
+ # @b = {"firstname":"JOE"}
147
+ # @u = @api.update('/users/' + @id.to_s, @b)
148
+ # puts @u.code
149
+ # puts JSON.parse(@u.body)["user"]["firstname"]
150
+
151
+ # @data = @api.getById('users', 35665)
152
+ # puts @data['user']['id']
153
+
154
+ # User Methods
155
+ # @users = @api.getAllUsers()
156
+ # puts @users
157
+
158
+ # @users = JSON.parse(@users)
159
+ # puts @users[@users.keys[0]]
160
+
161
+ # @user_id = @users[0]['id']
162
+ # @user = @api.getUserById(@user_id)
163
+ # puts @user['id'] == 35665
164
+
165
+ # @id = 35992
166
+ # @body = {"firstname":"KERMIT"}
167
+ # @api.update('/users/' + @id.to_s, @body)
168
+
169
+ # @new_user = @api.get('/users/' + @id.to_s)
170
+ # @new_name = @new_user["user"]["firstname"]
171
+ # puts @new_user
172
+ # puts @new_name
173
+
174
+ # @id = @api.getAllUsers()[9]["id"]
175
+ # puts @id
58
176
 
59
- #puts @user['id'] == 35665
177
+ # @delete = @api.delete('/users/' + @id.to_s)
178
+ #puts @delete.code != 200
60
179
 
180
+ # @user = @api.get('/users/' + @id.to_s)
181
+ # puts @user.class == 'RestClient::Response'
182
+ # puts @user.code != 200
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kenna
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - maynard
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-23 00:00:00.000000000 Z
11
+ date: 2018-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -66,6 +66,34 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '3.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rest-client
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: faker
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.8'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.8'
69
97
  description: Kenna is an API client that wraps the Kenna Security API (https://api.kennasecurity.com).
70
98
  email:
71
99
  - maynardblack@hotmail.com