From 6fb9b2cd916d1d38f75bb0032123985340e7d705 Mon Sep 17 00:00:00 2001 From: risk danger olson Date: Fri, 24 Mar 2017 12:37:00 -0600 Subject: [PATCH] lfsapi: respect ssh response ExpiresAt in the ssh cache --- lfsapi/ssh.go | 5 +++-- lfsapi/ssh_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lfsapi/ssh.go b/lfsapi/ssh.go index 69709088..bd5c8ca1 100644 --- a/lfsapi/ssh.go +++ b/lfsapi/ssh.go @@ -7,6 +7,7 @@ import ( "os/exec" "path/filepath" "strings" + "time" "github.com/git-lfs/git-lfs/tools" "github.com/rubyist/tracerx" @@ -34,7 +35,7 @@ func (c *sshCache) Resolve(e Endpoint, method string) (sshAuthResponse, error) { } key := strings.Join([]string{e.SshUserAndHost, e.SshPort, e.SshPath, method}, "//") - if res, ok := c.endpoints[key]; ok { + if res, ok := c.endpoints[key]; ok && (res.ExpiresAt.IsZero() || res.ExpiresAt.After(time.Now().Add(5*time.Second))) { tracerx.Printf("ssh cache: %s git-lfs-authenticate %s %s", e.SshUserAndHost, e.SshPath, endpointOperation(e, method)) return *res, nil @@ -51,7 +52,7 @@ type sshAuthResponse struct { Message string `json:"-"` Href string `json:"href"` Header map[string]string `json:"header"` - ExpiresAt string `json:"expires_at"` + ExpiresAt time.Time `json:"expires_at"` } type sshAuthClient struct { diff --git a/lfsapi/ssh_test.go b/lfsapi/ssh_test.go index dcbca7f3..1d0e4e1e 100644 --- a/lfsapi/ssh_test.go +++ b/lfsapi/ssh_test.go @@ -4,6 +4,7 @@ import ( "errors" "path/filepath" "testing" + "time" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -15,6 +16,7 @@ func TestSSHCacheResolveFromCache(t *testing.T) { cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{ Href: "cache", } + ssh.responses["userandhost"] = sshAuthResponse{Href: "real"} e := Endpoint{ SshUserAndHost: "userandhost", @@ -27,6 +29,46 @@ func TestSSHCacheResolveFromCache(t *testing.T) { assert.Equal(t, "cache", res.Href) } +func TestSSHCacheResolveFromCacheWithFutureExpiresAt(t *testing.T) { + ssh := newFakeResolver() + cache := withSSHCache(ssh).(*sshCache) + cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{ + Href: "cache", + ExpiresAt: time.Now().Add(time.Duration(1) * time.Hour), + } + ssh.responses["userandhost"] = sshAuthResponse{Href: "real"} + + e := Endpoint{ + SshUserAndHost: "userandhost", + SshPort: "1", + SshPath: "path", + } + + res, err := cache.Resolve(e, "post") + assert.Nil(t, err) + assert.Equal(t, "cache", res.Href) +} + +func TestSSHCacheResolveFromCacheWithPastExpiresAt(t *testing.T) { + ssh := newFakeResolver() + cache := withSSHCache(ssh).(*sshCache) + cache.endpoints["userandhost//1//path//post"] = &sshAuthResponse{ + Href: "cache", + ExpiresAt: time.Now().Add(time.Duration(-1) * time.Hour), + } + ssh.responses["userandhost"] = sshAuthResponse{Href: "real"} + + e := Endpoint{ + SshUserAndHost: "userandhost", + SshPort: "1", + SshPath: "path", + } + + res, err := cache.Resolve(e, "post") + assert.Nil(t, err) + assert.Equal(t, "real", res.Href) +} + func TestSSHCacheResolveWithoutError(t *testing.T) { ssh := newFakeResolver() cache := withSSHCache(ssh).(*sshCache)