Skip to content

Commit 2d657af

Browse files
committed
Adds service_account telemetry
1 parent ce7c72a commit 2d657af

File tree

2 files changed

+48
-38
lines changed

2 files changed

+48
-38
lines changed

internal/telemetry/event.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,19 @@ func withOS() EventOpt {
207207
}
208208

209209
type Authenticator interface {
210-
PublicAPIKey() string
211-
PrivateAPIKey() string
212-
AccessToken() string
210+
AuthType() config.AuthMechanism
213211
}
214212

215213
func withAuthMethod(c Authenticator) EventOpt {
216214
return func(event Event) {
217-
if c.PublicAPIKey() != "" && c.PrivateAPIKey() != "" {
215+
switch c.AuthType() {
216+
case config.APIKeys:
218217
event.Properties["auth_method"] = "api_key"
219218
return
220-
} else if c.AccessToken() != "" {
219+
case config.UserAccount:
221220
event.Properties["auth_method"] = "oauth"
221+
case config.ServiceAccount:
222+
event.Properties["auth_method"] = "service_account"
222223
}
223224
}
224225
}

internal/telemetry/event_test.go

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -135,20 +135,39 @@ func TestWithUserAgent(t *testing.T) {
135135
}
136136

137137
func TestWithAuthMethod(t *testing.T) {
138-
t.Run("api key", func(t *testing.T) {
139-
c := &configMock{
140-
publicKey: "test-public",
141-
privateKey: "test-private",
142-
}
143-
e := newEvent(withAuthMethod(c))
144-
assert.Equal(t, "api_key", e.Properties["auth_method"])
145-
})
146-
t.Run("Oauth", func(t *testing.T) {
147-
e := newEvent(withAuthMethod(&configMock{
148-
accessToken: "test",
149-
}))
150-
assert.Equal(t, "oauth", e.Properties["auth_method"])
151-
})
138+
tests := []struct {
139+
name string
140+
cfg *configMock
141+
expected string
142+
}{
143+
{
144+
name: "api key",
145+
cfg: &configMock{
146+
authType: config.APIKeys,
147+
},
148+
expected: "api_key",
149+
},
150+
{
151+
name: "user account",
152+
cfg: &configMock{
153+
authType: config.UserAccount,
154+
},
155+
expected: "oauth",
156+
},
157+
{
158+
name: "service account",
159+
cfg: &configMock{
160+
authType: config.ServiceAccount,
161+
},
162+
expected: "service_account",
163+
},
164+
}
165+
for _, tt := range tests {
166+
t.Run(tt.name, func(t *testing.T) {
167+
e := newEvent(withAuthMethod(tt.cfg))
168+
assert.Equal(t, tt.expected, e.Properties["auth_method"])
169+
})
170+
}
152171
}
153172

154173
func TestWithService(t *testing.T) {
@@ -446,15 +465,13 @@ func Test_withOutput(t *testing.T) {
446465
}
447466

448467
type configMock struct {
449-
name string
450-
publicKey string
451-
privateKey string
452-
accessToken string
453-
service string
454-
url string
455-
project string
456-
org string
457-
out string
468+
name string
469+
authType config.AuthMechanism
470+
service string
471+
url string
472+
project string
473+
org string
474+
out string
458475
}
459476

460477
var _ Authenticator = configMock{}
@@ -479,16 +496,8 @@ func (c configMock) OpsManagerURL() string {
479496
return c.url
480497
}
481498

482-
func (c configMock) PublicAPIKey() string {
483-
return c.publicKey
484-
}
485-
486-
func (c configMock) PrivateAPIKey() string {
487-
return c.privateKey
488-
}
489-
490-
func (c configMock) AccessToken() string {
491-
return c.accessToken
499+
func (c configMock) AuthType() config.AuthMechanism {
500+
return c.authType
492501
}
493502

494503
func (c configMock) Output() string {

0 commit comments

Comments
 (0)