chore(provisioner/terraform): preserve existing AWS_SDK_UA_APP_ID (#24606)

Co-authored-by: blink-so[bot] <211532188+blink-so[bot]@users.noreply.github.com>
Co-authored-by: Atif Ali <atif@coder.com>
This commit is contained in:
blinkagent[bot]
2026-05-30 13:05:51 +05:00
committed by GitHub
parent 3a727a9087
commit 9d28489abb
3 changed files with 81 additions and 1 deletions
+1 -1
View File
@@ -381,7 +381,7 @@ func provisionEnv(
"CODER_WORKSPACE_BUILD_ID="+metadata.GetWorkspaceBuildId(),
"CODER_TASK_ID="+metadata.GetTaskId(),
"CODER_TASK_PROMPT="+metadata.GetTaskPrompt(),
"AWS_SDK_UA_APP_ID=APN_1.1/pc_cdfmjwn8i6u8l9fwz8h82e4w3$",
awsSDKUserAgentEnv(safeEnvironValue(env, awsSDKUserAgentEnvKey)),
)
if metadata.GetPrebuiltWorkspaceBuildStage().IsPrebuild() {
env = append(env, provider.IsPrebuildEnvironmentVariable()+"=true")
+36
View File
@@ -53,3 +53,39 @@ func safeEnviron() []string {
}
return strippedEnv
}
// safeEnvironValue returns the value of the named variable in the given
// `KEY=VALUE` environment slice, or an empty string if it is not present.
func safeEnvironValue(env []string, name string) string {
prefix := name + "="
for _, e := range env {
if strings.HasPrefix(e, prefix) {
return strings.TrimPrefix(e, prefix)
}
}
return ""
}
const (
awsSDKUserAgentEnvKey = "AWS_SDK_UA_APP_ID"
// awsSDKUserAgentCoder is Coder's AWS Partner Revenue Measurement
// User-Agent string. The `APN_1.1/pc_<product-code>$` format and the
// space-delimited append behavior below follow AWS's guidance:
// https://docs.aws.amazon.com/PRM/latest/aws-prm-onboarding-guide/automated-user-agent.html
awsSDKUserAgentCoder = "APN_1.1/pc_cdfmjwn8i6u8l9fwz8h82e4w3$"
)
// awsSDKUserAgentEnv returns the AWS_SDK_UA_APP_ID value to pass to the
// Terraform subprocess. If the caller's environment already configures an
// Application ID (e.g. an operator who is also an AWS Partner and wants
// their own revenue attribution), Coder's value is appended with a space
// delimiter so both attributions are preserved. Otherwise Coder's value is
// used on its own.
//
// See: https://docs.aws.amazon.com/PRM/latest/aws-prm-onboarding-guide/automated-user-agent.html
func awsSDKUserAgentEnv(existing string) string {
if existing == "" {
return awsSDKUserAgentEnvKey + "=" + awsSDKUserAgentCoder
}
return awsSDKUserAgentEnvKey + "=" + existing + " " + awsSDKUserAgentCoder
}
@@ -0,0 +1,44 @@
package terraform
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestSafeEnvironValue(t *testing.T) {
t.Parallel()
env := []string{
"FOO=bar",
"AWS_SDK_UA_APP_ID=my-existing-id",
"BAZ=qux",
}
require.Equal(t, "my-existing-id", safeEnvironValue(env, "AWS_SDK_UA_APP_ID"))
require.Equal(t, "bar", safeEnvironValue(env, "FOO"))
require.Equal(t, "", safeEnvironValue(env, "MISSING"))
}
func TestAWSSDKUserAgentEnv(t *testing.T) {
t.Parallel()
t.Run("NoExisting", func(t *testing.T) {
t.Parallel()
require.Equal(t,
"AWS_SDK_UA_APP_ID=APN_1.1/pc_cdfmjwn8i6u8l9fwz8h82e4w3$",
awsSDKUserAgentEnv(""),
)
})
t.Run("AppendToExisting", func(t *testing.T) {
t.Parallel()
// When the operator is themselves an AWS Partner and has set their own
// Application ID, we append Coder's with a space delimiter so both
// attributions are preserved. See:
// https://docs.aws.amazon.com/PRM/latest/aws-prm-onboarding-guide/automated-user-agent.html
require.Equal(t,
"AWS_SDK_UA_APP_ID=EXISTING_APP_ID APN_1.1/pc_cdfmjwn8i6u8l9fwz8h82e4w3$",
awsSDKUserAgentEnv("EXISTING_APP_ID"),
)
})
}