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

Fix following 301 redirects for https upgrades #2

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 1 addition & 10 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,13 @@ on:

jobs:
test:
services:
presto:
image: ghcr.io/popsql/prestodb-sandbox:0.284
ports:
- "18080:8080"
trino:
image: trinodb/trino:418
ports:
- "18081:8080"

runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 16.x, 18.x, 20.x ]
steps:
- uses: actions/checkout@v3
- run: docker compose up --detach --wait
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}
Expand Down
11 changes: 11 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,14 @@ services:
ports:
- "18081:8080"

nginx:
image: nginx:1.25-alpine
depends_on:
- presto
ports:
- "80:80"
- "443:443"
volumes:
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
- ./nginx/ssl/nginx-selfsigned.crt:/etc/nginx/ssl/nginx-selfsigned.crt:ro
- ./nginx/ssl/nginx-selfsigned.key:/etc/nginx/ssl/nginx-selfsigned.key:ro
32 changes: 32 additions & 0 deletions index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,36 @@ describe('redirect tests', function(){
},
});
});

describe('when using reverse proxy that does not forward protocol', function(){
// The first request will go to https://localhost:443/v1/statement, but the
// nextUri will respond with http://localhost/v1/statement/... where then
// the nginx proxy will 301 redirect us to https again.
const client = new Client({
host: 'localhost',
port: 443,
catalog: 'tpch',
schema: 'tiny',
ssl: {
rejectUnauthorized: false,
}
});

test('that querying works', function(done){
expect.assertions(5);
client.execute({
query: 'SELECT 1 AS col',
data: function(error, data, columns){
expect(error).toBeNull();
expect(data).toEqual([[1]]);
expect(columns).toHaveLength(1);
expect(columns[0]).toEqual(expect.objectContaining({ name: 'col', type: 'integer' }));
},
callback: function(error){
expect(error).toBeNull();
done();
},
});
}, 10000);
});
});
28 changes: 13 additions & 15 deletions lib/presto-client/index.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
const { URL } = require('url') ;
const http = require('follow-redirects/http');
const https = require('follow-redirects/https');

var adapterFor = (function() {
var adapters = {
'http:': require('follow-redirects/http'),
'https:': require('follow-redirects/https'),
};

return function(protocol) {
return adapters[protocol];
};
}());
var adapters = {
'http:': http,
'https:': https,
};

var PrestoHeaders = require('./headers').Headers;
var TrinoHeaders = require('./headers').TrinoHeaders;
Expand Down Expand Up @@ -70,12 +66,12 @@ var Client = exports.Client = function(args){

Client.prototype.request = function(opts, callback) {
var client = this;
var adapter = adapterFor(client.protocol);
var contentBody = null;

if (opts instanceof Object) {
opts.host = client.host;
opts.port = client.port;
opts.protocol = client.protocol;
if (! opts.user)
opts.user = client.user
if (! opts.headers)
Expand All @@ -90,14 +86,13 @@ Client.prototype.request = function(opts, callback) {
port: href.port || (href.protocol === 'https:' ? '443' : '80'),
path: href.pathname + href.search,
headers: {},
protocol: href.protocol,
};
} catch (error) {
return callback(error);
}
}
opts.protocol = client.protocol;

opts = Object.assign({}, opts, client.ssl);
var adapter = adapters[opts.protocol];

if (opts.user)
opts.headers[client.headers.USER] = opts.user;
Expand All @@ -113,7 +108,10 @@ Client.prototype.request = function(opts, callback) {
if (opts.body)
contentBody = opts.body;

opts.agent = new adapter.Agent(opts); // Otherwise SSL params are ignored.
opts.agents = {
http: new http.Agent({ keepAlive: false }),
https: new https.Agent({ ...client.ssl, keepAlive: false }),
};

var parser = this.jsonParser;

Expand Down
31 changes: 31 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
events {
worker_connections 1024;
}

http {
server {
listen 80;
server_name _;
return 301 https://localhost:443$request_uri;
}

server {
listen 443 ssl;
server_name _;

ssl_certificate /etc/nginx/ssl/nginx-selfsigned.crt;
ssl_certificate_key /etc/nginx/ssl/nginx-selfsigned.key;

location / {
proxy_pass http://presto:8080/;
proxy_connect_timeout 3;

proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# don't forward the protocol, so that presto will default to responding with
# http:// URLs, to test the 301 redirect logic.
# proxy_set_header X-Forwarded-Proto $scheme;
}
}
}
21 changes: 21 additions & 0 deletions nginx/ssl/nginx-selfsigned.crt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-----BEGIN CERTIFICATE-----
MIIDazCCAlOgAwIBAgIUYAwPnvz5X7oQWyF/FbHEDYhiliAwDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMzExMjExNDA4MjVaFw0yNDEx
MjAxNDA4MjVaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQCMWr++NX5Hpt6vnkMg2I72ja7PLTQo1f8sP12L2dOc
sQbIpY4pMCcmpzh1mI+gH9pPSsxrcnoDiX+rMPMpu1VVREY95jOUWWKQ+8fosCtQ
T0sZ5X6XoPd4MLqiaDZajh6xxMgTz4L08xPMi7wMpSwnZGPTbr3KDqVD1gdwaj3L
YZZK1gfOYpt9XErZ+EePI0uCxRNyrkB+X3t2dJVRVQzWt23fFtsOVBW94kbG+Swh
7VO8UCJ56EJpC6767CdqNdQs119+yiVjabG3xA2PPczJIsLRcb4nZO25oNiDh3Lw
rfrxYoBvM6JN/IXuf4E3+nUMuwrzVsx6d1aPGZBm7qALAgMBAAGjUzBRMB0GA1Ud
DgQWBBRKe9ovcw7I2WQ/kz5CP3m9W1qxGjAfBgNVHSMEGDAWgBRKe9ovcw7I2WQ/
kz5CP3m9W1qxGjAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQAx
2Svbu20Fx1HLPrbpDzlAHpI0Pnw5zzeEoIc/XIZ2tLa13vuFiUyT18kbPPmnI8Zh
cdPhNGgNxfa7VNM1/lGbfSnPBKR6uJcgXKO+olWHZkaRYjeJh/JpsobOn7L3hNpA
ygNioc0oiffkGOznATkYmq6HeV8OQ5H7/Z4wu1VhZBDNjkSPPy+xdvM/k/eLuzvU
dpErzxd2GRsv/tVVEryvNTcgJB+cm/WSXd6AgLQtKLcIZCO20cqAP78G25KA7qAl
iBSH+pXJk1snl8PE9jmn+4uj+9axsCEpvIMlVCBnTxjgSfVvvimGdnPyrWamIN/9
CyW2rVZFarM0UVQ+BMw8
-----END CERTIFICATE-----
28 changes: 28 additions & 0 deletions nginx/ssl/nginx-selfsigned.key
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCMWr++NX5Hpt6v
nkMg2I72ja7PLTQo1f8sP12L2dOcsQbIpY4pMCcmpzh1mI+gH9pPSsxrcnoDiX+r
MPMpu1VVREY95jOUWWKQ+8fosCtQT0sZ5X6XoPd4MLqiaDZajh6xxMgTz4L08xPM
i7wMpSwnZGPTbr3KDqVD1gdwaj3LYZZK1gfOYpt9XErZ+EePI0uCxRNyrkB+X3t2
dJVRVQzWt23fFtsOVBW94kbG+Swh7VO8UCJ56EJpC6767CdqNdQs119+yiVjabG3
xA2PPczJIsLRcb4nZO25oNiDh3LwrfrxYoBvM6JN/IXuf4E3+nUMuwrzVsx6d1aP
GZBm7qALAg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