Skip to content

Commit fb61823

Browse files
author
Philipp Dorschner
authored
Merge pull request #43 from NETWAYS/feature/from-env
Set CLI flags via env variables
2 parents 83c4625 + 0b2ec88 commit fb61823

File tree

6 files changed

+94
-18
lines changed

6 files changed

+94
-18
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
- name: Set up Go
2020
uses: actions/setup-go@v4
2121
with:
22-
go-version: 1.19
22+
go-version: 1.21
2323

2424
- name: Test
2525
run: go test -v ./...

.github/workflows/golangci-lint.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,5 @@ jobs:
1616

1717
- name: golangci-lint
1818
uses: golangci/golangci-lint-action@v3
19+
with:
20+
version: v1.54

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,24 @@ Available Commands:
1515
query Checks the total hits/results of an Elasticsearch query
1616
1717
Flags:
18-
-H, --hostname string Hostname of the Elasticsearch instance (default "localhost")
18+
-H, --hostname string Hostname of the Elasticsearch instance (CHECK_ELASTICSEARCH_HOSTNAME) (default "localhost")
1919
-p, --port int Port of the Elasticsearch instance (default 9200)
20-
-U, --username string Username if authentication is required
21-
-P, --password string Password if authentication is required
20+
-U, --username string Username for HTTP Basic Authentication (CHECK_ELASTICSEARCH_USERNAME)
21+
-P, --password string Password for HTTP Basic Authentication (CHECK_ELASTICSEARCH_PASSWORD)
2222
-S, --tls Use a HTTPS connection
2323
--insecure Skip the verification of the server's TLS certificate
24+
--ca-file string Specify the CA File for TLS authentication (CHECK_ELASTICSEARCH_CA_FILE)
25+
--cert-file string Specify the Certificate File for TLS authentication (CHECK_ELASTICSEARCH_CERT_FILE)
26+
--key-file string Specify the Key File for TLS authentication (CHECK_ELASTICSEARCH_KEY_FILE)
2427
-t, --timeout int Timeout in seconds for the CheckPlugin (default 30)
2528
-h, --help help for check_elasticsearch
2629
-v, --version version for check_elasticsearch
2730
```
2831

2932
The check plugin respects the environment variables `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY`.
3033

34+
Various flags can be set with environment variables, refer to the help to see which flags.
35+
3136
### Health
3237

3338
Checks the health status of an Elasticsearch cluster.

cmd/config.go

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"net"
66
"net/http"
77
"net/url"
8+
"os"
9+
"reflect"
810
"strconv"
911
"time"
1012

@@ -14,17 +16,50 @@ import (
1416
)
1517

1618
type Config struct {
17-
Hostname string
18-
BasicAuth string
19-
Bearer string
20-
CAFile string
21-
CertFile string
22-
KeyFile string
23-
Username string
24-
Password string
25-
Port int
26-
TLS bool
27-
Insecure bool
19+
Bearer string // Currently unused in CLI
20+
Hostname string `env:"CHECK_ELASTICSEARCH_HOSTNAME"`
21+
CAFile string `env:"CHECK_ELASTICSEARCH_CA_FILE"`
22+
CertFile string `env:"CHECK_ELASTICSEARCH_CERT_FILE"`
23+
KeyFile string `env:"CHECK_ELASTICSEARCH_KEY_FILE"`
24+
Username string `env:"CHECK_ELASTICSEARCH_USERNAME"`
25+
Password string `env:"CHECK_ELASTICSEARCH_PASSWORD"`
26+
Port int
27+
TLS bool
28+
Insecure bool
29+
}
30+
31+
// LoadFromEnv can be used to load struct values from 'env' tags.
32+
// Mainly used to avoid passing secrets via the CLI
33+
//
34+
// type Config struct {
35+
// Token string `env:"BEARER_TOKEN"`
36+
// }
37+
func loadFromEnv(config interface{}) {
38+
configValue := reflect.ValueOf(config).Elem()
39+
configType := configValue.Type()
40+
41+
for i := 0; i < configValue.NumField(); i++ {
42+
field := configType.Field(i)
43+
tag := field.Tag.Get("env")
44+
45+
// If there's no "env" tag, skip this field.
46+
if tag == "" {
47+
continue
48+
}
49+
50+
envValue := os.Getenv(tag)
51+
52+
if envValue == "" {
53+
continue
54+
}
55+
56+
// Potential for addding different types
57+
// nolint: exhaustive, gocritic
58+
switch field.Type.Kind() {
59+
case reflect.String:
60+
configValue.Field(i).SetString(envValue)
61+
}
62+
}
2863
}
2964

3065
var cliConfig Config

cmd/config_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package cmd
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestLoadFromEnv(t *testing.T) {
9+
c := Config{}
10+
11+
err := os.Setenv("CHECK_ELASTICSEARCH_USERNAME", "foobar")
12+
defer os.Unsetenv("CHECK_ELASTICSEARCH_USERNAME") // to not impact other tests
13+
14+
if err != nil {
15+
t.Error("Did not expect error, got: %w", err)
16+
}
17+
18+
loadFromEnv(&c)
19+
20+
if "foobar" != c.Username {
21+
t.Error("\nActual: ", c.Username, "\nExpected: ", "foobar")
22+
}
23+
if "" != c.Password {
24+
t.Error("\nActual: ", c.Password, "\nExpected: ", "empty-string")
25+
}
26+
}

cmd/root.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,30 @@ func init() {
4242

4343
pfs := rootCmd.PersistentFlags()
4444
pfs.StringVarP(&cliConfig.Hostname, "hostname", "H", "localhost",
45-
"Hostname of the Elasticsearch instance")
45+
"Hostname of the Elasticsearch instance (CHECK_ELASTICSEARCH_HOSTNAME)")
4646
pfs.IntVarP(&cliConfig.Port, "port", "p", 9200,
4747
"Port of the Elasticsearch instance")
4848
pfs.StringVarP(&cliConfig.Username, "username", "U", "",
49-
"Username for HTTP Basic Authentication")
49+
"Username for HTTP Basic Authentication (CHECK_ELASTICSEARCH_USERNAME)")
5050
pfs.StringVarP(&cliConfig.Password, "password", "P", "",
51-
"Password for HTTP Basic Authentication")
51+
"Password for HTTP Basic Authentication (CHECK_ELASTICSEARCH_PASSWORD)")
5252
pfs.BoolVarP(&cliConfig.TLS, "tls", "S", false,
5353
"Use a HTTPS connection")
5454
pfs.BoolVar(&cliConfig.Insecure, "insecure", false,
5555
"Skip the verification of the server's TLS certificate")
56+
pfs.StringVarP(&cliConfig.CAFile, "ca-file", "", "",
57+
"Specify the CA File for TLS authentication (CHECK_ELASTICSEARCH_CA_FILE)")
58+
pfs.StringVarP(&cliConfig.CertFile, "cert-file", "", "",
59+
"Specify the Certificate File for TLS authentication (CHECK_ELASTICSEARCH_CERT_FILE)")
60+
pfs.StringVarP(&cliConfig.KeyFile, "key-file", "", "",
61+
"Specify the Key File for TLS authentication (CHECK_ELASTICSEARCH_KEY_FILE)")
5662
pfs.IntVarP(&timeout, "timeout", "t", timeout,
5763
"Timeout in seconds for the CheckPlugin")
5864

5965
rootCmd.Flags().SortFlags = false
6066
pfs.SortFlags = false
67+
68+
loadFromEnv(&cliConfig)
6169
}
6270

6371
func Help(cmd *cobra.Command, _ []string) {

0 commit comments

Comments
 (0)