forked from prometheus-community/fortigate_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
70 lines (63 loc) · 2.08 KB
/
Copy pathmain.go
File metadata and controls
70 lines (63 loc) · 2.08 KB
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 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fortigatehttpclient
import (
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"log"
"net/http"
"net/url"
"time"
"github.com/prometheus-community/fortigate_exporter/internal/config"
)
type FortiHTTP interface {
Get(path, query string, obj any) error
}
func NewFortiClient(ctx context.Context, tgt url.URL, hc *http.Client, aConfig config.FortiExporterConfig) (FortiHTTP, error) {
auth, ok := aConfig.AuthKeys[config.Target(tgt.String())]
if !ok {
return nil, fmt.Errorf("no API authentication registered for %q", tgt.String())
}
if auth.Token != "" {
if tgt.Scheme != "https" {
return nil, fmt.Errorf("FortiOS only supports token for HTTPS connections")
}
c, err := newFortiTokenClient(ctx, tgt, hc, auth.Token)
if err != nil {
return nil, err
}
return c, nil
}
return nil, fmt.Errorf("invalid authentication data for %q", tgt.String())
}
func Configure(config config.FortiExporterConfig) error {
roots, err := x509.SystemCertPool()
if err != nil {
log.Fatalf("Unable to fetch system CA store: %v", err)
return err
}
for _, cert := range config.TLSExtraCAs {
if ok := roots.AppendCertsFromPEM(cert.Content); !ok {
return fmt.Errorf("failed to append certs from PEM %q, unknown error", cert.Path)
}
}
tc := &tls.Config{RootCAs: roots}
if config.TLSInsecure {
tc.InsecureSkipVerify = true
}
http.DefaultTransport.(*http.Transport).TLSHandshakeTimeout = time.Duration(config.TLSTimeout) * time.Second
http.DefaultTransport.(*http.Transport).TLSClientConfig = tc
return nil
}