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

Add Get function for directories #124

Merged
merged 6 commits into from
Sep 6, 2019
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
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