Skip to content
This repository was archived by the owner on Jul 10, 2024. It is now read-only.

Commit 7435d8c

Browse files
authored
OBS-419: Fix distinct ID for Postman API key users. (#243)
Previously we were calling /v1/users to get an email to use as a distinct ID only when an Akita API Key was present.
1 parent 60c1cba commit 7435d8c

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

cfg/credentials.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,24 @@ func WritePostmanAPIKeyAndEnvironment(profile, postmanApiKey, postmanEnvironment
118118

119119
return writeConfigToFile(profile, keyValueMap)
120120
}
121+
122+
// Check whether credentials are present, of any variety.
123+
func CredentialsPresent() bool {
124+
key, _ := GetPostmanAPIKeyAndEnvironment()
125+
if key != "" {
126+
return true
127+
}
128+
129+
key, secret := GetAPIKeyAndSecret()
130+
return key != "" && secret != ""
131+
}
132+
133+
// If we can't call /v1/user to get a distinct ID, we can try using
134+
// the credentials provided -- for now this is just an Akita API Key ID.
135+
// To use a Postman API key we'd have to both obfuscate it (logging
136+
// the user's API key would be bad) and have a way to map it to a
137+
// particular user -- seems better to fall back to local IDs.
138+
func DistinctIDFromCredentials() string {
139+
key, _ := GetAPIKeyAndSecret()
140+
return key
141+
}

telemetry/telemetry.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,7 @@ func getDistinctID() string {
122122
// If there's no credentials configured, skip the API call and
123123
// do not emit a log message.
124124
// Similarly if telemetry is disabled.
125-
key, secret := cfg.GetAPIKeyAndSecret()
126-
if key != "" && secret != "" && analyticsEnabled {
125+
if cfg.CredentialsPresent() && analyticsEnabled {
127126
// Call the REST API to get the user email associated with the configured
128127
// API key.
129128
ctx, cancel := context.WithTimeout(context.Background(), userAPITimeout)
@@ -145,8 +144,11 @@ func getDistinctID() string {
145144
printer.Infof("but the agent will still attempt to send telemetry to Postman support.\n")
146145
}
147146

148-
if key != "" {
149-
return key
147+
// Try to derive a distinct ID from the credentials, if present, even
148+
// if the /v1/user call failed.
149+
keyID := cfg.DistinctIDFromCredentials()
150+
if keyID != "" {
151+
return keyID
150152
}
151153

152154
localUser, err := user.Current()

0 commit comments

Comments
 (0)