-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
To use existing docker volume and network plugin. We implement pouch plugin mechanism. We change the plugin discovery directory to /run/pouch/plugins, /etc/pouch/plugins, /usr/lib/pouch/plugins. In future, maybe we will use the grpc protocol to implement the plugin Signed-off-by: Eric Li <[email protected]>
- Loading branch information
Showing
12 changed files
with
965 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package httputils | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"fmt" | ||
"io/ioutil" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
) | ||
|
||
// ParseHost inputs a host address string, and output three type: | ||
// url.URL, basePath and an error. | ||
func ParseHost(host string) (*url.URL, string, string, error) { | ||
u, err := url.Parse(host) | ||
if err != nil { | ||
return nil, "", "", err | ||
} | ||
|
||
var basePath string | ||
switch u.Scheme { | ||
case "unix": | ||
basePath = "http://d" | ||
case "tcp": | ||
basePath = "http://" + u.Host | ||
case "http": | ||
basePath = host | ||
case "https": | ||
basePath = host | ||
default: | ||
return nil, "", "", fmt.Errorf("not support url scheme %v", u.Scheme) | ||
} | ||
|
||
return u, basePath, strings.TrimPrefix(host, u.Scheme+"://"), nil | ||
} | ||
|
||
// GenTLSConfig returns a tls config object according to inputting parameters. | ||
func GenTLSConfig(key, cert, ca string) (*tls.Config, error) { | ||
tlsConfig := &tls.Config{} | ||
tlsCert, err := tls.LoadX509KeyPair(cert, key) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read X509 key pair (cert: %q, key: %q): %v", cert, key, err) | ||
} | ||
tlsConfig.Certificates = []tls.Certificate{tlsCert} | ||
if ca == "" { | ||
return tlsConfig, nil | ||
} | ||
|
||
cp := x509.NewCertPool() | ||
pem, err := ioutil.ReadFile(ca) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to read CA certificate %q: %v", ca, err) | ||
} | ||
if !cp.AppendCertsFromPEM(pem) { | ||
return nil, fmt.Errorf("failed to append certificates from PEM file: %q", ca) | ||
} | ||
tlsConfig.ClientCAs = cp | ||
return tlsConfig, nil | ||
} | ||
|
||
// NewHTTPClient creates a http client using url and tlsconfig | ||
func NewHTTPClient(u *url.URL, tlsConfig *tls.Config, dialTimeout time.Duration) *http.Client { | ||
tr := &http.Transport{ | ||
TLSClientConfig: tlsConfig, | ||
} | ||
|
||
switch u.Scheme { | ||
case "unix": | ||
unixDial := func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
return net.DialTimeout("unix", u.Path, dialTimeout) | ||
} | ||
tr.DialContext = unixDial | ||
default: | ||
dial := func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
return net.DialTimeout(network, addr, dialTimeout) | ||
} | ||
tr.DialContext = dial | ||
} | ||
|
||
return &http.Client{ | ||
Transport: tr, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package httputils | ||
|
||
import ( | ||
"crypto/tls" | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var ( | ||
testHost = "unix:///var/run/pouchd.sock" | ||
) | ||
|
||
func TestParseHost(t *testing.T) { | ||
assert := assert.New(t) | ||
type parsed struct { | ||
host string | ||
expectError bool | ||
expectBasePath string | ||
expectAddr string | ||
} | ||
|
||
parseds := []parsed{ | ||
{host: testHost, expectError: false, expectBasePath: "http://d", expectAddr: "/var/run/pouchd.sock"}, | ||
{host: "tcp://localhost:1234", expectError: false, expectBasePath: "http://localhost:1234", expectAddr: "localhost:1234"}, | ||
{host: "http://localhost:5678", expectError: false, expectBasePath: "http://localhost:5678", expectAddr: "localhost:5678"}, | ||
{host: "foo:bar", expectError: true, expectBasePath: "", expectAddr: ""}, | ||
{host: "", expectError: true, expectBasePath: "", expectAddr: ""}, | ||
} | ||
|
||
for _, p := range parseds { | ||
_, basePath, addr, err := ParseHost(p.host) | ||
if p.expectError { | ||
assert.Error(err, fmt.Sprintf("test data %v", p.host)) | ||
} else { | ||
assert.NoError(err, fmt.Sprintf("test data %v", p.host)) | ||
} | ||
|
||
assert.Equal(basePath, p.expectBasePath) | ||
assert.Equal(addr, p.expectAddr) | ||
} | ||
} | ||
|
||
func TestGenTLSConfig(t *testing.T) { | ||
type args struct { | ||
key string | ||
cert string | ||
ca string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *tls.Config | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got, err := GenTLSConfig(tt.args.key, tt.args.cert, tt.args.ca) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("GenTLSConfig() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("GenTLSConfig() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.