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

[feature] Add a token command to retrieve your oauth tokens #186

Merged
merged 1 commit into from
Jul 20, 2020
Merged
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
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,31 @@ You can check your version with
ssh -V
```

## Commands

### run
`run` will run blessclient and attempt to fetch an SSH certificate from the CA. It requires blessclient to be properly configured beforehand.

### import-config
`import-config` will import blessclient configuration from a remote location and configure your local blessclient.

### token
`token` will print, json formatted, your oauth2/oidc id_token and access_token. This command requires blessclient to be properly configured beforehand. This command is not typically part of a common workflow.

The output will be written to stdout. The output is json formatted and looks like
```json
{
"version": 1,
"id_token": "<string>",
"access_token": "<string>",
"expiry": "2020-07-20T12:18:02-04:00"
}
```
When running this command, no other output will be written to stdout.

### version
`version` will print blessclient's version.

## Other
### Deploying BLESS
There are already [several](https://github.com/lyft/python-blessclient#run-a-bless-lambda-in-aws) [great](http://marcyoung.us/post/bless-part1/) [guides](https://www.tastycidr.net/a-practical-guide-to-deploying-netflixs-bless-certificate-authority/) on how to run a BLESS lambda. If you take a moment to skim through these, you'll notice that setting up a successful BLESS deployment requires thorough knowledge of AWS Lambda and IAM. Even then, you'll probably spend hours digging through CloudWatch logs (and who likes doing that).
Expand Down
66 changes: 66 additions & 0 deletions cmd/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package cmd

import (
"encoding/json"
"fmt"
"os"
"time"

"github.com/chanzuckerberg/blessclient/pkg/config"
oidc "github.com/chanzuckerberg/go-misc/oidc_cli"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(tokenCmd)
}

const (
stdoutTokenVersion = 1
)

type stdoutToken struct {
Version int `json:"version,omitempty"`

IDToken string `json:"id_token,omitempty"`
AccessToken string `json:"access_token,omitempty"`
Expiry time.Time `json:"expiry,omitempty"`
}

var tokenCmd = &cobra.Command{
Use: "token",
Short: "token prints the oidc tokens to stdout",
SilenceErrors: true,
RunE: func(cmd *cobra.Command, args []string) error {
stdoutToken := &stdoutToken{
Version: stdoutTokenVersion,
}

config, err := config.FromFile(config.DefaultConfigFile)
if err != nil {
return err
}

token, err := oidc.GetToken(
cmd.Context(),
config.ClientConfig.OIDCClientID,
config.ClientConfig.OIDCIssuerURL,
)
if err != nil {
return err
}

stdoutToken.AccessToken = token.AccessToken
stdoutToken.IDToken = token.IDToken
stdoutToken.Expiry = token.Expiry

data, err := json.Marshal(stdoutToken)
if err != nil {
return errors.Wrap(err, "could not json marshal oidc token")
}

_, err = fmt.Fprintln(os.Stdout, string(data))
return errors.Wrap(err, "could not print token to stdout")
},
}