Skip to content

Commit

Permalink
Merge pull request #25 from PremiereGlobal/clean_up_logs
Browse files Browse the repository at this point in the history
fixed how Errors are handled
  • Loading branch information
thorix committed Jun 19, 2019
2 parents e2f6dde + 92bf0b3 commit 3cb9939
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 27 deletions.
8 changes: 4 additions & 4 deletions pkg/vault/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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).(error)
}

if token != "" {
Expand Down Expand Up @@ -53,7 +53,7 @@ func (v *Vault) GetToken() (string, error) {
v.tokenHelper = token.InternalTokenHelper{}
token, err := v.tokenHelper.Get()
if err != nil {
return "", v.parseError(err)
return "", v.parseError(err).(error)
}

return token, nil
Expand Down Expand Up @@ -144,7 +144,7 @@ func (v *Vault) getCredentials() (string, string, error) {

if len(username) <= 0 { // If user just clicked enter
if v.config.Username == "" { // If there also isn't default
return "", "", v.newError("No username given")
return "", "", v.newError("No username given").(error)
}
username = v.config.Username
} else {
Expand All @@ -154,7 +154,7 @@ func (v *Vault) getCredentials() (string, string, error) {
fmt.Print("Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", "", v.parseError(err)
return "", "", v.parseError(err).(error)
}
fmt.Println("")
password := string(bytePassword)
Expand Down
31 changes: 23 additions & 8 deletions pkg/vault/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,37 @@ 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
}

// customError 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 CustomError interface {
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 {
func (v *Vault) parseError(err error) CustomError {

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

var verr Error
verr.OriginalError = err
verr := &CustomVaultError{OriginalError: err}

// Catch some known HTTP errors
if uerr, ok := err.(*url.Error); ok {
Expand Down Expand Up @@ -55,6 +70,6 @@ func (v *Vault) parseError(err error) Error {
}

// newError returns a new error based on a given string
func (v *Vault) newError(msg string) Error {
func (v *Vault) newError(msg string) CustomError {
return v.parseError(errors.New(msg))
}
2 changes: 1 addition & 1 deletion pkg/vault/mounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func (v *Vault) GetMounts(mountType string) ([]string, error) {

mounts, err := v.client.Sys().ListMounts()
if err != nil {
return nil, v.parseError(err)
return nil, v.parseError(err).(error)
}

var result []string
Expand Down
17 changes: 9 additions & 8 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 All @@ -14,17 +15,17 @@ func (v *Vault) GetSecretKey(path string, key string) (string, error) {

secret, err := v.client.Logical().Read(path)
if err != nil {
return "", v.parseError(err)
return "", v.parseError(err).(error)
}

// If we got back an empty response, fail
if secret == nil {
return "", v.newError("Could not find secret `" + path + "`")
return "", v.newError("Could not find secret `" + path + "`").(error)
}

// If the provided key doesn't exist, fail
if secret.Data[key] == nil {
return "", v.newError("Vault: Could not find key `" + key + "` for secret `" + path + "`")
return "", v.newError("Vault: Could not find key `" + key + "` for secret `" + path + "`").(error)
}

return secret.Data[key].(string), nil
Expand All @@ -36,7 +37,7 @@ func (v *Vault) GetSecretKeys(path string) (map[string]string, error) {

secret, err := v.client.Logical().Read(path)
if err != nil {
return nil, v.parseError(err)
return nil, v.parseError(err).(error)
}

// If we got back an empty response, fail
Expand All @@ -60,12 +61,12 @@ func (v *Vault) ListSecrets(path string) ([]string, error) {

secret, err := v.client.Logical().List(path)
if err != nil {
return nil, v.parseError(err)
return nil, v.parseError(err).(error)
}

// If we got back an empty response, fail
if secret == nil {
return nil, v.newError("Could not find secret `" + path + "`")
return nil, v.newError("Could not find secret `" + path + "`").(error)
}

// Loop through and get all the keys
Expand All @@ -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).(error)
}

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

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

result, err := v.client.Sys().Health()
if err != nil {
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).(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

0 comments on commit 3cb9939

Please sign in to comment.