Skip to content

Commit

Permalink
added TLS
Browse files Browse the repository at this point in the history
  • Loading branch information
zekiunal@gmail.com committed Jul 23, 2017
1 parent e221e7d commit 6a38a78
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,14 @@ import (

"strconv"
"sync"
"net/http"
"github.com/docker/go-connections/tlsconfig"
"path/filepath"
"errors"
)

var ErrRedirect = errors.New("unexpected redirect in response")

type Service struct {
Host string
Services map[string]SW
Expand Down Expand Up @@ -62,9 +68,30 @@ func NewServiceFromEnv(host string) *Service {
return NewService(host)
}

func NewService(host string) *Service {
func NewService(host string) (*Service) {
var clients *http.Client

if dockerCertPath := os.Getenv("DOCKER_CERT_PATH"); dockerCertPath != "" {
options := tlsconfig.Options{
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
InsecureSkipVerify: os.Getenv("DOCKER_TLS_VERIFY") == "",
}
tlsc, err := tlsconfig.Client(options)
if err != nil {
debug(err.Error())
}

clients = &http.Client{
Transport: &http.Transport{
TLSClientConfig: tlsc,
},
CheckRedirect: CheckRedirect,
}
}

docker_client, err := client.NewClient(host, "v1.24", nil, map[string]string{"User-Agent":"engine-api-cli-1.0"})
docker_client, err := client.NewClient(host, "v1.24", clients, map[string]string{"User-Agent":"engine-api-cli-1.0"})

if err != nil {
debug(err.Error())
Expand All @@ -77,6 +104,16 @@ func NewService(host string) *Service {
}
}


func CheckRedirect(req *http.Request, via []*http.Request) error {
if via[0].Method == http.MethodGet {
return http.ErrUseLastResponse
}
return ErrRedirect
}



func (service *Service) GetServices() ([]SW, error) {

services, err := service.DockerClient.ServiceList(context.Background(), types.ServiceListOptions{})
Expand Down

0 comments on commit 6a38a78

Please sign in to comment.