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

Remove assumption that there is always a "Username" and "Password" field #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
11 changes: 6 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

log "github.com/Sirupsen/logrus"
vaultApi "github.com/hashicorp/vault/api"
"github.com/uswitch/vault-creds/pkg/kube"
"github.com/uswitch/vault-creds/pkg/vault"
"gopkg.in/alecthomas/kingpin.v2"
Expand Down Expand Up @@ -116,7 +117,7 @@ func main() {

credsProvider := vault.NewCredentialsProvider(client, *secretPath)

var creds *vault.Credentials
var creds *vaultApi.Secret

// if there's already a lease, use that and don't generate new credentials
if leaseExist {
Expand Down Expand Up @@ -177,7 +178,7 @@ func main() {
cleanUp(leasePath, tokenPath)
log.Fatal("auth token could no longer be renewed")
}
err = leaseManager.RenewSecret(ctx, creds.Secret, *leaseDuration)
err = leaseManager.RenewSecret(ctx, creds, *leaseDuration)
if err != nil {
log.Errorf("error renewing db credentials: %s", err)
}
Expand Down Expand Up @@ -213,7 +214,7 @@ func main() {
}
defer file.Close()

t.Execute(file, creds)
t.Execute(file, creds.Data)
log.Printf("wrote credentials to %s", file.Name())

//write out token
Expand All @@ -227,7 +228,7 @@ func main() {
log.Printf("wrote token to %s", tokenPath)

//write out full secret
bytes, err := yaml.Marshal(creds)
bytes, err := yaml.Marshal(creds.Data)
if err != nil {
log.Fatal(err)
}
Expand All @@ -241,7 +242,7 @@ func main() {
c <- os.Interrupt
}
} else if !leaseExist {
t.Execute(os.Stdout, creds)
t.Execute(os.Stdout, creds.Data)
}

<-c
Expand Down
12 changes: 3 additions & 9 deletions pkg/vault/vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var ErrPermissionDenied = errors.New("permission denied")
var ErrLeaseNotFound = errors.New("lease not found or is not renewable")

type CredentialsProvider interface {
Fetch() (*Credentials, error)
Fetch() (*api.Secret, error)
}

type CredentialsRenewer interface {
Expand Down Expand Up @@ -114,7 +114,7 @@ func NewCredentialsProvider(client *api.Client, secretPath string) *DefaultCrede
return &DefaultCredentialsProvider{client: client, path: secretPath}
}

func (c *DefaultCredentialsProvider) Fetch() (*Credentials, error) {
func (c *DefaultCredentialsProvider) Fetch() (*api.Secret, error) {
log.Infof("requesting credentials")
secret, err := c.client.Logical().Read(c.path)
if err != nil {
Expand All @@ -123,13 +123,7 @@ func (c *DefaultCredentialsProvider) Fetch() (*Credentials, error) {

log.WithFields(secretFields(secret)).Infof("succesfully retrieved credentials")

return &Credentials{secret.Data["username"].(string), secret.Data["password"].(string), secret}, nil
}

type Credentials struct {
Username string
Password string
Secret *api.Secret
return secret, nil
}

type TLSConfig struct {
Expand Down