@@ -23,12 +23,14 @@ import (
23
23
uuid "github.com/hashicorp/go-uuid"
24
24
"github.com/hashicorp/vault/sdk/helper/jsonutil"
25
25
"github.com/hashicorp/vault/sdk/logical"
26
+ "github.com/hashicorp/vault/sdk/physical"
26
27
"github.com/hashicorp/vault/vault/cluster"
27
28
)
28
29
29
30
const (
30
31
// Storage path where the local cluster name and identifier are stored
31
32
coreLocalClusterInfoPath = "core/cluster/local/info"
33
+ coreLocalClusterNamePath = "core/cluster/local/name"
32
34
33
35
corePrivateKeyTypeP521 = "p521"
34
36
corePrivateKeyTypeED25519 = "ed25519"
@@ -61,18 +63,30 @@ type Cluster struct {
61
63
// when Vault is sealed.
62
64
func (c * Core ) Cluster (ctx context.Context ) (* Cluster , error ) {
63
65
var cluster Cluster
66
+ var logicalEntry * logical.StorageEntry
67
+ var physicalEntry * physical.Entry
64
68
65
69
// Fetch the storage entry. This call fails when Vault is sealed.
66
- entry , err := c .barrier .Get (ctx , coreLocalClusterInfoPath )
70
+ logicalEntry , err := c .barrier .Get (ctx , coreLocalClusterInfoPath )
67
71
if err != nil {
68
- return nil , err
72
+ // Vault is sealed, pull cluster name from unencrypted storage
73
+ physicalEntry , err = c .physical .Get (ctx , coreLocalClusterNamePath )
74
+ if err != nil {
75
+ return nil , err
76
+ }
69
77
}
70
- if entry == nil {
78
+ if logicalEntry == nil && physicalEntry == nil {
71
79
return & cluster , nil
72
80
}
73
81
74
82
// Decode the cluster information
75
- if err = jsonutil .DecodeJSON (entry .Value , & cluster ); err != nil {
83
+ var value []byte
84
+ if logicalEntry != nil {
85
+ value = logicalEntry .Value
86
+ } else {
87
+ value = physicalEntry .Value
88
+ }
89
+ if err = jsonutil .DecodeJSON (value , & cluster ); err != nil {
76
90
return nil , fmt .Errorf ("failed to decode cluster details: %w" , err )
77
91
}
78
92
@@ -162,6 +176,7 @@ func (c *Core) setupCluster(ctx context.Context) error {
162
176
}
163
177
164
178
var modified bool
179
+ var generatedClusterName bool
165
180
166
181
if cluster == nil {
167
182
cluster = & Cluster {}
@@ -178,6 +193,7 @@ func (c *Core) setupCluster(ctx context.Context) error {
178
193
}
179
194
180
195
c .clusterName = fmt .Sprintf ("vault-cluster-%08x" , clusterNameBytes )
196
+ generatedClusterName = true
181
197
}
182
198
183
199
cluster .Name = c .clusterName
@@ -270,7 +286,7 @@ func (c *Core) setupCluster(ctx context.Context) error {
270
286
return err
271
287
}
272
288
273
- // Store it
289
+ // Store cluster information in logical storage
274
290
err = c .barrier .Put (ctx , & logical.StorageEntry {
275
291
Key : coreLocalClusterInfoPath ,
276
292
Value : rawCluster ,
@@ -279,6 +295,32 @@ func (c *Core) setupCluster(ctx context.Context) error {
279
295
c .logger .Error ("failed to store cluster details" , "error" , err )
280
296
return err
281
297
}
298
+
299
+ // Store only cluster name in physical storage, but only if name isn't provided in config
300
+ if generatedClusterName {
301
+ rawCluster , err = json .Marshal (& Cluster {Name : cluster .Name })
302
+ if err != nil {
303
+ c .logger .Error ("failed to marshal cluster name" , "error" , err )
304
+ return err
305
+ }
306
+
307
+ err = c .physical .Put (ctx , & physical.Entry {
308
+ Key : coreLocalClusterNamePath ,
309
+ Value : rawCluster ,
310
+ })
311
+ if err != nil {
312
+ c .logger .Error ("failed to store cluster name" , "error" , err )
313
+ return err
314
+ }
315
+ } else {
316
+ // check to ensure there is no entry at coreLocalClusterNamePath
317
+ err = c .physical .Delete (ctx , coreLocalClusterNamePath )
318
+ if err != nil {
319
+ c .logger .Error ("failed to clear cluster name" , "error" , err )
320
+ return err
321
+ }
322
+
323
+ }
282
324
}
283
325
284
326
c .clusterID .Store (cluster .ID )
0 commit comments