Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocol not supported error on plugin redirecting to HTTPS target #81

Closed
victoranaylton opened this issue May 24, 2017 · 8 comments
Closed

Comments

@victoranaylton
Copy link

victoranaylton commented May 24, 2017

Hello All,

I have a plugin that overrides the target URL for one set in the plugin configuration.

On this plugin, i'm changing the req.targetHostname and req.targetPath and, if the target protocol is https, i'm also changing the req.targetSecure to true and the req.targetPort to 443

req.targetHostname = targetPathHostname // new target hostname
req.targetPath = targetPath // new target path

if (targetPathProtocol === 'https') {
  req.targetSecure = true
  req.targetPort = 443
} 

With this, when the request is executed, i'm getting the following error:

curl -i http://localhost:8000/health
HTTP/1.1 500 Internal Server Error
Date: Wed, 24 May 2017 12:57:14 GMT
Connection: keep-alive
Content-Length: 67

{"message":"Protocol \"https:\" not supported. Expected \"http:\""}

Just to be sure, i've configured my edgemicro instance to work with ssl but the error was the same:

curl -i https://localhost:8000/health
HTTP/1.1 500 Internal Server Error
Date: Wed, 24 May 2017 12:57:14 GMT
Connection: keep-alive
Content-Length: 67

{"message":"Protocol \"https:\" not supported. Expected \"http:\""}

Is it possible to do this? Change the target URL and also change from HTTP to HTTPS?

I'm using the following versions:

current nodejs version is v6.10.0
current edgemicro version is 2.4.5-beta
@mdobson
Copy link
Contributor

mdobson commented May 24, 2017

While it is possible to do this. It's not recommended. Here is one of two ways to make this work.

Set the target for your proxy to be an https proxy in edge. This will correctly configure everything internally.

You can also attempt to replace the agent used to make requests. You simply create an instance of https.Agent and assign it as the proxy agent. Brief code snippet below to document how to possibly do this.

var https = require('https');

//...elided 
onrequest: function(req, res, next) {
  var agent = new https.Agent({
    keepAlive: true
  })

  res.proxy.agent = agent;
//...elided
}

@victoranaylton
Copy link
Author

Just to add more information
Found this debugging the execution:

In the plugins-middleware.js

const proxy = sourceResponse.proxy; // get the proxy from sourceResponse

...  

var httpLibrary = http;
if (sourceRequest.targetSecure || proxy.secureHttpProxy) {
  httpLibrary = https; 
}
// As the plugin sets the targetSecure to "true", the httpLibrary will be "https"

var targetRequestOptions = {
  hostname: sourceRequest.targetHostname,
  port: parseInt(sourceRequest.targetPort),
  path: sourceRequest.targetPath,
  method: sourceRequest.method,
  headers: target_headers, 
  agent: proxy.agent // this agent has a "protocol" property with "http" as value
};

...

const targetRequest = httpLibrary.request(targetRequestOptions,
  (targetResponse) => cb(null, targetResponse)); // execute the request passing the targetRequestOptions above

the execution will land in the following code in the _http_client.js file

 var protocol = options.protocol || defaultAgent.protocol; // protocol will be "https"
 var expectedProtocol = defaultAgent.protocol;
 if (self.agent && self.agent.protocol) // self.agent is the same targetRequestOptions.agent
    expectedProtocol = self.agent.protocol; // expectedProtocol will be set to "http"

  if (options.path && / /.test(options.path)) {
    throw new TypeError('Request path contains unescaped characters');
  } else if (protocol !== expectedProtocol) { //this will be true:  Error!!
    throw new Error('Protocol "' + protocol + '" not supported. ' +
                    'Expected "' + expectedProtocol + '"');
  }

@victoranaylton
Copy link
Author

@mdobson Thanks for the answer!
I'll try as you suggested.

@victoranaylton
Copy link
Author

victoranaylton commented May 25, 2017

@mdobson It worked as you said, sorry about the delay and thank you again!

@mdobson
Copy link
Contributor

mdobson commented May 25, 2017

Great. One small change. You'll want to create an https.Agent outside of the onrequest function, or you'll get some performance issues down the line.

var https = require('https');
var agent = new https.Agent({
  keepAlive: true
})
//...elided 
onrequest: function(req, res, next) {
  res.proxy.agent = agent;
//...elided
}

@victoranaylton
Copy link
Author

Great! thanks for the tip!

@asnowfix
Copy link

@mdobson is this issue addressed by https://github.com/apigee-internal/microgateway/releases/tag/v2.4.6-beta or should we still use that tiop?

@mdobson
Copy link
Contributor

mdobson commented May 29, 2017

Still need to use the above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants