From f9cb6eab9a639df67cc59b4dd3140dc9b2501c79 Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Fri, 30 Aug 2019 17:19:19 +0200 Subject: [PATCH 1/5] Add Get function for directories --- pkg/secrethub/dir.go | 32 ++++++++++++++++++++++++++ pkg/secrethub/fakeclient/dir.go | 2 ++ pkg/secrethub/internals/http/client.go | 9 ++++++++ 3 files changed, 43 insertions(+) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index 10ebd690..8496f3b8 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -9,6 +9,8 @@ import ( type DirService interface { // Create a directory at a given path. Create(path string) (*api.Dir, error) + // Get returns the directory on the given path. + Get(path string) (*api.Dir, error) // Delete removes the directory at the given path. Delete(path string) error // GetTree retrieves a directory at a given path and all of its descendants up to a given depth. @@ -27,6 +29,36 @@ type dirService struct { client *Client } +// Get returns the directory on the given path. +func (s dirService) Get(path string) (*api.Dir, error) { + p, err := api.NewDirPath(path) + if err != nil { + return nil, err + } + + blindName, err := s.client.convertPathToBlindName(p) + if err != nil { + return nil, errio.Error(err) + } + + encDir, err := s.client.httpClient.GetDir(p.GetNamespace(), p.GetRepo(), blindName) + if err != nil { + return nil, errio.Error(err) + } + + accountKey, err := s.client.getAccountKey() + if err != nil { + return nil, errio.Error(err) + } + + dir, err := encDir.Decrypt(accountKey) + if err != nil { + return nil, errio.Error(err) + } + + return dir, nil +} + // GetTree retrieves a directory tree at a given path. The contents to the given depth // are returned. When depth is -1 all contents of the directory are included in the tree. // When ancestors is true, the parent directories of the dir at the given path will also diff --git a/pkg/secrethub/fakeclient/dir.go b/pkg/secrethub/fakeclient/dir.go index fdb6289d..5d7dc9ba 100644 --- a/pkg/secrethub/fakeclient/dir.go +++ b/pkg/secrethub/fakeclient/dir.go @@ -4,6 +4,7 @@ package fakeclient import ( "github.com/secrethub/secrethub-go/internals/api" + "github.com/secrethub/secrethub-go/pkg/secrethub" ) // DirService is a mock of the DirService interface. @@ -11,6 +12,7 @@ type DirService struct { Creater DirCreater Deleter DirDeleter TreeGetter TreeGetter + secrethub.DirService } // Create implements the DirService interface Create function. diff --git a/pkg/secrethub/internals/http/client.go b/pkg/secrethub/internals/http/client.go index b7fcfffd..d6587288 100644 --- a/pkg/secrethub/internals/http/client.go +++ b/pkg/secrethub/internals/http/client.go @@ -52,6 +52,7 @@ const ( pathRepoKey = "%s/namespaces/%s/repos/%s/keys" pathRepoAccounts = "%s/namespaces/%s/repos/%s/accounts" pathRepoEvents = "%s/namespaces/%s/repos/%s/events" + pathRepoDir = "%s/namespaces/%s/repos/%s/dirs/%s" pathRepoDirSecrets = "%s/namespaces/%s/repos/%s/dirs/%s/secrets" pathRepoUsers = "%s/namespaces/%s/repos/%s/users" pathRepoUser = "%s/namespaces/%s/repos/%s/users/%s" @@ -335,6 +336,14 @@ func (c *Client) CreateDir(namespace, repoName string, in *api.CreateDirRequest) return out, errio.Error(err) } +// GetDir retrieves a directory encrypted for the authenticated user. +func (c *Client) GetDir(namespace, repoName, dirBlindName string) (*api.EncryptedDir, error) { + rawURL := fmt.Sprintf(pathRepoDir, c.base, namespace, repoName, dirBlindName) + out := &api.EncryptedDir{} + err := c.get(rawURL, true, out) + return out, err +} + // GetTree gets a directory and all of it subdirs and secrets recursively by blind name. // If depth is > 0 then the result is limited to depth // If ancestors = true then ancestors are added. From da95bd8ba55aaf70991e0586170e3579711b7bac Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Wed, 4 Sep 2019 14:42:25 +0200 Subject: [PATCH 2/5] Use the dirs/{dir_id} route to get a directory --- pkg/secrethub/dir.go | 17 ++++------------- pkg/secrethub/internals/http/client.go | 6 +++--- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index 8496f3b8..a88a3935 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -2,6 +2,7 @@ package secrethub import ( "github.com/secrethub/secrethub-go/internals/api" + "github.com/secrethub/secrethub-go/internals/api/uuid" "github.com/secrethub/secrethub-go/internals/errio" ) @@ -10,7 +11,7 @@ type DirService interface { // Create a directory at a given path. Create(path string) (*api.Dir, error) // Get returns the directory on the given path. - Get(path string) (*api.Dir, error) + Get(id *uuid.UUID) (*api.Dir, error) // Delete removes the directory at the given path. Delete(path string) error // GetTree retrieves a directory at a given path and all of its descendants up to a given depth. @@ -30,18 +31,8 @@ type dirService struct { } // Get returns the directory on the given path. -func (s dirService) Get(path string) (*api.Dir, error) { - p, err := api.NewDirPath(path) - if err != nil { - return nil, err - } - - blindName, err := s.client.convertPathToBlindName(p) - if err != nil { - return nil, errio.Error(err) - } - - encDir, err := s.client.httpClient.GetDir(p.GetNamespace(), p.GetRepo(), blindName) +func (s dirService) Get(id *uuid.UUID) (*api.Dir, error) { + encDir, err := s.client.httpClient.GetDir(id) if err != nil { return nil, errio.Error(err) } diff --git a/pkg/secrethub/internals/http/client.go b/pkg/secrethub/internals/http/client.go index d6587288..62af34b0 100644 --- a/pkg/secrethub/internals/http/client.go +++ b/pkg/secrethub/internals/http/client.go @@ -10,6 +10,7 @@ import ( "github.com/op/go-logging" "github.com/secrethub/secrethub-go/internals/api" + "github.com/secrethub/secrethub-go/internals/api/uuid" "github.com/secrethub/secrethub-go/internals/auth" "github.com/secrethub/secrethub-go/internals/errio" ) @@ -52,7 +53,6 @@ const ( pathRepoKey = "%s/namespaces/%s/repos/%s/keys" pathRepoAccounts = "%s/namespaces/%s/repos/%s/accounts" pathRepoEvents = "%s/namespaces/%s/repos/%s/events" - pathRepoDir = "%s/namespaces/%s/repos/%s/dirs/%s" pathRepoDirSecrets = "%s/namespaces/%s/repos/%s/dirs/%s/secrets" pathRepoUsers = "%s/namespaces/%s/repos/%s/users" pathRepoUser = "%s/namespaces/%s/repos/%s/users/%s" @@ -337,8 +337,8 @@ func (c *Client) CreateDir(namespace, repoName string, in *api.CreateDirRequest) } // GetDir retrieves a directory encrypted for the authenticated user. -func (c *Client) GetDir(namespace, repoName, dirBlindName string) (*api.EncryptedDir, error) { - rawURL := fmt.Sprintf(pathRepoDir, c.base, namespace, repoName, dirBlindName) +func (c *Client) GetDir(id *uuid.UUID) (*api.EncryptedDir, error) { + rawURL := fmt.Sprintf(pathDir, c.base, id.String()) out := &api.EncryptedDir{} err := c.get(rawURL, true, out) return out, err From ee7fa63d5a1587fb56418773f6aaa8f04410d62c Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Fri, 6 Sep 2019 09:25:15 +0200 Subject: [PATCH 3/5] Accept a uuid value instead of pointer in Get function for directories --- pkg/secrethub/dir.go | 4 ++-- pkg/secrethub/internals/http/client.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index a88a3935..6777b9ab 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -11,7 +11,7 @@ type DirService interface { // Create a directory at a given path. Create(path string) (*api.Dir, error) // Get returns the directory on the given path. - Get(id *uuid.UUID) (*api.Dir, error) + Get(id uuid.UUID) (*api.Dir, error) // Delete removes the directory at the given path. Delete(path string) error // GetTree retrieves a directory at a given path and all of its descendants up to a given depth. @@ -31,7 +31,7 @@ type dirService struct { } // Get returns the directory on the given path. -func (s dirService) Get(id *uuid.UUID) (*api.Dir, error) { +func (s dirService) Get(id uuid.UUID) (*api.Dir, error) { encDir, err := s.client.httpClient.GetDir(id) if err != nil { return nil, errio.Error(err) diff --git a/pkg/secrethub/internals/http/client.go b/pkg/secrethub/internals/http/client.go index 62af34b0..3ed3bad1 100644 --- a/pkg/secrethub/internals/http/client.go +++ b/pkg/secrethub/internals/http/client.go @@ -337,7 +337,7 @@ func (c *Client) CreateDir(namespace, repoName string, in *api.CreateDirRequest) } // GetDir retrieves a directory encrypted for the authenticated user. -func (c *Client) GetDir(id *uuid.UUID) (*api.EncryptedDir, error) { +func (c *Client) GetDir(id uuid.UUID) (*api.EncryptedDir, error) { rawURL := fmt.Sprintf(pathDir, c.base, id.String()) out := &api.EncryptedDir{} err := c.get(rawURL, true, out) From 8da879257e651d6992357fdbb8bb5ac6d46282db Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Fri, 6 Sep 2019 13:15:36 +0200 Subject: [PATCH 4/5] Fix comments on getting a directory to reflect its parameter --- pkg/secrethub/dir.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index 6777b9ab..d821cfb9 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -10,7 +10,7 @@ import ( type DirService interface { // Create a directory at a given path. Create(path string) (*api.Dir, error) - // Get returns the directory on the given path. + // Get returns the directory with the given ID. Get(id uuid.UUID) (*api.Dir, error) // Delete removes the directory at the given path. Delete(path string) error @@ -30,7 +30,7 @@ type dirService struct { client *Client } -// Get returns the directory on the given path. +// Get returns the directory with the given ID. func (s dirService) Get(id uuid.UUID) (*api.Dir, error) { encDir, err := s.client.httpClient.GetDir(id) if err != nil { From 298c0725d0c11d9e98c24e09277bb4ca957f18ff Mon Sep 17 00:00:00 2001 From: Simon Barendse Date: Fri, 6 Sep 2019 14:27:29 +0200 Subject: [PATCH 5/5] Rename dirs.Get to GetByID We'll use DoByID pattern for functions that take a UUID as parameter and use Do for functions that take the name (e.g. path) as a parameter. --- pkg/secrethub/dir.go | 6 +++--- pkg/secrethub/internals/http/client.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pkg/secrethub/dir.go b/pkg/secrethub/dir.go index d821cfb9..227fb608 100644 --- a/pkg/secrethub/dir.go +++ b/pkg/secrethub/dir.go @@ -11,7 +11,7 @@ type DirService interface { // Create a directory at a given path. Create(path string) (*api.Dir, error) // Get returns the directory with the given ID. - Get(id uuid.UUID) (*api.Dir, error) + GetByID(id uuid.UUID) (*api.Dir, error) // Delete removes the directory at the given path. Delete(path string) error // GetTree retrieves a directory at a given path and all of its descendants up to a given depth. @@ -31,8 +31,8 @@ type dirService struct { } // Get returns the directory with the given ID. -func (s dirService) Get(id uuid.UUID) (*api.Dir, error) { - encDir, err := s.client.httpClient.GetDir(id) +func (s dirService) GetByID(id uuid.UUID) (*api.Dir, error) { + encDir, err := s.client.httpClient.GetDirByID(id) if err != nil { return nil, errio.Error(err) } diff --git a/pkg/secrethub/internals/http/client.go b/pkg/secrethub/internals/http/client.go index 3ed3bad1..7c471cc4 100644 --- a/pkg/secrethub/internals/http/client.go +++ b/pkg/secrethub/internals/http/client.go @@ -336,8 +336,8 @@ func (c *Client) CreateDir(namespace, repoName string, in *api.CreateDirRequest) return out, errio.Error(err) } -// GetDir retrieves a directory encrypted for the authenticated user. -func (c *Client) GetDir(id uuid.UUID) (*api.EncryptedDir, error) { +// GetDirByID retrieves a directory encrypted for the authenticated user. +func (c *Client) GetDirByID(id uuid.UUID) (*api.EncryptedDir, error) { rawURL := fmt.Sprintf(pathDir, c.base, id.String()) out := &api.EncryptedDir{} err := c.get(rawURL, true, out)