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

Capture account and user name on traces #412

Merged
merged 2 commits into from
Jun 19, 2024
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
4 changes: 1 addition & 3 deletions cmd/tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type FinalReportingModel interface {
func CmdWrapper(action string, requiredScopes []string, commandModel func(args []string, parent *cmdModel, width int) tea.Model) func(cmd *cobra.Command, args []string) {
return func(cmd *cobra.Command, args []string) {
// set up a context for the command
ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(cmd.Context())
defer cancel()

cmdName := fmt.Sprintf("CLI %v", cmd.CommandPath())
Expand Down Expand Up @@ -92,8 +92,6 @@ func CmdWrapper(action string, requiredScopes []string, commandModel func(args [

// wrap the rest of the function in a closure to allow for cleaner error handling and deferring.
err := func() error {
ctx := cmd.Context()

timeout, err := time.ParseDuration(viper.GetString("timeout"))
if err != nil {
return flagError{usage: fmt.Sprintf("invalid --timeout value '%v'\n\n%v", viper.GetString("timeout"), cmd.UsageString())}
Expand Down
24 changes: 20 additions & 4 deletions cmd/tea_ensuretoken.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import (

"connectrpc.com/connect"
tea "github.com/charmbracelet/bubbletea"
"github.com/go-jose/go-jose/v4"
josejwt "github.com/go-jose/go-jose/v4/jwt"
"github.com/overmindtech/sdp-go"
"github.com/pkg/browser"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -340,9 +342,6 @@ func (m ensureTokenModel) awaitTokenCmd() tea.Msg {
}
}

span := trace.SpanFromContext(m.ctx)
span.SetAttributes(attribute.Bool("ovm.cli.authenticated", true))

// Save the token locally
home, err := os.UserHomeDir()
if err != nil {
Expand Down Expand Up @@ -470,8 +469,25 @@ func getOauthToken(ctx context.Context, oi OvermindInstance, requiredScopes []st
os.Exit(1)
}

tok, err := josejwt.ParseSigned(m.token.AccessToken, []jose.SignatureAlgorithm{jose.RS256})
if err != nil {
fmt.Println("Error running program: received invalid token:", err)
os.Exit(1)
}
out := josejwt.Claims{}
customClaims := sdp.CustomClaims{}
err = tok.UnsafeClaimsWithoutVerification(&out, &customClaims)
if err != nil {
fmt.Println("Error running program: received unparsable token:", err)
os.Exit(1)
}

span := trace.SpanFromContext(ctx)
span.SetAttributes(attribute.Bool("ovm.cli.authenticated", true))
span.SetAttributes(
attribute.Bool("ovm.cli.authenticated", true),
attribute.String("ovm.cli.accountName", customClaims.AccountName),
attribute.String("ovm.cli.userId", out.Subject),
)

// Save the token locally
if home, err := os.UserHomeDir(); err == nil {
Expand Down
20 changes: 20 additions & 0 deletions cmd/tea_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

tea "github.com/charmbracelet/bubbletea"
"github.com/getsentry/sentry-go"
"github.com/go-jose/go-jose/v4"
josejwt "github.com/go-jose/go-jose/v4/jwt"
"github.com/overmindtech/sdp-go"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
Expand Down Expand Up @@ -211,6 +213,24 @@ func (m *cmdModel) tokenChecks(token *oauth2.Token) tea.Cmd {
// some of the models require access to viper (for GetConfig/SetConfig) or
// contortions to store that data somewhere else.
return func() tea.Msg {
tok, err := josejwt.ParseSigned(token.AccessToken, []jose.SignatureAlgorithm{jose.RS256})
if err != nil {
return fatalError{err: fmt.Errorf("received invalid token: %w", err)}
}
out := josejwt.Claims{}
customClaims := sdp.CustomClaims{}
err = tok.UnsafeClaimsWithoutVerification(&out, &customClaims)
if err != nil {
return fatalError{err: fmt.Errorf("received unparsable token: %w", err)}
}

span := trace.SpanFromContext(m.ctx)
span.SetAttributes(
attribute.Bool("ovm.cli.authenticated", true),
attribute.String("ovm.cli.accountName", customClaims.AccountName),
attribute.String("ovm.cli.userId", out.Subject),
)

return loadSourcesConfigMsg{
ctx: m.ctx,
oi: m.oi,
Expand Down
Loading