krakend-openapi-importer 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d1a7b6aa237ddcad0132e8c293cd42181382f5f4e32cbf174b18c9760abb5120
4
+ data.tar.gz: 243242837fbc0ebca42f8e311eeb8da9a7a1aed32fc4b1f2d7ceddf9f70db7c7
5
+ SHA512:
6
+ metadata.gz: 9b6f24df2079fd9131a46a1cb2e19af2e17c4eba7a5b47db4b1fbccc57ed9c3d6b707663918117bf5f0d3b09bde7e8e122476eab8b8cfa2be6e8142af162ef1f
7
+ data.tar.gz: 67ec817c14e10bb62d087b7038750100fad0f3f219e32028d5531b054c2b74263df2ecdde38fb75fbdf6d35e196fa69bbb510c1f7a007b04eedc787e6471b9b4
@@ -0,0 +1,16 @@
1
+ name: Ruby
2
+
3
+ on: [push,pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+ steps:
9
+ - uses: actions/checkout@v2
10
+ - name: Set up Ruby
11
+ uses: ruby/setup-ruby@v1
12
+ with:
13
+ ruby-version: 2.7.6
14
+ bundler-cache: true
15
+ - name: Run the default task
16
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ .idea/
2
+ /.bundle/
3
+ /.yardoc
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /data/
11
+ output.json
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1,2 @@
1
+ 2.7.6
2
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in krakend-openapi-importer.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rspec', '~> 3.0'
10
+ gem 'rubocop', require: false
data/Gemfile.lock ADDED
@@ -0,0 +1,61 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ krakend-openapi-importer (0.2.0)
5
+ thor (~> 1.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ ast (2.4.2)
11
+ diff-lcs (1.4.4)
12
+ json (2.6.2)
13
+ parallel (1.22.1)
14
+ parser (3.1.2.1)
15
+ ast (~> 2.4.1)
16
+ rainbow (3.1.1)
17
+ rake (13.0.6)
18
+ regexp_parser (2.6.1)
19
+ rexml (3.2.5)
20
+ rspec (3.10.0)
21
+ rspec-core (~> 3.10.0)
22
+ rspec-expectations (~> 3.10.0)
23
+ rspec-mocks (~> 3.10.0)
24
+ rspec-core (3.10.1)
25
+ rspec-support (~> 3.10.0)
26
+ rspec-expectations (3.10.1)
27
+ diff-lcs (>= 1.2.0, < 2.0)
28
+ rspec-support (~> 3.10.0)
29
+ rspec-mocks (3.10.2)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.10.0)
32
+ rspec-support (3.10.2)
33
+ rubocop (1.39.0)
34
+ json (~> 2.3)
35
+ parallel (~> 1.10)
36
+ parser (>= 3.1.2.1)
37
+ rainbow (>= 2.2.2, < 4.0)
38
+ regexp_parser (>= 1.8, < 3.0)
39
+ rexml (>= 3.2.5, < 4.0)
40
+ rubocop-ast (>= 1.23.0, < 2.0)
41
+ ruby-progressbar (~> 1.7)
42
+ unicode-display_width (>= 1.4.0, < 3.0)
43
+ rubocop-ast (1.23.0)
44
+ parser (>= 3.1.1.0)
45
+ ruby-progressbar (1.11.0)
46
+ thor (1.2.1)
47
+ unicode-display_width (2.3.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+ x86_64-darwin-20
52
+ x86_64-darwin-21
53
+
54
+ DEPENDENCIES
55
+ krakend-openapi-importer!
56
+ rake (~> 13.0)
57
+ rspec (~> 3.0)
58
+ rubocop
59
+
60
+ BUNDLED WITH
61
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Denis Semenenko
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.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # KrakenD OpenAPI Importer
2
+
3
+ Import endpoints from OpenAPI spec to KrakenD endpoint configuration. Supports OpenAPI v3.0 and up.
4
+
5
+ [![Ruby](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml/badge.svg)](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml)
6
+
7
+ In case you have a different version of OpenAPI you can use https://github.com/LucyBot-Inc/api-spec-converter to convert to the v3.0.
8
+
9
+ ## Installation
10
+
11
+ Execute
12
+
13
+ gem install krakend-openapi-importer
14
+
15
+ ## Usage
16
+
17
+ Import OpenAPI spec from SPEC file. Writes KrakenD config to output.json
18
+
19
+ ```bash
20
+ krakend-openapi-importer import SPEC -c CONFIG
21
+ ```
22
+
23
+ ```bash
24
+ Options:
25
+ -c, --config=CONFIG # Path to importer.yaml config
26
+ -s, [--syntax=SYNTAX] # Specifies input data syntax: json or yaml. Defaults to json
27
+ # Default: json
28
+ ```
29
+
30
+ ## Configuration
31
+
32
+ Example config
33
+
34
+ ```yaml
35
+ ---
36
+ all_roles: ['admin', 'guest'] # all available roles for JWT validator
37
+ pretty: false
38
+ defaults:
39
+ endpoint:
40
+ output_encoding: "no-op" # act like a no-op proxy
41
+ input_headers: [ "*" ]
42
+ input_query_strings: [ "*" ]
43
+ plugins:
44
+ auth_validator:
45
+ alg: 'RS256'
46
+ jwk_url: 'https://KEYCLOAK_URL/auth/realms/master/protocol/openid-connect/certs'
47
+ cache: false
48
+ operation_debug: true
49
+ roles_key: 'realm_access.roles'
50
+ roles_key_is_nested: true
51
+ ```
52
+
53
+ ## Development
54
+
55
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
56
+
57
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at https://github.com/denblackstache/krakend-openapi-importer.
62
+
63
+ ## License
64
+
65
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'importer'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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,19 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ lib_path = File.expand_path('../lib', __dir__)
5
+ $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
6
+ require 'importer'
7
+
8
+ Signal.trap('INT') do
9
+ warn("\n#{caller.join("\n")}: interrupted")
10
+ exit(1)
11
+ end
12
+
13
+ begin
14
+ KrakendOpenAPI::Importer.start(ARGV)
15
+ rescue StandardError => e
16
+ raise e # TODO: Remove this line after debugging
17
+ puts "ERROR: #{e.message}"
18
+ exit 1
19
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './lib/importer/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'krakend-openapi-importer'
7
+ spec.version = KrakendOpenAPI::VERSION
8
+ spec.authors = ['Denis Semenenko']
9
+ spec.email = ['hypercoderx@gmail.com']
10
+
11
+ spec.summary = 'Import OpenAPI spec to KrakenD configuration'
12
+ spec.description = 'Import endpoints from OpenAPI spec to KrakenD endpoint configuration. Supports OpenAPI v3.0'
13
+ spec.homepage = 'https://hypercoder.net'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = '>= 2.7'
16
+
17
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/denblackstache/krakend-openapi-importer'
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
26
+ end
27
+ spec.bindir = 'exe'
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ['lib']
30
+
31
+ spec.add_dependency 'thor', '~> 1.2'
32
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+ require_relative '../readers/oa3_reader'
5
+ require_relative '../transformers/oa3_transformer'
6
+ require_relative '../writers/krakend_writer'
7
+
8
+ module KrakendOpenAPI
9
+ # Import Command
10
+ class ImportCommand
11
+ def initialize(spec:, syntax:, config:)
12
+ @spec = spec
13
+ @syntax = syntax
14
+ @config = config
15
+
16
+ @importer_config = YAML.safe_load(File.read("#{Dir.pwd}/#{@config}"))
17
+ end
18
+
19
+ def execute
20
+ paths = OA3Reader.new.read(@spec).paths
21
+ endpoints = OA3ToKrakendTransformer.new(@importer_config).transform_paths(paths)
22
+ KrakendWriter.new(@importer_config).write(endpoints)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrakendOpenAPI
4
+ VERSION = '0.2.0'
5
+ end
data/lib/importer.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'thor'
4
+ require_relative 'commands/import'
5
+
6
+ module KrakendOpenAPI
7
+ # Importer CLI
8
+ class Importer < Thor
9
+ desc 'import SPEC', 'Import OpenAPI spec from SPEC file. Writes KrakenD config to output.json'
10
+ method_option :config, aliases: '-c', desc: 'Path to importer.yaml config', required: true
11
+ method_option :syntax, aliases: '-s', default: 'json',
12
+ desc: 'Specifies input data syntax: json or yaml. Defaults to json'
13
+ def import(spec)
14
+ ImportCommand.new(spec: spec, syntax: options[:syntax], config: options[:config]).execute
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrakendOpenAPI
4
+ # Reads JSON files
5
+ class JsonReader
6
+ def initialize(file_path)
7
+ @file_path = file_path
8
+ end
9
+
10
+ def read
11
+ JSON.parse(File.read("#{Dir.pwd}/#{@file_path}"))
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../readers/json_reader'
4
+ require_relative '../readers/yaml_reader'
5
+
6
+ module KrakendOpenAPI
7
+ # Reads OpenAPI spec files
8
+ class OA3Reader
9
+ attr_reader :data
10
+
11
+ def read(path)
12
+ if ['.json'].include?(File.extname(path))
13
+ @data = JsonReader.new(path).read
14
+ elsif %w[.yaml .yml].include?(File.extname(path))
15
+ @data = YamlReader.new(path).read
16
+ else
17
+ raise StandardError, 'OA3Reader does not support this format'
18
+ end
19
+
20
+ self
21
+ end
22
+
23
+ def paths
24
+ data['paths']
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'yaml'
4
+
5
+ module KrakendOpenAPI
6
+ # Reads YAML files
7
+ class YamlReader
8
+ def initialize(file_path)
9
+ @file_path = file_path
10
+ end
11
+
12
+ def read
13
+ YAML.safe_load(File.read("#{Dir.pwd}/#{@file_path}"))
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrakendOpenAPI
4
+ # Transforms OpenAPI paths to KrakenD endpoints
5
+ class JwtValidatorTransformer
6
+ def transform_to_hash(roles:, config:)
7
+ {
8
+ name: 'auth/validator',
9
+ value: {
10
+ 'alg': config['alg'],
11
+ 'jwk_url': config['jwk_url'],
12
+ 'cache': config['cache'],
13
+ 'operation_debug': config['operation_debug'],
14
+ 'roles_key_is_nested': config['roles_key_is_nested'],
15
+ 'roles_key': config['roles_key'],
16
+ 'roles': roles
17
+ }
18
+ }
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './jwt_validator_transformer'
4
+
5
+ module KrakendOpenAPI
6
+ # Transforms OpenAPI paths to KrakenD endpoints
7
+ class OA3ToKrakendTransformer
8
+ def initialize(importer_config)
9
+ @importer_config = importer_config
10
+ end
11
+
12
+ def transform_paths(paths)
13
+ paths.map { |path, methods| transform_path(path, methods) }.flatten
14
+ end
15
+
16
+ def transform_path(path, methods)
17
+ methods.map { |method, operation| transform_method(path, method, operation) }
18
+ end
19
+
20
+ def transform_method(path, method, operation)
21
+ roles = operation['x-jwt-roles']&.length ? operation['x-jwt-roles'] : @importer_config['all_roles']
22
+
23
+ plugins = []
24
+ plugins << JwtValidatorTransformer
25
+ .new
26
+ .transform_to_hash(roles: roles,
27
+ config: @importer_config['defaults']['plugins']['auth_validator'])
28
+
29
+ endpoint = {
30
+ endpoint: path,
31
+ method: method.upcase,
32
+ output_encoding: @importer_config['defaults']['endpoint']['output_encoding'],
33
+ input_headers: @importer_config['defaults']['endpoint']['input_headers'],
34
+ input_query_strings: @importer_config['defaults']['endpoint']['input_query_strings'],
35
+ backend: [{ url_pattern: path }]
36
+ }
37
+
38
+ extra_config = plugins.each_with_object({}) do |plugin, memo|
39
+ memo[plugin[:name].to_sym] = plugin[:value]
40
+ end
41
+
42
+ endpoint[:extra_config] = extra_config
43
+ endpoint
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module KrakendOpenAPI
6
+ # Writes KrakenD configuration to a file
7
+ class KrakendWriter
8
+ def initialize(importer_config)
9
+ @importer_config = importer_config
10
+ @file_path = "#{Dir.pwd}/output.json"
11
+ end
12
+
13
+ def write(endpoints)
14
+ pretty_output = !!@importer_config['pretty'] # rubocop:disable Style/DoubleNegation
15
+ json_generate = pretty_output ? ->(obj) { JSON.pretty_generate(obj) } : ->(obj) { JSON.dump(obj) }
16
+ File.write(@file_path, json_generate.call({
17
+ '$schema': 'https://www.krakend.io/schema/v3.json',
18
+ version: 3,
19
+ endpoints: endpoints
20
+ }))
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: krakend-openapi-importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Denis Semenenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.2'
27
+ description: Import endpoints from OpenAPI spec to KrakenD endpoint configuration.
28
+ Supports OpenAPI v3.0
29
+ email:
30
+ - hypercoderx@gmail.com
31
+ executables:
32
+ - krakend-openapi-importer
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - ".github/workflows/main.yml"
37
+ - ".gitignore"
38
+ - ".rspec"
39
+ - ".ruby-version"
40
+ - Gemfile
41
+ - Gemfile.lock
42
+ - LICENSE.txt
43
+ - README.md
44
+ - Rakefile
45
+ - bin/console
46
+ - bin/setup
47
+ - exe/krakend-openapi-importer
48
+ - krakend-openapi-importer.gemspec
49
+ - lib/commands/import.rb
50
+ - lib/importer.rb
51
+ - lib/importer/version.rb
52
+ - lib/readers/json_reader.rb
53
+ - lib/readers/oa3_reader.rb
54
+ - lib/readers/yaml_reader.rb
55
+ - lib/transformers/jwt_validator_transformer.rb
56
+ - lib/transformers/oa3_transformer.rb
57
+ - lib/writers/krakend_writer.rb
58
+ homepage: https://hypercoder.net
59
+ licenses:
60
+ - MIT
61
+ metadata:
62
+ allowed_push_host: https://rubygems.org
63
+ homepage_uri: https://hypercoder.net
64
+ source_code_uri: https://github.com/denblackstache/krakend-openapi-importer
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '2.7'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.1.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Import OpenAPI spec to KrakenD configuration
84
+ test_files: []