-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
25ce5b3
commit a374f93
Showing
7 changed files
with
725 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package cli | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"time" | ||
|
||
"github.com/peterbourgon/ff/v3/ffcli" | ||
|
||
"github.com/philips-labs/spiffe-vault/pkg/spiffe" | ||
"github.com/philips-labs/spiffe-vault/pkg/vault" | ||
) | ||
|
||
func Auth() *ffcli.Command { | ||
var ( | ||
flagset = flag.NewFlagSet("spiffe-vault version", flag.ExitOnError) | ||
authPath = flagset.String("authPath", "jwt", "the authentication path in Vault (default: jwt)") | ||
role = flagset.String("role", "", "the role to authenticate with against Vault") | ||
) | ||
return &ffcli.Command{ | ||
Name: "auth", | ||
FlagSet: flagset, | ||
Exec: func(ctx context.Context, args []string) error { | ||
if *role == "" { | ||
return fmt.Errorf("role flag required") | ||
} | ||
|
||
if *authPath == "" { | ||
return fmt.Errorf("authPath flag required") | ||
} | ||
|
||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second) | ||
defer cancel() | ||
|
||
jwt, err := spiffe.FetchJWT(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c, err := vault.NewClient(*authPath) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.Authenticate(jwt, *role) | ||
|
||
fmt.Println("# Export following environment variable to authenticate to Hashicorp Vault") | ||
fmt.Printf("export VAULT_TOKEN=%s\n", c.Token()) | ||
|
||
return nil | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.