forked from circonus-labs/circonus-agent
/
api.go
88 lines (71 loc) · 1.92 KB
/
api.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
// Copyright © 2017 Circonus, Inc. <support@circonus.com>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package config
import (
"net/url"
"strings"
"github.com/circonus-labs/circonus-agent/internal/config/defaults"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
// apiRequired checks to see if any options are set which would *require* accessing the API
func apiRequired() bool {
// reverse connections require API access
if viper.GetBool(KeyReverse) {
return true
}
// statsd w/group check enabled require API access
if !viper.GetBool(KeyStatsdDisabled) && viper.GetString(KeyStatsdGroupCID) != "" {
return true
}
return false
}
func validateAPIOptions() error {
apiKey := viper.GetString(KeyAPITokenKey)
apiApp := viper.GetString(KeyAPITokenApp)
apiURL := viper.GetString(KeyAPIURL)
apiCAFile := viper.GetString(KeyAPICAFile)
// if key is 'cosi' - load the cosi api config
if strings.ToLower(apiKey) == cosiName {
cfg, err := loadCosiAPIConfig()
if err != nil {
return err
}
apiKey = cfg.Key
apiApp = cfg.App
apiURL = cfg.URL
}
// API is required for reverse and/or statsd
if apiKey == "" {
return errors.New("API key is required")
}
if apiApp == "" {
return errors.New("API app is required")
}
if apiURL == "" {
return errors.New("API URL is required")
}
if apiURL != defaults.APIURL {
parsedURL, err := url.Parse(apiURL)
if err != nil {
return errors.Wrap(err, "Invalid API URL")
}
if parsedURL.Scheme == "" || parsedURL.Host == "" || parsedURL.Path == "" {
return errors.Errorf("Invalid API URL (%s)", apiURL)
}
}
// NOTE the api ca file doesn't come from the cosi config
if apiCAFile != "" {
f, err := verifyFile(apiCAFile)
if err != nil {
return err
}
viper.Set(KeyAPICAFile, f)
}
viper.Set(KeyAPITokenKey, apiKey)
viper.Set(KeyAPITokenApp, apiApp)
viper.Set(KeyAPIURL, apiURL)
return nil
}