Skip to content

Commit

Permalink
[feature] enable honeycomb server-side tracing (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
kuannie1 authored Jun 25, 2020
1 parent c977fc3 commit 078ee6b
Show file tree
Hide file tree
Showing 13 changed files with 391 additions and 30 deletions.
29 changes: 22 additions & 7 deletions cmd/creds-process.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/chanzuckerberg/aws-oidc/pkg/aws_config_client"
"github.com/chanzuckerberg/aws-oidc/pkg/getter"
oidc "github.com/chanzuckerberg/go-misc/oidc_cli"
oidc_client "github.com/chanzuckerberg/go-misc/oidc_cli/client"
"github.com/honeycombio/beeline-go"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -82,18 +84,31 @@ func assumeRole(
awsOIDCConfig *aws_config_client.AWSOIDCConfiguration,
sessionDuration time.Duration,
) (*sts.AssumeRoleWithWebIdentityOutput, error) {
token, err := oidc.GetToken(
ctx,
awsOIDCConfig.ClientID,
awsOIDCConfig.IssuerURL)
ctx, span := beeline.StartSpan(ctx, "assumeAWSRole")
defer span.Send()

token, err := getOIDCToken(ctx, awsOIDCConfig)
if err != nil {
return nil, errors.Wrap(err, "unable to obtain OIDC token")
return nil, err
}
assumeRoleOutput, err := getter.GetAWSAssumeIdentity(

return getter.GetAWSAssumeIdentity(
ctx,
token,
awsOIDCConfig.RoleARN,
sessionDuration,
)
return assumeRoleOutput, errors.Wrap(err, "unable to assume role")
}

func getOIDCToken(
ctx context.Context,
awsOIDCConfig *aws_config_client.AWSOIDCConfiguration,
) (*oidc_client.Token, error) {
ctx, span := beeline.StartSpan(ctx, "get_oidc_token")
defer span.Send()

return oidc.GetToken(
ctx,
awsOIDCConfig.ClientID,
awsOIDCConfig.IssuerURL)
}
5 changes: 4 additions & 1 deletion cmd/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/chanzuckerberg/aws-oidc/pkg/aws_config_client"
"github.com/honeycombio/beeline-go"
"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -41,7 +42,9 @@ var envCmd = &cobra.Command{
}

func envRun(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
ctx, span := beeline.StartSpan(cmd.Context(), "env_command")
defer span.Send()

awsOIDCConfig, err := aws_config_client.FetchParamsFromAWSConfig(
cmd,
aws_config_client.DefaultAWSConfigPath)
Expand Down
45 changes: 44 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/evalphobia/logrus_sentry"
"github.com/honeycombio/beeline-go"
"github.com/kelseyhightower/envconfig"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -32,6 +33,21 @@ func loadSentryEnv() (*SentryEnvironment, error) {
return env, nil
}

type HoneycombEnvironment struct {
SECRET_KEY string
DATASET_NAME string `default:"aws-oidc"`
SERVICE_NAME string `default:"aws-oidc"`
}

func loadHoneycombEnv() (*HoneycombEnvironment, error) {
env := &HoneycombEnvironment{}
err := envconfig.Process("HONEYCOMB", env)
if err != nil {
return env, errors.Wrap(err, "Unable to load all the honeycomb environment variables")
}
return env, nil
}

func init() {
rootCmd.PersistentFlags().BoolP(flagVerbose, "v", false, "Use this to enable verbose mode")
}
Expand All @@ -49,22 +65,29 @@ var rootCmd = &cobra.Command{
log.SetLevel(log.DebugLevel)
log.SetReportCaller(true)
}

err = configureLogrusHooks()
if err != nil {
return errors.Wrap(err, "Unable to configure Logrus Hooks")
}

err = configureHoneycombTelemetry()
if err != nil {
return errors.Wrap(err, "Unable to set up Honeycomb Telemetry")
}

return nil
},
}

func configureLogrusHooks() error {
// Load Sentry Env
sentryEnv, err := loadSentryEnv()
if err != nil {
return err
}
// if env var not set, ignore
if sentryEnv.DSN == "" {
logrus.Debug("Sentry DSN not set. Skipping Sentry Configuration")
return nil
}

Expand All @@ -81,6 +104,26 @@ func configureLogrusHooks() error {
return nil
}

func configureHoneycombTelemetry() error {
honeycombEnv, err := loadHoneycombEnv()
if err != nil {
return err
}
// if env var not set, ignore
if honeycombEnv.SECRET_KEY == "" {
logrus.Debug("Honeycomb Secret Key not set. Skipping Honeycomb Configuration")
return nil
}
beeline.Init(beeline.Config{
WriteKey: honeycombEnv.SECRET_KEY,
Dataset: honeycombEnv.DATASET_NAME,
ServiceName: honeycombEnv.SERVICE_NAME,
})

return nil
}

func Execute(ctx context.Context) error {
defer beeline.Close()
return rootCmd.ExecuteContext(ctx)
}
8 changes: 4 additions & 4 deletions cmd/serve-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (

"github.com/aws/aws-sdk-go/aws/session"
webserver "github.com/chanzuckerberg/aws-oidc/pkg/aws_config_server"
"github.com/chanzuckerberg/aws-oidc/pkg/okta"
CZIOkta "github.com/chanzuckerberg/aws-oidc/pkg/okta"
"github.com/coreos/go-oidc"
"github.com/kelseyhightower/envconfig"
Expand Down Expand Up @@ -46,7 +45,7 @@ func loadOktaEnv() (*OktaWebserverEnvironment, error) {
env := &OktaWebserverEnvironment{}
err := envconfig.Process("OKTA", env)
if err != nil {
return env, errors.Wrap(err, "Unable to load all the environment variables")
return env, errors.Wrap(err, "Unable to load all the okta environment variables")
}
return env, nil
}
Expand All @@ -55,12 +54,12 @@ func loadAWSEnv() (*AWSEnvironment, error) {
env := &AWSEnvironment{}
err := envconfig.Process("AWS", env)
if err != nil {
return env, errors.Wrap(err, "Unable to load all the environment variables")
return env, errors.Wrap(err, "Unable to load all the aws environment variables")
}
return env, nil
}

func createOktaClientApps(ctx context.Context, orgURL, privateKey, oktaClientID string) (okta.AppResource, error) {
func createOktaClientApps(ctx context.Context, orgURL, privateKey, oktaClientID string) (CZIOkta.AppResource, error) {
oktaConfig := &CZIOkta.OktaClientConfig{
ClientID: oktaClientID,
PrivateKeyPEM: privateKey,
Expand All @@ -76,6 +75,7 @@ func createOktaClientApps(ctx context.Context, orgURL, privateKey, oktaClientID
func serveConfigRun(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()

// Initialize everything else
oktaEnv, err := loadOktaEnv()
if err != nil {
return err
Expand Down
9 changes: 6 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.14

require (
github.com/AlecAivazis/survey/v2 v2.0.7
github.com/aws/aws-sdk-go v1.32.8
github.com/aws/aws-sdk-go v1.32.10
github.com/blang/semver v3.5.1+incompatible
github.com/certifi/gocertifi v0.0.0-20200211180108-c7c1fbc02894 // indirect
github.com/chanzuckerberg/go-misc v0.0.0-20200622164707-c19d1f2e9ea7
Expand All @@ -13,9 +13,10 @@ require (
github.com/getsentry/raven-go v0.2.0 // indirect
github.com/golang/mock v1.4.3
github.com/gorilla/handlers v1.4.2
github.com/honeycombio/beeline-go v0.5.1
github.com/julienschmidt/httprouter v1.3.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/mattn/go-colorable v0.1.6 // indirect
github.com/klauspost/compress v1.10.10 // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/okta/okta-sdk-golang/v2 v2.0.0
github.com/peterhellberg/link v1.1.0
Expand All @@ -25,8 +26,10 @@ require (
github.com/spf13/cobra v1.0.0
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9 // indirect
golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 // indirect
golang.org/x/text v0.3.3 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/ini.v1 v1.57.0
gopkg.in/yaml.v2 v2.3.0 // indirect
)
Loading

0 comments on commit 078ee6b

Please sign in to comment.