swagger-api/swagger-js

Set client/server TLS options

tomchiverton opened this issue · 2 comments

I need to set a different CA in order to call a particular API, and it also requires setting a client TLS certificate (and so a key).

So I want do something like this :

import https from 'node:https';
// server 
https.globalAgent.options.ca = fs.readFileSync('ca.pem',{encoding:'utf8'})

// client 
https.globalAgent.options.key  = process.env.CLIENT_KEY_FILE;
https.globalAgent.options.cert = fs.readFileSync('client.pem',{encoding:'utf8'})

const api = await new SwaggerClient({spec:spec,request:https})

var t = await api.apis.Handshake.handshake( {things:'here'} )

It's not possible to do this with the request interceptor, as far as I can see.

Yeah I noticed this issue as well. Currently it's possible to set TLS options with the requestInterceptor for node versions below 18. For node version 18 and above swagger-client uses the native node fetch which treats requestInterceptor differently.

Here's an example that I think should be possible for versions below node 18 in case it helps:

    const https = require('https');
    const requestClient = await Swagger({
      spec: yourOpenAPISpec,
      requestInterceptor: req => {
        req.agent = new https.Agent({
           key: '/path/to/your/key.pem',
           cert: '/path/to/your/cert.pem'
       })
    });
   const operation = {
     // your swagger operation params here
   } 
   const result = await requestClient.execute(operation);
char0n commented

Hi @tomchiverton,

Looks like related to #3163. Please have a look at #3163 (comment).

Reopen if my answer didn't help.