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

AWS polish #117

Merged
merged 36 commits into from
Aug 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
58b8906
Implement configdir package to handle location of configuration files
jpcoenen Aug 27, 2019
49c7397
Implement new credentials.Creator interface
jpcoenen Aug 27, 2019
2a96e57
Remove unused function
jpcoenen Aug 27, 2019
f2fafdf
Use readKey() in UseKey()
jpcoenen Aug 27, 2019
3408dbf
Use a custom Reader interface for reading credentials
jpcoenen Aug 27, 2019
9adb23a
Move finding default credentials to Client
jpcoenen Aug 27, 2019
0a18f68
Handle cases where no passphrase is given
jpcoenen Aug 27, 2019
30a1b0f
Fix tests
jpcoenen Aug 27, 2019
c214dbc
Add GoDoc to configdir package
mackenbach Aug 27, 2019
f6f2d76
Add godoc to WithConfigDir option
mackenbach Aug 27, 2019
25755ca
Add/improve godoc for credential readers
mackenbach Aug 27, 2019
12df21a
Add package godoc for credentials package
mackenbach Aug 27, 2019
7e47dfd
Add/amend comments for credential creators
mackenbach Aug 27, 2019
6d1d24b
Add/improve secrethub.Client godoc
mackenbach Aug 27, 2019
231e49d
Add godoc for Client.DefaultCredential
mackenbach Aug 27, 2019
27e6a00
Run gofmt
mackenbach Aug 27, 2019
ca1e134
Let configdir.Dir implement Stringer interface
jpcoenen Aug 27, 2019
6dd978e
Extract setting options on Client to separate function
jpcoenen Aug 27, 2019
99a643e
Initialize ConfigDir to nil
jpcoenen Aug 27, 2019
bd89778
Remove readKey() in favour of already existing ImportKey()
jpcoenen Aug 27, 2019
fba9d15
Rearrange functions for clarity and remove duplication
jpcoenen Aug 27, 2019
e2d6d28
Make function clearer
jpcoenen Aug 27, 2019
4400bc8
Export error for non-existing credential file
jpcoenen Aug 27, 2019
11f0cda
Set filemode to previously used defaults
jpcoenen Aug 27, 2019
8a84569
Fix missing/wrong comments
jpcoenen Aug 27, 2019
0e84a45
Use bytes functions instead of strings
jpcoenen Aug 27, 2019
a035e0b
Add missing comments
jpcoenen Aug 27, 2019
b399490
Merge remote-tracking branch 'origin/feature/aws-integration-merge' i…
SimonBarendse Aug 28, 2019
9a15196
Rename encodeCredentialPartsToString => encodeCredentialParts
SimonBarendse Aug 28, 2019
7191415
Change encodeCredentialParts test to expect bytes
SimonBarendse Aug 28, 2019
d909589
Make path in configdir private
jpcoenen Aug 28, 2019
737803b
Retry getting passphrase if none is provided
jpcoenen Aug 28, 2019
06bbef3
Combine Fingerprint and Verifier in Export method
SimonBarendse Aug 28, 2019
c3e4ef9
Rename Export() to Encode() for keys
jpcoenen Aug 28, 2019
72be34a
Fix incorrect comment
jpcoenen Aug 28, 2019
c2bfdd3
Merge pull request #120 from secrethub/feature/verifier-export
jpcoenen Aug 28, 2019
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
14 changes: 13 additions & 1 deletion pkg/secrethub/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/secrethub/secrethub-go/internals/api"
"github.com/secrethub/secrethub-go/internals/crypto"
"github.com/secrethub/secrethub-go/internals/errio"
"github.com/secrethub/secrethub-go/pkg/secrethub/configdir"
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
"github.com/secrethub/secrethub-go/pkg/secrethub/internals/http"
)
Expand Down Expand Up @@ -47,7 +48,8 @@ type Client struct {
// These are cached
repoIndexKeys map[api.RepoPath]*crypto.SymmetricKey

appInfo *AppInfo
appInfo *AppInfo
ConfigDir *configdir.Dir
}

// AppInfo contains information about the application that is using the SecretHub client.
Expand Down Expand Up @@ -80,6 +82,7 @@ func NewClient(with ...ClientOption) (*Client, error) {
client := &Client{
httpClient: http.NewClient(),
repoIndexKeys: make(map[api.RepoPath]*crypto.SymmetricKey),
ConfigDir: &configdir.Dir{},
jpcoenen marked this conversation as resolved.
Show resolved Hide resolved
}
for _, option := range with {
err := option(client)
Expand All @@ -88,6 +91,15 @@ func NewClient(with ...ClientOption) (*Client, error) {
}
}

// ConfigDir should be fully initialized before loading any default credentials.
if client.ConfigDir == nil {
configDir, err := configdir.Default()
if err != nil {
return nil, err
}
client.ConfigDir = configDir
}

// Try to use default key credentials if none provided explicitly
if client.decrypter == nil {
err := WithCredentials(credentials.UseKey(nil, nil))(client)
Expand Down
8 changes: 8 additions & 0 deletions pkg/secrethub/client_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"time"

"github.com/secrethub/secrethub-go/pkg/secrethub/configdir"
"github.com/secrethub/secrethub-go/pkg/secrethub/credentials"
httpclient "github.com/secrethub/secrethub-go/pkg/secrethub/internals/http"
)
Expand Down Expand Up @@ -47,6 +48,13 @@ func WithAppInfo(appInfo *AppInfo) ClientOption {
}
}

func WithConfigDir(configDir configdir.Dir) ClientOption {
mackenbach marked this conversation as resolved.
Show resolved Hide resolved
return func(c *Client) error {
c.ConfigDir = &configDir
return nil
}
}

// WithCredentials sets the credential to be used for authenticating to the API and decrypting the account key.
func WithCredentials(provider credentials.Provider) ClientOption {
return func(c *Client) error {
Expand Down
71 changes: 71 additions & 0 deletions pkg/secrethub/configdir/dir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package configdir
mackenbach marked this conversation as resolved.
Show resolved Hide resolved

import (
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
)

type Dir struct {
Path string
SimonBarendse marked this conversation as resolved.
Show resolved Hide resolved
}

func Default() (*Dir, error) {
// TODO: move to constant?
jpcoenen marked this conversation as resolved.
Show resolved Hide resolved
envDir := os.Getenv("SECRETHUB_CONFIG_DIR")
if envDir != "" {
return &Dir{
Path: envDir,
}, nil
}

homeDir, err := homedir.Dir()
if err != nil {
return &Dir{}, fmt.Errorf("cannot get home directory: %v", err)
}
return &Dir{
Path: filepath.Join(homeDir, ".secrethub"),
}, nil
}

func (c Dir) Credential() *CredentialFile {
return &CredentialFile{
Path: filepath.Join(c.Path, "credential"),
}
}

type CredentialFile struct {
Path string
}

func (f *CredentialFile) Write(p []byte) error {
// TOOD: correct permission?
jpcoenen marked this conversation as resolved.
Show resolved Hide resolved
err := os.MkdirAll(filepath.Dir(f.Path), 0600)
if err != nil {
return err
}
// TOOD: correct permission?
jpcoenen marked this conversation as resolved.
Show resolved Hide resolved
return ioutil.WriteFile(f.Path, p, 0600)
}

func (f *CredentialFile) Exists() bool {
if _, err := os.Stat(f.Path); os.IsNotExist(err) {
return false
}
return true
}

func (f *CredentialFile) Read() ([]byte, error) {
file, err := os.Open(f.Path)
if os.IsNotExist(err) {
// TOOD: return more usable error
jpcoenen marked this conversation as resolved.
Show resolved Hide resolved
return nil, errors.New("credential not found. Please signup first")
} else if err != nil {
return nil, err
}
return ioutil.ReadAll(file)
}