krakend-openapi-importer 0.3.1 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d3a9eaa2da627c77ada30ddbb82c624063130d80c59087f3a57b9a8eb366afb8
4
- data.tar.gz: a6e0280bee0c8a7a14cf669ac29bd341b29125e34fda209170c9eb8497556110
3
+ metadata.gz: 7fa4ce1569286791f7a82d516f39378a66443d7e62edb4fcb58f91e44a34e71c
4
+ data.tar.gz: 2481a698b885aa0f7e958e86abaa608b433b25b9ad927c5e02aaeaa4daaebdda
5
5
  SHA512:
6
- metadata.gz: 2d41c42b8df2419cf1c50dc3688d1461abceedb1fc25506a5c13ab32751a9323eff94d8b62fd15c4a8782903d3e299c3ec180976f3fbe2035df95f1b8589325d
7
- data.tar.gz: cd0bcff75ae8effeca81e6fadb7afea44de51c2460a9c09e8c581370f67a5b05f5b216b5ae4f999571c9d001eb3869a749dabb7879d828b14f9a23c7ac937508
6
+ metadata.gz: 92c1c325b4bc2a16302fb75df279fd8cb62f305d3379c27e1d91f52fdbf109a8604b20343640a9fcaf1412585e1065c82433112d7e746b74e9672d23e6262416
7
+ data.tar.gz: a3a73662c1d3e274e82d29a357e82004fe5ed6408afd51feac8795efac9ba7929b265e5fa4c9c45741818407d63e7d44cb37f7ba97c680ab0623ee3df68a2037
@@ -1,16 +1,31 @@
1
- name: Ruby
1
+ name: testing
2
2
 
3
3
  on: [push,pull_request]
4
4
 
5
5
  jobs:
6
- build:
6
+ test:
7
7
  runs-on: ubuntu-latest
8
+ strategy:
9
+ fail-fast: false
10
+ matrix:
11
+ ruby-version: [ '2.7', '3.0', '3.1', '3.2', '3.3', '3.4' ]
8
12
  steps:
9
- - uses: actions/checkout@v2
13
+ - uses: actions/checkout@v4
10
14
  - name: Set up Ruby
11
- uses: ruby/setup-ruby@v1
15
+ uses: ruby/setup-ruby@v1.216.0
12
16
  with:
13
- ruby-version: 2.7.6
17
+ ruby-version: ${{ matrix.ruby-version }}
14
18
  bundler-cache: true
15
- - name: Run the default task
19
+ - name: Run tests
16
20
  run: bundle exec rake
21
+ lint:
22
+ runs-on: ubuntu-latest
23
+ steps:
24
+ - uses: actions/checkout@v4
25
+ - name: Set up Ruby
26
+ uses: ruby/setup-ruby@v1.216.0
27
+ with:
28
+ ruby-version: '3.0'
29
+ bundler-cache: true
30
+ - name: Run linter
31
+ run: bundle exec rubocop
data/.rubocop.yml ADDED
@@ -0,0 +1,23 @@
1
+ require:
2
+ - rubocop-performance
3
+
4
+ AllCops:
5
+ NewCops: enable
6
+
7
+ Layout/LineLength:
8
+ Max: 120
9
+
10
+ Metrics/AbcSize:
11
+ Max: 34
12
+
13
+ Metrics/CyclomaticComplexity:
14
+ Max: 14
15
+
16
+ Metrics/PerceivedComplexity:
17
+ Max: 16
18
+
19
+ Metrics/MethodLength:
20
+ Enabled: false
21
+
22
+ Metrics/BlockLength:
23
+ Enabled: false
data/.ruby-version CHANGED
@@ -1,2 +1 @@
1
1
  2.7.6
2
-
data/Gemfile CHANGED
@@ -5,13 +5,19 @@ source 'https://rubygems.org'
5
5
  # Specify your gem's dependencies in krakend-openapi-importer.gemspec
6
6
  gemspec
7
7
 
8
- gem 'rake', '~> 13.0'
8
+ gem 'rake', '~> 13.0', '>= 13.2.1'
9
9
 
10
10
  group :test do
11
- gem 'fakefs', '~> 2.0'
12
- gem 'minitest', '~> 5.13'
11
+ gem 'fakefs', '~> 2.8', '>= 2.8.0'
12
+ gem 'minitest', '~> 5.25', '>= 5.25.4'
13
13
  end
14
14
 
15
15
  group :development do
16
- gem 'rubocop', require: false
16
+ gem 'bump', '~> 0.10.0'
17
+ gem 'rubocop', '~> 1.71', require: false
18
+ gem 'rubocop-performance', require: false
19
+ end
20
+
21
+ group :test, :development do
22
+ gem 'awesome_print'
17
23
  end
data/Gemfile.lock CHANGED
@@ -1,38 +1,47 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- krakend-openapi-importer (0.3.1)
4
+ krakend-openapi-importer (1.0.0)
5
5
  thor (~> 1.2)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
10
  ast (2.4.2)
11
- fakefs (2.0.0)
12
- json (2.6.2)
13
- minitest (5.13.0)
14
- parallel (1.22.1)
15
- parser (3.1.2.1)
11
+ awesome_print (1.9.2)
12
+ bump (0.10.0)
13
+ fakefs (2.8.0)
14
+ json (2.9.1)
15
+ language_server-protocol (3.17.0.4)
16
+ minitest (5.25.4)
17
+ parallel (1.26.3)
18
+ parser (3.3.7.1)
16
19
  ast (~> 2.4.1)
20
+ racc
21
+ racc (1.8.1)
17
22
  rainbow (3.1.1)
18
- rake (13.0.6)
19
- regexp_parser (2.6.1)
20
- rexml (3.2.5)
21
- rubocop (1.39.0)
23
+ rake (13.2.1)
24
+ regexp_parser (2.10.0)
25
+ rubocop (1.71.2)
22
26
  json (~> 2.3)
27
+ language_server-protocol (>= 3.17.0)
23
28
  parallel (~> 1.10)
24
- parser (>= 3.1.2.1)
29
+ parser (>= 3.3.0.2)
25
30
  rainbow (>= 2.2.2, < 4.0)
26
- regexp_parser (>= 1.8, < 3.0)
27
- rexml (>= 3.2.5, < 4.0)
28
- rubocop-ast (>= 1.23.0, < 2.0)
31
+ regexp_parser (>= 2.9.3, < 3.0)
32
+ rubocop-ast (>= 1.38.0, < 2.0)
29
33
  ruby-progressbar (~> 1.7)
30
- unicode-display_width (>= 1.4.0, < 3.0)
31
- rubocop-ast (1.23.0)
32
- parser (>= 3.1.1.0)
33
- ruby-progressbar (1.11.0)
34
- thor (1.2.1)
35
- unicode-display_width (2.3.0)
34
+ unicode-display_width (>= 2.4.0, < 4.0)
35
+ rubocop-ast (1.38.0)
36
+ parser (>= 3.3.1.0)
37
+ rubocop-performance (1.23.1)
38
+ rubocop (>= 1.48.1, < 2.0)
39
+ rubocop-ast (>= 1.31.1, < 2.0)
40
+ ruby-progressbar (1.13.0)
41
+ thor (1.3.2)
42
+ unicode-display_width (3.1.4)
43
+ unicode-emoji (~> 4.0, >= 4.0.4)
44
+ unicode-emoji (4.0.4)
36
45
 
37
46
  PLATFORMS
38
47
  ruby
@@ -40,11 +49,14 @@ PLATFORMS
40
49
  x86_64-darwin-21
41
50
 
42
51
  DEPENDENCIES
43
- fakefs (~> 2.0)
52
+ awesome_print
53
+ bump (~> 0.10.0)
54
+ fakefs (~> 2.8, >= 2.8.0)
44
55
  krakend-openapi-importer!
45
- minitest (~> 5.13)
46
- rake (~> 13.0)
47
- rubocop
56
+ minitest (~> 5.25, >= 5.25.4)
57
+ rake (~> 13.0, >= 13.2.1)
58
+ rubocop (~> 1.71)
59
+ rubocop-performance
48
60
 
49
61
  BUNDLED WITH
50
- 2.1.4
62
+ 2.4.22
data/README.md CHANGED
@@ -4,7 +4,7 @@ Import endpoints from OpenAPI spec to KrakenD endpoint configuration. Supports O
4
4
 
5
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) [![Gem Version](https://badge.fury.io/rb/krakend-openapi-importer.svg)](https://badge.fury.io/rb/krakend-openapi-importer)
6
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.
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
8
 
9
9
  ## Installation
10
10
 
@@ -17,14 +17,12 @@ Execute
17
17
  Import OpenAPI spec from SPEC file. Writes KrakenD config to output.json
18
18
 
19
19
  ```bash
20
- krakend-openapi-importer import SPEC -c CONFIG
20
+ krakend-openapi-importer import SPEC -c CONFIG
21
21
  ```
22
22
 
23
23
  ```bash
24
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
25
+ -c, [--config=CONFIG] # Path to importer.yaml config
28
26
  ```
29
27
 
30
28
  ## Configuration
@@ -33,24 +31,28 @@ Example config
33
31
 
34
32
  ```yaml
35
33
  ---
36
- all_roles: ['admin', 'guest'] # all available roles for JWT validator
34
+ all_roles: ["admin", "guest"] # all available roles for JWT validator
37
35
  pretty: true
38
- output: 'output.json'
36
+ format: "json" # can be 'json' or 'yaml', defaults to `json`
37
+ output: "output.json"
39
38
  defaults:
39
+ base:
40
+ name: Example application
40
41
  endpoint:
41
42
  output_encoding: "no-op" # act like a no-op proxy
42
- input_headers: [ "*" ]
43
- input_query_strings: [ "*" ]
43
+ input_headers: ["*"]
44
+ input_query_strings: ["*"]
44
45
  backend:
45
46
  - encoding: "no-op"
46
47
  plugins:
47
48
  auth_validator:
48
- alg: 'RS256'
49
- jwk_url: 'https://KEYCLOAK_URL/auth/realms/master/protocol/openid-connect/certs'
49
+ alg: "RS256"
50
+ jwk_url: "https://KEYCLOAK_URL/auth/realms/master/protocol/openid-connect/certs"
50
51
  cache: false
51
52
  operation_debug: true
52
- roles_key: 'realm_access.roles'
53
+ roles_key: "realm_access.roles"
53
54
  roles_key_is_nested: true
55
+ scopes_key: scopes # only needed when defining scopes in openapi spec
54
56
  ```
55
57
 
56
58
  ## Development
@@ -61,7 +63,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
61
63
 
62
64
  ## Contributing
63
65
 
64
- Bug reports and pull requests are welcome on GitHub at https://github.com/denblackstache/krakend-openapi-importer.
66
+ Bug reports and pull requests are welcome on GitHub at <https://github.com/denblackstache/krakend-openapi-importer>.
65
67
 
66
68
  ## License
67
69
 
data/Rakefile CHANGED
@@ -2,6 +2,11 @@
2
2
 
3
3
  require 'bundler/gem_tasks'
4
4
  require 'rake/testtask'
5
+ require 'rubocop/rake_task'
6
+
7
+ RuboCop::RakeTask.new do |task|
8
+ task.requires << 'rubocop-rake'
9
+ end
5
10
 
6
11
  Rake::TestTask.new(:test) do |t|
7
12
  t.libs << 'test'
@@ -13,7 +13,6 @@ end
13
13
  begin
14
14
  KrakendOpenAPI::Importer.start(ARGV)
15
15
  rescue StandardError => e
16
- raise e # TODO: Remove this line after debugging
17
16
  puts "ERROR: #{e.message}"
18
17
  exit 1
19
18
  end
@@ -1,20 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './lib/importer/version'
3
+ require_relative 'lib/importer/version'
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = 'krakend-openapi-importer'
7
7
  spec.version = KrakendOpenAPI::VERSION
8
8
  spec.authors = ['Denis Semenenko']
9
- spec.email = ['hypercoderx@gmail.com']
9
+ spec.email = ['den@densemenenko.com']
10
10
 
11
11
  spec.summary = 'Import OpenAPI spec to KrakenD configuration'
12
12
  spec.description = 'Import endpoints from OpenAPI spec to KrakenD endpoint configuration. Supports OpenAPI v3.0'
13
- spec.homepage = 'https://hypercoder.net'
13
+ spec.homepage = 'https://github.com/denblackstache/krakend-openapi-importer'
14
14
  spec.license = 'MIT'
15
15
  spec.required_ruby_version = '>= 2.7'
16
16
 
17
17
  spec.metadata['allowed_push_host'] = 'https://rubygems.org'
18
+ spec.metadata['rubygems_mfa_required'] = 'true'
18
19
 
19
20
  spec.metadata['homepage_uri'] = spec.homepage
20
21
  spec.metadata['source_code_uri'] = 'https://github.com/denblackstache/krakend-openapi-importer'
@@ -1,24 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'pathname'
3
4
  require 'yaml'
4
5
  require_relative '../readers/oa3_reader'
6
+ require_relative '../readers/yaml_reader'
5
7
  require_relative '../transformers/oa3_transformer'
6
8
  require_relative '../writers/krakend_writer'
7
9
 
8
10
  module KrakendOpenAPI
9
11
  # Import Command
10
12
  class ImportCommand
11
- def initialize(spec:, syntax:, config:)
13
+ def initialize(spec:, config:)
12
14
  @spec = spec
13
- @syntax = syntax
14
- @config = config
15
-
16
- @importer_config = YAML.safe_load(File.read("#{Dir.pwd}/#{@config}"))
15
+ @importer_config = config ? YamlReader.new(config).read : {}
17
16
  end
18
17
 
19
18
  def execute
20
- paths = OA3Reader.new(@spec).paths
21
- endpoints = OA3ToKrakendTransformer.new(paths, @importer_config).transform_paths
19
+ oa3_doc = OA3Reader.new(@spec)
20
+ endpoints = OA3ToKrakendTransformer.new(oa3_doc, @importer_config).transform_paths
22
21
  KrakendWriter.new(endpoints, @importer_config).write
23
22
  end
24
23
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module KrakendOpenAPI
4
- VERSION = '0.3.1'
4
+ VERSION = '1.0.0'
5
5
  end
data/lib/importer.rb CHANGED
@@ -7,11 +7,9 @@ module KrakendOpenAPI
7
7
  # Importer CLI
8
8
  class Importer < Thor
9
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'
10
+ method_option :config, aliases: '-c', desc: 'Path to importer.yaml config'
13
11
  def import(spec)
14
- ImportCommand.new(spec: spec, syntax: options[:syntax], config: options[:config]).execute
12
+ ImportCommand.new(spec: spec, config: options[:config]).execute
15
13
  end
16
14
  end
17
15
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrakendOpenAPI
4
+ # Reads absolute/relative files
5
+ class FileReader
6
+ def initialize(file_path)
7
+ @file_path = file_path
8
+ end
9
+
10
+ def read
11
+ pwd = File.expand_path(Dir.pwd)
12
+ if Pathname.new(@file_path).absolute?
13
+ File.read(@file_path)
14
+ else
15
+ File.read(File.expand_path(@file_path, pwd))
16
+ end
17
+ end
18
+ end
19
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
+ require_relative 'file_reader'
4
5
 
5
6
  module KrakendOpenAPI
6
7
  # Reads JSON files
@@ -10,7 +11,7 @@ module KrakendOpenAPI
10
11
  end
11
12
 
12
13
  def read
13
- JSON.parse(File.read("#{Dir.pwd}/#{@file_path}"))
14
+ JSON.parse(KrakendOpenAPI::FileReader.new(@file_path).read)
14
15
  end
15
16
  end
16
17
  end
@@ -11,22 +11,27 @@ module KrakendOpenAPI
11
11
  end
12
12
 
13
13
  def paths
14
- read unless @data
15
- @data['paths']
14
+ data['paths']
16
15
  end
17
16
 
18
- private
17
+ def security
18
+ data['security']
19
+ end
19
20
 
20
- def read
21
- if ['.json'].include?(File.extname(@path))
22
- @data = JsonReader.new(@path).read
23
- elsif %w[.yaml .yml].include?(File.extname(@path))
24
- @data = YamlReader.new(@path).read
25
- else
26
- raise StandardError, 'OA3Reader does not support this format'
27
- end
21
+ def security_schemes
22
+ data.dig('components', 'securitySchemes')
23
+ end
24
+
25
+ private
28
26
 
29
- self
27
+ def data
28
+ @data ||= if ['.json'].include?(File.extname(@path))
29
+ JsonReader.new(@path).read
30
+ elsif %w[.yaml .yml].include?(File.extname(@path))
31
+ YamlReader.new(@path).read
32
+ else
33
+ raise StandardError, 'OA3Reader does not support this format'
34
+ end
30
35
  end
31
36
  end
32
37
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'yaml'
4
+ require_relative 'file_reader'
4
5
 
5
6
  module KrakendOpenAPI
6
7
  # Reads YAML files
@@ -10,7 +11,7 @@ module KrakendOpenAPI
10
11
  end
11
12
 
12
13
  def read
13
- YAML.safe_load(File.read("#{Dir.pwd}/#{@file_path}"))
14
+ YAML.safe_load(KrakendOpenAPI::FileReader.new(@file_path).read)
14
15
  end
15
16
  end
16
17
  end
@@ -1,54 +1,88 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative './jwt_validator_transformer'
3
+ require_relative 'plugins/auth_validator_transformer'
4
4
 
5
5
  module KrakendOpenAPI
6
6
  # Transforms OpenAPI paths to KrakenD endpoints
7
7
  class OA3ToKrakendTransformer
8
- def initialize(paths, importer_config)
9
- @paths = paths
8
+ SCHEME_TYPES_WITH_ROLES = %w[openIdConnect oauth2].freeze
9
+
10
+ def initialize(spec, importer_config)
11
+ @spec = spec
10
12
  @importer_config = importer_config
11
13
  end
12
14
 
13
15
  def transform_paths
14
- @paths.map { |path, methods| transform_path(path, methods) }.flatten
16
+ @spec.paths.map { |path, methods| transform_path(path, methods) }.flatten
15
17
  end
16
18
 
17
19
  private
18
20
 
21
+ def oauth_scopes_for(securities)
22
+ return nil if securities.nil?
23
+
24
+ securities.map do |security|
25
+ security.filter_map do |name, scopes|
26
+ next nil unless oauth_security_scheme_names.include?(name)
27
+
28
+ scopes
29
+ end
30
+ end.flatten.uniq
31
+ end
32
+
33
+ def oauth_security_scheme_names
34
+ @spec.security_schemes.filter_map do |name, scheme|
35
+ next nil unless SCHEME_TYPES_WITH_ROLES.include?(scheme['type'])
36
+
37
+ name
38
+ end
39
+ end
40
+
19
41
  def transform_path(path, methods)
20
42
  methods.map { |method, operation| transform_method(path, method, operation) }
21
43
  end
22
44
 
23
45
  def transform_method(path, method, operation)
24
46
  roles = operation['x-jwt-roles']&.length ? operation['x-jwt-roles'] : @importer_config['all_roles']
25
-
47
+ scopes = oauth_scopes_for(operation['security']) || oauth_scopes_for(@spec.security)
26
48
  plugins = []
27
- if @importer_config['defaults']&.dig('plugins', 'auth_validator')
28
- plugins << JwtValidatorTransformer
29
- .new
30
- .transform_to_hash(roles: roles,
31
- config: @importer_config['defaults']['plugins']['auth_validator'])
32
- end
49
+ plugins << auth_validator_plugin(roles, scopes) if auth_validator_plugin_enabled?(roles, scopes)
50
+ endpoint = krakend_endpoint(path, method)
51
+ endpoint[:extra_config] = endpoint_extra_config(plugins) unless plugins.empty?
33
52
 
34
- endpoint = {
53
+ endpoint
54
+ end
55
+
56
+ def krakend_endpoint(path, method)
57
+ {
35
58
  endpoint: path,
36
59
  method: method.upcase,
37
- output_encoding: @importer_config['defaults']['endpoint']['output_encoding'],
38
- input_headers: @importer_config['defaults']['endpoint']['input_headers'],
39
- input_query_strings: @importer_config['defaults']['endpoint']['input_query_strings'],
40
- backend: [{ url_pattern: path, encoding: @importer_config['defaults']['backend'][0]['encoding'] }]
41
- }
42
-
43
- if plugins&.length > 0
44
- extra_config = plugins.each_with_object({}) do |plugin, memo|
45
- memo[plugin[:name].to_sym] = plugin[:value]
46
- end
60
+ output_encoding: @importer_config['defaults']&.dig('endpoint', 'output_encoding'),
61
+ input_headers: @importer_config['defaults']&.dig('endpoint', 'input_headers'),
62
+ input_query_strings: @importer_config['defaults']&.dig('endpoint', 'input_query_strings'),
63
+ backend: [{ url_pattern: path, encoding: @importer_config['defaults']&.dig('backend', 0, 'encoding') }.compact]
64
+ }.compact
65
+ end
47
66
 
48
- endpoint[:extra_config] = extra_config
49
- end
67
+ def auth_validator_plugin_enabled?(roles, scopes)
68
+ @importer_config['defaults']&.dig('plugins', 'auth_validator') &&
69
+ (roles&.any? || scopes&.any?)
70
+ end
50
71
 
51
- endpoint
72
+ def auth_validator_plugin(roles, scopes)
73
+ Plugins::AuthValidatorTransformer
74
+ .new
75
+ .transform_to_hash(
76
+ roles: roles,
77
+ scopes: scopes,
78
+ config: @importer_config['defaults']['plugins']['auth_validator']
79
+ )
80
+ end
81
+
82
+ def endpoint_extra_config(plugins)
83
+ plugins.each_with_object({}) do |plugin, memo|
84
+ memo[plugin[:name].to_sym] = plugin[:value]
85
+ end
52
86
  end
53
87
  end
54
88
  end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module KrakendOpenAPI
4
+ module Plugins
5
+ # Transforms Auth Validator plugin definition
6
+ class AuthValidatorTransformer
7
+ def transform_to_hash(config:, roles: [], scopes: [])
8
+ value = config.dup
9
+
10
+ value['roles'] = roles unless roles.nil? || roles.empty?
11
+ value['scopes'] = scopes unless scopes.nil? || scopes.empty?
12
+
13
+ {
14
+ name: 'auth/validator',
15
+ value: value
16
+ }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
+ require 'yaml'
4
5
 
5
6
  module KrakendOpenAPI
6
7
  # Writes KrakenD configuration to a file
@@ -8,18 +9,54 @@ module KrakendOpenAPI
8
9
  def initialize(endpoints, importer_config)
9
10
  @endpoints = endpoints
10
11
  @importer_config = importer_config
11
- @output = @importer_config['output'] || 'output.json'
12
- @file_path = "#{Dir.pwd}/#{@output}"
12
+ @output_file_path = @importer_config['output'] || 'output.json'
13
13
  end
14
14
 
15
15
  def write
16
+ config = {
17
+ '$schema': 'https://www.krakend.io/schema/v3.json',
18
+ version: 3
19
+ }
20
+
21
+ config.merge!(
22
+ @importer_config.dig('defaults', 'base') || {}
23
+ )
24
+
25
+ config[:endpoints] = @endpoints
26
+
27
+ File.write(file_path, format(config))
28
+ end
29
+
30
+ def file_path
31
+ pwd = File.expand_path(Dir.pwd)
32
+ if Pathname.new(@output_file_path).absolute?
33
+ @output_file_path
34
+ else
35
+ File.expand_path(@output_file_path, pwd)
36
+ end
37
+ end
38
+
39
+ def format(obj)
40
+ format = @importer_config['format']
16
41
  pretty_output = !!@importer_config['pretty'] # rubocop:disable Style/DoubleNegation
17
- json_generate = pretty_output ? ->(obj) { JSON.pretty_generate(obj) } : ->(obj) { JSON.dump(obj) }
18
- File.write(@file_path, json_generate.call({
19
- '$schema': 'https://www.krakend.io/schema/v3.json',
20
- version: 3,
21
- endpoints: @endpoints
22
- }))
42
+
43
+ if format == 'yaml'
44
+ YAML.dump(stringify(obj))
45
+ elsif pretty_output
46
+ JSON.pretty_generate(obj)
47
+ else
48
+ JSON.dump(obj)
49
+ end
50
+ end
51
+
52
+ def stringify(obj)
53
+ if obj.is_a?(Array)
54
+ obj.map { |v| stringify(v) } if obj.is_a?(Array)
55
+ elsif obj.is_a?(Hash)
56
+ obj.to_h { |k, v| [k.to_s, stringify(v)] }
57
+ else
58
+ obj
59
+ end
23
60
  end
24
61
  end
25
62
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: krakend-openapi-importer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Semenenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-03-09 00:00:00.000000000 Z
11
+ date: 2025-02-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -27,7 +27,7 @@ dependencies:
27
27
  description: Import endpoints from OpenAPI spec to KrakenD endpoint configuration.
28
28
  Supports OpenAPI v3.0
29
29
  email:
30
- - hypercoderx@gmail.com
30
+ - den@densemenenko.com
31
31
  executables:
32
32
  - krakend-openapi-importer
33
33
  extensions: []
@@ -35,6 +35,7 @@ extra_rdoc_files: []
35
35
  files:
36
36
  - ".github/workflows/main.yml"
37
37
  - ".gitignore"
38
+ - ".rubocop.yml"
38
39
  - ".ruby-version"
39
40
  - Gemfile
40
41
  - Gemfile.lock
@@ -48,18 +49,20 @@ files:
48
49
  - lib/commands/import.rb
49
50
  - lib/importer.rb
50
51
  - lib/importer/version.rb
52
+ - lib/readers/file_reader.rb
51
53
  - lib/readers/json_reader.rb
52
54
  - lib/readers/oa3_reader.rb
53
55
  - lib/readers/yaml_reader.rb
54
- - lib/transformers/jwt_validator_transformer.rb
55
56
  - lib/transformers/oa3_transformer.rb
57
+ - lib/transformers/plugins/auth_validator_transformer.rb
56
58
  - lib/writers/krakend_writer.rb
57
- homepage: https://hypercoder.net
59
+ homepage: https://github.com/denblackstache/krakend-openapi-importer
58
60
  licenses:
59
61
  - MIT
60
62
  metadata:
61
63
  allowed_push_host: https://rubygems.org
62
- homepage_uri: https://hypercoder.net
64
+ rubygems_mfa_required: 'true'
65
+ homepage_uri: https://github.com/denblackstache/krakend-openapi-importer
63
66
  source_code_uri: https://github.com/denblackstache/krakend-openapi-importer
64
67
  post_install_message:
65
68
  rdoc_options: []
@@ -1,21 +0,0 @@
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