Commit 5273f2ca authored by Patrick Bajao's avatar Patrick Bajao

Accept GetArchiveRequest param

Parse the GetArchiveRequest from the send data header and
use it when making calling the GetArchive RPC on gitaly.
parent f39e248b
...@@ -184,6 +184,48 @@ func TestAllowedGetGitBlob(t *testing.T) { ...@@ -184,6 +184,48 @@ func TestAllowedGetGitBlob(t *testing.T) {
func TestAllowedGetGitArchive(t *testing.T) { func TestAllowedGetGitArchive(t *testing.T) {
skipUnlessRealGitaly(t) skipUnlessRealGitaly(t)
// Create the repository in the Gitaly server
apiResponse := realGitalyOkBody(t)
require.NoError(t, ensureGitalyRepository(t, apiResponse))
archivePath := path.Join(scratchDir, "my/path")
archivePrefix := "repo-1"
msg := serializedProtoMessage("GetArchiveRequest", &gitalypb.GetArchiveRequest{
Repository: &apiResponse.Repository,
CommitId: "HEAD",
Prefix: archivePrefix,
Format: gitalypb.GetArchiveRequest_TAR,
Path: []byte("files"),
})
jsonParams := buildGitalyRPCParams(gitalyAddress, rpcArg{"ArchivePath", archivePath}, msg)
resp, body, err := doSendDataRequest("/archive.tar", "git-archive", jsonParams)
require.NoError(t, err)
assert.Equal(t, 200, resp.StatusCode, "GET %q: status code", resp.Request.URL)
assertNginxResponseBuffering(t, "no", resp, "GET %q: nginx response buffering", resp.Request.URL)
// Ensure the tar file is readable
foundEntry := false
tr := tar.NewReader(bytes.NewReader(body))
for {
hdr, err := tr.Next()
if err != nil {
break
}
if hdr.Name == archivePrefix+"/" {
foundEntry = true
break
}
}
assert.True(t, foundEntry, "Couldn't find %v directory entry", archivePrefix)
}
func TestAllowedGetGitArchiveOldPayload(t *testing.T) {
skipUnlessRealGitaly(t)
// Create the repository in the Gitaly server // Create the repository in the Gitaly server
apiResponse := realGitalyOkBody(t) apiResponse := realGitalyOkBody(t)
repo := apiResponse.Repository repo := apiResponse.Repository
......
...@@ -2,6 +2,7 @@ package main ...@@ -2,6 +2,7 @@ package main
import ( import (
"bytes" "bytes"
"encoding/base64"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
...@@ -635,6 +636,16 @@ func serializedMessage(name string, arg proto.Message) rpcArg { ...@@ -635,6 +636,16 @@ func serializedMessage(name string, arg proto.Message) rpcArg {
return rpcArg{name, str} return rpcArg{name, str}
} }
func serializedProtoMessage(name string, arg proto.Message) rpcArg {
msg, err := proto.Marshal(arg)
if err != nil {
panic(err)
}
return rpcArg{name, base64.URLEncoding.EncodeToString(msg)}
}
type combinedServer struct { type combinedServer struct {
*grpc.Server *grpc.Server
*testhelper.GitalyTestServer *testhelper.GitalyTestServer
......
...@@ -15,6 +15,8 @@ import ( ...@@ -15,6 +15,8 @@ import (
"regexp" "regexp"
"time" "time"
"github.com/golang/protobuf/proto"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
"gitlab.com/gitlab-org/gitaly-proto/go/gitalypb" "gitlab.com/gitlab-org/gitaly-proto/go/gitalypb"
...@@ -26,12 +28,13 @@ import ( ...@@ -26,12 +28,13 @@ import (
type archive struct{ senddata.Prefix } type archive struct{ senddata.Prefix }
type archiveParams struct { type archiveParams struct {
ArchivePath string ArchivePath string
ArchivePrefix string ArchivePrefix string
CommitId string CommitId string
GitalyServer gitaly.Server GitalyServer gitaly.Server
GitalyRepository gitalypb.Repository GitalyRepository gitalypb.Repository
DisableCache bool DisableCache bool
GetArchiveRequest []byte
} }
var ( var (
...@@ -101,7 +104,7 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string ...@@ -101,7 +104,7 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
var archiveReader io.Reader var archiveReader io.Reader
archiveReader, err = handleArchiveWithGitaly(r, params, format) archiveReader, err = handleArchiveWithGitaly(w, r, params, format)
if err != nil { if err != nil {
helper.Fail500(w, r, fmt.Errorf("operations.GetArchive: %v", err)) helper.Fail500(w, r, fmt.Errorf("operations.GetArchive: %v", err))
return return
...@@ -129,17 +132,27 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string ...@@ -129,17 +132,27 @@ func (a *archive) Inject(w http.ResponseWriter, r *http.Request, sendData string
} }
} }
func handleArchiveWithGitaly(r *http.Request, params archiveParams, format gitalypb.GetArchiveRequest_Format) (io.Reader, error) { func handleArchiveWithGitaly(w http.ResponseWriter, r *http.Request, params archiveParams, format gitalypb.GetArchiveRequest_Format) (io.Reader, error) {
var request *gitalypb.GetArchiveRequest
c, err := gitaly.NewRepositoryClient(params.GitalyServer) c, err := gitaly.NewRepositoryClient(params.GitalyServer)
if err != nil { if err != nil {
return nil, err return nil, err
} }
request := &gitalypb.GetArchiveRequest{ if params.GetArchiveRequest != nil {
Repository: &params.GitalyRepository, request = &gitalypb.GetArchiveRequest{}
CommitId: params.CommitId,
Prefix: params.ArchivePrefix, if err := proto.Unmarshal(params.GetArchiveRequest, request); err != nil {
Format: format, helper.Fail500(w, r, fmt.Errorf("SendArchive: unmarshal GetArchiveRequest: %v", err))
return nil, err
}
} else {
request = &gitalypb.GetArchiveRequest{
Repository: &params.GitalyRepository,
CommitId: params.CommitId,
Prefix: params.ArchivePrefix,
Format: format,
}
} }
return c.ArchiveReader(r.Context(), request) return c.ArchiveReader(r.Context(), request)
......
...@@ -168,3 +168,11 @@ func (s *GitalyTestServer) SetConfig(context.Context, *gitalypb.SetConfigRequest ...@@ -168,3 +168,11 @@ func (s *GitalyTestServer) SetConfig(context.Context, *gitalypb.SetConfigRequest
func (s *GitalyTestServer) DiffStats(*gitalypb.DiffStatsRequest, gitalypb.DiffService_DiffStatsServer) error { func (s *GitalyTestServer) DiffStats(*gitalypb.DiffStatsRequest, gitalypb.DiffService_DiffStatsServer) error {
return nil return nil
} }
func (s *GitalyTestServer) FetchHTTPRemote(context.Context, *gitalypb.FetchHTTPRemoteRequest) (*gitalypb.FetchHTTPRemoteResponse, error) {
return nil, nil
}
func (s *GitalyTestServer) PreFetch(context.Context, *gitalypb.PreFetchRequest) (*gitalypb.PreFetchResponse, error) {
return nil, nil
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment