kongfig icon indicating copy to clipboard operation
kongfig copied to clipboard

kongfig apply command uses default localhost instead of host parameter value

Open theoskolnik opened this issue 7 years ago • 3 comments

When trying to apply kongfig to an API in Kong, the apply command explicitly sets a host parameter; however, the error shows a request being made to http://localhost:8001. Seems like the host is getting lost somewhere.

kongfig apply --header='apikey: KEY’ --host=api.domain/admin --https --path=./kongfig.yml
Loading config ./kongfig.yml
Apply config to api.domain/admin
FetchError: request to http://localhost:8001/apis?offset=Mg%3D%3D&size=100 failed, reason: connect ECONNREFUSED 127.0.0.1:8001
 FetchError: request to http://localhost:8001/apis?offset=Mg%3D%3D&size=100 failed, reason: connect ECONNREFUSED 127.0.0.1:8001
    at ClientRequest.<anonymous> (/var/go/.nvm/versions/node/v7.4.0/lib/node_modules/kongfig/node_modules/node-fetch/index.js:133:11)
    at emitOne (events.js:96:13)
    at ClientRequest.emit (events.js:188:7)
    at Socket.socketErrorListener (_http_client.js:309:9)
    at emitOne (events.js:96:13)
    at Socket.emit (events.js:188:7)
    at emitErrorNT (net.js:1281:8)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

The kongfig.yml:

apis:
- attributes:
    https_only: true
    upstream_url: https://some-upstream-url
    uris: /reference-api-dev
  name: reference-api-dev
  plugins:
  - attributes:
      config.whitelist: team
    name: acl
  - attributes:
      config:
        key_names: integration
    name: key-auth

kongfig --version: 1.3.0

npm --version: 4.0.5

The error is coming from node-fetch, a transitive dependency of kongfig's isomorphic-fetch; however I'm not sure if the host is getting set to localhost even before it reaches that call.

npm show kongfig dependencies
 
{ 'babel-polyfill': '^6.2.0',
  colors: '^1.1.2',
  commander: '^2.9.0',
  invariant: '^2.2.2',
  'isomorphic-fetch': '^2.2.0',
  'js-yaml': '^3.4.6',
  minimist: '^1.2.0',
  'object-assign': '^4.0.1',
  pad: '^1.1.0',
  prettyjson: '^1.1.3',
  semver: '^5.3.0' }

theoskolnik avatar Feb 16 '18 22:02 theoskolnik

After some investigation, we figured out that this is a pagination issue. When Kongfig runs an apply, it will also make a GET request to admin_domain/apis when there are over 100 APIs registered in Kong (which is the default limit for paginated results in json data). The json response returns a "next" token with the hostname; in our case, we are not getting back admin_domain, but are instead getting back localhost:8001.

We've unsuccessfully tried a number of ways to preserve the scheme, hostname, and path in the next token; at this point, we would like to make the pagination limit configurable in this line of code.

@CyExy, is this a change you'd be willing to accept a pull request for?

theoskolnik avatar Feb 20 '18 23:02 theoskolnik

Sorry, it took so long to get back to you; I think we should be able to fix the issue by overriding the host for the next link when --host has been given. Would that work for you?

CyExy avatar Feb 22 '18 15:02 CyExy

@theoskolnik please could you test this PR https://github.com/mybuilder/kongfig/pull/123

Thanks

CyExy avatar Feb 28 '18 22:02 CyExy