Skip to content

Commit e423de2

Browse files
authored
Merge pull request #2 from marriva/add-tls-auth
add mtls client auth
2 parents 3ecdc2d + afe3a8e commit e423de2

File tree

3 files changed

+81
-8
lines changed

3 files changed

+81
-8
lines changed

modules/config/config.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,15 @@ type Config struct {
2828
TimeOutRaw *int `yaml:"timeout"`
2929
} `yaml:"app"`
3030
Elastic struct {
31-
Host string `yaml:"host`
32-
SSL bool `yaml:"ssl"`
33-
Username string `yaml:"username"`
34-
Password string `yaml:"password"`
35-
Cert string `yaml:"certfile"`
36-
Include bool `yaml:"include_system"`
31+
Host string `yaml:"host"`
32+
SSL bool `yaml:"ssl"`
33+
Username string `yaml:"username"`
34+
Password string `yaml:"password"`
35+
CAcert string `yaml:"ca_cert"`
36+
ClientCert string `yaml:"client_cert"`
37+
ClientKey string `yaml:"client_key"`
38+
InsecureSkipVerify bool `yaml:"insecure"`
39+
Include bool `yaml:"include_system"`
3740
} `yaml:"elastic"`
3841
}
3942

modules/router/methods.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
package router
1515

1616
import (
17-
"crypto/tls"
1817
"encoding/json"
1918
"errors"
2019
"fmt"
@@ -42,11 +41,13 @@ type esError struct {
4241
}
4342

4443
func (rt *Router) netClientPrepare() {
44+
tlsClientConfig := createTLSConfig(rt.conf.Elastic.CAcert, rt.conf.Elastic.ClientCert,
45+
rt.conf.Elastic.ClientKey, rt.conf.Elastic.InsecureSkipVerify)
4546
var netTransport = &http.Transport{
4647
Dial: (&net.Dialer{
4748
Timeout: time.Duration(rt.conf.App.TimeOut) * time.Second,
4849
}).Dial,
49-
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
50+
TLSClientConfig: tlsClientConfig,
5051
}
5152

5253
rt.nc = &http.Client{

modules/router/tls.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2021 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package router
15+
16+
import (
17+
"crypto/tls"
18+
"crypto/x509"
19+
"io/ioutil"
20+
"log"
21+
)
22+
23+
func createTLSConfig(pemFile, pemCertFile, pemPrivateKeyFile string, insecureSkipVerify bool) *tls.Config {
24+
tlsConfig := tls.Config{}
25+
if insecureSkipVerify {
26+
// pem settings are irrelevant if we're skipping verification anyway
27+
tlsConfig.InsecureSkipVerify = true
28+
}
29+
if len(pemFile) > 0 {
30+
rootCerts, err := loadCertificatesFrom(pemFile)
31+
if err != nil {
32+
log.Fatalf("Couldn't load root certificate from %s. Got %s.", pemFile, err)
33+
return nil
34+
}
35+
tlsConfig.RootCAs = rootCerts
36+
}
37+
if len(pemCertFile) > 0 && len(pemPrivateKeyFile) > 0 {
38+
// Load files once to catch configuration error early.
39+
_, err := loadPrivateKeyFrom(pemCertFile, pemPrivateKeyFile)
40+
if err != nil {
41+
log.Fatalf("Couldn't setup client authentication. Got %s.", err)
42+
return nil
43+
}
44+
// Define a function to load certificate and key lazily at TLS handshake to
45+
// ensure that the latest files are used in case they have been rotated.
46+
tlsConfig.GetClientCertificate = func(*tls.CertificateRequestInfo) (*tls.Certificate, error) {
47+
return loadPrivateKeyFrom(pemCertFile, pemPrivateKeyFile)
48+
}
49+
}
50+
return &tlsConfig
51+
}
52+
53+
func loadCertificatesFrom(pemFile string) (*x509.CertPool, error) {
54+
caCert, err := ioutil.ReadFile(pemFile)
55+
if err != nil {
56+
return nil, err
57+
}
58+
certificates := x509.NewCertPool()
59+
certificates.AppendCertsFromPEM(caCert)
60+
return certificates, nil
61+
}
62+
63+
func loadPrivateKeyFrom(pemCertFile, pemPrivateKeyFile string) (*tls.Certificate, error) {
64+
privateKey, err := tls.LoadX509KeyPair(pemCertFile, pemPrivateKeyFile)
65+
if err != nil {
66+
return nil, err
67+
}
68+
return &privateKey, nil
69+
}

0 commit comments

Comments
 (0)