Skip to content
This repository has been archived by the owner on May 18, 2021. It is now read-only.

Commit

Permalink
Merge pull request #39 from segmentio/add-analytics
Browse files Browse the repository at this point in the history
Add analytics
  • Loading branch information
dfuentes committed Apr 10, 2018
2 parents 450ef50 + 4d54de7 commit 5e2d76e
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 13 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ $ export CIRCLE_TAG=`git describe --tags`
$ make release-mac
```

## Analytics

`aws-okta` includes some usage analytics code which Segment uses internally for tracking usage of internal tools. This analytics code is turned off by default, and can only be enabled via a linker flag at build time, which we do not set for public github releases.

## Internals

### Authentication process
Expand Down
12 changes: 12 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
log "github.com/Sirupsen/logrus"

"github.com/99designs/keyring"
analytics "github.com/segmentio/analytics-go"
"github.com/segmentio/aws-okta/lib"
"github.com/spf13/cobra"
)
Expand All @@ -33,6 +34,17 @@ func add(cmd *cobra.Command, args []string) error {
log.Fatal(err)
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("backend", backend).
Set("aws-okta-version", version).
Set("command", "add"),
})
}

// Ask username password from prompt
organization, err := lib.Prompt("Okta organization", false)
if err != nil {
Expand Down
13 changes: 13 additions & 0 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

"github.com/99designs/keyring"
analytics "github.com/segmentio/analytics-go"
"github.com/segmentio/aws-okta/lib"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -115,6 +116,18 @@ func execRun(cmd *cobra.Command, args []string) error {
return err
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("backend", backend).
Set("aws-okta-version", version).
Set("profile", profile).
Set("command", "exec"),
})
}

p, err := lib.NewProvider(kr, profile, opts)
if err != nil {
return err
Expand Down
13 changes: 13 additions & 0 deletions cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"net/url"

"github.com/99designs/keyring"
analytics "github.com/segmentio/analytics-go"
"github.com/segmentio/aws-okta/lib"
"github.com/skratchdot/open-golang/open"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -67,6 +68,18 @@ func loginRun(cmd *cobra.Command, args []string) error {
return err
}

if analyticsEnabled && analyticsClient != nil {
analyticsClient.Enqueue(analytics.Track{
UserId: username,
Event: "Ran Command",
Properties: analytics.NewProperties().
Set("backend", backend).
Set("aws-okta-version", version).
Set("profile", profile).
Set("command", "login"),
})
}

p, err := lib.NewProvider(kr, profile, opts)
if err != nil {
return err
Expand Down
46 changes: 37 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/99designs/keyring"
log "github.com/Sirupsen/logrus"
analytics "github.com/segmentio/analytics-go"
"github.com/spf13/cobra"
)

Expand All @@ -20,24 +21,31 @@ var (

// global flags
var (
backend string
debug bool
version string
backend string
debug bool
version string
analyticsWriteKey string
analyticsEnabled bool
analyticsClient analytics.Client
username string
)

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Use: "aws-okta",
Short: "aws-okta allows you to authenticate with AWS using your okta credentials",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: prerun,
Use: "aws-okta",
Short: "aws-okta allows you to authenticate with AWS using your okta credentials",
SilenceUsage: true,
SilenceErrors: true,
PersistentPreRun: prerun,
PersistentPostRun: postrun,
}

// Execute adds all child commands to the root command sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute(vers string) {
func Execute(vers string, writeKey string) {
version = vers
analyticsWriteKey = writeKey
analyticsEnabled = analyticsWriteKey != ""
if err := RootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
switch err {
Expand All @@ -60,6 +68,26 @@ func prerun(cmd *cobra.Command, args []string) {
if debug {
log.SetLevel(log.DebugLevel)
}

if analyticsEnabled {
// set up analytics client
analyticsClient, _ = analytics.NewWithConfig(analyticsWriteKey, analytics.Config{
BatchSize: 1,
})

username = os.Getenv("USER")
analyticsClient.Enqueue(analytics.Identify{
UserId: username,
Traits: analytics.NewTraits().
Set("aws-okta-version", version),
})
}
}

func postrun(cmd *cobra.Command, args []string) {
if analyticsEnabled && analyticsClient != nil {
analyticsClient.Close()
}
}

func init() {
Expand Down
12 changes: 8 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package main

import "github.com/segmentio/aws-okta/cmd"
import (
"github.com/segmentio/aws-okta/cmd"
)

// These are set via linker flags
var (
// This is updated via linker flags
Version = "dev"
Version = "dev"
AnalyticsWriteKey = ""
)

func main() {
cmd.Execute(Version)
// vars set by linker flags must be strings...
cmd.Execute(Version, AnalyticsWriteKey)
}
26 changes: 26 additions & 0 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,12 @@
"revision": "76626ae9c91c4f2a10f34cad8ce83ea42c93bb75",
"revisionTime": "2014-10-17T20:07:13Z"
},
{
"checksumSHA1": "WbdgSkC2Tbn1Fs1WAAvXPHBvkEk=",
"path": "github.com/jehiah/go-strftime",
"revision": "1d33003b386959af197ba96475f198c114627b5e",
"revisionTime": "2017-12-01T14:10:54Z"
},
{
"checksumSHA1": "0ZrwvB6KoGPj2PoDNSEJwxQ6Mog=",
"origin": "github.com/aws/aws-sdk-go/vendor/github.com/jmespath/go-jmespath",
Expand All @@ -245,6 +251,20 @@
"revision": "b8bc1bf767474819792c23f32d8286a45736f1c6",
"revisionTime": "2016-12-03T19:45:07Z"
},
{
"checksumSHA1": "8xpFdZVp0QVAhbq20SJEb4qTcEk=",
"path": "github.com/segmentio/analytics-go",
"revision": "4f08212e9b32c48b4a4d20997d3f90dc0b56eba8",
"revisionTime": "2018-03-19T16:54:24Z",
"version": "v3.0",
"versionExact": "v3.0"
},
{
"checksumSHA1": "jK8cjKr2eA3JiQP+T4HLjQRV5ak=",
"path": "github.com/segmentio/backo-go",
"revision": "204274ad699c0983a70203a566887f17a717fef4",
"revisionTime": "2016-04-24T05:23:52Z"
},
{
"checksumSHA1": "h/HMhokbQHTdLUbruoBBTee+NYw=",
"path": "github.com/skratchdot/open-golang/open",
Expand All @@ -269,6 +289,12 @@
"revision": "a98ad7ee00ec53921f08832bc06ecf7fd600e6a1",
"revisionTime": "2013-09-23T14:52:12Z"
},
{
"checksumSHA1": "LAL/r7KfCdqp4M6MM13+7NWsUNc=",
"path": "github.com/xtgo/uuid",
"revision": "a0b114877d4caeffbd7f87e3757c17fce570fea7",
"revisionTime": "2014-08-04T02:12:11Z"
},
{
"checksumSHA1": "ZaU56svwLgiJD0y8JOB3+/mpYBA=",
"path": "golang.org/x/crypto/ssh/terminal",
Expand Down

0 comments on commit 5e2d76e

Please sign in to comment.