-
Notifications
You must be signed in to change notification settings - Fork 69
/
config_validator.go
148 lines (118 loc) · 3.46 KB
/
config_validator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package diagnose
import (
"context"
"fmt"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"github.com/shirou/gopsutil/v3/host"
"github.com/newrelic/newrelic-cli/internal/config"
configAPI "github.com/newrelic/newrelic-cli/internal/config/api"
"github.com/newrelic/newrelic-cli/internal/utils"
"github.com/newrelic/newrelic-cli/internal/utils/validation"
"github.com/newrelic/newrelic-client-go/v2/newrelic"
"github.com/newrelic/newrelic-client-go/v2/pkg/apiaccess"
nrErrors "github.com/newrelic/newrelic-client-go/v2/pkg/errors"
)
const (
validationEventType = "NrIntegrationError"
DefaultMaxValidationAttempts = 20
)
type ConfigValidator struct {
client *newrelic.NewRelic
*validation.PollingNRQLValidator
PostRetryDelaySec int
PostMaxRetries int
}
type ValidationTracerEvent struct {
EventType string `json:"eventType"`
Hostname string `json:"hostname"`
Purpose string `json:"purpose"`
GUID string `json:"guid"`
}
func NewConfigValidator(client *newrelic.NewRelic) *ConfigValidator {
v := validation.NewPollingNRQLValidator(&client.Nrdb)
v.MaxAttempts = DefaultMaxValidationAttempts
return &ConfigValidator{
client: client,
PollingNRQLValidator: v,
PostRetryDelaySec: config.DefaultPostRetryDelaySec,
PostMaxRetries: config.DefaultPostMaxRetries,
}
}
func (c *ConfigValidator) Validate(ctx context.Context) error {
accountID := configAPI.GetActiveProfileAccountID()
if err := c.validateKeys(ctx); err != nil {
return err
}
i, err := host.InfoWithContext(ctx)
if err != nil {
log.Debug(err)
return ErrDiscovery
}
evt := ValidationTracerEvent{
EventType: validationEventType,
Hostname: i.Hostname,
Purpose: "New Relic CLI configuration validation",
GUID: uuid.NewString(),
}
postEvent := func() error {
if err = c.client.Events.CreateEventWithContext(ctx, accountID, evt); err != nil {
log.Debug(err)
if e, ok := err.(*nrErrors.PaymentRequiredError); ok {
return e
}
return ErrPostEvent
}
return nil
}
r := utils.NewRetry(c.PostMaxRetries, c.PostRetryDelaySec*1000, postEvent)
retryCtx := r.ExecWithRetries(ctx)
if !retryCtx.Success {
return retryCtx.MostRecentError()
}
query := fmt.Sprintf(`
FROM %s
SELECT count(*)
WHERE hostname LIKE '%s%%'
AND guid = '%s'
SINCE 10 MINUTES AGO
`, evt.EventType, evt.Hostname, evt.GUID)
if _, err = c.PollingNRQLValidator.Validate(ctx, query); err != nil {
log.Debug(err)
err = ErrValidation
}
return err
}
func (c *ConfigValidator) validateKeys(ctx context.Context) error {
validateKeyFunc := func() error {
return c.validateLicenseKey(ctx)
}
r := utils.NewRetry(c.PostMaxRetries, c.PostRetryDelaySec*1000, validateKeyFunc)
retryCtx := r.ExecWithRetries(ctx)
if !retryCtx.Success {
return retryCtx.MostRecentError()
}
return nil
}
func (c *ConfigValidator) validateLicenseKey(ctx context.Context) error {
accountID := configAPI.GetActiveProfileAccountID()
licenseKey := configAPI.GetActiveProfileString(config.LicenseKey)
params := apiaccess.APIAccessKeySearchQuery{
Scope: apiaccess.APIAccessKeySearchScope{
AccountIDs: []int{accountID},
},
Types: []apiaccess.APIAccessKeyType{
apiaccess.APIAccessKeyTypeTypes.INGEST,
},
}
licenseKeys, err := c.client.APIAccess.SearchAPIAccessKeysWithContext(ctx, params)
if err != nil {
return fmt.Errorf(ErrConnectionStringFormat, err)
}
for _, k := range licenseKeys {
if k.Key == licenseKey {
return nil
}
}
return ErrLicenseKey
}