Skip to content

Commit d8694f3

Browse files
feat(servicediscovery): add telemetry header to all API requests
- Introduce SetTelementryRequestHeader to set X-Cybr-Telemetry header - Ensure telemetry header is present in service discovery requests - Update mock server to validate telemetry header presence Signed-off-by: Richard Wall <[email protected]>
1 parent 32d6bf1 commit d8694f3

File tree

4 files changed

+68
-1
lines changed

4 files changed

+68
-1
lines changed

internal/cyberark/api/telemetry.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package api
2+
3+
import (
4+
"encoding/base64"
5+
"net/http"
6+
7+
"github.com/jetstack/preflight/pkg/version"
8+
)
9+
10+
// Integrations working with the Identity Security Platform, should add metadata
11+
// in their API calls, to provide insights into how customers utilize each API.
12+
//
13+
// - IntegrationName (in): The vendor integration name (required)
14+
// - IntegrationType (it): Integration Type (required)
15+
// - IntegrationVersion (iv): The plugin version being used (required)
16+
// - VendorName (vn): Vendor name (required)
17+
// - VendorVersion (vv): Version of the vendor product in which the plugin is used (if applicable)
18+
19+
const (
20+
// TelemetryHeaderKey is the name of the HTTP header to use for telemetry
21+
TelemetryHeaderKey = "X-Cybr-Telemetry"
22+
// staticHeaderPrefix is the static part of the telemetry header value
23+
// The version is appended to this string before being base64 encoded
24+
staticHeaderPrefix = "in=cyberark-disco-agent&vn=CyberArk&it=KubernetesAgent&iv="
25+
)
26+
27+
// SetTelemetryRequestHeader adds the x-cybr-telemetry header to the given HTTP
28+
// request, with information about this integration.
29+
func SetTelemetryRequestHeader(req *http.Request) {
30+
encodedValue := base64.URLEncoding.EncodeToString(
31+
[]byte(staticHeaderPrefix + version.PreflightVersion),
32+
)
33+
req.Header.Set(TelemetryHeaderKey, encodedValue)
34+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package api
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
// Test the SetTelemetryRequestHeader function
10+
func ExampleSetTelemetryRequestHeader() {
11+
req, _ := http.NewRequest("GET", "http://example.com", nil)
12+
SetTelemetryRequestHeader(req)
13+
encodedValue := req.Header.Get("X-Cybr-Telemetry")
14+
value, err := base64.StdEncoding.DecodeString(encodedValue)
15+
fmt.Println(
16+
"encodedValue:", encodedValue, "\n",
17+
"value:", string(value), "\n",
18+
"err:", err,
19+
)
20+
// Output: encodedValue: aW49Y3liZXJhcmstZGlzY28tYWdlbnQmdm49Q3liZXJBcmsmaXQ9S3ViZXJuZXRlc0FnZW50Jml2PWRldmVsb3BtZW50
21+
// value: in=cyberark-disco-agent&vn=CyberArk&it=KubernetesAgent&iv=development
22+
// err: <nil>
23+
//
24+
}

internal/cyberark/servicediscovery/discovery.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"os"
1111
"path"
1212

13+
arkapi "github.com/jetstack/preflight/internal/cyberark/api"
1314
"github.com/jetstack/preflight/pkg/version"
1415
)
1516

@@ -109,7 +110,8 @@ func (c *Client) DiscoverServices(ctx context.Context, subdomain string) (*Servi
109110

110111
request.Header.Set("Accept", "application/json")
111112
version.SetUserAgent(request)
112-
113+
// Add telemetry headers
114+
arkapi.SetTelemetryRequestHeader(request)
113115
resp, err := c.client.Do(request)
114116
if err != nil {
115117
return nil, fmt.Errorf("failed to perform HTTP request: %s", err)

internal/cyberark/servicediscovery/mock.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"k8s.io/client-go/transport"
1515

16+
arkapi "github.com/jetstack/preflight/internal/cyberark/api"
1617
"github.com/jetstack/preflight/pkg/version"
1718

1819
_ "embed"
@@ -92,6 +93,12 @@ func (mds *mockDiscoveryServer) ServeHTTP(w http.ResponseWriter, r *http.Request
9293
return
9394
}
9495

96+
if r.Header.Get(arkapi.TelemetryHeaderKey) == "" {
97+
w.WriteHeader(http.StatusInternalServerError)
98+
_, _ = w.Write([]byte("should set telemetry header on all requests"))
99+
return
100+
}
101+
95102
if r.Header.Get("Accept") != "application/json" {
96103
w.WriteHeader(http.StatusInternalServerError)
97104
_, _ = w.Write([]byte("should request JSON on all requests"))

0 commit comments

Comments
 (0)