Skip to content

Commit 9df7a67

Browse files
authored
Merge pull request #76 from genegr/add-TLS
Add support for TLS, as per Issue #70
2 parents 366a7ea + 1c1c3c8 commit 9df7a67

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,15 @@ Authentication is used by the exporter as the mechanism to cross authenticate to
8080
The first option requires specifying the api-token value as the authorization parameter of the specific job in the Prometheus configuration file.
8181
The second option provides the FlashArray/api-token key-pair map for a list of arrays in a simple YAML configuration file that is passed as parameter to the exporter. This makes possible to write more concise Prometheus configuration files and also to configure other scrapers that cannot use the HTTP authentication header.
8282

83+
### TLS Support
84+
85+
The exporter can be started in TLS mode (HTTPS, mutually exclusive with the HTTP mode) by providing the X.509 certificate and key files in the command parameters. Self-signed certificates are also accepted.
86+
8387
### Usage
8488

8589
```shell
8690

87-
usage: pure-fa-om-exporter [-h|--help] [-a|--address "<value>"] [-p|--port <integer>] [-d|--debug] [-t|--tokens <file>]
91+
usage: pure-fa-om-exporter [-h|--help] [-a|--address "<value>"] [-p|--port <integer>] [-d|--debug] [-t|--tokens <file>] [-k|--key <file>] [-c|--cert <file>]
8892

8993
Pure Storage FA OpenMetrics exporter
9094

@@ -95,6 +99,8 @@ Arguments:
9599
-p --port Port for this exporter to listen. Default: 9490
96100
-d --debug Enable debug. Default: false
97101
-t --tokens API token(s) map file
102+
-c --cert SSL/TLS certificate file. Required only for TLS
103+
-k --key SSL/TLS private key file. Required only for TLS
98104
```
99105

100106
The array token configuration file must have to following syntax:

cmd/fa-om-exporter/main.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,28 @@ var version string = "development"
2222
var debug bool = false
2323
var arraytokens config.FlashArrayList
2424

25-
func FileExists(args []string) error {
25+
func fileExists(args []string) error {
2626
_, err := os.Stat(args[0])
2727
return err
2828
}
2929

30+
func isFile(filename string) bool {
31+
info, err := os.Stat(filename)
32+
if os.IsNotExist(err) {
33+
return false
34+
}
35+
return !info.IsDir()
36+
}
37+
3038
func main() {
3139

3240
parser := argparse.NewParser("pure-fa-om-exporter", "Pure Storage FA OpenMetrics exporter")
3341
host := parser.String("a", "address", &argparse.Options{Required: false, Help: "IP address for this exporter to bind to", Default: "0.0.0.0"})
3442
port := parser.Int("p", "port", &argparse.Options{Required: false, Help: "Port for this exporter to listen", Default: 9490})
3543
d := parser.Flag("d", "debug", &argparse.Options{Required: false, Help: "Enable debug", Default: false})
36-
at := parser.File("t", "tokens", os.O_RDONLY, 0600, &argparse.Options{Required: false, Validate: FileExists, Help: "API token(s) map file"})
44+
at := parser.File("t", "tokens", os.O_RDONLY, 0600, &argparse.Options{Required: false, Validate: fileExists, Help: "API token(s) map file"})
45+
cert := parser.String("c", "cert", &argparse.Options{Required: false, Help: "SSL/TLS certificate file. Required only for TLS"})
46+
key := parser.String("k", "key", &argparse.Options{Required: false, Help: "SSL/TLS private key file. Required only for TLS"})
3747
err := parser.Parse(os.Args)
3848
if err != nil {
3949
log.Fatalf("Error in token file: %v", err)
@@ -60,6 +70,16 @@ func main() {
6070
log.Fatalf("Unmarshalling token file: %v", err)
6171
}
6272
}
73+
if (len(*cert) > 0 && len(*key) == 0) || (len(*cert) == 0 && len(*key) > 0) {
74+
log.Fatal("Both certificate and key must be specified to enable TLS")
75+
}
76+
if (len(*cert) > 0 && len(*key) > 0) {
77+
if !isFile(*cert) {
78+
log.Fatal("TLS cert file not found")
79+
} else if !isFile (*key) {
80+
log.Fatal("TLS key file not found")
81+
}
82+
}
6383
debug = *d
6484
addr := fmt.Sprintf("%s:%d", *host, *port)
6585
log.Printf("Start Pure FlashArray exporter %s on %s", version, addr)
@@ -83,7 +103,11 @@ func main() {
83103
http.HandleFunc("/metrics", func(w http.ResponseWriter, r *http.Request) {
84104
metricsHandler(w, r)
85105
})
86-
log.Fatal(http.ListenAndServe(addr, nil))
106+
if isFile(*cert) && isFile(*key) {
107+
log.Fatal(http.ListenAndServeTLS(addr, *cert, *key, nil))
108+
} else {
109+
log.Fatal(http.ListenAndServe(addr, nil))
110+
}
87111
}
88112

89113
func metricsHandler(w http.ResponseWriter, r *http.Request) {

0 commit comments

Comments
 (0)