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: make pkg client independent to other inner pkgs #962

Merged
merged 1 commit into from Mar 28, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions apis/server/server.go
Expand Up @@ -10,10 +10,10 @@ import (
"syscall"

"github.com/alibaba/pouch/apis/plugins"
"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/daemon/config"
"github.com/alibaba/pouch/daemon/mgr"

"github.com/alibaba/pouch/pkg/utils"
"github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ func (s *Server) Start() (err error) {

var tlsConfig *tls.Config
if s.Config.TLS.Key != "" && s.Config.TLS.Cert != "" {
tlsConfig, err = utils.GenTLSConfig(s.Config.TLS.Key, s.Config.TLS.Cert, s.Config.TLS.CA)
tlsConfig, err = client.GenTLSConfig(s.Config.TLS.Key, s.Config.TLS.Cert, s.Config.TLS.CA)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions cli/cli.go
Expand Up @@ -7,7 +7,6 @@ import (
"text/tabwriter"

"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/pkg/utils"

"github.com/fatih/structs"
"github.com/sirupsen/logrus"
Expand All @@ -22,7 +21,7 @@ var pouchDescription = "pouch is a client side tool pouch to interact with daemo
// Option uses to define the global options.
type Option struct {
host string
TLS utils.TLSConfig
TLS client.TLSConfig
}

// Cli is the client's core struct, it will be used to manage all subcommand, send http request
Expand Down
46 changes: 40 additions & 6 deletions client/client.go
Expand Up @@ -3,15 +3,15 @@ package client
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strings"
"time"

"github.com/alibaba/pouch/pkg/utils"
)

var (
Expand All @@ -32,8 +32,16 @@ type APIClient struct {
version string
}

// TLSConfig contains information of tls which users can specify
type TLSConfig struct {
CA string `json:"tlscacert,omitempty"`
Cert string `json:"tlscert,omitempty"`
Key string `json:"tlskey,omitempty"`
VerifyRemote bool
}

// NewAPIClient initializes a new API client for the given host
func NewAPIClient(host string, tls utils.TLSConfig) (CommonAPIClient, error) {
func NewAPIClient(host string, tls TLSConfig) (CommonAPIClient, error) {
if host == "" {
host = defaultHost
}
Expand Down Expand Up @@ -110,10 +118,10 @@ func newHTTPClient(u *url.URL, tlsConfig *tls.Config) *http.Client {
}

// generateTLSConfig configures TLS for API Client.
func generateTLSConfig(host string, tls utils.TLSConfig) *tls.Config {
func generateTLSConfig(host string, tls TLSConfig) *tls.Config {
// init tls config
if tls.Key != "" && tls.Cert != "" && !strings.HasPrefix(host, "unix://") {
tlsCfg, err := utils.GenTLSConfig(tls.Key, tls.Cert, tls.CA)
tlsCfg, err := GenTLSConfig(tls.Key, tls.Cert, tls.CA)
if err != nil {
fmt.Fprintf(os.Stderr, "fail to parse tls config %v", err)
os.Exit(1)
Expand All @@ -125,7 +133,7 @@ func generateTLSConfig(host string, tls utils.TLSConfig) *tls.Config {
return nil
}

func generateBaseURL(u *url.URL, tls utils.TLSConfig) string {
func generateBaseURL(u *url.URL, tls TLSConfig) string {
if tls.Key != "" && tls.Cert != "" && u.Scheme != "unix" {
return "https://" + u.Host
}
Expand Down Expand Up @@ -165,3 +173,29 @@ func (client *APIClient) GetAPIPath(path string, query url.Values) string {
func (client *APIClient) UpdateClientVersion(v string) {
client.version = v
}

// 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, err := x509.SystemCertPool()
if err != nil {
return nil, fmt.Errorf("failed to read system certificates: %v", err)
}
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
}
35 changes: 32 additions & 3 deletions client/client_test.go
@@ -1,11 +1,12 @@
package client

import (
"crypto/tls"
"fmt"
"net/url"
"reflect"
"testing"

"github.com/alibaba/pouch/pkg/utils"
"github.com/stretchr/testify/assert"
)

Expand All @@ -23,7 +24,7 @@ func TestNewAPIClient(t *testing.T) {
}

for host, expectError := range kvs {
cli, err := NewAPIClient(host, utils.TLSConfig{})
cli, err := NewAPIClient(host, TLSConfig{})
if expectError {
assert.Error(err, fmt.Sprintf("test data: %v", host))
} else {
Expand Down Expand Up @@ -67,7 +68,7 @@ func TestParseHost(t *testing.T) {
func Test_generateBaseURL(t *testing.T) {
type args struct {
u *url.URL
tls utils.TLSConfig
tls TLSConfig
}
tests := []struct {
name string
Expand All @@ -84,3 +85,31 @@ func Test_generateBaseURL(t *testing.T) {
})
}
}

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)
}
})
}
}
5 changes: 2 additions & 3 deletions client/utils.go
Expand Up @@ -2,15 +2,14 @@ package client

import (
"encoding/json"
"fmt"
"io"
"io/ioutil"

"github.com/pkg/errors"
)

func decodeBody(obj interface{}, body io.Reader) error {
if err := json.NewDecoder(body).Decode(obj); err != nil {
return errors.Wrap(err, "failed to decode body")
return fmt.Errorf("failed to decode body: %v", err)
}

return nil
Expand Down
4 changes: 2 additions & 2 deletions daemon/config/config.go
@@ -1,9 +1,9 @@
package config

import (
"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/cri"
"github.com/alibaba/pouch/network"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/volume"
)

Expand Down Expand Up @@ -44,7 +44,7 @@ type Config struct {
ContainerdPath string `json:"containerd-path"`

// TLS configuration
TLS utils.TLSConfig
TLS client.TLSConfig

// Default OCI Runtime
DefaultRuntime string `json:"default-runtime,omitempty"`
Expand Down
43 changes: 0 additions & 43 deletions pkg/utils/tls.go

This file was deleted.

35 changes: 0 additions & 35 deletions pkg/utils/tls_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions test/environment/env.go
Expand Up @@ -4,7 +4,7 @@ import (
"os"
"runtime"

"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/client"
"github.com/gotestyourself/gotestyourself/icmd"
)

Expand All @@ -19,7 +19,7 @@ var (
PouchdUnixDomainSock = "/var/run/pouchd.sock"

// TLSConfig is default tls config
TLSConfig = utils.TLSConfig{}
TLSConfig = client.TLSConfig{}
)

// IsLinux checks if the OS of test environment is Linux.
Expand Down
3 changes: 1 addition & 2 deletions test/request/request.go
Expand Up @@ -14,7 +14,6 @@ import (
"time"

"github.com/alibaba/pouch/client"
"github.com/alibaba/pouch/pkg/utils"
"github.com/alibaba/pouch/test/environment"
)

Expand Down Expand Up @@ -117,7 +116,7 @@ func Post(endpoint string, opts ...Option) (*http.Response, error) {
// newAPIClient return new HTTP client with tls.
//
// FIXME: Could we make some functions exported in alibaba/pouch/client?
func newAPIClient(host string, tls utils.TLSConfig) (*client.APIClient, error) {
func newAPIClient(host string, tls client.TLSConfig) (*client.APIClient, error) {
commonAPIClient, err := client.NewAPIClient(host, tls)
if err != nil {
return nil, err
Expand Down