kodmin 0.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: e01fb7cb8499fb6440f0eb149c4ceeba1ed722160382803310069235bed179f7
4
+ data.tar.gz: c2de270472ce412a75c8b95f05fac239308f2d91dc0e89163c6adf95e3dad318
5
+ SHA512:
6
+ metadata.gz: bec3f58de443e770824833207894ac1a082bf7304effbc4f94d6196d45851238b0847a9137f45895b3bd71d0f66c698ad8f9c229335bdcfd4a4bb9f392c31784
7
+ data.tar.gz: fbe60ec196e0cc88048d5993d9e65babc54ac5c2239cceb65e4f1dddd81bf0f8f020b88fa33c0bdd4c25dffb338f091c749df3f22820e3be15a718386c9a1abc
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ *.gem
11
+ *.DS_Store
12
+ .idea
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in kodmin.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 13.0"
7
+ gem "minitest", "~> 5.0"
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ kodmin (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.0)
10
+ rake (13.0.1)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ kodmin!
17
+ minitest (~> 5.0)
18
+ rake (~> 13.0)
19
+
20
+ BUNDLED WITH
21
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Wu Zixing
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,43 @@
1
+ # Kodmin
2
+
3
+ It is the ruby client of [kong](https://github.com/Kong/kong)'s admin API.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'kodmin'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install kodmin
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'kodmin'
25
+
26
+ # initialize the client
27
+ Kodmin.init('http://kong.amdin.url')
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wuzixing/kodmin.
39
+
40
+
41
+ ## License
42
+
43
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kodmin"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ require_relative 'lib/kodmin/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "kodmin"
5
+ spec.version = Kodmin::VERSION
6
+ spec.authors = ["Wu Zixing"]
7
+ spec.email = ["wzixing@gmail.com"]
8
+
9
+ spec.summary = %q{A ruby client of kong's admin API v2.x.}
10
+ spec.homepage = "https://github.com/WuZixing/kodmin"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = "https://github.com/WuZixing/kodmin.git"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+ end
@@ -0,0 +1,16 @@
1
+ require_relative 'kodmin/version'
2
+ require_relative 'kodmin/consumers'
3
+ require_relative 'kodmin/plugins'
4
+
5
+ # Client of kong's admin API.
6
+ module Kodmin
7
+ class << self
8
+ def consumers
9
+ Kodmin::Consumers.instance
10
+ end
11
+
12
+ def plugins
13
+ Kodmin::Plugins.instance
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,95 @@
1
+ require 'json'
2
+ require 'net/http'
3
+
4
+ # Client of kong's admin API.
5
+ module Kodmin
6
+ # Error.
7
+ class Error < StandardError
8
+ def initialize(code, error)
9
+ super("#{code}: #{error}")
10
+ end
11
+ end
12
+
13
+ # HTTP client of API.
14
+ module Client
15
+ class << self
16
+ attr_accessor :kong_url
17
+ end
18
+
19
+ def get(path, query_params = {})
20
+ uri = uri_of(path, query_params)
21
+ resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
22
+ req = Net::HTTP::Get.new(uri.request_uri)
23
+ set_request_headers(req)
24
+ http.request(req)
25
+ end
26
+ check_and_return(resp)
27
+ end
28
+
29
+ def post(path, data = {})
30
+ uri = uri_of(path)
31
+ resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
32
+ req = Net::HTTP::Post.new(uri.request_uri)
33
+ set_request_headers(req)
34
+ req['Content-Type'] = 'application/json'
35
+ req.body = data.to_json
36
+ http.request(req)
37
+ end
38
+ check_and_return(resp)
39
+ end
40
+
41
+ def put(path, data = {})
42
+ uri = uri_of(path)
43
+ resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
44
+ req = Net::HTTP::Put.new(uri.request_uri)
45
+ set_request_headers(req)
46
+ req['Content-Type'] = 'application/json'
47
+ req.body = data.to_json
48
+ http.request(req)
49
+ end
50
+ check_and_return(resp)
51
+ end
52
+
53
+ def delete(path, query_params = {})
54
+ uri = uri_of(path, query_params)
55
+ resp = Net::HTTP.start(uri.host, uri.port, nil, nil, nil, nil, use_ssl: (uri.scheme == 'https')) do |http|
56
+ req = Net::HTTP::Delete.new(uri.request_uri)
57
+ set_request_headers(req)
58
+ http.request(req)
59
+ end
60
+ check_and_return(resp)
61
+ end
62
+
63
+ private
64
+
65
+ def uri_of(path, query_params = {})
66
+ url = Client.kong_url + path
67
+ if query_params.empty?
68
+ URI(url)
69
+ else
70
+ queries = URI.encode_www_form(query_params)
71
+ url.include?('?') ? URI(url + '&' + queries) : URI(url + '?' + queries)
72
+ end
73
+ end
74
+
75
+ def set_request_headers(request)
76
+ request[:Accept] = 'application/json'
77
+ end
78
+
79
+ def check_and_return(response)
80
+ if response.is_a?(Net::HTTPSuccess)
81
+ response.body
82
+ else
83
+ raise Error.new(response.code, response.body)
84
+ end
85
+ end
86
+ end
87
+
88
+ class << self
89
+ # Initialize the Kong admin client.
90
+ # @param kong_url [String] URL of the Kong Admin API, e.x. 'http://127.0.0.1:8001'.
91
+ def init(kong_url)
92
+ Client.kong_url = kong_url.delete_suffix('/')
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,133 @@
1
+ require 'singleton'
2
+ require 'securerandom'
3
+ require_relative 'client'
4
+
5
+ module Kodmin
6
+ # Consumer of Kong API.
7
+ class Consumer
8
+ attr_accessor :id, :username, :custom_id, :created_at, :tags
9
+
10
+ # New a consumer.
11
+ # @param attributes [Hash] attributes of a consumer.
12
+ def initialize(attributes = {})
13
+ from_hash(attributes)
14
+ end
15
+
16
+ # Assign attributes from a Hash.
17
+ # @param attributes [Hash] attributes.
18
+ def from_hash(attributes)
19
+ attributes.each_key do |key|
20
+ setter = "#{key}=".to_sym
21
+ send(setter, attributes[key]) if respond_to?(setter)
22
+ end
23
+ end
24
+
25
+ def to_hash
26
+ hash = {}
27
+ hash[:id] = id unless id.nil?
28
+ hash[:username] = username unless username.nil?
29
+ hash[:custom_id] = custom_id unless custom_id.nil?
30
+ hash[:created_at] = created_at unless created_at.nil?
31
+ hash[:tags] = tags unless tags.nil?
32
+ hash
33
+ end
34
+
35
+ # Consumer's HMAC credential.
36
+ class HmacAuth
37
+ attr_accessor :id, :consumer, :username, :secret, :created_at
38
+
39
+ # New a HmacAuth.
40
+ # @param attributes [Hash] attributes of the HmacAuth.
41
+ def initialize(attributes = {})
42
+ from_hash(attributes)
43
+ end
44
+
45
+ # Assign attributes from a Hash object.
46
+ # @param attributes [Hash] hash representation of attributes.
47
+ def from_hash(attributes)
48
+ attributes.each_key do |key|
49
+ if key.to_sym == :consumer
50
+ self.consumer = Consumer.new(attributes[key])
51
+ else
52
+ setter = "#{key}=".to_sym
53
+ send(setter, attributes[key]) if respond_to?(setter)
54
+ end
55
+ end
56
+ end
57
+
58
+ def to_hash
59
+ hash = {}
60
+ hash[:id] = id unless id.nil?
61
+ hash[:consumer] = consumer.to_hash unless consumer.nil?
62
+ hash[:username] = username unless username.nil?
63
+ hash[:secret] = secret unless secret.nil?
64
+ hash[:created_at] = created_at unless created_at.nil?
65
+ hash
66
+ end
67
+ end
68
+ end
69
+
70
+ # Operations on consumer.
71
+ class Consumers
72
+ include Singleton
73
+ include Client
74
+
75
+ # List a page of consumers.
76
+ # @param offset [String] the offset token for pagination. An empty offset token refers to the first page.
77
+ # @return [Hash] a Hash with keys of :consumers and :offset.
78
+ def list_consumers(offset = '')
79
+ params = offset.empty? ? {} : { offset: offset }
80
+ resp = get('/consumers', params)
81
+ resp = JSON.parse(resp)
82
+ consumers = []
83
+ resp['data'].each do |v|
84
+ consumers << Consumer.new(v)
85
+ end
86
+ offset_token = nil
87
+ unless resp['next'].nil? || resp['next'].empty?
88
+ offset_token = resp['next'].split('=')[1]
89
+ end
90
+ { consumers: consumers, offset: offset_token }
91
+ end
92
+
93
+ # Create a new consumer.
94
+ # @param attributes [Hash] attributes of the consumer.
95
+ # @return [Consumer] the created consumer.
96
+ def create_consumer(attributes)
97
+ resp = post('/consumers', attributes)
98
+ Consumer.new(JSON.parse(resp))
99
+ end
100
+
101
+ # Delete a consumer.
102
+ # @param id [String] id of the consumer to be removed.
103
+ def delete_consumer(id)
104
+ delete("/consumers/#{id}")
105
+ end
106
+
107
+ # Get a consumers's HMAC credentials.
108
+ # @param id [String] id of the consumer.
109
+ # @param offset [String] the offset token for pagination. An empty offset token refers to the first page.
110
+ # @return [Hash] a Hash with keys of :credentials and :offset.
111
+ def list_consumer_hmac_credentials(id, offset = '')
112
+ params = offset.empty? ? {} : { offset: offset }
113
+ resp = get("/consumers/#{id}/hmac-auth", params)
114
+ resp = JSON.parse(resp)
115
+ credentials = []
116
+ resp['data'].each do |v|
117
+ credentials << Consumer::HmacAuth.new(v)
118
+ end
119
+ offset_token = nil
120
+ unless resp['next'].nil? || resp['next'].empty?
121
+ offset_token = resp['next'].split('=')[1]
122
+ end
123
+ { credentials: credentials, offset: offset_token }
124
+ end
125
+
126
+ def create_consumer_hmac_credential(id)
127
+ access_key = SecureRandom.hex(16)
128
+ secret_key = SecureRandom.hex(16)
129
+ resp = post("/consumers/#{id}/hmac-auth", {username: access_key, secret: secret_key})
130
+ Consumer::HmacAuth.new(JSON.parse(resp))
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,43 @@
1
+ require 'singleton'
2
+ require_relative 'client'
3
+ require_relative 'plugins/hmac_auth'
4
+
5
+ module Kodmin
6
+ # Plugins of Kong.
7
+ class Plugins
8
+ include Singleton
9
+ include Client
10
+
11
+ def list_plugins(offset = '')
12
+ params = offset.empty? ? {} : { offset: offset }
13
+ resp = get('/plugins', params)
14
+ resp = JSON.parse(resp)
15
+ plugins = []
16
+ resp['data'].each do |v|
17
+ plugins << plugin_from_hash(v)
18
+ end
19
+ offset_token = nil
20
+ unless resp['next'].nil? || resp['next'].empty?
21
+ offset_token = resp['next'].split('=')[1]
22
+ end
23
+ { plugins: plugins, offset: offset_token }
24
+ end
25
+
26
+ def enable_plugin(attributes)
27
+ resp = post('/plugins', attributes)
28
+ resp = JSON.parse(resp)
29
+ plugin_from_hash(resp)
30
+ end
31
+
32
+ def disable_plugin(id)
33
+ delete("/plugins/#{id}")
34
+ end
35
+
36
+ def plugin_from_hash(attributes)
37
+ case attributes['name']
38
+ when Kodmin::Plugins::HmacAuth::NAME then Kodmin::Plugins::HmacAuth.new(attributes)
39
+ else Kodmin::Plugins::Plugin.new(attributes)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,61 @@
1
+ require_relative 'plugin'
2
+
3
+ module Kodmin
4
+ class Plugins
5
+ # Plugin: hmac-auth
6
+ class HmacAuth < Plugin
7
+ NAME = 'hmac-auth'.freeze
8
+
9
+ # HMAC auth's nested configuration.
10
+ class Config
11
+ attr_accessor :hide_credentials, :clock_skew, :anonymous, :validate_request_body, :enforce_headers, :algorithms
12
+ def initialize(attrs = {})
13
+ attrs.each_key do |key|
14
+ setter = "#{key}="
15
+ send(setter, attrs[key]) if respond_to?(setter)
16
+ end
17
+ end
18
+
19
+ def to_hash
20
+ hash = {}
21
+ hash[:hide_credentials] = hide_credentials unless hide_credentials.nil?
22
+ hash[:clock_skew] = clock_skew unless clock_skew.nil?
23
+ hash[:anonymous] = anonymous unless anonymous.nil?
24
+ hash[:validate_request_body] = validate_request_body unless validate_request_body.nil?
25
+ hash[:enforce_headers] = enforce_headers unless enforce_headers.nil?
26
+ hash[:algorithms] = algorithms unless algorithms.nil?
27
+ hash
28
+ end
29
+ end
30
+
31
+ attr_accessor :config
32
+
33
+ def initialize(attrs = {})
34
+ form_hash(attrs)
35
+ end
36
+
37
+ def name
38
+ HmacAuth::NAME
39
+ end
40
+
41
+ def to_hash
42
+ hash = super
43
+ hash[:config] = config.to_hash unless config.nil?
44
+ hash
45
+ end
46
+
47
+ # Assign attributes from Hash.
48
+ # @param attrs [Hash] attributes in Hash object.
49
+ def form_hash(attrs = {})
50
+ attrs.each_key do |key|
51
+ if key.to_sym == :config
52
+ self.config = HmacAuth::Config.new(attrs[key])
53
+ else
54
+ setter = "#{key}=".to_sym
55
+ send(setter, attrs[key]) if respond_to?(setter)
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,31 @@
1
+ module Kodmin
2
+ class Plugins
3
+ # Basic definition of plugin.
4
+ class Plugin
5
+ attr_reader :name
6
+ attr_accessor :id, :created_at, :protocols, :enabled, :tags
7
+
8
+ def initialize(attributes = {})
9
+ from_hash(attributes)
10
+ end
11
+
12
+ def from_hash(attributes)
13
+ attributes.each_key do |key|
14
+ setter = "#{key}=".to_sym
15
+ send(setter, attributes[key]) if respond_to?(setter)
16
+ end
17
+ end
18
+
19
+ def to_hash
20
+ hash = {}
21
+ hash[:name] = name unless name.nil?
22
+ hash[:id] = id unless nil.nil?
23
+ hash[:created_at] = created_at unless created_at.nil?
24
+ hash[:protocols] = protocols unless protocols.nil?
25
+ hash[:enabled] = enabled unless enabled.nil?
26
+ hash[:tags] = tags unless tags.nil?
27
+ hash
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,3 @@
1
+ module Kodmin
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kodmin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Wu Zixing
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-13 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - wzixing@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - kodmin.gemspec
30
+ - lib/kodmin.rb
31
+ - lib/kodmin/client.rb
32
+ - lib/kodmin/consumers.rb
33
+ - lib/kodmin/plugins.rb
34
+ - lib/kodmin/plugins/hmac_auth.rb
35
+ - lib/kodmin/plugins/plugin.rb
36
+ - lib/kodmin/version.rb
37
+ homepage: https://github.com/WuZixing/kodmin
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/WuZixing/kodmin
42
+ source_code_uri: https://github.com/WuZixing/kodmin.git
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.7.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.1.2
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: A ruby client of kong's admin API v2.x.
62
+ test_files: []