Skip to content

Commit f269080

Browse files
oktalzmjuraga
authored andcommitted
BUG/MINOR: always check if storage-dir is acceptable
1 parent 278578d commit f269080

File tree

3 files changed

+41
-24
lines changed

3 files changed

+41
-24
lines changed

configuration/cluster_sync.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ func (c *ClusterSync) monitorBootstrapKey() {
219219
if err != nil {
220220
log.Warning(err)
221221
}
222+
// check if storage key is ok
223+
errStorageDir := CheckIfStorageDirIsOK(data["storage-dir"], c.cfg)
224+
if errStorageDir != err {
225+
log.Error(errStorageDir)
226+
continue
227+
}
222228
url := fmt.Sprintf("%s://%s", data["schema"], data["address"])
223229
c.cfg.Cluster.URL.Store(url)
224230
c.cfg.Cluster.Port.Store(func() int {

configuration/misc.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ package configuration
1818
import (
1919
"encoding/base64"
2020
"encoding/json"
21+
"errors"
2122
"fmt"
2223
"os"
24+
"path"
2325
"syscall"
26+
27+
"github.com/haproxytech/client-native/v2/misc"
28+
"github.com/haproxytech/client-native/v2/storage"
2429
)
2530

2631
func DecodeBootstrapKey(key string) (map[string]string, error) {
@@ -52,3 +57,29 @@ func fileExists(filename string) bool {
5257
}
5358
return !info.IsDir()
5459
}
60+
61+
func CheckIfStorageDirIsOK(storageDir string, config *Configuration) error {
62+
if storageDir == "" {
63+
return errors.New("storage-dir in bootstrap key is empty")
64+
}
65+
_, errStorage := misc.CheckOrCreateWritableDirectory(storageDir)
66+
if errStorage != nil {
67+
return errStorage
68+
}
69+
dirs := []storage.FileType{
70+
storage.BackupsType, storage.MapsType, storage.SSLType,
71+
storage.SpoeTransactionsType, storage.SpoeType,
72+
storage.TransactionsType,
73+
storage.FileType("certs-cluster"),
74+
}
75+
for _, dir := range dirs {
76+
_, errStorage := misc.CheckOrCreateWritableDirectory(path.Join(storageDir, string(dir)))
77+
if errStorage != nil {
78+
return errStorage
79+
}
80+
}
81+
config.Cluster.StorageDir.Store(storageDir)
82+
config.HAProxy.ClusterTLSCertDir = path.Join(storageDir, "certs-cluster")
83+
config.Cluster.CertificateDir.Store(path.Join(storageDir, "certs-cluster"))
84+
return nil
85+
}

handlers/cluster.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ package handlers
1717

1818
import (
1919
"fmt"
20-
"path"
2120

2221
"github.com/go-openapi/runtime/middleware"
2322
"github.com/google/renameio"
2423
client_native "github.com/haproxytech/client-native/v2"
25-
"github.com/haproxytech/client-native/v2/misc"
2624
"github.com/haproxytech/client-native/v2/models"
27-
"github.com/haproxytech/client-native/v2/storage"
2825

2926
"github.com/haproxytech/dataplaneapi/configuration"
3027
"github.com/haproxytech/dataplaneapi/haproxy"
@@ -104,28 +101,11 @@ func (h *CreateClusterHandlerImpl) Handle(params cluster.PostClusterParams, prin
104101
if err != nil {
105102
return h.err406(err, nil)
106103
}
107-
storageDir := key["storage-dir"]
108-
if storageDir != "" {
109-
_, errStorage := misc.CheckOrCreateWritableDirectory(storageDir)
110-
if errStorage != nil {
111-
return h.err409(errStorage, nil)
112-
}
113-
dirs := []storage.FileType{
114-
storage.BackupsType, storage.MapsType, storage.SSLType,
115-
storage.SpoeTransactionsType, storage.SpoeType,
116-
storage.TransactionsType,
117-
storage.FileType("certs-cluster"),
118-
}
119-
for _, dir := range dirs {
120-
_, errStorage := misc.CheckOrCreateWritableDirectory(path.Join(storageDir, string(dir)))
121-
if errStorage != nil {
122-
return h.err409(errStorage, nil)
123-
}
124-
}
125-
h.Config.Cluster.StorageDir.Store(storageDir)
126-
h.Config.HAProxy.ClusterTLSCertDir = path.Join(storageDir, "certs-cluster")
127-
h.Config.Cluster.CertificateDir.Store(path.Join(storageDir, "certs-cluster"))
104+
errStorageDir := configuration.CheckIfStorageDirIsOK(key["storage-dir"], h.Config)
105+
if errStorageDir != nil {
106+
return h.err409(errStorageDir, nil)
128107
}
108+
129109
// enforcing API advertising options
130110
if a := params.AdvertisedAddress; a != nil {
131111
h.Config.APIOptions.APIAddress = *a

0 commit comments

Comments
 (0)