Skip to content
Open
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
2 changes: 1 addition & 1 deletion .goreleaser.macos-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ builds:
- -trimpath
- -v
ldflags:
- -s -w -X main.Version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
- -s -w -X 'github.com/versent/saml2aws/v2/pkg/version.Version={{.Version}}'
goos:
- darwin
goarch:
Expand Down
4 changes: 2 additions & 2 deletions .goreleaser.ubuntu-22.04.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ builds:
- -trimpath
- -v
ldflags:
- -s -w -X main.Version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
- -s -w -X 'github.com/versent/saml2aws/v2/pkg/version.Version={{.Version}}'
goos:
- linux
goarch:
Expand Down Expand Up @@ -91,4 +91,4 @@ docker_manifests:
- name_template: ghcr.io/{{ .Env.IMAGE_NAME }}:latest
image_templates:
- ghcr.io/{{ .Env.IMAGE_NAME }}:latest-amd64
- ghcr.io/{{ .Env.IMAGE_NAME }}:latest-arm64
- ghcr.io/{{ .Env.IMAGE_NAME }}:latest-arm64
2 changes: 1 addition & 1 deletion .goreleaser.ubuntu-latest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ builds:
- -trimpath
- -v
ldflags:
- -s -w -X main.Version={{.Version}} -X main.commit={{.Commit}} -X main.date={{.Date}}
- -s -w -X 'github.com/versent/saml2aws/v2/pkg/version.Version={{.Version}}'
goos:
- windows
- linux
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ Flags:
--disable-keychain Do not use keychain at all. This will also disable Okta sessions & remembering MFA device. (env: SAML2AWS_DISABLE_KEYCHAIN)
-r, --region=REGION AWS region to use for API requests, e.g. us-east-1, us-gov-west-1, cn-north-1 (env: SAML2AWS_REGION)
--prompter=PROMPTER The prompter to use for user input (default, pinentry)
--user-agent
--user-agent-override String to append to the user-agent (env: SAML2AWS_USER_AGENT")

Commands:
help [<command>...]
Expand Down
10 changes: 4 additions & 6 deletions cmd/saml2aws/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ import (
"github.com/versent/saml2aws/v2/cmd/saml2aws/commands"
"github.com/versent/saml2aws/v2/pkg/flags"
"github.com/versent/saml2aws/v2/pkg/prompter"
)

var (
// Version app version
Version = "1.0.0"
"github.com/versent/saml2aws/v2/pkg/version"
)

// The `cmdLineList` type is used to make a `[]string` meet the requirements
Expand Down Expand Up @@ -60,7 +56,7 @@ func main() {
}

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

// Settings not related to commands
verbose := app.Flag("verbose", "Enable verbose logging").Bool()
Expand Down Expand Up @@ -92,6 +88,8 @@ func main() {
app.Flag("region", "AWS region to use for API requests, e.g. us-east-1, us-gov-west-1, cn-north-1 (env: SAML2AWS_REGION)").Envar("SAML2AWS_REGION").Short('r').StringVar(&commonFlags.Region)
app.Flag("prompter", "The prompter to use for user input (default, pinentry)").StringVar(&commonFlags.Prompter)
app.Flag("kc-broker", "The kc broker to use when authenticating via keycloak").StringVar(&commonFlags.KCBroker)
app.Flag("user-agent", "String to append to the user-agent (env: SAML2AWS_USER_AGENT").Envar("SAML2AWS_USER_AGENT").StringVar(&commonFlags.UserAgent)
app.Flag("user-agent-override", "String to replace the existing user-agent with (env: SAML2AWS_USER_AGENT_OVERRIDE)").Envar("SAML2AWS_USER_AGENT_OVERRIDE").StringVar(&commonFlags.UserAgentOverride)

// `configure` command and settings
cmdConfigure := app.Command("configure", "Configure a new IDP account.")
Expand Down
2 changes: 2 additions & 0 deletions pkg/cfg/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ type IDPAccount struct {
KCAuthErrorMessage string `ini:"kc_auth_error_message,omitempty"` // used by KeyCloak; hide from user if not set
KCAuthErrorElement string `ini:"kc_auth_error_element,omitempty"` // used by KeyCloak; hide from user if not set
KCBroker string `ini:"kc_broker"` // used by KeyCloak;
UserAgent string `ini:"user_agent"`
UserAgentOverride string `ini:"user_agent_override"`
}

func (ia IDPAccount) String() string {
Expand Down
8 changes: 8 additions & 0 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type CommonFlags struct {
DisableSessions bool
Prompter string
KCBroker string
UserAgent string
UserAgentOverride string
}

// LoginExecFlags flags for the Login / Exec commands
Expand Down Expand Up @@ -156,4 +158,10 @@ func ApplyFlagOverrides(commonFlags *CommonFlags, account *cfg.IDPAccount) {
if commonFlags.Prompter != "" {
account.Prompter = commonFlags.Prompter
}
if commonFlags.UserAgent != "" {
account.UserAgent = commonFlags.UserAgent
}
if commonFlags.UserAgentOverride != "" {
account.UserAgentOverride = commonFlags.UserAgentOverride
}
}
18 changes: 14 additions & 4 deletions pkg/provider/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/versent/saml2aws/v2/pkg/cfg"
"github.com/versent/saml2aws/v2/pkg/cookiejar"
"github.com/versent/saml2aws/v2/pkg/dump"
"github.com/versent/saml2aws/v2/pkg/version"
"golang.org/x/net/publicsuffix"
)

Expand All @@ -32,9 +33,11 @@ const (
)

type HTTPClientOptions struct {
IsWithRetries bool //http retry feature switch
AttemptsCount uint
RetryDelay time.Duration
IsWithRetries bool //http retry feature switch
AttemptsCount uint
RetryDelay time.Duration
UserAgent string
UserAgentOverride string
}

// NewDefaultTransport configure a transport with the TLS skip verify option
Expand Down Expand Up @@ -93,7 +96,14 @@ 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("saml2aws/1.0 (%s %s) Versent", runtime.GOOS, runtime.GOARCH))
req.Header.Set("User-Agent", fmt.Sprintf("saml2aws/%s (%s %s) Versent", version.Version, runtime.GOOS, runtime.GOARCH))

if hc.Options.UserAgent != "" {
req.Header.Set("User-Agent", fmt.Sprintf("%s (%s)", req.UserAgent(), hc.Options.UserAgent))
}
if hc.Options.UserAgentOverride != "" {
req.Header.Set("User-Agent", hc.Options.UserAgent)
}

var resp *http.Response
var err error
Expand Down
48 changes: 48 additions & 0 deletions pkg/provider/http_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package provider

import (
"fmt"
"net/http"
"net/http/httptest"
"runtime"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -72,3 +74,49 @@ func TestClientDoResponseCheck(t *testing.T) {
require.Error(t, err)
require.Equal(t, 400, res.StatusCode)
}

func TestClientDoUserAgent(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, fmt.Sprintf("saml2aws/1.0 (%s %s) Versent (Test)", runtime.GOOS, runtime.GOARCH), r.UserAgent())
_, _ = w.Write([]byte("OK"))
}))
defer ts.Close()

rt := NewDefaultTransport(false)
opts := &HTTPClientOptions{IsWithRetries: false, UserAgent: "Test"}
hc, err := NewHTTPClient(rt, opts)
require.Nil(t, err)

// hc := &HTTPClient{Client: http.Client{}}

req, err := http.NewRequest("GET", ts.URL, nil)
require.Nil(t, err)

res, err := hc.Do(req)
require.Nil(t, err)

require.Equal(t, 200, res.StatusCode)
}

func TestClientDoUserAgentOverride(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "Test", r.UserAgent())
_, _ = w.Write([]byte("OK"))
}))
defer ts.Close()

rt := NewDefaultTransport(false)
opts := &HTTPClientOptions{IsWithRetries: false, UserAgentOverride: "Test"}
hc, err := NewHTTPClient(rt, opts)
require.Nil(t, err)

// hc := &HTTPClient{Client: http.Client{}}

req, err := http.NewRequest("GET", ts.URL, nil)
require.Nil(t, err)

res, err := hc.Do(req)
require.Nil(t, err)

require.Equal(t, 200, res.StatusCode)
}
3 changes: 3 additions & 0 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package version

var Version = "1.0"