kong 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3aee0b2fdf56d12d85778e534f4c8165ff537938
4
- data.tar.gz: 2ef60a6ddadc60cb9f29ca6dfef80bb881815556
3
+ metadata.gz: 16715f84192ef0b86892c41bd5a64233f14bd1f9
4
+ data.tar.gz: 140668cf943e425c3ddc7d68bd8b498eba655805
5
5
  SHA512:
6
- metadata.gz: 4ab2f7efcd3f83b0213ac1e414ccb5de86284b40316518d9e7bb00e21e591b11f761c748a873ccdced21cb83f02d8b2c56f7e66d053e0f765c0663b0c80a21d0
7
- data.tar.gz: fd7c4e700bd2869cfbfe88ef9090a41cd18e96d784cca01c9a61167b8d4ddc0f9f4b9c727c1655b47f96af2f12b5224df5cf0fecff7347792be4cc2dbb0c5b31
6
+ metadata.gz: 071e6c144db8b6b556e03c49eb5f1e5747d289284164cacafd9802ebcde59f4d66927eb1460877a6fb54d851beb47a0411d3d5f7c68d580da3e693c40a6402b5
7
+ data.tar.gz: 0a0bb5ee564595b160910c2d325fe4ae0c67d67bf65dd5c6be60804b2874047060ccc69f23badae923f5e24ac940dc56643335f401607242fd66abcd3d66c97a
data/.hound.yml ADDED
@@ -0,0 +1,2 @@
1
+ ruby:
2
+ config_file: .rubocop.yml
data/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # 0.1.2 (2017-01-12)
2
+ - Add JWT support ([#2](https://github.com/kontena/kong-client-ruby/pull/2))
3
+ - Allow to properly set a custom api_url ([#4](https://github.com/kontena/kong-client-ruby/pull/4))
4
+ - Allow to redefine api_url
5
+ ([#6](https://github.com/kontena/kong-client-ruby/pull/6))
6
+ - Add ACL support
7
+ ([#9](https://github.com/kontena/kong-client-ruby/pull/9))
8
+
9
+ # 0.1.1 (2016-10-10)
10
+ - Fix Kong::Base.respond_to? to return true for find_by_* methods
data/README.md CHANGED
@@ -203,6 +203,24 @@ token.delete
203
203
  token.oauth_app
204
204
  ```
205
205
 
206
+ #### ACL
207
+
208
+ ```ruby
209
+
210
+ acl = Kong::Acl.new({
211
+ consumer_id: 'a3dX2dh2-1adb-40a5-c042-63b19dbx83hF4',
212
+ group: 'group1'
213
+ })
214
+
215
+ acl.create
216
+ acl.update
217
+ acl.save # requests create_or_update action
218
+ acl.delete
219
+
220
+ consumer = Kong::Consumer.find_by_username('testuser')
221
+ consumer.acls
222
+ ```
223
+
206
224
  ## Contributing
207
225
 
208
226
  1. Fork it ( https://github.com/kontena/kong-client-ruby/fork )
data/lib/kong/acl.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Kong
2
+ class Acl
3
+ include Base
4
+ include BelongsToConsumer
5
+ ATTRIBUTE_NAMES = %w(id group consumer_id).freeze
6
+ API_END_POINT = "/acls/".freeze
7
+ end
8
+ end
data/lib/kong/base.rb CHANGED
@@ -49,7 +49,7 @@ module Kong
49
49
  return true
50
50
  else
51
51
  super
52
- end
52
+ end
53
53
  else
54
54
  super
55
55
  end
data/lib/kong/client.rb CHANGED
@@ -5,33 +5,41 @@ require_relative './error'
5
5
 
6
6
  module Kong
7
7
  class Client
8
+ class << self
9
+ attr_accessor :http_client
10
+ end
11
+
8
12
  include Singleton
9
13
 
10
14
  attr_accessor :default_headers
11
- attr_reader :http_client
12
15
 
13
16
  # Initialize api client
14
17
  #
15
18
  def initialize
16
19
  Excon.defaults[:ssl_verify_peer] = false if ignore_ssl_errors?
17
20
  @api_url = api_url
18
- @http_client = Excon.new(@api_url, omit_default_port: true)
21
+ self.class.http_client = Excon.new(@api_url, omit_default_port: true)
19
22
  @default_headers = { 'Accept' => 'application/json' }
20
23
  end
21
24
 
22
25
  def self.api_url
23
- self.instance.api_url
26
+ @api_url || ENV['KONG_URI'] || 'http://localhost:8001'
24
27
  end
25
28
 
26
29
  def self.api_url=(url)
27
- self.instance.api_url = url
30
+ @api_url = url
31
+ @http_client = Excon.new(self.api_url, omit_default_port: true)
32
+ end
33
+
34
+ def http_client
35
+ self.class.http_client
28
36
  end
29
37
 
30
38
  # Kong Admin API URL
31
39
  #
32
40
  # @return [String]
33
- def api_url
34
- @api_url ||= ENV['KONG_URI'] || 'http://localhost:8001'
41
+ def api_url
42
+ self.class.api_url
35
43
  end
36
44
 
37
45
  def api_url=(url)
data/lib/kong/consumer.rb CHANGED
@@ -65,7 +65,39 @@ module Kong
65
65
  #
66
66
  # @return [Array<Kong::OAuth2Token>]
67
67
  def oauth2_tokens
68
- OAuth2Token.list({ authenticated_userid: self.custom_id })
68
+ if self.custom_id
69
+ OAuth2Token.list({ authenticated_userid: self.custom_id })
70
+ else
71
+ []
72
+ end
73
+ end
74
+
75
+ # List Acls
76
+ #
77
+ # @return [Array<Kong::Acl>]
78
+ def acls
79
+ acls = []
80
+ response = client.get("#{@api_end_point}#{self.username}/acls") rescue nil
81
+ if response
82
+ response['data'].each do |attributes|
83
+ acls << Kong::Acl.new(attributes)
84
+ end
85
+ end
86
+ acls
87
+ end
88
+
89
+ # List JWTs
90
+ #
91
+ # @return [Array<Kong::JWT>]
92
+ def jwts
93
+ apps = []
94
+ response = client.get("#{@api_end_point}#{self.username}/jwt") rescue nil
95
+ if response
96
+ response['data'].each do |attributes|
97
+ apps << Kong::JWT.new(attributes)
98
+ end
99
+ end
100
+ apps
69
101
  end
70
102
  end
71
103
  end
data/lib/kong/jwt.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Kong
2
+ class JWT
3
+ include Base
4
+ include BelongsToConsumer
5
+ ATTRIBUTE_NAMES = %w(id key secret consumer_id).freeze
6
+ API_END_POINT = '/jwt/'.freeze
7
+ end
8
+ end
data/lib/kong/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kong
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.2'.freeze
3
3
  end
data/lib/kong.rb CHANGED
@@ -1,13 +1,15 @@
1
1
  require 'kong/version'
2
- require 'kong/base'
3
- require 'kong/api'
4
- require 'kong/belongs_to_api'
5
- require 'kong/client'
6
- require 'kong/consumer'
7
- require 'kong/belongs_to_consumer'
8
- require 'kong/plugin'
9
- require 'kong/error'
10
- require 'kong/oauth_app'
11
- require 'kong/oauth2_token'
12
- require 'kong/basic_auth'
13
- require 'kong/key_auth'
2
+ require_relative 'kong/base'
3
+ require_relative 'kong/api'
4
+ require_relative 'kong/belongs_to_api'
5
+ require_relative 'kong/client'
6
+ require_relative 'kong/consumer'
7
+ require_relative 'kong/belongs_to_consumer'
8
+ require_relative 'kong/plugin'
9
+ require_relative 'kong/error'
10
+ require_relative 'kong/oauth_app'
11
+ require_relative 'kong/oauth2_token'
12
+ require_relative 'kong/basic_auth'
13
+ require_relative 'kong/key_auth'
14
+ require_relative 'kong/jwt'
15
+ require_relative 'kong/acl'
@@ -0,0 +1,19 @@
1
+ require_relative "../spec_helper"
2
+
3
+ describe Kong::Acl do
4
+ let(:valid_attribute_names) do
5
+ %w(id group consumer_id)
6
+ end
7
+
8
+ describe '::ATTRIBUTE_NAMES' do
9
+ it 'contains valid names' do
10
+ expect(subject.class::ATTRIBUTE_NAMES).to eq(valid_attribute_names)
11
+ end
12
+ end
13
+
14
+ describe '::API_END_POINT' do
15
+ it 'contains valid end point' do
16
+ expect(subject.class::API_END_POINT).to eq('/acls/')
17
+ end
18
+ end
19
+ end
@@ -12,6 +12,7 @@ describe Kong::Client do
12
12
 
13
13
  describe '#initialize' do
14
14
  it 'initializes Excon with Kong URI' do
15
+ described_class.api_url = nil
15
16
  expect(Excon).to receive(:new).with('http://localhost:8001', { omit_default_port: true })
16
17
  described_class.send(:new)
17
18
  end
@@ -23,7 +24,22 @@ describe Kong::Client do
23
24
  end
24
25
 
25
26
  describe '#api_url' do
27
+
28
+ def client_params
29
+ uri = URI.parse described_class.api_url
30
+ params = {
31
+ host: uri.host,
32
+ hostname: uri.hostname,
33
+ path: uri.path,
34
+ port: uri.port,
35
+ query: uri.query,
36
+ scheme: uri.scheme
37
+ }
38
+ described_class.instance.http_client.params.merge(params)
39
+ end
40
+
26
41
  it 'returns localhost as default' do
42
+ described_class.api_url = nil
27
43
  expect(subject.api_url).to eq('http://localhost:8001')
28
44
  end
29
45
 
@@ -32,9 +48,23 @@ describe Kong::Client do
32
48
  allow(ENV).to receive(:[]).with('no_proxy')
33
49
  allow(ENV).to receive(:[]).with('SSL_IGNORE_ERRORS')
34
50
  allow(ENV).to receive(:[]).with('KONG_URI').and_return('http://kong-api:8001')
51
+ described_class.api_url = nil
35
52
  subject = described_class.send(:new)
36
53
  expect(subject.api_url).to eq('http://kong-api:8001')
37
54
  end
55
+
56
+ it 'returns custom api_url if set' do
57
+ url = 'http://foo.bar:1337'
58
+ described_class.api_url = url
59
+ expect(described_class.send(:new).api_url).to eq(url)
60
+ end
61
+
62
+ it 'can edit api_url' do
63
+ described_class.api_url = nil
64
+ expect(described_class.instance.http_client.params).to eq(client_params)
65
+ described_class.api_url = 'http://foo.bar:1337'
66
+ expect(described_class.instance.http_client.params).to eq(client_params)
67
+ end
38
68
  end
39
69
 
40
70
  describe '#get' do
@@ -35,10 +35,38 @@ describe Kong::Consumer do
35
35
  end
36
36
 
37
37
  describe '#oauth2_tokens' do
38
- it 'requests oauth2_tokens assigned to consumer' do
39
- subject.custom_id = 'custom_id'
40
- expect(Kong::OAuth2Token).to receive(:list).with({ authenticated_userid: subject.custom_id })
41
- subject.oauth2_tokens
38
+ context 'when custom_id is set' do
39
+ it 'requests oauth2_tokens assigned to consumer' do
40
+ subject.custom_id = 'custom_id'
41
+ expect(Kong::OAuth2Token).to receive(:list).with({ authenticated_userid: subject.custom_id })
42
+ subject.oauth2_tokens
43
+ end
44
+ end
45
+ context 'when custom_id is not set' do
46
+ it 'requests oauth2_tokens assigned to consumer' do
47
+ expect(Kong::OAuth2Token).not_to receive(:list)
48
+ subject.oauth2_tokens
49
+ end
50
+ it 'returns empty array' do
51
+ expect(subject.oauth2_tokens).to eq([])
52
+ end
53
+ end
54
+ end
55
+
56
+ describe '#acls' do
57
+ it 'requests consumer\'s acls' do
58
+ subject.username = ':username'
59
+ expect(Kong::Client.instance).to receive(:get).with("/consumers/:username/acls")
60
+ .and_return({ 'data' => [{ 'id' => '123456', 'group' => 'group1' }] })
61
+ subject.acls
62
+ end
63
+
64
+ it 'returns list of Acls' do
65
+ subject.username = ':username'
66
+ allow(Kong::Client.instance).to receive(:get).with("/consumers/:username/acls")
67
+ .and_return({ 'data' => [{ 'id' => '123456', 'group' => 'group1' }] })
68
+ result = subject.acls
69
+ expect(result.first.is_a?(Kong::Acl)).to be_truthy
42
70
  end
43
71
  end
44
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kong
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lauri Nevala
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-10 00:00:00.000000000 Z
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -60,14 +60,17 @@ extensions: []
60
60
  extra_rdoc_files: []
61
61
  files:
62
62
  - ".gitignore"
63
+ - ".hound.yml"
63
64
  - ".rubocop.yml"
64
65
  - ".travis.yml"
66
+ - CHANGELOG.md
65
67
  - Gemfile
66
68
  - LICENSE
67
69
  - README.md
68
70
  - Rakefile
69
71
  - kong.gemspec
70
72
  - lib/kong.rb
73
+ - lib/kong/acl.rb
71
74
  - lib/kong/api.rb
72
75
  - lib/kong/base.rb
73
76
  - lib/kong/basic_auth.rb
@@ -76,11 +79,13 @@ files:
76
79
  - lib/kong/client.rb
77
80
  - lib/kong/consumer.rb
78
81
  - lib/kong/error.rb
82
+ - lib/kong/jwt.rb
79
83
  - lib/kong/key_auth.rb
80
84
  - lib/kong/oauth2_token.rb
81
85
  - lib/kong/oauth_app.rb
82
86
  - lib/kong/plugin.rb
83
87
  - lib/kong/version.rb
88
+ - spec/kong/acl_spec.rb
84
89
  - spec/kong/api_spec.rb
85
90
  - spec/kong/base_spec.rb
86
91
  - spec/kong/basic_auth_spec.rb
@@ -112,11 +117,12 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
117
  version: '0'
113
118
  requirements: []
114
119
  rubyforge_project:
115
- rubygems_version: 2.4.8
120
+ rubygems_version: 2.6.6
116
121
  signing_key:
117
122
  specification_version: 4
118
123
  summary: A Ruby client for the Kong API
119
124
  test_files:
125
+ - spec/kong/acl_spec.rb
120
126
  - spec/kong/api_spec.rb
121
127
  - spec/kong/base_spec.rb
122
128
  - spec/kong/basic_auth_spec.rb