-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from carolynvs/user-agent-env-var
Set azure user agent environment variable
- Loading branch information
Showing
12 changed files
with
184 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,54 @@ | ||
package az | ||
|
||
import ( | ||
"strconv" | ||
"strings" | ||
|
||
"get.porter.sh/porter/pkg" | ||
) | ||
|
||
const AZURE_HTTP_USER_AGENT = "AZURE_HTTP_USER_AGENT" | ||
const ( | ||
// AzureUserAgentEnvVar is the environment variable used by the az CLI to set | ||
// the user agent string sent to Azure. | ||
AzureUserAgentEnvVar = "AZURE_HTTP_USER_AGENT" | ||
|
||
// UserAgentOptOutEnvVar is the name of the environment variable that disables | ||
// user agent reporting. | ||
UserAgentOptOutEnvVar = "PORTER_AZ_MIXIN_USER_AGENT_OPT_OUT" | ||
) | ||
|
||
// SetUserAgent sets the AZURE_HTTP_USER_AGENT environment variable with | ||
// the full user agent string, which includes both a portion for porter and the | ||
// mixin. | ||
func (m *Mixin) SetUserAgent() { | ||
value := []string{pkg.UserAgent(), m.UserAgent()} | ||
// Check if PORTER_AZ_MIXIN_USER_AGENT_OPT_OUT=true, which disables editing the user agent string | ||
if optOut, _ := strconv.ParseBool(m.Getenv(UserAgentOptOutEnvVar)); optOut { | ||
return | ||
} | ||
|
||
if agentStr, ok := m.LookupEnv(AZURE_HTTP_USER_AGENT); ok { | ||
// Check if we have already set the user agent | ||
if m.userAgent != "" { | ||
return | ||
} | ||
|
||
// Append porter and the mixin's version to the user agent string. Some clouds and | ||
// environments will have set the environment variable already and we don't want | ||
// to clobber it. | ||
porterUserAgent := pkg.UserAgent() | ||
value := []string{porterUserAgent, m.GetMixinUserAgent()} | ||
if agentStr, ok := m.LookupEnv(AzureUserAgentEnvVar); ok { | ||
value = append(value, agentStr) | ||
} | ||
|
||
m.Setenv(AZURE_HTTP_USER_AGENT, strings.Join(value, " ")) | ||
m.userAgent = strings.Join(value, " ") | ||
|
||
// Set the az CLI user agent as an environment variable so that when we call the | ||
// az CLI, it's automatically passed too. | ||
m.Setenv(AzureUserAgentEnvVar, m.userAgent) | ||
} | ||
|
||
func (m *Mixin) UserAgent() string { | ||
// GetMixinUserAgent returns the portion of the user agent string for the mixin. | ||
func (m *Mixin) GetMixinUserAgent() string { | ||
v := m.Version() | ||
return "getporter/" + v.Name + "/" + v.Version | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,81 @@ | ||
package az | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"get.porter.sh/mixin/az/pkg" | ||
"get.porter.sh/porter/pkg/runtime" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSetUserAgent(t *testing.T) { | ||
pkg.Commit = "abc123" | ||
pkg.Version = "v1.2.3" | ||
func TestMixinSetsUserAgentEnvVar(t *testing.T) { | ||
// CI sets this value and we need to clear it out to make the test reproducible | ||
os.Unsetenv(AzureUserAgentEnvVar) | ||
|
||
m := NewTestMixin(t) | ||
m.SetUserAgent() | ||
t.Run("sets env var", func(t *testing.T) { | ||
pkg.Commit = "abc123" | ||
pkg.Version = "v1.2.3" | ||
|
||
expected := "getporter/porter getporter/az/" + pkg.Version | ||
require.Contains(t, m.Getenv(AZURE_HTTP_USER_AGENT), expected) | ||
m := New() | ||
expected := "getporter/porter getporter/az/" + pkg.Version | ||
require.Equal(t, expected, m.Getenv(AzureUserAgentEnvVar)) | ||
require.Equal(t, expected, m.userAgent, "validate we remember the user agent string for later") | ||
}) | ||
|
||
t.Run("edits env var", func(t *testing.T) { | ||
// Validate that if the user customizations of the env var are preserved | ||
pkg.Commit = "abc123" | ||
pkg.Version = "v1.2.3" | ||
|
||
cfg := runtime.NewConfig() | ||
customUserAgent := "mycustom/v1.2.3" | ||
cfg.Setenv(AzureUserAgentEnvVar, customUserAgent) | ||
m := NewFor(cfg) | ||
|
||
expected := "getporter/porter getporter/az/v1.2.3 mycustom/v1.2.3" | ||
require.Equal(t, expected, m.Getenv(AzureUserAgentEnvVar)) | ||
require.Equal(t, expected, m.userAgent, "validate we remember the user agent string for later") | ||
}) | ||
|
||
t.Run("call multiple times", func(t *testing.T) { | ||
// Validate that calling multiple times doesn't make a messed up env var | ||
pkg.Commit = "abc123" | ||
pkg.Version = "v1.2.3" | ||
|
||
m := New() | ||
m.SetUserAgent() | ||
m.SetUserAgent() | ||
|
||
expected := "getporter/porter getporter/az/v1.2.3" | ||
require.Equal(t, expected, m.Getenv(AzureUserAgentEnvVar)) | ||
require.Equal(t, expected, m.userAgent, "validate we remember the user agent string for later") | ||
}) | ||
} | ||
|
||
func TestMixinSetsUserAgentEnvVar_OptOut(t *testing.T) { | ||
// CI sets this value and we need to clear it out to make the test reproducible | ||
os.Unsetenv(AzureUserAgentEnvVar) | ||
|
||
t.Run("opt-out", func(t *testing.T) { | ||
// Validate that at runtime when we are calling the az cli, that if the bundle author opted-out, we don't set the user agent string | ||
cfg := runtime.NewConfig() | ||
cfg.Setenv(UserAgentOptOutEnvVar, "true") | ||
m := NewFor(cfg) | ||
|
||
_, hasEnv := m.LookupEnv(AzureUserAgentEnvVar) | ||
require.False(t, hasEnv, "expected the opt out to skip setting the AZURE_HTTP_USER_AGENT environment variable") | ||
}) | ||
|
||
t.Run("opt-out preserves original value", func(t *testing.T) { | ||
// Validate that at runtime when we are calling the az cli, that if the bundle author opted-out, we don't set the user agent string | ||
cfg := runtime.NewConfig() | ||
cfg.Setenv(UserAgentOptOutEnvVar, "true") | ||
customUserAgent := "mycustom/v1.2.3" | ||
cfg.Setenv(AzureUserAgentEnvVar, customUserAgent) | ||
m := NewFor(cfg) | ||
|
||
require.Equal(t, customUserAgent, m.Getenv(AzureUserAgentEnvVar), "expected opting out to not prevent the user from setting a custom user agent") | ||
require.Empty(t, m.userAgent, "validate we remember that we opted out") | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
config: | ||
userAgentOptOut: true | ||
clientVersion: 1.2.3 | ||
extensions: | ||
- iot | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters