Skip to content

Commit a9f22bd

Browse files
authored
Merge pull request #1 from radenui/feat/add-mtls-capabilities
Feat/add mtls capabilities
2 parents 782da2a + 86a2e77 commit a9f22bd

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-7
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ specifying flags is:
143143
--consul-url=
144144
--consul-acl=
145145
--consul-base-path=
146+
--ca-file=
147+
--key-file=
148+
--cert-file=
146149
--log-level=
147150
--expand-json=
148151
--secrets-file=
@@ -328,6 +331,27 @@ automatically appended.
328331
This is useful when the Consul cluster as all the KV paths segregated (namespaced) by teams or
329332
projects.
330333

334+
### `--ca-file`
335+
336+
> `require:` **no**
337+
> `example:` **`--ca-file=/path/my_ca.crt`**
338+
339+
This is the path for ca certfile for mTLS connection.
340+
341+
### `--cert-file`
342+
343+
> `require:` **no**
344+
> `example:` **`--cert-file=/path/my_cert.crt`**
345+
346+
This is the path for cert certfile mTLS connection.
347+
348+
### `--key-file`
349+
350+
> `require:` **no**
351+
> `example:` **`--key-file=/path/my_key.crt`**
352+
353+
This is the path for key certfile mTLS connection.
354+
331355
### `--log-level`
332356

333357
> `require:` **no**

app/app_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import (
44
"github.com/miniclip/gonsul/internal/config"
55
"github.com/miniclip/gonsul/tests/mocks"
66

7-
. "github.com/onsi/gomega"
87
"os"
98
"testing"
9+
10+
. "github.com/onsi/gomega"
1011
)
1112

1213
func getCommonMocks() (cfg *mocks.IConfig, log *mocks.ILogger, exp *mocks.IExporter, imp *mocks.IImporter) {

cmd/gonsul.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package main
22

33
import (
4+
"crypto/tls"
5+
"crypto/x509"
6+
"fmt"
7+
"io/ioutil"
8+
"log"
9+
"net/http"
10+
"os"
11+
"time"
12+
413
"github.com/miniclip/gonsul/app"
514
"github.com/miniclip/gonsul/internal/config"
615
"github.com/miniclip/gonsul/internal/exporter"
716
"github.com/miniclip/gonsul/internal/importer"
817
"github.com/miniclip/gonsul/internal/util"
9-
10-
"fmt"
11-
"net/http"
12-
"os"
13-
"time"
1418
)
1519

1620
func main() {
@@ -41,9 +45,31 @@ func start() {
4145
return
4246
}
4347

48+
var certificate tls.Certificate
49+
var caCertPool *x509.CertPool
50+
if len(cfg.GetKeyFile()) != 0 && len(cfg.GetCaFile()) != 0 && len(cfg.GetCertFile()) != 0 {
51+
cert, err := ioutil.ReadFile(cfg.GetCaFile())
52+
if err != nil {
53+
log.Fatalf("could not open certificate file: %v", err)
54+
}
55+
caCertPool = x509.NewCertPool()
56+
caCertPool.AppendCertsFromPEM(cert)
57+
58+
certificate, err = tls.LoadX509KeyPair(cfg.GetCertFile(), cfg.GetKeyFile())
59+
if err != nil {
60+
log.Fatalf("could not load certificate: %v", err)
61+
}
62+
}
63+
4464
// Build all dependencies for our application
4565
hookHttpServer := app.NewHookHttp(cfg, logger)
46-
httpClient := &http.Client{Timeout: time.Second * time.Duration(cfg.GetTimeout())}
66+
httpClient := &http.Client{Transport: &http.Transport{
67+
TLSClientConfig: &tls.Config{
68+
RootCAs: caCertPool,
69+
Certificates: []tls.Certificate{certificate},
70+
},
71+
}, Timeout: time.Second * time.Duration(cfg.GetTimeout())}
72+
4773
exp := exporter.NewExporter(cfg, logger)
4874
imp := importer.NewImporter(cfg, logger, httpClient)
4975
sigChannel := make(chan os.Signal)

internal/config/config.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"io/ioutil"
1010
"os"
1111
"strings"
12+
1213
"github.com/namsral/flag"
1314
)
1415

@@ -31,6 +32,9 @@ type config struct {
3132
consulURL string
3233
consulACL string
3334
consulBasePath string
35+
keyFile string
36+
certFile string
37+
caFile string
3438
expandJSON bool
3539
expandYAML bool
3640
doSecrets bool
@@ -60,6 +64,9 @@ type IConfig interface {
6064
GetConsulURL() string
6165
GetConsulACL() string
6266
GetConsulBasePath() string
67+
GetKeyFile() string
68+
GetCaFile() string
69+
GetCertFile() string
6370
ShouldExpandJSON() bool
6471
ShouldExpandYAML() bool
6572
DoSecrets() bool
@@ -155,6 +162,9 @@ func buildConfig(flags ConfigFlags) (*config, error) {
155162
consulURL: *flags.ConsulURL,
156163
consulACL: *flags.ConsulACL,
157164
consulBasePath: *flags.ConsulBasePath,
165+
keyFile: *flags.KeyFile,
166+
caFile: *flags.CaFile,
167+
certFile: *flags.CertFile,
158168
expandJSON: *flags.ExpandJSON,
159169
expandYAML: *flags.ExpandYAML,
160170
doSecrets: doSecrets,
@@ -205,6 +215,18 @@ func (config *config) GetRepoBasePath() string {
205215
return config.repoBasePath
206216
}
207217

218+
func (config *config) GetKeyFile() string {
219+
return config.keyFile
220+
}
221+
222+
func (config *config) GetCaFile() string {
223+
return config.caFile
224+
}
225+
226+
func (config *config) GetCertFile() string {
227+
return config.certFile
228+
}
229+
208230
func (config *config) GetRepoRootDir() string {
209231
return config.repoRootDir
210232
}

internal/config/flags_parser.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package config
33
import (
44
"fmt"
55
"os"
6+
67
"github.com/miniclip/gonsul/internal/util"
78
"github.com/namsral/flag"
89
)
@@ -20,6 +21,9 @@ type ConfigFlags struct {
2021
ConsulURL *string
2122
ConsulACL *string
2223
ConsulBasePath *string
24+
KeyFile *string
25+
CaFile *string
26+
CertFile *string
2327
ExpandJSON *bool
2428
ExpandYAML *bool
2529
SecretsFile *string
@@ -50,6 +54,9 @@ func parseFlags() ConfigFlags {
5054
flags.ConsulURL = flag.String("consul-url", "", "(REQUIRED) The Consul URL REST API endpoint (Full URL with scheme)")
5155
flags.ConsulACL = flag.String("consul-acl", "", "The Consul ACL to use (Must have write on the KV following --consul-base path)")
5256
flags.ConsulBasePath = flag.String("consul-base-path", "", "The base KV path will be prefixed to dir path")
57+
flags.KeyFile = flag.String("key-file", "", "The key path for mTls")
58+
flags.CaFile = flag.String("ca-file", "", "The ca certificate path for mTLS")
59+
flags.CertFile = flag.String("cert-file", "", "The certificate path for mTLS")
5360
flags.ExpandJSON = flag.Bool("expand-json", false, "Expand and parse JSON files as full paths? (Default false)")
5461
flags.ExpandYAML = flag.Bool("expand-yaml", false, "Expand and parse YAML files as full paths? (Default false)")
5562
flags.SecretsFile = flag.String("secrets-file", "", "A key value json file with placeholders->secrets mapping, in order to do on the fly replace")

0 commit comments

Comments
 (0)