/
api.go
70 lines (59 loc) · 1.55 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
// Copyright © 2019 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"
"os"
"github.com/circonus-labs/circonus-kubernetes-agent/internal/config/defaults"
"github.com/circonus-labs/circonus-kubernetes-agent/internal/config/keys"
"github.com/pkg/errors"
"github.com/spf13/viper"
)
func validateAPIOptions(apiKey, apiKeyFile, apiApp, apiURL, apiCAFile string) error {
if apiKeyFile == "" && 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 apiKeyFile != "" && apiKey == "" {
f, err := verifyFile(apiKeyFile)
if err != nil {
return err
}
data, err := os.ReadFile(f)
if err != nil {
return err
}
apiKey = string(data)
if apiKey == "" {
return errors.Errorf("invalid API key file (%s), empty key", apiKeyFile)
}
}
if apiURL != defaults.APIURL {
parsedURL, err := url.Parse(apiURL)
if err != nil {
return errors.Wrap(err, "invalid API URL")
}
if parsedURL.Scheme == "" || parsedURL.Host == "" {
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(keys.APICAFile, f)
}
viper.Set(keys.APITokenKey, apiKey)
viper.Set(keys.APITokenApp, apiApp)
viper.Set(keys.APIURL, apiURL)
return nil
}