Skip to content

Commit 41855f7

Browse files
committed
Reuse the Kine TLS options for a DB connection
1 parent 9a0724c commit 41855f7

4 files changed

Lines changed: 29 additions & 64 deletions

File tree

pkg/drivers/fdb/fdb.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/k3s-io/kine/pkg/broadcaster"
99
"github.com/k3s-io/kine/pkg/drivers"
1010
"github.com/k3s-io/kine/pkg/server"
11+
"github.com/k3s-io/kine/pkg/tls"
1112
"github.com/sirupsen/logrus"
1213
"sync/atomic"
1314
"time"
@@ -24,10 +25,11 @@ func init() {
2425

2526
func New(_ context.Context, cfg *drivers.Config) (bool, server.Backend, error) {
2627
logrus.Info("New FDB backend")
27-
return false, NewFdbStructured(cfg.DataSourceName, Directory), nil
28+
return false, NewFdbStructured(cfg.DataSourceName, cfg.BackendTLSConfig, Directory), nil
2829
}
2930

3031
type FDB struct {
32+
tlsConfig tls.Config
3133
connectionString string
3234
dirName string
3335

@@ -46,10 +48,11 @@ type FDB struct {
4648
lastWatchRev atomic.Int64
4749
}
4850

49-
func NewFdbStructured(connectionString string, dirName string) server.Backend {
51+
func NewFdbStructured(connectionString string, tlsConfig tls.Config, dirName string) server.Backend {
5052
logrus.Infof("Creating a FoundationDB driver with directory: '%s'", dirName)
5153
ThisFDB = &FDB{
5254
connectionString: connectionString,
55+
tlsConfig: tlsConfig,
5356
dirName: dirName,
5457
}
5558
return &FdbLogger{
@@ -62,7 +65,7 @@ func (f *FDB) Start(ctx context.Context) error {
6265
fdb.MustAPIVersion(730)
6366
f.ctx = ctx
6467

65-
if err := setTLSConfig(); err != nil {
68+
if err := f.setTLSConfig(); err != nil {
6669
return err
6770
}
6871

@@ -115,29 +118,26 @@ func (f *FDB) Start(ctx context.Context) error {
115118
return nil
116119
}
117120

118-
func setTLSConfig() error {
119-
if TLSCertificateFile != "" {
120-
if err := fdb.Options().SetTLSCertPath(TLSCertificateFile); err != nil {
121+
// https://apple.github.io/foundationdb/tls.html#configuring-tls
122+
func (f *FDB) setTLSConfig() error {
123+
if f.tlsConfig.CertFile != "" {
124+
if err := fdb.Options().SetTLSCertPath(f.tlsConfig.CertFile); err != nil {
121125
return err
122126
}
123127
}
124-
if TLSKeyFile != "" {
125-
if err := fdb.Options().SetTLSKeyPath(TLSKeyFile); err != nil {
128+
if f.tlsConfig.KeyFile != "" {
129+
if err := fdb.Options().SetTLSKeyPath(f.tlsConfig.KeyFile); err != nil {
126130
return err
127131
}
128132
}
129-
if TLSPassword != "" {
130-
if err := fdb.Options().SetTLSPassword(TLSPassword); err != nil {
133+
if f.tlsConfig.CAFile != "" {
134+
if err := fdb.Options().SetTLSCaPath(f.tlsConfig.CAFile); err != nil {
131135
return err
132136
}
133137
}
134-
if TLSCAFile != "" {
135-
if err := fdb.Options().SetTLSCaPath(TLSCAFile); err != nil {
136-
return err
137-
}
138-
}
139-
if TLSVerifyPeers != "" {
140-
if err := fdb.Options().SetTLSVerifyPeers([]byte(TLSVerifyPeers)); err != nil {
138+
if f.tlsConfig.SkipVerify {
139+
// https://apple.github.io/foundationdb/tls.html#turning-down-the-validation
140+
if err := fdb.Options().SetTLSVerifyPeers([]byte("Check.Valid=0")); err != nil {
141141
return err
142142
}
143143
}

pkg/drivers/fdb/fdb_config.go

Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,7 @@ var (
88
Directory = "etcd"
99
CleanDirOnStart = false
1010
LogConflictingKeys = false
11-
12-
// FDB TLS
13-
// https://apple.github.io/foundationdb/tls.html#configuring-tls
14-
TLSCertificateFile = ""
15-
TLSKeyFile = ""
16-
TLSPassword = ""
17-
TLSCAFile = ""
18-
TLSVerifyPeers = ""
19-
11+
2012
// For testing only
2113
UseSequentialId = false
2214
APITest = false
@@ -35,31 +27,6 @@ func ConfigFlags() []cli.Flag {
3527
Usage: "Clean the directory on start. Useful for testing.",
3628
Destination: &CleanDirOnStart,
3729
},
38-
&cli.StringFlag{
39-
Name: "fdb-tls-certificate-file",
40-
Usage: "Path to the file from which the local certificates can be loaded",
41-
Destination: &TLSCertificateFile,
42-
},
43-
&cli.StringFlag{
44-
Name: "fdb-tls-key-file",
45-
Usage: "Path to the file from which to load the private key",
46-
Destination: &TLSKeyFile,
47-
},
48-
&cli.StringFlag{
49-
Name: "fdb-tls-password",
50-
Usage: "The byte-string representing the passcode for unencrypting the private key",
51-
Destination: &TLSPassword,
52-
},
53-
&cli.StringFlag{
54-
Name: "fdb-tls-ca-file",
55-
Usage: "The byte-string for the verification of peer certificates and sessions",
56-
Destination: &TLSCAFile,
57-
},
58-
&cli.StringFlag{
59-
Name: "fdb-tls-verify-peers",
60-
Usage: "The byte-string for the verification of peer certificates and sessions",
61-
Destination: &TLSVerifyPeers,
62-
},
6330
&cli.BoolFlag{
6431
Name: "fdb-log-conflicting-keys",
6532
Usage: "Log conflicting keys when a transaction conflict occurs. Useful for debugging.",

pkg/drivers/fdb/fdb_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func TestMain(m *testing.M) {
3636
func TestFDB(t *testing.T) {
3737
n := 4
3838
sameKeyN := 3
39-
f := NewFdbStructured(connectionString, "dir1")
39+
f := NewFdbStructured(connectionString,, "dir1")
4040
ctx, cancelCtx := context.WithTimeout(context.Background(), time.Duration(60)*time.Second)
4141
err := f.Start(ctx)
4242
require.NoError(t, err)
@@ -45,7 +45,7 @@ func TestFDB(t *testing.T) {
4545

4646
forceRetryTransaction = func(i int) bool { return i < 2 }
4747

48-
f = NewFdbStructured(connectionString, "dir2")
48+
f = NewFdbStructured(connectionString,, "dir2")
4949
ctx, cancelCtx = context.WithTimeout(context.Background(), time.Duration(60)*time.Second)
5050
defer cancelCtx()
5151
err = f.Start(ctx)
@@ -255,7 +255,7 @@ func TestFDB(t *testing.T) {
255255
func TestFDBLargeRecords(t *testing.T) {
256256
forceRetryTransaction = func(i int) bool { return i < 1 }
257257

258-
f := NewFdbStructured(connectionString, "dir1")
258+
f := NewFdbStructured(connectionString,, "dir1")
259259
ctx, cancelCtx := context.WithTimeout(context.Background(), time.Duration(100)*time.Second)
260260
defer cancelCtx()
261261

@@ -341,7 +341,7 @@ func TestCompaction(t *testing.T) {
341341
newValue := []byte("newVal123")
342342
updatedLease := int64(123)
343343

344-
f := NewFdbStructured(connectionString, "dir1")
344+
f := NewFdbStructured(connectionString,, "dir1")
345345
ctx, cancelCtx := context.WithTimeout(context.Background(), time.Duration(20)*time.Second)
346346
defer cancelCtx()
347347

@@ -472,7 +472,7 @@ func TestWatchAll(t *testing.T) {
472472
maxBatchSize = 10
473473
recordsCount := 53
474474

475-
f := NewFdbStructured(connectionString, "dir1")
475+
f := NewFdbStructured(connectionString,, "dir1")
476476
ctx, cancelCtx := context.WithTimeout(context.Background(), time.Duration(3)*time.Second)
477477
defer cancelCtx()
478478

@@ -502,7 +502,7 @@ func TestExceedSizeLarge(t *testing.T) {
502502
_, err := rand.Read(value)
503503
require.NoError(t, err)
504504

505-
f := NewFdbStructured(connectionString, "dir1")
505+
f := NewFdbStructured(connectionString,, "dir1")
506506
ctx, cancelCtx := context.WithTimeout(context.Background(), time.Duration(3)*time.Second)
507507
defer cancelCtx()
508508

tests/k3s/docker-compose.yaml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,17 @@ services:
3939
dockerfile: Dockerfile
4040
volumes:
4141
- ./fdb_pki:/opt/fdb/tls:ro
42+
environment:
43+
FDB_TLS_PASSWORD: client_password
4244
command:
4345
- --endpoint
4446
- "fdb://docker:docker@fdb:4500:tls"
45-
- --fdb-tls-certificate-file
47+
- --cert-file
4648
- "/opt/fdb/tls/client-certificate.pem"
47-
- --fdb-tls-key-file
49+
- --key-file
4850
- "/opt/fdb/tls/client-private-key.pem"
49-
- --fdb-tls-password
50-
- "client_password"
51-
- --fdb-tls-ca-file
51+
- --ca-file
5252
- "/opt/fdb/tls/ca-certificate.pem"
53-
- --fdb-tls-verify-peers
54-
- "Check.Valid=1"
5553
k3s:
5654
container_name: k3s
5755
depends_on:

0 commit comments

Comments
 (0)