Skip to content
This repository was archived by the owner on Sep 11, 2020. It is now read-only.

update transport Command interface to support SetAuth after creation #210

Merged
merged 2 commits into from
Jan 17, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var (
type CloneOptions struct {
// The (possibly remote) repository URL to clone from
URL string
// Auth credentials, if required, to uses with the remote repository
// Auth credentials, if required, to use with the remote repository
Auth transport.AuthMethod
// Name of the remote to be added, by default `origin`
RemoteName string
Expand Down Expand Up @@ -61,6 +61,8 @@ type PullOptions struct {
SingleBranch bool
// Limit fetching to the specified number of commits.
Depth int
// Auth credentials, if required, to use with the remote repository
Auth transport.AuthMethod
}

// Validate validate the fields and set the default values.
Expand All @@ -84,6 +86,8 @@ type FetchOptions struct {
// Depth limit fetching to the specified number of commits from the tip of
// each remote branch history.
Depth int
// Auth credentials, if required, to use with the remote repository
Auth transport.AuthMethod
}

// Validate validate the fields and set the default values
Expand All @@ -108,6 +112,8 @@ type PushOptions struct {
// RefSpecs specify what destination ref to update with what source
// object. A refspec with empty src can be used to delete a reference.
RefSpecs []config.RefSpec
// Auth credentials, if required, to use with the remote repository
Auth transport.AuthMethod
}

// Validate validate the fields and set the default values
Expand Down
4 changes: 2 additions & 2 deletions plumbing/transport/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ type dummyClient struct {
*http.Client
}

func (*dummyClient) NewUploadPackSession(transport.Endpoint) (
func (*dummyClient) NewUploadPackSession(transport.Endpoint, transport.AuthMethod) (
transport.UploadPackSession, error) {
return nil, nil
}

func (*dummyClient) NewReceivePackSession(transport.Endpoint) (
func (*dummyClient) NewReceivePackSession(transport.Endpoint, transport.AuthMethod) (
transport.ReceivePackSession, error) {
return nil, nil
}
Expand Down
7 changes: 3 additions & 4 deletions plumbing/transport/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
ErrAuthorizationRequired = errors.New("authorization required")
ErrEmptyUploadPackRequest = errors.New("empty git-upload-pack given")
ErrInvalidAuthMethod = errors.New("invalid auth method")
ErrAlreadyConnected = errors.New("session already established")
)

const (
Expand All @@ -41,9 +42,9 @@ const (
// It is implemented both by the client and the server, making this a RPC.
type Transport interface {
// NewUploadPackSession starts a git-upload-pack session for an endpoint.
NewUploadPackSession(Endpoint) (UploadPackSession, error)
NewUploadPackSession(Endpoint, AuthMethod) (UploadPackSession, error)
// NewReceivePackSession starts a git-receive-pack session for an endpoint.
NewReceivePackSession(Endpoint) (ReceivePackSession, error)
NewReceivePackSession(Endpoint, AuthMethod) (ReceivePackSession, error)
}

type Session interface {
Expand All @@ -52,8 +53,6 @@ type Session interface {
// If the repository does not exist, returns ErrRepositoryNotFound.
// If the repository exists, but is empty, returns ErrEmptyRemoteRepository.
AdvertisedReferences() (*packp.AdvRefs, error)
//TODO: Move to Client level.
SetAuth(auth AuthMethod) error
io.Closer
}

Expand Down
15 changes: 6 additions & 9 deletions plumbing/transport/file/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,18 @@ func NewClient(uploadPackBin, receivePackBin string) transport.Transport {
})
}

func (r *runner) Command(cmd string, ep transport.Endpoint) (common.Command, error) {
func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod) (common.Command, error) {
switch cmd {
case transport.UploadPackServiceName:
cmd = r.UploadPackBin
case transport.ReceivePackServiceName:
cmd = r.ReceivePackBin
}

if _, err := exec.LookPath(cmd); err != nil {
return nil, err
}

return &command{cmd: exec.Command(cmd, ep.Path)}, nil
}

Expand All @@ -43,14 +48,6 @@ type command struct {
closed bool
}

func (c *command) SetAuth(auth transport.AuthMethod) error {
if auth != nil {
return transport.ErrInvalidAuthMethod
}

return nil
}

func (c *command) Start() error {
return c.cmd.Start()
}
Expand Down
6 changes: 3 additions & 3 deletions plumbing/transport/file/receive_pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (s *ReceivePackSuite) TestCommandNoOutput(c *C) {
}

client := NewClient("true", "true")
session, err := client.NewReceivePackSession(s.Endpoint)
session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth)
c.Assert(err, IsNil)
ar, err := session.AdvertisedReferences()
c.Assert(err, IsNil)
Expand All @@ -59,7 +59,7 @@ func (s *ReceivePackSuite) TestMalformedInputNoErrors(c *C) {
}

client := NewClient("yes", "yes")
session, err := client.NewReceivePackSession(s.Endpoint)
session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth)
c.Assert(err, IsNil)
ar, err := session.AdvertisedReferences()
c.Assert(err, NotNil)
Expand All @@ -69,7 +69,7 @@ func (s *ReceivePackSuite) TestMalformedInputNoErrors(c *C) {
func (s *ReceivePackSuite) TestNonExistentCommand(c *C) {
cmd := "/non-existent-git"
client := NewClient(cmd, cmd)
session, err := client.NewReceivePackSession(s.Endpoint)
session, err := client.NewReceivePackSession(s.Endpoint, s.EmptyAuth)
c.Assert(err, ErrorMatches, ".*no such file or directory.*")
c.Assert(session, IsNil)
}
6 changes: 4 additions & 2 deletions plumbing/transport/file/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ func ServeUploadPack(path string) error {
return err
}

s, err := server.DefaultServer.NewUploadPackSession(ep)
// TODO: define and implement a server-side AuthMethod
s, err := server.DefaultServer.NewUploadPackSession(ep, nil)
if err != nil {
return fmt.Errorf("error creating session: %s", err)
}
Expand All @@ -36,7 +37,8 @@ func ServeReceivePack(path string) error {
return err
}

s, err := server.DefaultServer.NewReceivePackSession(ep)
// TODO: define and implement a server-side AuthMethod
s, err := server.DefaultServer.NewReceivePackSession(ep, nil)
if err != nil {
return fmt.Errorf("error creating session: %s", err)
}
Expand Down
6 changes: 3 additions & 3 deletions plumbing/transport/file/upload_pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (s *UploadPackSuite) TestCommandNoOutput(c *C) {
}

client := NewClient("true", "true")
session, err := client.NewUploadPackSession(s.Endpoint)
session, err := client.NewUploadPackSession(s.Endpoint, s.EmptyAuth)
c.Assert(err, IsNil)
ar, err := session.AdvertisedReferences()
c.Assert(err, IsNil)
Expand All @@ -65,7 +65,7 @@ func (s *UploadPackSuite) TestMalformedInputNoErrors(c *C) {
}

client := NewClient("yes", "yes")
session, err := client.NewUploadPackSession(s.Endpoint)
session, err := client.NewUploadPackSession(s.Endpoint, s.EmptyAuth)
c.Assert(err, IsNil)
ar, err := session.AdvertisedReferences()
c.Assert(err, NotNil)
Expand All @@ -75,7 +75,7 @@ func (s *UploadPackSuite) TestMalformedInputNoErrors(c *C) {
func (s *UploadPackSuite) TestNonExistentCommand(c *C) {
cmd := "/non-existent-git"
client := NewClient(cmd, cmd)
session, err := client.NewUploadPackSession(s.Endpoint)
session, err := client.NewUploadPackSession(s.Endpoint, s.EmptyAuth)
c.Assert(err, ErrorMatches, ".*no such file or directory.*")
c.Assert(session, IsNil)
}
19 changes: 6 additions & 13 deletions plumbing/transport/git/common.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package git

import (
"errors"
"fmt"
"io"
"net"
Expand All @@ -13,22 +12,21 @@ import (
"gopkg.in/src-d/go-git.v4/utils/ioutil"
)

var (
errAlreadyConnected = errors.New("tcp connection already connected")
)

// DefaultClient is the default git client.
var DefaultClient = common.NewClient(&runner{})

type runner struct{}

// Command returns a new Command for the given cmd in the given Endpoint
func (r *runner) Command(cmd string, ep transport.Endpoint) (common.Command, error) {
func (r *runner) Command(cmd string, ep transport.Endpoint, auth transport.AuthMethod) (common.Command, error) {
// auth not allowed since git protocol doesn't support authentication
if auth != nil {
return nil, transport.ErrInvalidAuthMethod
}
c := &command{command: cmd, endpoint: ep}
if err := c.connect(); err != nil {
return nil, err
}

return c, nil
}

Expand All @@ -39,11 +37,6 @@ type command struct {
endpoint transport.Endpoint
}

// SetAuth cannot be called since git protocol doesn't support authentication
func (c *command) SetAuth(auth transport.AuthMethod) error {
return transport.ErrInvalidAuthMethod
}

// Start executes the command sending the required message to the TCP connection
func (c *command) Start() error {
cmd := endpointToCommand(c.command, c.endpoint)
Expand All @@ -54,7 +47,7 @@ func (c *command) Start() error {

func (c *command) connect() error {
if c.connected {
return errAlreadyConnected
return transport.ErrAlreadyConnected
}

var err error
Expand Down
18 changes: 4 additions & 14 deletions plumbing/transport/http/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ func NewClient(c *http.Client) transport.Transport {
}
}

func (c *client) NewUploadPackSession(ep transport.Endpoint) (
func (c *client) NewUploadPackSession(ep transport.Endpoint, auth transport.AuthMethod) (
transport.UploadPackSession, error) {

return newUploadPackSession(c.c, ep), nil
return newUploadPackSession(c.c, ep, auth)
}

func (c *client) NewReceivePackSession(ep transport.Endpoint) (
func (c *client) NewReceivePackSession(ep transport.Endpoint, auth transport.AuthMethod) (
transport.ReceivePackSession, error) {

return newReceivePackSession(c.c, ep), nil
return newReceivePackSession(c.c, ep, auth)
}

type session struct {
Expand All @@ -54,16 +54,6 @@ type session struct {
advRefs *packp.AdvRefs
}

func (s *session) SetAuth(auth transport.AuthMethod) error {
a, ok := auth.(AuthMethod)
if !ok {
return transport.ErrInvalidAuthMethod
}

s.auth = a
return nil
}

func (*session) Close() error {
return nil
}
Expand Down
11 changes: 5 additions & 6 deletions plumbing/transport/http/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
func Test(t *testing.T) { TestingT(t) }

type ClientSuite struct {
Endpoint transport.Endpoint
Endpoint transport.Endpoint
EmptyAuth transport.AuthMethod
}

var _ = Suite(&ClientSuite{})
Expand Down Expand Up @@ -76,9 +77,8 @@ func (s *ClientSuite) testNewHTTPError(c *C, code int, msg string) {

func (s *ClientSuite) TestSetAuth(c *C) {
auth := &BasicAuth{}
r, err := DefaultClient.NewUploadPackSession(s.Endpoint)
r, err := DefaultClient.NewUploadPackSession(s.Endpoint, auth)
c.Assert(err, IsNil)
r.SetAuth(auth)
c.Assert(auth, Equals, r.(*upSession).auth)
}

Expand All @@ -88,7 +88,6 @@ func (*mockAuth) Name() string { return "" }
func (*mockAuth) String() string { return "" }

func (s *ClientSuite) TestSetAuthWrongType(c *C) {
r, err := DefaultClient.NewUploadPackSession(s.Endpoint)
c.Assert(err, IsNil)
c.Assert(r.SetAuth(&mockAuth{}), Equals, transport.ErrInvalidAuthMethod)
_, err := DefaultClient.NewUploadPackSession(s.Endpoint, &mockAuth{})
c.Assert(err, Equals, transport.ErrInvalidAuthMethod)
}
4 changes: 2 additions & 2 deletions plumbing/transport/http/receive_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type rpSession struct {
*session
}

func newReceivePackSession(c *http.Client, ep transport.Endpoint) transport.ReceivePackSession {
return &rpSession{&session{}}
func newReceivePackSession(c *http.Client, ep transport.Endpoint, auth transport.AuthMethod) (transport.ReceivePackSession, error) {
return &rpSession{&session{}}, nil
}

func (s *rpSession) AdvertisedReferences() (*packp.AdvRefs, error) {
Expand Down
22 changes: 14 additions & 8 deletions plumbing/transport/http/upload_pack.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,22 @@ type upSession struct {
*session
}

func newUploadPackSession(c *http.Client,
ep transport.Endpoint) transport.UploadPackSession {
func newUploadPackSession(c *http.Client, ep transport.Endpoint, auth transport.AuthMethod) (transport.UploadPackSession, error) {
s := &session{
auth: basicAuthFromEndpoint(ep),
client: c,
endpoint: ep,
}
if auth != nil {
a, ok := auth.(AuthMethod)
if !ok {
return nil, transport.ErrInvalidAuthMethod
}

return &upSession{
session: &session{
auth: basicAuthFromEndpoint(ep),
client: c,
endpoint: ep,
},
s.auth = a
}

return &upSession{session: s}, nil
}

func (s *upSession) AdvertisedReferences() (*packp.AdvRefs, error) {
Expand Down
2 changes: 1 addition & 1 deletion plumbing/transport/http/upload_pack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func (s *UploadPackSuite) SetUpSuite(c *C) {

// Overwritten, different behaviour for HTTP.
func (s *UploadPackSuite) TestAdvertisedReferencesNotExists(c *C) {
r, err := s.Client.NewUploadPackSession(s.NonExistentEndpoint)
r, err := s.Client.NewUploadPackSession(s.NonExistentEndpoint, s.EmptyAuth)
c.Assert(err, IsNil)
info, err := r.AdvertisedReferences()
c.Assert(err, Equals, transport.ErrAuthorizationRequired)
Expand Down
Loading