@@ -28,9 +28,11 @@ import (
2828 "fmt"
2929 "io/ioutil"
3030 "net/http"
31+ "path"
3132 "strings"
3233 "time"
3334
35+ "github.com/google/renameio"
3436 client_native "github.com/haproxytech/client-native/v2"
3537 "github.com/haproxytech/config-parser/v2/types"
3638 "github.com/haproxytech/dataplaneapi/haproxy"
@@ -75,7 +77,7 @@ func (c *ClusterSync) Monitor(cfg *Configuration, cli *client_native.HAProxyClie
7577 go c .fetchCert ()
7678
7779 key := c .cfg .BootstrapKey .Load ()
78- certFetched := cfg .Cluster .CertFetched .Load ()
80+ certFetched := cfg .Cluster .Certificate . Fetched .Load ()
7981
8082 if key != "" && ! certFetched {
8183 c .cfg .Notify .BootstrapKeyChanged .Notify ()
@@ -99,7 +101,7 @@ func (c *ClusterSync) monitorCertificateRefresh() {
99101 log .Warning (err )
100102 continue
101103 }
102- err = ioutil .WriteFile (c .cfg .Cluster . CertificateCSR . Load (), []byte (csr ), 0644 )
104+ err = renameio .WriteFile (path . Join ( c .cfg .GetClusterCertDir (), fmt . Sprintf ( "dataplane-%s-csr.crt" , c . cfg . Name . Load ()) ), []byte (csr ), 0644 )
103105 if err != nil {
104106 log .Warning (err )
105107 continue
@@ -154,12 +156,12 @@ func (c *ClusterSync) issueRefreshRequest(url, port, basePath string, nodesPath
154156 return err
155157 }
156158 log .Infof ("Cluster re joined, status: %s" , responseData .Status )
157- err = ioutil .WriteFile (c .cfg .Cluster . CertificatePath . Load (), []byte (responseData . Certificate ), 0644 )
159+ err = renameio .WriteFile (path . Join ( c .cfg .GetClusterCertDir (), fmt . Sprintf ( "dataplane-%s.crt" , c . cfg . Name . Load ())) , []byte (csr ), 0644 )
158160 if err != nil {
159161 log .Warning (err )
160162 return err
161163 }
162- err = ioutil .WriteFile (c .cfg .Cluster . CertificateKeyPath . Load (), []byte (key ), 0644 )
164+ err = renameio .WriteFile (path . Join ( c .cfg .GetClusterCertDir (), fmt . Sprintf ( "dataplane-%s.key" , c . cfg . Name . Load ()) ), []byte (key ), 0644 )
163165 if err != nil {
164166 log .Warning (err )
165167 return err
@@ -177,7 +179,7 @@ func (c *ClusterSync) issueRefreshRequest(url, port, basePath string, nodesPath
177179func (c * ClusterSync ) monitorBootstrapKey () {
178180 for range c .cfg .Notify .BootstrapKeyChanged .Subscribe ("monitorBootstrapKey" ) {
179181 key := c .cfg .BootstrapKey .Load ()
180- c .cfg .Cluster .CertFetched .Store (false )
182+ c .cfg .Cluster .Certificate . Fetched .Store (false )
181183 if key == "" {
182184 //do we need to delete cert here maybe?
183185 c .cfg .Cluster .ActiveBootstrapKey .Store ("" )
@@ -188,7 +190,7 @@ func (c *ClusterSync) monitorBootstrapKey() {
188190 continue
189191 }
190192 if key == c .cfg .Cluster .ActiveBootstrapKey .Load () {
191- fetched := c .cfg .Cluster .CertFetched .Load ()
193+ fetched := c .cfg .Cluster .Certificate . Fetched .Load ()
192194 if ! fetched {
193195 c .certFetch <- struct {}{}
194196 }
@@ -215,12 +217,12 @@ func (c *ClusterSync) monitorBootstrapKey() {
215217 log .Warning (err )
216218 continue
217219 }
218- err = ioutil .WriteFile (c .cfg .Cluster . CertificateKeyPath . Load (), []byte (key ), 0644 )
220+ err = renameio .WriteFile (path . Join ( c .cfg .GetClusterCertDir (), fmt . Sprintf ( "dataplane-%s.key" , c . cfg . Name . Load ()) ), []byte (key ), 0644 )
219221 if err != nil {
220222 log .Warning (err )
221223 continue
222224 }
223- err = ioutil .WriteFile (c .cfg .Cluster . CertificateCSR . Load (), []byte (csr ), 0644 )
225+ err = renameio .WriteFile (path . Join ( c .cfg .GetClusterCertDir (), fmt . Sprintf ( "dataplane-%s-csr.crt" , c . cfg . Name . Load ()) ), []byte (csr ), 0644 )
224226 if err != nil {
225227 log .Warning (err )
226228 continue
@@ -388,12 +390,12 @@ func (c *ClusterSync) checkCertificate(node Node) (fetched bool, err error) {
388390 c .cfg .Status .Store ("unconfigured" )
389391 return false , nil
390392 }
391- err = ioutil .WriteFile (c .cfg .Cluster . CertificatePath . Load (), []byte (node .Certificate ), 0644 )
393+ err = renameio .WriteFile (path . Join ( c .cfg .GetClusterCertDir (), fmt . Sprintf ( "dataplane-%s.crt" , c . cfg . Name . Load ()) ), []byte (node .Certificate ), 0644 )
392394 if err != nil {
393395 c .cfg .Status .Store ("unconfigured" )
394396 return false , err
395397 }
396- c .cfg .Cluster .CertFetched .Store (true )
398+ c .cfg .Cluster .Certificate . Fetched .Store (true )
397399 c .cfg .Notify .Reload .Notify ()
398400 c .cfg .Status .Store ("active" )
399401 return true , nil
@@ -414,7 +416,7 @@ func (c *ClusterSync) fetchCert() {
414416 continue
415417 }
416418 //if not, sleep and start all over again
417- certFetched := c .cfg .Cluster .CertFetched .Load ()
419+ certFetched := c .cfg .Cluster .Certificate . Fetched .Load ()
418420 if ! certFetched {
419421 url := c .cfg .Cluster .URL .Load ()
420422 port := c .cfg .Cluster .Port .Load ()
0 commit comments