krakend-openapi-importer 0.3.1 → 0.4.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 +4 -4
- data/Gemfile +5 -0
- data/Gemfile.lock +5 -1
- data/README.md +1 -3
- data/exe/krakend-openapi-importer +0 -1
- data/lib/commands/import.rb +4 -5
- data/lib/importer/version.rb +1 -1
- data/lib/importer.rb +2 -4
- data/lib/readers/file_reader.rb +19 -0
- data/lib/readers/json_reader.rb +2 -1
- data/lib/readers/oa3_reader.rb +1 -1
- data/lib/readers/yaml_reader.rb +2 -1
- data/lib/transformers/oa3_transformer.rb +11 -11
- data/lib/transformers/plugins/auth_validator_transformer.rb +25 -0
- data/lib/writers/krakend_writer.rb +15 -7
- metadata +4 -3
- data/lib/transformers/jwt_validator_transformer.rb +0 -21
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA256:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 8982d7b1d96d3d3a4f1213075474be32a5d7a4d95c7671bd9bdd266090027bd3
         | 
| 4 | 
            +
              data.tar.gz: c8006aa961b9df1cf3fb5ea9f2a7b1f47874f4757592517bfc6777a48f91f9fc
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 6 | 
            +
              metadata.gz: 7b379be6f47cb870610a66b4471aab2cc9a6cce059724099f2f78c49a509d60cf312bd06d8d123e65196ec6ce4965c5e6d5f2a83d9c5ec50d630920def216d67
         | 
| 7 | 
            +
              data.tar.gz: 1581b80a865b139636892bab36ee36353af888e15d5b66259fb92f6c74fb20917928540a200f75d42a844dedfe56052285548aea2d83142b78c3c2c01cf691c0
         | 
    
        data/Gemfile
    CHANGED
    
    
    
        data/Gemfile.lock
    CHANGED
    
    | @@ -1,13 +1,15 @@ | |
| 1 1 | 
             
            PATH
         | 
| 2 2 | 
             
              remote: .
         | 
| 3 3 | 
             
              specs:
         | 
| 4 | 
            -
                krakend-openapi-importer (0. | 
| 4 | 
            +
                krakend-openapi-importer (0.4.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 | 
            +
                awesome_print (1.9.2)
         | 
| 12 | 
            +
                bump (0.10.0)
         | 
| 11 13 | 
             
                fakefs (2.0.0)
         | 
| 12 14 | 
             
                json (2.6.2)
         | 
| 13 15 | 
             
                minitest (5.13.0)
         | 
| @@ -40,6 +42,8 @@ PLATFORMS | |
| 40 42 | 
             
              x86_64-darwin-21
         | 
| 41 43 |  | 
| 42 44 | 
             
            DEPENDENCIES
         | 
| 45 | 
            +
              awesome_print
         | 
| 46 | 
            +
              bump (~> 0.10.0)
         | 
| 43 47 | 
             
              fakefs (~> 2.0)
         | 
| 44 48 | 
             
              krakend-openapi-importer!
         | 
| 45 49 | 
             
              minitest (~> 5.13)
         | 
    
        data/README.md
    CHANGED
    
    | @@ -22,9 +22,7 @@ krakend-openapi-importer import SPEC -c CONFIG | |
| 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
         | 
    
        data/lib/commands/import.rb
    CHANGED
    
    | @@ -1,19 +1,18 @@ | |
| 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:,  | 
| 13 | 
            +
                def initialize(spec:, config:)
         | 
| 12 14 | 
             
                  @spec = spec
         | 
| 13 | 
            -
                  @ | 
| 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
         | 
    
        data/lib/importer/version.rb
    CHANGED
    
    
    
        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' | 
| 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,  | 
| 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
         | 
    
        data/lib/readers/json_reader.rb
    CHANGED
    
    | @@ -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( | 
| 14 | 
            +
                  JSON.parse(KrakendOpenAPI::FileReader.new(@file_path).read)
         | 
| 14 15 | 
             
                end
         | 
| 15 16 | 
             
              end
         | 
| 16 17 | 
             
            end
         | 
    
        data/lib/readers/oa3_reader.rb
    CHANGED
    
    
    
        data/lib/readers/yaml_reader.rb
    CHANGED
    
    | @@ -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( | 
| 14 | 
            +
                  YAML.safe_load(KrakendOpenAPI::FileReader.new(@file_path).read)
         | 
| 14 15 | 
             
                end
         | 
| 15 16 | 
             
              end
         | 
| 16 17 | 
             
            end
         | 
| @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 | 
            -
            require_relative './ | 
| 3 | 
            +
            require_relative './plugins/auth_validator_transformer'
         | 
| 4 4 |  | 
| 5 5 | 
             
            module KrakendOpenAPI
         | 
| 6 6 | 
             
              # Transforms OpenAPI paths to KrakenD endpoints
         | 
| @@ -25,22 +25,22 @@ module KrakendOpenAPI | |
| 25 25 |  | 
| 26 26 | 
             
                  plugins = []
         | 
| 27 27 | 
             
                  if @importer_config['defaults']&.dig('plugins', 'auth_validator')
         | 
| 28 | 
            -
                    plugins <<  | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 28 | 
            +
                    plugins << Plugins::AuthValidatorTransformer
         | 
| 29 | 
            +
                               .new
         | 
| 30 | 
            +
                               .transform_to_hash(roles: roles,
         | 
| 31 | 
            +
                                                  config: @importer_config['defaults']['plugins']['auth_validator'])
         | 
| 32 32 | 
             
                  end
         | 
| 33 33 |  | 
| 34 34 | 
             
                  endpoint = {
         | 
| 35 35 | 
             
                    endpoint: path,
         | 
| 36 36 | 
             
                    method: method.upcase,
         | 
| 37 | 
            -
                    output_encoding: @importer_config['defaults'] | 
| 38 | 
            -
                    input_headers: @importer_config['defaults'] | 
| 39 | 
            -
                    input_query_strings: @importer_config['defaults'] | 
| 40 | 
            -
                    backend: [{ url_pattern: path, encoding: @importer_config['defaults'] | 
| 41 | 
            -
                  }
         | 
| 37 | 
            +
                    output_encoding: @importer_config['defaults']&.dig('endpoint', 'output_encoding'),
         | 
| 38 | 
            +
                    input_headers: @importer_config['defaults']&.dig('endpoint', 'input_headers'),
         | 
| 39 | 
            +
                    input_query_strings: @importer_config['defaults']&.dig('endpoint', 'input_query_strings'),
         | 
| 40 | 
            +
                    backend: [{ url_pattern: path, encoding: @importer_config['defaults']&.dig('backend', 0, 'encoding') }.compact]
         | 
| 41 | 
            +
                  }.compact
         | 
| 42 42 |  | 
| 43 | 
            -
                  if plugins&.length  | 
| 43 | 
            +
                  if plugins&.length&.> 0
         | 
| 44 44 | 
             
                    extra_config = plugins.each_with_object({}) do |plugin, memo|
         | 
| 45 45 | 
             
                      memo[plugin[:name].to_sym] = plugin[:value]
         | 
| 46 46 | 
             
                    end
         | 
| @@ -0,0 +1,25 @@ | |
| 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(roles:, config:)
         | 
| 8 | 
            +
                    raise(StandardError, 'Roles are empty') if roles.empty?
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                    {
         | 
| 11 | 
            +
                      name: 'auth/validator',
         | 
| 12 | 
            +
                      value: {
         | 
| 13 | 
            +
                        'alg': config['alg'],
         | 
| 14 | 
            +
                        'jwk_url': config['jwk_url'],
         | 
| 15 | 
            +
                        'cache': config['cache'],
         | 
| 16 | 
            +
                        'operation_debug': config['operation_debug'],
         | 
| 17 | 
            +
                        'roles_key_is_nested': config['roles_key_is_nested'],
         | 
| 18 | 
            +
                        'roles_key': config['roles_key'],
         | 
| 19 | 
            +
                        'roles': roles
         | 
| 20 | 
            +
                      }.compact
         | 
| 21 | 
            +
                    }
         | 
| 22 | 
            +
                  end
         | 
| 23 | 
            +
                end
         | 
| 24 | 
            +
              end
         | 
| 25 | 
            +
            end
         | 
| @@ -8,18 +8,26 @@ module KrakendOpenAPI | |
| 8 8 | 
             
                def initialize(endpoints, importer_config)
         | 
| 9 9 | 
             
                  @endpoints = endpoints
         | 
| 10 10 | 
             
                  @importer_config = importer_config
         | 
| 11 | 
            -
                  @ | 
| 12 | 
            -
                  @file_path = "#{Dir.pwd}/#{@output}"
         | 
| 11 | 
            +
                  @output_file_path = @importer_config['output'] || 'output.json'
         | 
| 13 12 | 
             
                end
         | 
| 14 13 |  | 
| 15 14 | 
             
                def write
         | 
| 16 15 | 
             
                  pretty_output = !!@importer_config['pretty'] # rubocop:disable Style/DoubleNegation
         | 
| 17 16 | 
             
                  json_generate = pretty_output ? ->(obj) { JSON.pretty_generate(obj) } : ->(obj) { JSON.dump(obj) }
         | 
| 18 | 
            -
                  File.write( | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 17 | 
            +
                  File.write(file_path, json_generate.call({
         | 
| 18 | 
            +
                                                             '$schema': 'https://www.krakend.io/schema/v3.json',
         | 
| 19 | 
            +
                                                             version: 3,
         | 
| 20 | 
            +
                                                             endpoints: @endpoints
         | 
| 21 | 
            +
                                                           }))
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                def file_path
         | 
| 25 | 
            +
                  pwd = File.expand_path(Dir.pwd)
         | 
| 26 | 
            +
                  if Pathname.new(@output_file_path).absolute?
         | 
| 27 | 
            +
                    @output_file_path
         | 
| 28 | 
            +
                  else
         | 
| 29 | 
            +
                    File.expand_path(@output_file_path, pwd)
         | 
| 30 | 
            +
                  end
         | 
| 23 31 | 
             
                end
         | 
| 24 32 | 
             
              end
         | 
| 25 33 | 
             
            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.4.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- | 
| 11 | 
            +
            date: 2023-03-13 00:00:00.000000000 Z
         | 
| 12 12 | 
             
            dependencies:
         | 
| 13 13 | 
             
            - !ruby/object:Gem::Dependency
         | 
| 14 14 | 
             
              name: thor
         | 
| @@ -48,11 +48,12 @@ files: | |
| 48 48 | 
             
            - lib/commands/import.rb
         | 
| 49 49 | 
             
            - lib/importer.rb
         | 
| 50 50 | 
             
            - lib/importer/version.rb
         | 
| 51 | 
            +
            - lib/readers/file_reader.rb
         | 
| 51 52 | 
             
            - lib/readers/json_reader.rb
         | 
| 52 53 | 
             
            - lib/readers/oa3_reader.rb
         | 
| 53 54 | 
             
            - lib/readers/yaml_reader.rb
         | 
| 54 | 
            -
            - lib/transformers/jwt_validator_transformer.rb
         | 
| 55 55 | 
             
            - lib/transformers/oa3_transformer.rb
         | 
| 56 | 
            +
            - lib/transformers/plugins/auth_validator_transformer.rb
         | 
| 56 57 | 
             
            - lib/writers/krakend_writer.rb
         | 
| 57 58 | 
             
            homepage: https://hypercoder.net
         | 
| 58 59 | 
             
            licenses:
         | 
| @@ -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
         |