Skip to content
This repository was archived by the owner on Oct 12, 2023. It is now read-only.

Commit ba80f8f

Browse files
authored
feat: add configuration for custom user agent (#965)
1 parent 966b9e2 commit ba80f8f

File tree

8 files changed

+67
-10
lines changed

8 files changed

+67
-10
lines changed

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU
313313
golang.org/x/mobile v0.0.0-20190719004257-d2bd2a29d028/go.mod h1:E/iHnbuqvinMTCcRqshq8CkpyQDoeVncDDYHnLhea+o=
314314
golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc=
315315
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
316+
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee h1:WG0RUwxtNT4qqaXX3DPA8zHFNm/D9xaBpxzHt1WcA/E=
316317
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
317318
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
318319
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

manifest_staging/charts/aad-pod-identity/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ The following tables list the configurable parameters of the aad-pod-identity ch
289289
| `rbac.allowAccessToSecrets` | NMI requires permissions to get secrets when service principal (type: 1) is used in AzureIdentity. If using only MSI (type: 0) in AzureIdentity, secret get permission can be disabled by setting this to false. | `true` |
290290
| `azureIdentities` | Map of azure identities and azure identity bindings resources to create. The map key is the `AzureIdentity` name. | `{}` |
291291
| `installCRDs` | If true, install necessary custom resources | `false` |
292+
| `customUserAgent` | Custom user agent to add to ADAL, ARM and Kubernetes API server requests | `""` |
292293

293294
## Troubleshooting
294295

manifest_staging/charts/aad-pod-identity/templates/mic-deployment.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ spec:
8989
{{- if .Values.mic.identityAssignmentReconcileInterval }}
9090
- --identity-assignment-reconcile-interval={{ .Values.mic.identityAssignmentReconcileInterval }}
9191
{{- end }}
92+
{{- if .Values.customUserAgent }}
93+
- --custom-user-agent={{ .Values.customUserAgent }}
94+
{{- end }}
9295
{{- if not .Values.adminsecret }}
9396
securityContext:
9497
runAsUser: 0

manifest_staging/charts/aad-pod-identity/templates/nmi-daemonset.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ spec:
107107
{{- if semverCompare ">= 1.7.0-0" .Values.nmi.tag }}
108108
- --kubelet-config={{ .Values.nmi.kubeletConfig }}
109109
{{- end }}
110+
{{- if .Values.customUserAgent }}
111+
- --custom-user-agent={{ .Values.customUserAgent }}
112+
{{- end }}
110113
env:
111114
{{- if semverCompare "<= 1.6.1-0" .Values.nmi.tag }}
112115
- name: HOST_IP

manifest_staging/charts/aad-pod-identity/values.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -245,3 +245,7 @@ azureIdentities:
245245

246246
# If true, install necessary custom resources.
247247
installCRDs: false
248+
249+
# If provided, the userAgent string will be appended to the pod identity user agents for all
250+
# ADAL, ARM and Kube API server requests.
251+
customUserAgent: ""

test/e2e/framework/helm/helm_helpers.go

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
. "github.com/onsi/ginkgo"
1414
. "github.com/onsi/gomega"
15+
"golang.org/x/mod/semver"
1516
)
1617

1718
const (
@@ -101,6 +102,11 @@ func generateValueArgs(config *framework.Config) []string {
101102
fmt.Sprintf("--set=nmi.tag=%s", config.NMIVersion),
102103
}
103104

105+
// TODO (aramase) bump this to compare against v1.7.3 after next release
106+
if semver.Compare(config.MICVersion, "v1.7.2") > 1 && semver.Compare(config.NMIVersion, "v1.7.2") > 1 {
107+
args = append(args, fmt.Sprintf("--set=customUserAgent=pi-e2e"))
108+
}
109+
104110
if config.ImmutableUserMSIs != "" {
105111
args = append(args, fmt.Sprintf("--set=mic.immutableUserMSIs=%s", config.ImmutableUserMSIs))
106112
}

version/version.go

+16-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
package version
22

33
import (
4+
"flag"
45
"fmt"
56
"os"
67
)
78

8-
// BuildDate is the date when the binary was built
9-
var BuildDate string
9+
var (
10+
// BuildDate is the date when the binary was built
11+
BuildDate string
12+
// GitCommit is the commit hash when the binary was built
13+
GitCommit string
14+
// MICVersion is the version of the MIC component
15+
MICVersion string
16+
// NMIVersion is the version of the NMI component
17+
NMIVersion string
1018

11-
// GitCommit is the commit hash when the binary was built
12-
var GitCommit string
13-
14-
// MICVersion is the version of the MIC component
15-
var MICVersion string
16-
17-
// NMIVersion is the version of the NMI component
18-
var NMIVersion string
19+
// custom user agent to append for adal and arm calls
20+
customUserAgent = flag.String("custom-user-agent", "", "User agent to append in addition to pod identity component and versions.")
21+
)
1922

2023
// GetUserAgent is used to get the user agent string which is then provided to adal
2124
// to use as the extended user agent header.
2225
// The format is: aad-pod-identity/<component - either NMI or MIC>/<Version of component>/<Git commit>/<Build date>
2326
func GetUserAgent(component, version string) string {
27+
if *customUserAgent != "" {
28+
return fmt.Sprintf("aad-pod-identity/%s/%s/%s/%s %s", component, version, GitCommit, BuildDate, *customUserAgent)
29+
}
2430
return fmt.Sprintf("aad-pod-identity/%s/%s/%s/%s", component, version, GitCommit, BuildDate)
2531
}
2632

version/version_test.go

+33
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,36 @@ func TestVersion(t *testing.T) {
1616
t.Fatalf("got unexpected user agent string: %s. Expected: %s.", gotUserAgentStr, expectedUserAgentStr)
1717
}
1818
}
19+
20+
func TestGetUserAgent(t *testing.T) {
21+
BuildDate = "now"
22+
GitCommit = "commit"
23+
NMIVersion = "version"
24+
25+
tests := []struct {
26+
name string
27+
customUserAgent string
28+
expectedUserAgent string
29+
}{
30+
{
31+
name: "default NMI user agent",
32+
customUserAgent: "",
33+
expectedUserAgent: "aad-pod-identity/NMI/version/commit/now",
34+
},
35+
{
36+
name: "default NMI user agent and custom user agent",
37+
customUserAgent: "managedBy:aks",
38+
expectedUserAgent: "aad-pod-identity/NMI/version/commit/now managedBy:aks",
39+
},
40+
}
41+
42+
for _, test := range tests {
43+
t.Run(test.name, func(t *testing.T) {
44+
customUserAgent = &test.customUserAgent
45+
actualUserAgent := GetUserAgent("NMI", NMIVersion)
46+
if !strings.EqualFold(test.expectedUserAgent, actualUserAgent) {
47+
t.Fatalf("got unexpected user agent string: %s. Expected: %s.", test.expectedUserAgent, actualUserAgent)
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)