Skip to content

Commit edbb5a2

Browse files
authored
Merge pull request #208 from cofide/me/add-resource-uuid
Add UUIDs to local resources
2 parents f9c956d + 9f1b04b commit edbb5a2

File tree

4 files changed

+72
-5
lines changed

4 files changed

+72
-5
lines changed

internal/pkg/config/schema.cue

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#DataSource: string
22

33
#TrustZone: {
4+
id?: string
45
name!: string
56
trust_domain!: string
67
bundle_endpoint_url?: string
@@ -32,6 +33,7 @@
3233
}
3334

3435
#Cluster: {
36+
id?: string
3537
name!: string
3638
trust_zone!: string
3739
kubernetes_context!: string
@@ -47,12 +49,14 @@
4749
}
4850

4951
#APBinding: {
52+
id?: string
5053
trust_zone!: string
5154
policy!: string
5255
federates_with: [...string]
5356
}
5457

5558
#AttestationPolicy: {
59+
id?: string
5660
name!: string
5761
#APKubernetes | #APStatic
5862
}
@@ -89,6 +93,7 @@
8993
}
9094

9195
#Federation: {
96+
id?: string
9297
from!: string
9398
to!: string
9499
}

internal/pkg/config/validator_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func TestValidator_ValidateInvalid(t *testing.T) {
9494
{
9595
name: "missing attestation policy field",
9696
data: string(readTestConfig(t, "missing_attestation_policy_field.yaml")),
97-
wantErr: "attestation_policies.0: incomplete value {name:\"ap1\",kubernetes?:{namespace_selector?:{match_labels?:{},match_expressions?:[]},pod_selector?:{match_labels?:{},match_expressions?:[]}}} | {name:\"ap1\",static?:{spiffe_id!:string,selectors!:[]}}",
97+
wantErr: "attestation_policies.0: incomplete value {name:\"ap1\",id?:string,kubernetes?:{namespace_selector?:{match_labels?:{},match_expressions?:[]},pod_selector?:{match_labels?:{},match_expressions?:[]}}} | {name:\"ap1\",id?:string,static?:{spiffe_id!:string,selectors!:[]}}",
9898
},
9999
{
100100
name: "plugins not a map",

pkg/plugin/local/local.go

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,19 @@ import (
1818
"github.com/cofide/cofidectl/internal/pkg/config"
1919
"github.com/cofide/cofidectl/internal/pkg/proto"
2020
"github.com/cofide/cofidectl/pkg/plugin/datasource"
21+
22+
"github.com/google/uuid"
2123
)
2224

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+
2334
var _ datasource.DataSource = (*LocalDataSource)(nil)
2435

2536
type LocalDataSource struct {
@@ -70,10 +81,20 @@ func (lds *LocalDataSource) updateDataFile() error {
7081
}
7182

7283
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+
7394
if _, ok := lds.config.GetTrustZoneByName(trustZone.Name); ok {
7495
return nil, fmt.Errorf("trust zone %s already exists in local config", trustZone.Name)
7596
}
76-
trustZone, err := proto.CloneTrustZone(trustZone)
97+
trustZone, err = proto.CloneTrustZone(trustZone)
7798
if err != nil {
7899
return nil, err
79100
}
@@ -187,6 +208,15 @@ func (lds *LocalDataSource) AddCluster(cluster *clusterpb.Cluster) (*clusterpb.C
187208
name := cluster.GetName()
188209
trustZone := cluster.GetTrustZone()
189210

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+
190220
if _, ok := lds.config.GetClusterByName(name, trustZone); ok {
191221
return nil, fmt.Errorf("cluster %s already exists in trust zone %s in local config", name, trustZone)
192222
}
@@ -195,7 +225,7 @@ func (lds *LocalDataSource) AddCluster(cluster *clusterpb.Cluster) (*clusterpb.C
195225
return nil, fmt.Errorf("trust zone %s already has a cluster", trustZone)
196226
}
197227

198-
cluster, err := proto.CloneCluster(cluster)
228+
cluster, err = proto.CloneCluster(cluster)
199229
if err != nil {
200230
return nil, err
201231
}
@@ -306,10 +336,20 @@ func validateTrustProviderUpdate(cluster, tzName string, current, new *trust_pro
306336
}
307337

308338
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+
309349
if _, ok := lds.config.GetAttestationPolicyByName(policy.Name); ok {
310350
return nil, fmt.Errorf("attestation policy %s already exists in local config", policy.Name)
311351
}
312-
policy, err := proto.CloneAttestationPolicy(policy)
352+
policy, err = proto.CloneAttestationPolicy(policy)
313353
if err != nil {
314354
return nil, err
315355
}
@@ -364,6 +404,16 @@ func (lds *LocalDataSource) ListAttestationPolicies() ([]*attestation_policy_pro
364404
}
365405

366406
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+
367417
// nolint:staticcheck
368418
localTrustZone, ok := lds.config.GetTrustZoneByName(binding.TrustZone)
369419
if !ok {
@@ -410,7 +460,7 @@ func (lds *LocalDataSource) AddAPBinding(binding *ap_binding_proto.APBinding) (*
410460
}
411461
}
412462

413-
binding, err := proto.CloneAPBinding(binding)
463+
binding, err = proto.CloneAPBinding(binding)
414464
if err != nil {
415465
return nil, err
416466
}
@@ -477,6 +527,13 @@ func (lds *LocalDataSource) ListAPBindings(filter *datasourcepb.ListAPBindingsRe
477527
}
478528

479529
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+
}
480537
// nolint:staticcheck
481538
fromTrustZone, ok := lds.config.GetTrustZoneByName(federationProto.From)
482539
if !ok {

pkg/plugin/local/local_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func TestLocalDataSource_AddTrustZone(t *testing.T) {
132132
gotTrustZone, ok := gotConfig.GetTrustZoneByName(tt.trustZone.Name)
133133
assert.True(t, ok)
134134
assert.EqualExportedValues(t, tt.trustZone, gotTrustZone)
135+
assert.NotNil(t, gotTrustZone.Id)
135136
}
136137
})
137138
}
@@ -447,6 +448,7 @@ func TestLocalDataSource_AddCluster(t *testing.T) {
447448
gotCluster, ok := gotConfig.GetClusterByName(tt.cluster.GetName(), tt.cluster.GetTrustZone())
448449
assert.True(t, ok)
449450
assert.EqualExportedValues(t, tt.cluster, gotCluster)
451+
assert.NotNil(t, gotCluster.Id)
450452
}
451453
})
452454
}
@@ -775,6 +777,7 @@ func TestLocalDataSource_AddAttestationPolicy(t *testing.T) {
775777
gotPolicy, ok := gotConfig.GetAttestationPolicyByName(tt.policy.Name)
776778
assert.True(t, ok)
777779
assert.EqualExportedValues(t, tt.policy, gotPolicy)
780+
assert.NotNil(t, gotPolicy.Id)
778781
}
779782
})
780783
}
@@ -1025,6 +1028,7 @@ func TestLocalDataSource_AddAPBinding(t *testing.T) {
10251028
// nolint:staticcheck
10261029
gotBinding := gotConfig.TrustZones[0].AttestationPolicies[1]
10271030
assert.EqualExportedValues(t, tt.binding, gotBinding)
1031+
assert.NotNil(t, gotBinding.Id)
10281032
}
10291033
})
10301034
}
@@ -1268,6 +1272,7 @@ func TestLocalDataSource_AddFederation(t *testing.T) {
12681272
// nolint:staticcheck
12691273
gotFederation := gotConfig.TrustZones[0].Federations[1]
12701274
assert.EqualExportedValues(t, tt.federation, gotFederation)
1275+
assert.NotNil(t, gotFederation.Id)
12711276
}
12721277
})
12731278
}

0 commit comments

Comments
 (0)