Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #124 from secrethub/feature/get-dir
Browse files Browse the repository at this point in the history
Add Get function for directories
  • Loading branch information
SimonBarendse authored Sep 6, 2019
2 parents 5268dc9 + 298c072 commit 6587150
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/secrethub/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ 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"
)

// DirService handles operations on directories from SecretHub.
type DirService interface {
// Create a directory at a given path.
Create(path string) (*api.Dir, error)
// Get returns the directory with the given ID.
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.
Expand All @@ -27,6 +30,26 @@ type dirService struct {
client *Client
}

// Get returns the directory with the given 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)
}

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
Expand Down
2 changes: 2 additions & 0 deletions pkg/secrethub/fakeclient/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ 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.
type DirService struct {
Creater DirCreater
Deleter DirDeleter
TreeGetter TreeGetter
secrethub.DirService
}

// Create implements the DirService interface Create function.
Expand Down
9 changes: 9 additions & 0 deletions pkg/secrethub/internals/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -335,6 +336,14 @@ func (c *Client) CreateDir(namespace, repoName string, in *api.CreateDirRequest)
return out, errio.Error(err)
}

// 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)
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.
Expand Down

0 comments on commit 6587150

Please sign in to comment.