Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the role of https.Agent in Node?

In the Node https module docs, regarding https.request, an example is shown:

const options = {
  hostname: 'encrypted.google.com',
  port: 443,
  path: '/',
  method: 'GET',
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
options.agent = new https.Agent(options);

const req = https.request(options, (res) => {
  // ...
});

This example is slightly ambiguous in my opinion, and I've asked an SO question regarding this ambiguity and following a comment reaffirming the strange wording, opened an issue for this.

Regardless, I am still trying to understand the part that the Agent plays in this scenario, seeing as the https.Agent module does accept TLS connection options:

interface AgentOptions extends http.AgentOptions, tls.ConnectionOptions

The definition of the https.Agent object is:

An Agent object for HTTPS similar to http.Agent.

And the definition for the http.Agent object is:

An Agent is responsible for managing connection persistence and reuse for HTTP clients.

From this I understand that an Agent is 'in charge' of managing a connection - and clearly, the fact that https.Agent exists on top of the 'plain' http.Agent exists would imply that it is 'in charge' of managing an HTTPS connection - hence the TLS configuration options it may receive.

My question is this - does this mean that the Agent in this case has an added responsibility of configuring the network security of the requests? this is a strange API if this is true - I would have expected to see the network connection config on a separate key for the https.request (as is shown in the example after the snippet above). Why overload the same object for another responsibility? Really, why have an https.Agent at all? The http.Agent should control connection pooling and keeping connections alive, while another layer should control configuring the actual requests. The https.Agent object doesn't seem well-defined to me.

like image 323
notepadNinja Avatar asked Sep 29 '20 09:09

notepadNinja


People also ask

What does HTTPS agent do?

Agent exists would imply that it is 'in charge' of managing an HTTPS connection - hence the TLS configuration options it may receive.

What is nodeJs HTTP agent?

The Agent manages connection persistence for HTTP clients. It maintains a queue of pending requests for a given host and port, reusing a single socket connection for each until the queue is empty. After that, the socket is destroyed, if the keepAlive is set to false .

Why we use HTTPS in node JS?

The HTTPS module provides a way of making Node. js transfer data over HTTP TLS/SSL protocol, which is the secure HTTP protocol.

What is the role of node JS HTTP module?

Node.js has a built-in module called HTTP, which allows Node.js to transfer data over the Hyper Text Transfer Protocol (HTTP).


1 Answers

Actually the HTTPS doc points to some good resources, it also contains a link to HTTPS module source code, which reveals a lot. But to answer your questions:

does this mean that the Agent in this case has an added responsibility of configuring the network security of the requests

Yes sort of, your HTTPS agent can perform custom security feature, or even use external HTTPS implementations, but for the built-in https.Agent, the native TLS module is used, and the options you passed to constructor is ultimately passed to tls.connect, allowing you to configure custom TLS options.

Why overload the same object for another responsibility? Really, why have an https.Agent at all?

Because HTTPS runs on TLS. Actually https.Agent internally calls and constructs with http.Agent. The extra bits are mainly the use of SSL "session" over TCP "socket", if you search getName in https.Agent source and compare that to http.Agent's, you will find the SSL session cache are based on many more fields, like DHparams, client cert, etc. things that does not exist for HTTP at all.

like image 79
Eric Wong Avatar answered Oct 04 '22 21:10

Eric Wong