Skip to content

Commit

Permalink
Merge pull request #157 from pimterry/mac-user-socket-fallback
Browse files Browse the repository at this point in the history
Use the per-user Docker socket as the Unix default, if present
  • Loading branch information
apocas authored Sep 23, 2023
2 parents 2fbe36a + a04e0ef commit 669e227
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 16 deletions.
42 changes: 33 additions & 9 deletions lib/modem.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ var querystring = require('querystring'),
utils = require('./utils'),
util = require('util'),
splitca = require('split-ca'),
isWin = require('os').type() === 'Windows_NT',
os = require('os'),
isWin = os.type() === 'Windows_NT',
stream = require('stream');

var defaultOpts = function () {
Expand All @@ -18,12 +19,12 @@ var defaultOpts = function () {

if (!process.env.DOCKER_HOST) {
// Windows socket path: //./pipe/docker_engine ( Windows 10 )
// Linux & Darwin socket path: /var/run/docker.sock
opts.socketPath = isWin ? '//./pipe/docker_engine' : '/var/run/docker.sock';
// Linux & Darwin socket path is /var/run/docker.sock when running system-wide,
// or $HOME/.docker/run/docker.sock in new Docker Desktop installs.
opts.socketPath = isWin ? '//./pipe/docker_engine' : findDefaultUnixSocket;
} else if (process.env.DOCKER_HOST.indexOf('unix://') === 0) {
// Strip off unix://, fall back to default of /var/run/docker.sock if
// unix:// was passed without a path
opts.socketPath = process.env.DOCKER_HOST.substring(7) || '/var/run/docker.sock';
// Strip off unix://, fall back to default if unix:// was passed without a path
opts.socketPath = process.env.DOCKER_HOST.substring(7) || findDefaultUnixSocket;
} else if (process.env.DOCKER_HOST.indexOf('npipe://') === 0) {
// Strip off npipe://, fall back to default of //./pipe/docker_engine if
// npipe:// was passed without a path
Expand Down Expand Up @@ -76,6 +77,16 @@ var defaultOpts = function () {
return opts;
};

var findDefaultUnixSocket = function () {
return new Promise(function (resolve) {
var userDockerSocket = path.join(os.homedir(), '.docker', 'run', 'docker.sock');
fs.access(userDockerSocket, function (err) {
if (err) resolve('/var/run/docker.sock');
else resolve(userDockerSocket);
})
});
}


var Modem = function (options) {
var optDefaults = defaultOpts();
Expand Down Expand Up @@ -220,17 +231,30 @@ Modem.prototype.dial = function (options, callback) {
}

if (this.socketPath) {
optionsf.socketPath = this.socketPath;
// SocketPath may be a function that can return a promise:
this.getSocketPath().then((socketPath) => {
optionsf.socketPath = socketPath;
this.buildRequest(optionsf, options, data, callback);
});
} else {
var urlp = url.parse(address);
optionsf.hostname = urlp.hostname;
optionsf.port = urlp.port;
optionsf.path = urlp.path;
}

this.buildRequest(optionsf, options, data, callback);
this.buildRequest(optionsf, options, data, callback);
}
};

Modem.prototype.getSocketPath = function () {
if (!this.socketPath) return;

var socketPathValue = typeof this.socketPath === 'function'
? this.socketPath() : this.socketPath;

return Promise.resolve(socketPathValue);
}

Modem.prototype.buildRequest = function (options, context, data, callback) {
var self = this;
var connectionTimeoutTimer;
Expand Down
26 changes: 19 additions & 7 deletions test/modem_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
var assert = require('assert');
var path = require('path');
var os = require('os');
var http = require('http');
var Modem = require('../lib/modem');
var defaultSocketPath = require('os').type() === 'Windows_NT' ? '//./pipe/docker_engine' : '/var/run/docker.sock';

var unixDefaultSocketPaths = ['/var/run/docker.sock', path.join(os.homedir(), '.docker/run/docker.sock')]
var defaultSocketPaths = os.type() === 'Windows_NT' ? ['//./pipe/docker_engine'] : unixDefaultSocketPaths;

describe('Modem', function () {
beforeEach(function () {
Expand All @@ -10,8 +14,11 @@ describe('Modem', function () {

it('should default to default socket path', function () {
var modem = new Modem();
assert.ok(modem.socketPath);
assert.strictEqual(modem.socketPath, defaultSocketPath);

return modem.getSocketPath().then((socketPath) => {
assert.ok(socketPath);
assert.ok(defaultSocketPaths.includes(socketPath));
});
});

it('should use specific cert, key and ca', function () {
Expand Down Expand Up @@ -50,9 +57,12 @@ describe('Modem', function () {
headers: customHeaders
});
assert.ok(modem.headers);
assert.ok(modem.socketPath);
assert.strictEqual(modem.socketPath, defaultSocketPath);
assert.strictEqual(modem.headers, customHeaders);

return modem.getSocketPath().then((socketPath) => {
assert.ok(socketPath);
assert.ok(defaultSocketPaths.includes(socketPath));
});
});

it('should allow DOCKER_HOST=unix:///path/to/docker.sock', function () {
Expand All @@ -67,8 +77,10 @@ describe('Modem', function () {
process.env.DOCKER_HOST = 'unix://';

var modem = new Modem();
assert.ok(modem.socketPath);
assert.strictEqual(modem.socketPath, '/var/run/docker.sock');
return modem.getSocketPath().then((socketPath) => {
assert.ok(socketPath);
assert.ok(unixDefaultSocketPaths.includes(socketPath));
});
});

it('should interpret DOCKER_HOST=tcp://N.N.N.N:2376 as https', function () {
Expand Down

0 comments on commit 669e227

Please sign in to comment.