krakend-openapi-importer 0.2.0 → 0.3.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 +4 -4
- data/Gemfile +9 -2
- data/Gemfile.lock +5 -16
- data/README.md +5 -2
- data/Rakefile +7 -3
- data/lib/commands/import.rb +3 -3
- data/lib/importer/version.rb +1 -1
- data/lib/readers/json_reader.rb +2 -0
- data/lib/readers/oa3_reader.rb +15 -10
- data/lib/transformers/oa3_transformer.rb +19 -11
- data/lib/writers/krakend_writer.rb +6 -4
- metadata +2 -3
- data/.rspec +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d3a9eaa2da627c77ada30ddbb82c624063130d80c59087f3a57b9a8eb366afb8
|
4
|
+
data.tar.gz: a6e0280bee0c8a7a14cf669ac29bd341b29125e34fda209170c9eb8497556110
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d41c42b8df2419cf1c50dc3688d1461abceedb1fc25506a5c13ab32751a9323eff94d8b62fd15c4a8782903d3e299c3ec180976f3fbe2035df95f1b8589325d
|
7
|
+
data.tar.gz: cd0bcff75ae8effeca81e6fadb7afea44de51c2460a9c09e8c581370f67a5b05f5b216b5ae4f999571c9d001eb3869a749dabb7879d828b14f9a23c7ac937508
|
data/Gemfile
CHANGED
@@ -6,5 +6,12 @@ source 'https://rubygems.org'
|
|
6
6
|
gemspec
|
7
7
|
|
8
8
|
gem 'rake', '~> 13.0'
|
9
|
-
|
10
|
-
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem 'fakefs', '~> 2.0'
|
12
|
+
gem 'minitest', '~> 5.13'
|
13
|
+
end
|
14
|
+
|
15
|
+
group :development do
|
16
|
+
gem 'rubocop', require: false
|
17
|
+
end
|
data/Gemfile.lock
CHANGED
@@ -1,15 +1,16 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
krakend-openapi-importer (0.
|
4
|
+
krakend-openapi-importer (0.3.1)
|
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
|
-
|
11
|
+
fakefs (2.0.0)
|
12
12
|
json (2.6.2)
|
13
|
+
minitest (5.13.0)
|
13
14
|
parallel (1.22.1)
|
14
15
|
parser (3.1.2.1)
|
15
16
|
ast (~> 2.4.1)
|
@@ -17,19 +18,6 @@ GEM
|
|
17
18
|
rake (13.0.6)
|
18
19
|
regexp_parser (2.6.1)
|
19
20
|
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
21
|
rubocop (1.39.0)
|
34
22
|
json (~> 2.3)
|
35
23
|
parallel (~> 1.10)
|
@@ -52,9 +40,10 @@ PLATFORMS
|
|
52
40
|
x86_64-darwin-21
|
53
41
|
|
54
42
|
DEPENDENCIES
|
43
|
+
fakefs (~> 2.0)
|
55
44
|
krakend-openapi-importer!
|
45
|
+
minitest (~> 5.13)
|
56
46
|
rake (~> 13.0)
|
57
|
-
rspec (~> 3.0)
|
58
47
|
rubocop
|
59
48
|
|
60
49
|
BUNDLED WITH
|
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
Import endpoints from OpenAPI spec to KrakenD endpoint configuration. Supports OpenAPI v3.0 and up.
|
4
4
|
|
5
|
-
[](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml)
|
5
|
+
[](https://github.com/denblackstache/krakend-openapi-importer/actions/workflows/main.yml) [](https://badge.fury.io/rb/krakend-openapi-importer)
|
6
6
|
|
7
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
|
|
@@ -34,12 +34,15 @@ Example config
|
|
34
34
|
```yaml
|
35
35
|
---
|
36
36
|
all_roles: ['admin', 'guest'] # all available roles for JWT validator
|
37
|
-
pretty:
|
37
|
+
pretty: true
|
38
|
+
output: 'output.json'
|
38
39
|
defaults:
|
39
40
|
endpoint:
|
40
41
|
output_encoding: "no-op" # act like a no-op proxy
|
41
42
|
input_headers: [ "*" ]
|
42
43
|
input_query_strings: [ "*" ]
|
44
|
+
backend:
|
45
|
+
- encoding: "no-op"
|
43
46
|
plugins:
|
44
47
|
auth_validator:
|
45
48
|
alg: 'RS256'
|
data/Rakefile
CHANGED
@@ -1,8 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'bundler/gem_tasks'
|
4
|
-
require '
|
4
|
+
require 'rake/testtask'
|
5
5
|
|
6
|
-
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.libs << 'lib'
|
9
|
+
t.test_files = FileList['test/**/*_test.rb']
|
10
|
+
end
|
7
11
|
|
8
|
-
task default: :
|
12
|
+
task default: :test
|
data/lib/commands/import.rb
CHANGED
@@ -17,9 +17,9 @@ module KrakendOpenAPI
|
|
17
17
|
end
|
18
18
|
|
19
19
|
def execute
|
20
|
-
paths = OA3Reader.new
|
21
|
-
endpoints = OA3ToKrakendTransformer.new(@importer_config).transform_paths
|
22
|
-
KrakendWriter.new(@importer_config).write
|
20
|
+
paths = OA3Reader.new(@spec).paths
|
21
|
+
endpoints = OA3ToKrakendTransformer.new(paths, @importer_config).transform_paths
|
22
|
+
KrakendWriter.new(endpoints, @importer_config).write
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|
data/lib/importer/version.rb
CHANGED
data/lib/readers/json_reader.rb
CHANGED
data/lib/readers/oa3_reader.rb
CHANGED
@@ -6,22 +6,27 @@ require_relative '../readers/yaml_reader'
|
|
6
6
|
module KrakendOpenAPI
|
7
7
|
# Reads OpenAPI spec files
|
8
8
|
class OA3Reader
|
9
|
-
|
9
|
+
def initialize(path)
|
10
|
+
@path = path
|
11
|
+
end
|
12
|
+
|
13
|
+
def paths
|
14
|
+
read unless @data
|
15
|
+
@data['paths']
|
16
|
+
end
|
10
17
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
@data =
|
18
|
+
private
|
19
|
+
|
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
|
16
25
|
else
|
17
26
|
raise StandardError, 'OA3Reader does not support this format'
|
18
27
|
end
|
19
28
|
|
20
29
|
self
|
21
30
|
end
|
22
|
-
|
23
|
-
def paths
|
24
|
-
data['paths']
|
25
|
-
end
|
26
31
|
end
|
27
32
|
end
|
@@ -5,14 +5,17 @@ require_relative './jwt_validator_transformer'
|
|
5
5
|
module KrakendOpenAPI
|
6
6
|
# Transforms OpenAPI paths to KrakenD endpoints
|
7
7
|
class OA3ToKrakendTransformer
|
8
|
-
def initialize(importer_config)
|
8
|
+
def initialize(paths, importer_config)
|
9
|
+
@paths = paths
|
9
10
|
@importer_config = importer_config
|
10
11
|
end
|
11
12
|
|
12
|
-
def transform_paths
|
13
|
-
paths.map { |path, methods| transform_path(path, methods) }.flatten
|
13
|
+
def transform_paths
|
14
|
+
@paths.map { |path, methods| transform_path(path, methods) }.flatten
|
14
15
|
end
|
15
16
|
|
17
|
+
private
|
18
|
+
|
16
19
|
def transform_path(path, methods)
|
17
20
|
methods.map { |method, operation| transform_method(path, method, operation) }
|
18
21
|
end
|
@@ -21,10 +24,12 @@ module KrakendOpenAPI
|
|
21
24
|
roles = operation['x-jwt-roles']&.length ? operation['x-jwt-roles'] : @importer_config['all_roles']
|
22
25
|
|
23
26
|
plugins = []
|
24
|
-
plugins
|
25
|
-
|
26
|
-
|
27
|
-
|
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
|
28
33
|
|
29
34
|
endpoint = {
|
30
35
|
endpoint: path,
|
@@ -32,14 +37,17 @@ module KrakendOpenAPI
|
|
32
37
|
output_encoding: @importer_config['defaults']['endpoint']['output_encoding'],
|
33
38
|
input_headers: @importer_config['defaults']['endpoint']['input_headers'],
|
34
39
|
input_query_strings: @importer_config['defaults']['endpoint']['input_query_strings'],
|
35
|
-
backend: [{ url_pattern: path }]
|
40
|
+
backend: [{ url_pattern: path, encoding: @importer_config['defaults']['backend'][0]['encoding'] }]
|
36
41
|
}
|
37
42
|
|
38
|
-
|
39
|
-
|
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
|
47
|
+
|
48
|
+
endpoint[:extra_config] = extra_config
|
40
49
|
end
|
41
50
|
|
42
|
-
endpoint[:extra_config] = extra_config
|
43
51
|
endpoint
|
44
52
|
end
|
45
53
|
end
|
@@ -5,18 +5,20 @@ require 'json'
|
|
5
5
|
module KrakendOpenAPI
|
6
6
|
# Writes KrakenD configuration to a file
|
7
7
|
class KrakendWriter
|
8
|
-
def initialize(importer_config)
|
8
|
+
def initialize(endpoints, importer_config)
|
9
|
+
@endpoints = endpoints
|
9
10
|
@importer_config = importer_config
|
10
|
-
@
|
11
|
+
@output = @importer_config['output'] || 'output.json'
|
12
|
+
@file_path = "#{Dir.pwd}/#{@output}"
|
11
13
|
end
|
12
14
|
|
13
|
-
def write
|
15
|
+
def write
|
14
16
|
pretty_output = !!@importer_config['pretty'] # rubocop:disable Style/DoubleNegation
|
15
17
|
json_generate = pretty_output ? ->(obj) { JSON.pretty_generate(obj) } : ->(obj) { JSON.dump(obj) }
|
16
18
|
File.write(@file_path, json_generate.call({
|
17
19
|
'$schema': 'https://www.krakend.io/schema/v3.json',
|
18
20
|
version: 3,
|
19
|
-
endpoints: endpoints
|
21
|
+
endpoints: @endpoints
|
20
22
|
}))
|
21
23
|
end
|
22
24
|
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.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Denis Semenenko
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-03-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: thor
|
@@ -35,7 +35,6 @@ extra_rdoc_files: []
|
|
35
35
|
files:
|
36
36
|
- ".github/workflows/main.yml"
|
37
37
|
- ".gitignore"
|
38
|
-
- ".rspec"
|
39
38
|
- ".ruby-version"
|
40
39
|
- Gemfile
|
41
40
|
- Gemfile.lock
|
data/.rspec
DELETED