Skip to content

Commit

Permalink
Merge pull request #55 from GESkunkworks/feature/user-agent
Browse files Browse the repository at this point in the history
set user agent header to gossamer3
  • Loading branch information
Squwid authored Oct 28, 2020
2 parents 8dec79f + 243a9c8 commit d174298
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 4 deletions.
6 changes: 5 additions & 1 deletion cmd/gossamer3/commands/bulk_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ func BulkLogin(loginFlags *flags.LoginExecFlags) error {

// If no creds are expired, return to sender, no work to be done
if noCredsExpired {
logger.Infof("No credentials expired")
logger.Infof("Credentials are not expired (use --force to login anyways)")
return nil
}

Expand Down Expand Up @@ -658,6 +658,10 @@ func assumeRole(parentCreds *awsconfig.AWSCredentials, roleArn string, roleSessi
if err != nil {
return nil, errors.Wrap(err, "failed to create session")
}

// Set user agent handler
awsconfig.OverrideUserAgent(sess)

svc := sts.New(sess)

// Generate input
Expand Down
3 changes: 3 additions & 0 deletions cmd/gossamer3/commands/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,9 @@ func getRoleSessionNameFromCredentials(account *cfg.IDPAccount, awsCreds *awscon
return "", errors.Wrap(err, "failed to create session")
}

// Set user agent handler
awsconfig.OverrideUserAgent(sess)

// Call to STS Get Caller Identity
svc := sts.New(sess)
resp, err := svc.GetCallerIdentity(&sts.GetCallerIdentityInput{})
Expand Down
6 changes: 6 additions & 0 deletions cmd/gossamer3/commands/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ func assumeRoleWithProfile(targetProfile string, sessionDuration int) (*awsconfi
AssumeRoleDuration: duration,
}))

// Set user agent handler
awsconfig.OverrideUserAgent(sess)

// use an STS client to perform the multiple role assumptions
stsClient := sts.New(sess)
input := &sts.GetCallerIdentityInput{}
Expand Down Expand Up @@ -144,6 +147,9 @@ func checkToken(account *cfg.IDPAccount) (bool, error) {
return false, err
}

// Set user agent handler
awsconfig.OverrideUserAgent(sess)

svc := sts.New(sess)

params := &sts.GetCallerIdentityInput{}
Expand Down
5 changes: 4 additions & 1 deletion cmd/gossamer3/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Login(loginFlags *flags.LoginExecFlags) error {
}

if !sharedCreds.Expired() && !loginFlags.Force {
log.Println("credentials are not expired skipping")
log.Println("Credentials are not expired (use --force to login anyways)")
return nil
}

Expand Down Expand Up @@ -286,6 +286,9 @@ func loginToStsUsingRole(role *g3.AWSRole, sessionDuration int, samlAssertion, r
return nil, errors.Wrap(err, "failed to create session")
}

// Set user agent handler
awsconfig.OverrideUserAgent(sess)

svc := sts.New(sess)

params := &sts.AssumeRoleWithSAMLInput{
Expand Down
3 changes: 3 additions & 0 deletions cmd/gossamer3/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"os"
"runtime"

"github.com/GESkunkworks/gossamer3/pkg/cfg"

"github.com/GESkunkworks/gossamer3/cmd/gossamer3/commands"
"github.com/GESkunkworks/gossamer3/pkg/flags"
"github.com/alecthomas/kingpin"
Expand Down Expand Up @@ -59,6 +61,7 @@ func main() {

app := kingpin.New("gossamer3", "A command line tool to help with SAML access to the AWS token service.")
app.Version(Version)
cfg.Version = Version

// Settings not related to commands
verbose := app.Flag("verbose", "Enable verbose logging").Bool()
Expand Down
10 changes: 10 additions & 0 deletions pkg/awsconfig/awsconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ import (
"runtime"
"time"

"github.com/GESkunkworks/gossamer3/pkg/cfg"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"

homedir "github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -47,6 +51,12 @@ type CredentialsProvider struct {
Profile string
}

func OverrideUserAgent(sess *session.Session) {
sess.Handlers.Build.PushBack(func(r *request.Request) {
r.HTTPRequest.Header.Set("User-Agent", cfg.GetUserAgent())
})
}

// NewSharedCredentials helper to create the credentials provider
func NewSharedCredentials(profile string) *CredentialsProvider {
return &CredentialsProvider{
Expand Down
7 changes: 7 additions & 0 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import (
"io/ioutil"
"net/url"
"os"
"runtime"

"github.com/mitchellh/go-homedir"
"github.com/pkg/errors"
yaml "gopkg.in/yaml.v3"
)

var Version string

// ErrIdpAccountNotFound returned if the idp account is not found in the configuration file
var ErrIdpAccountNotFound = errors.New("IDP account not found, run configure to set it up")

Expand Down Expand Up @@ -56,6 +59,10 @@ type IDPAccount struct {
HttpRetryDelay string `yaml:"http_retry_delay"`
}

func GetUserAgent() string {
return fmt.Sprintf("gossamer3/%s (%s; %s; %s)", Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
}

func (ia IDPAccount) String() string {
return fmt.Sprintf(`account {
Name: %s
Expand Down
3 changes: 1 addition & 2 deletions pkg/provider/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ func NewHTTPClient(tr http.RoundTripper, opts *HTTPClientOptions) (*HTTPClient,

// Do do the request
func (hc *HTTPClient) Do(req *http.Request) (*http.Response, error) {

//req.Header.Set("User-Agent", fmt.Sprintf("gossamer3/1.0 (%s %s)", runtime.GOOS, runtime.GOARCH))
req.Header.Set("User-Agent", cfg.GetUserAgent())

var resp *http.Response
var err error
Expand Down

0 comments on commit d174298

Please sign in to comment.