Skip to content

Commit

Permalink
add support for unix domain sockets as URI
Browse files Browse the repository at this point in the history
Sockets are passed as regular URIs (unix:/tmp/foo) in place of the HTTP
URI. Passing additional path fragments is not supported but largely
should not be necessary unless one proxies the socket, which would be
highly suspicious behavior.

Combined with the proposed UDS support in aptly this allows clients to
talk to the API over sockets rather than host-wide ports.

Also see https://github.com/smira/aptly/pull/493
  • Loading branch information
hsitter committed Feb 22, 2017
1 parent c61635f commit 2911425
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/aptly/configuration.rb
Expand Up @@ -6,6 +6,8 @@ class Configuration
extend Gem::Deprecate

# @!attribute uri
# Generally any suitable URI is allowed. This can also be a Unix domain
# socket which needs to be used in the notation unix:/tmp/sock.
# @return [URI] the base URI for the API (http://localhost by default)
attr_accessor :uri

Expand Down
8 changes: 8 additions & 0 deletions lib/aptly/connection.rb
Expand Up @@ -47,8 +47,16 @@ def method_missing(symbol, *args, **kwords)
private

def uri
@adapter_options ||= {}
@uri ||= begin
uri = @base_uri.clone
return uri unless uri.scheme == 'unix'
# For Unix domain sockets we need to divide the bits apart as Excon
# expects the path URI without a socket path and the socket path as
# option.
@adapter_options[:socket] = uri.path
uri.host = nil
uri
end
end

Expand Down
12 changes: 12 additions & 0 deletions test/connection_test.rb
Expand Up @@ -63,4 +63,16 @@ def test_uri_option
stub_request(:get, 'http://otherhost/api').to_return(status: 200)
c.get
end

def test_unix
# webmock is a bit screwed up with sockets but is suitably enough
# for this test case anyway. Basically the actual request URI is meant to
# be unix:/api which in webmock ends up being 'http://unix/api', the
# actual socket is passed as param to Excon. We are not testing this but
# if the path in webmock is well-formed we'll simply assume our socket
# actually works :O
stub_request(:get, 'http://unix/api').to_return(status: 200)
c = ::Aptly::Connection.new(uri: URI.parse('unix:///tmp/sock'))
c.get
end
end

0 comments on commit 2911425

Please sign in to comment.