Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed how Errors are handled #25

Merged
merged 3 commits into from
Jun 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions pkg/vault/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

// Login will authenticate the user with Vault
// Will detect if user needs to re-login
func (v *Vault) Login() error {
func (v *Vault) Login() Error {
// get the token from the user's environment
if v.client.Token() != "" {
v.log.Debug("Reading token from environment 'VAULT_TOKEN'")
Expand All @@ -24,7 +24,7 @@ func (v *Vault) Login() error {
v.tokenHelper = token.InternalTokenHelper{}
token, err := v.tokenHelper.Get()
if err != nil {
return err
return v.parseError(err)
}

if token != "" {
Expand Down
29 changes: 23 additions & 6 deletions pkg/vault/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,39 @@ import (
"syscall"
)

// Error is the custom error type for this package
type Error struct {
// CustomVaultError is the custom error type for this package
type CustomVaultError struct {
MessageParts []string
OriginalError error
}

// Error is custom error interface with one method that will block other functions
// from using this Error. This is not interchangeable with the standard error.
type Error interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I would change this error name to, could possibly get away with keeping CustomVaultError private customVaultError and then only using the interface everywhere

Error() string
blockInterface()
}

// Error returns the error string
func (verr Error) Error() string {
return fmt.Sprintf("Vault Error: %s", strings.Join(verr.MessageParts, "; "))
func (verr CustomVaultError) Error() string {
return fmt.Sprintf("Vault: %s", strings.Join(verr.MessageParts, "; "))
}

//
func (verr CustomVaultError) blockInterface() {
}

// parseError parses known errors into more user-friendly messages
func (v *Vault) parseError(err error) Error {

var verr Error
verr.OriginalError = err
// Provent parseError from calling parseError again
if serr, ok := err.(Error); ok {
return serr
}

// var verr CustomVaultError
verr := &CustomVaultError{OriginalError: err}
// verr.OriginalError = err
thorix marked this conversation as resolved.
Show resolved Hide resolved

// Catch some known HTTP errors
if uerr, ok := err.(*url.Error); ok {
Expand Down
5 changes: 3 additions & 2 deletions pkg/vault/secrets.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package vault

import (
"github.com/hashicorp/vault/api"
"path/filepath"

"github.com/hashicorp/vault/api"
)

// Using Vaults Logical client:
Expand Down Expand Up @@ -81,7 +82,7 @@ func (v *Vault) ListSecrets(path string) ([]string, error) {
func (v *Vault) GetSecret(path string) (*api.Secret, error) {
secret, err := v.client.Logical().Read(path)
if err != nil {
return nil, err
return nil, v.parseError(err)
}

return secret, nil
Expand Down
4 changes: 2 additions & 2 deletions pkg/vault/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"time"
)

func (v *Vault) isVaultHealthy() (bool, error) {
func (v *Vault) isVaultHealthy() (bool, Error) {

result, err := v.client.Sys().Health()
if err != nil {
return false, v.parseError(err)
return false, v.parseError(err).(Error)
thorix marked this conversation as resolved.
Show resolved Hide resolved
}

v.log.Debug("Vault server info from (" + v.client.Address() + ")")
Expand Down
2 changes: 1 addition & 1 deletion pkg/vault/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (v *Vault) GetCurrentTokenTTL() (time.Duration, error) {
// Get the token info from Vault
secret, err := v.client.Auth().Token().LookupSelf()
if err != nil {
return 0, err
return 0, v.parseError(err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should be returning Error not error

}

// Get our TTL from the Vault secret interface{}
Expand Down
8 changes: 4 additions & 4 deletions pkg/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ func New(config *Config) (*Vault, error) {
// Ensure Vault is up and Healthy
_, err = v.isVaultHealthy()
if err != nil {
return nil, v.parseError(err)
return nil, err
}

// Run Login logic
err = v.Login()
if err != nil {
return nil, v.parseError(err)
return nil, err
}

// If user wants, extend the token timeout
Expand All @@ -81,13 +81,13 @@ func New(config *Config) (*Vault, error) {
v.log.Debug("Attempting to set token duration to ", v.config.InitialTokenDuration)
_, err = v.client.Auth().Token().RenewSelf(int(v.config.InitialTokenDuration.Seconds()))
if err != nil {
return nil, err
return nil, v.parseError(err)
}

// Show the actual TTL and warn if different from requested
actualDuration, err := v.GetCurrentTokenTTL()
if err != nil {
return nil, v.parseError(err)
return nil, err
}
v.log.Debug("Current token is valid for {}", actualDuration.String())

Expand Down
2 changes: 2 additions & 0 deletions stim/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func (stim *Stim) Vault() *vault.Vault {
timeInDuration = time.Duration(0)
}

stim.log.Debug("Vault Address: ({})", stim.GetConfig("vault-address"))

// Create the Vault object and pass in the needed address
vault, err := vault.New(&vault.Config{
Address: stim.GetConfig("vault-address"), // Default is 127.0.0.1
Expand Down