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

Auth token renewal #1

Open
wants to merge 4 commits 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.PHONY: docker-build docker-push

export DOCKER_REPO=pyrsh/tbot-vault-bridge
export DOCKER_REPO=registry.pyr.sh/tbot-vault-bridge/tbot-vault-bridge
export GIT_COMMIT=$(shell git rev-parse --short HEAD)

docker-build:
Expand Down
82 changes: 76 additions & 6 deletions cmd/vault-pusher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,17 @@ func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

if err := run(ctx); err != nil {
tokenRenewalDoneCh := make(chan struct{})
err := run(ctx, tokenRenewalDoneCh)
log.Println("Stopping token renewal goroutine...")
<-tokenRenewalDoneCh
if err != nil {
panic(err)
}
log.Println("Exiting")
}

func run(ctx context.Context) error {
func run(ctx context.Context, tokenRenewalDoneCh chan struct{}) error {
// connect to vault
config := vault.DefaultConfig()
transport := config.HttpClient.Transport.(*http.Transport)
Expand All @@ -67,12 +72,66 @@ func run(ctx context.Context) error {
}

// perform vault auth
authInfo, err := client.Auth().Login(ctx, appRoleAuth)
authInfo, err := login(ctx, client, appRoleAuth)
if err != nil {
return fmt.Errorf("failed to log in using the approle method: %w", err)
return err
}
if authInfo == nil {
return errors.New("auth method did not return valid credentials")

// And start token renewal lifecycle
// Reference: https://github.com/hashicorp/vault-examples/blob/main/examples/token-renewal/go/example.go
if authInfo.Auth.Renewable {
go func() {
defer func() { tokenRenewalDoneCh <- struct{}{} }()
log.Println("Starting auto renewal goroutine")
for {
select {
case <-ctx.Done():
return
default:
}

if authInfo == nil {
log.Println("Reattempting login")
authInfo, err = login(ctx, client, appRoleAuth)
if err != nil {
log.Printf("Failed to reattempt login: %v", err)
continue
}
}

watcher, err := client.NewLifetimeWatcher(&vault.LifetimeWatcherInput{
Secret: authInfo,
Increment: 60 * 60 * 24,
})
if err != nil {
log.Printf("unable to initialize new lifetime watcher for renewing auth token: %v", err)
continue
}

go watcher.Start()
defer watcher.Stop()
renewalLoop:
for {
select {
case <-ctx.Done():
return
case err := <-watcher.DoneCh():
if err != nil {
log.Printf("Failed to renew token: %v", err)
} else {
log.Printf("Token can no longer be renewed")
}
authInfo = nil
watcher.Stop()
break renewalLoop
case renewal := <-watcher.RenewCh():
log.Printf("Successfully renewed: %#v", renewal)
}
}
}
}()
} else {
tokenRenewalDoneCh <- struct{}{}
}

mux := http.NewServeMux()
Expand Down Expand Up @@ -159,3 +218,14 @@ func run(ctx context.Context) error {
log.Println("Shutting down...")
return nil
}

func login(ctx context.Context, client *vault.Client, appRoleAuth *auth.AppRoleAuth) (*vault.Secret, error) {
authInfo, err := client.Auth().Login(ctx, appRoleAuth)
if err != nil {
return nil, fmt.Errorf("failed to log in using the approle method: %w", err)
}
if authInfo == nil {
return nil, errors.New("auth method did not return valid credentials")
}
return authInfo, nil
}