@@ -18,8 +18,19 @@ import (
18
18
"github.com/cofide/cofidectl/internal/pkg/config"
19
19
"github.com/cofide/cofidectl/internal/pkg/proto"
20
20
"github.com/cofide/cofidectl/pkg/plugin/datasource"
21
+
22
+ "github.com/google/uuid"
21
23
)
22
24
25
+ func generateId () (* string , error ) {
26
+ uid , err := uuid .NewUUID ()
27
+ if err != nil {
28
+ return nil , fmt .Errorf ("failed to generate UUID: %w" , err )
29
+ }
30
+ id := uid .String ()
31
+ return & id , nil
32
+ }
33
+
23
34
var _ datasource.DataSource = (* LocalDataSource )(nil )
24
35
25
36
type LocalDataSource struct {
@@ -70,10 +81,20 @@ func (lds *LocalDataSource) updateDataFile() error {
70
81
}
71
82
72
83
func (lds * LocalDataSource ) AddTrustZone (trustZone * trust_zone_proto.TrustZone ) (* trust_zone_proto.TrustZone , error ) {
84
+ if trustZone .GetId () != "" {
85
+ return nil , fmt .Errorf ("trust zone %s should not have an ID set, this will be auto generated" , trustZone .GetId ())
86
+ }
87
+
88
+ id , err := generateId ()
89
+ if err != nil {
90
+ return nil , fmt .Errorf ("failed to generate UUID for trust zone: %w" , err )
91
+ }
92
+ trustZone .Id = id
93
+
73
94
if _ , ok := lds .config .GetTrustZoneByName (trustZone .Name ); ok {
74
95
return nil , fmt .Errorf ("trust zone %s already exists in local config" , trustZone .Name )
75
96
}
76
- trustZone , err : = proto .CloneTrustZone (trustZone )
97
+ trustZone , err = proto .CloneTrustZone (trustZone )
77
98
if err != nil {
78
99
return nil , err
79
100
}
@@ -187,6 +208,15 @@ func (lds *LocalDataSource) AddCluster(cluster *clusterpb.Cluster) (*clusterpb.C
187
208
name := cluster .GetName ()
188
209
trustZone := cluster .GetTrustZone ()
189
210
211
+ if cluster .GetId () != "" {
212
+ return nil , fmt .Errorf ("cluster %s should not have an ID set, this will be auto generated" , cluster .GetId ())
213
+ }
214
+ id , err := generateId ()
215
+ if err != nil {
216
+ return nil , fmt .Errorf ("failed to generate UUID for cluster: %w" , err )
217
+ }
218
+ cluster .Id = id
219
+
190
220
if _ , ok := lds .config .GetClusterByName (name , trustZone ); ok {
191
221
return nil , fmt .Errorf ("cluster %s already exists in trust zone %s in local config" , name , trustZone )
192
222
}
@@ -195,7 +225,7 @@ func (lds *LocalDataSource) AddCluster(cluster *clusterpb.Cluster) (*clusterpb.C
195
225
return nil , fmt .Errorf ("trust zone %s already has a cluster" , trustZone )
196
226
}
197
227
198
- cluster , err : = proto .CloneCluster (cluster )
228
+ cluster , err = proto .CloneCluster (cluster )
199
229
if err != nil {
200
230
return nil , err
201
231
}
@@ -306,10 +336,20 @@ func validateTrustProviderUpdate(cluster, tzName string, current, new *trust_pro
306
336
}
307
337
308
338
func (lds * LocalDataSource ) AddAttestationPolicy (policy * attestation_policy_proto.AttestationPolicy ) (* attestation_policy_proto.AttestationPolicy , error ) {
339
+ if policy .GetId () != "" {
340
+ return nil , fmt .Errorf ("attestation policy %s should not have an ID set, this will be auto generated" , * policy .Id )
341
+ }
342
+
343
+ id , err := generateId ()
344
+ if err != nil {
345
+ return nil , fmt .Errorf ("failed to generate UUID for attestation policy: %w" , err )
346
+ }
347
+ policy .Id = id
348
+
309
349
if _ , ok := lds .config .GetAttestationPolicyByName (policy .Name ); ok {
310
350
return nil , fmt .Errorf ("attestation policy %s already exists in local config" , policy .Name )
311
351
}
312
- policy , err : = proto .CloneAttestationPolicy (policy )
352
+ policy , err = proto .CloneAttestationPolicy (policy )
313
353
if err != nil {
314
354
return nil , err
315
355
}
@@ -364,6 +404,16 @@ func (lds *LocalDataSource) ListAttestationPolicies() ([]*attestation_policy_pro
364
404
}
365
405
366
406
func (lds * LocalDataSource ) AddAPBinding (binding * ap_binding_proto.APBinding ) (* ap_binding_proto.APBinding , error ) {
407
+ if binding .GetId () != "" {
408
+ return nil , fmt .Errorf ("attestation policy binding %s should not have an ID set, this will be auto generated" , * binding .Id )
409
+ }
410
+
411
+ id , err := generateId ()
412
+ if err != nil {
413
+ return nil , fmt .Errorf ("failed to generate UUID for attestation policy binding: %w" , err )
414
+ }
415
+ binding .Id = id
416
+
367
417
// nolint:staticcheck
368
418
localTrustZone , ok := lds .config .GetTrustZoneByName (binding .TrustZone )
369
419
if ! ok {
@@ -410,7 +460,7 @@ func (lds *LocalDataSource) AddAPBinding(binding *ap_binding_proto.APBinding) (*
410
460
}
411
461
}
412
462
413
- binding , err : = proto .CloneAPBinding (binding )
463
+ binding , err = proto .CloneAPBinding (binding )
414
464
if err != nil {
415
465
return nil , err
416
466
}
@@ -477,6 +527,13 @@ func (lds *LocalDataSource) ListAPBindings(filter *datasourcepb.ListAPBindingsRe
477
527
}
478
528
479
529
func (lds * LocalDataSource ) AddFederation (federationProto * federation_proto.Federation ) (* federation_proto.Federation , error ) {
530
+ if federationProto .Id == nil || * federationProto .Id == "" {
531
+ id , err := generateId ()
532
+ if err != nil {
533
+ return nil , fmt .Errorf ("failed to generate UUID for federation: %w" , err )
534
+ }
535
+ federationProto .Id = id
536
+ }
480
537
// nolint:staticcheck
481
538
fromTrustZone , ok := lds .config .GetTrustZoneByName (federationProto .From )
482
539
if ! ok {
0 commit comments