Skip to content

Commit 6bf9704

Browse files
committed
Use ID fields for internal referencing in local source
This makes cofidctl use UUIDs everywhere instead of names. It also includes the needed API changes and CLI changes to make this happen Signed-off-by: Maartje Eyskens <maartje@eyskens.me>
1 parent 16e9ff2 commit 6bf9704

File tree

39 files changed

+1170
-732
lines changed

39 files changed

+1170
-732
lines changed

cmd/cofidectl/cmd/apbinding/apbinding.go

Lines changed: 143 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
package apbinding
55

66
import (
7+
"errors"
78
"os"
89
"strings"
910

1011
ap_binding_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/ap_binding/v1alpha1"
11-
datasourcepb "github.com/cofide/cofide-api-sdk/gen/go/proto/cofidectl_plugin/v1alpha1"
12+
datasourcepb "github.com/cofide/cofide-api-sdk/gen/go/proto/cofidectl/datasource_plugin/v1alpha2"
13+
trust_zone_proto "github.com/cofide/cofide-api-sdk/gen/go/proto/trust_zone/v1alpha1"
1214
cmdcontext "github.com/cofide/cofidectl/pkg/cmd/context"
1315
"github.com/cofide/cofidectl/pkg/plugin/datasource"
1416

17+
"slices"
18+
1519
"github.com/olekukonko/tablewriter"
1620
"github.com/spf13/cobra"
1721
)
@@ -52,8 +56,8 @@ This command will list attestation policy bindings in the Cofide configuration s
5256
`
5357

5458
type ListOpts struct {
55-
trustZone string
56-
attestationPolicy string
59+
trustZoneID string
60+
attestationPolicyID string
5761
}
5862

5963
func (c *APBindingCommand) GetListCommand() *cobra.Command {
@@ -79,33 +83,39 @@ func (c *APBindingCommand) GetListCommand() *cobra.Command {
7983
}
8084

8185
f := cmd.Flags()
82-
f.StringVar(&opts.trustZone, "trust-zone", "", "list the attestation policies bound to a specific trust zone")
83-
f.StringVar(&opts.attestationPolicy, "attestation-policy", "", "list the bindings for a specific attestation policy")
86+
f.StringVar(&opts.trustZoneID, "trust-zone-id", "", "list the attestation policies bound to a specific trust zone")
87+
f.StringVar(&opts.attestationPolicyID, "attestation-policy-id", "", "list the bindings for a specific attestation policy")
8488

8589
return cmd
8690
}
8791

8892
func (c *APBindingCommand) list(source datasource.DataSource, opts ListOpts) ([]*ap_binding_proto.APBinding, error) {
8993
filter := &datasourcepb.ListAPBindingsRequest_Filter{}
90-
if opts.trustZone != "" {
91-
filter.TrustZoneName = &opts.trustZone
94+
if opts.trustZoneID != "" {
95+
filter.TrustZoneId = &opts.trustZoneID
9296
}
93-
if opts.attestationPolicy != "" {
94-
filter.PolicyName = &opts.attestationPolicy
97+
if opts.attestationPolicyID != "" {
98+
filter.PolicyId = &opts.attestationPolicyID
9599
}
96100
return source.ListAPBindings(filter)
97101
}
98102

103+
func renderFederations(bindings []*ap_binding_proto.APBindingFederation) string {
104+
federations := []string{}
105+
for _, binding := range bindings {
106+
federations = append(federations, binding.GetTrustZoneId())
107+
}
108+
109+
return strings.Join(federations, ", ")
110+
}
111+
99112
func renderList(bindings []*ap_binding_proto.APBinding) {
100113
data := make([][]string, len(bindings))
101114
for i, binding := range bindings {
102115
data[i] = []string{
103-
// nolint:staticcheck
104-
binding.TrustZone,
105-
// nolint:staticcheck
106-
binding.Policy,
107-
// nolint:staticcheck
108-
strings.Join(binding.FederatesWith, ", "),
116+
binding.GetTrustZoneId(),
117+
binding.GetPolicyId(),
118+
renderFederations(binding.GetFederations()),
109119
}
110120
}
111121

@@ -120,9 +130,12 @@ var apBindingAddCmdDesc = `
120130
This command will bind an attestation policy to a trust zone.`
121131

122132
type AddOpts struct {
123-
trustZone string
124-
attestationPolicy string
125-
federatesWith []string
133+
trustZonename string
134+
trustZoneID string
135+
attestationPolicy string
136+
attestationPolicyID string
137+
federatesWith []string
138+
federatesWithIDs []string
126139
}
127140

128141
func (c *APBindingCommand) GetAddCommand() *cobra.Command {
@@ -138,32 +151,92 @@ func (c *APBindingCommand) GetAddCommand() *cobra.Command {
138151
return err
139152
}
140153

154+
trustZoneID := opts.trustZoneID
155+
if trustZoneID == "" {
156+
tz, err := ds.GetTrustZoneByName(opts.trustZonename)
157+
if err != nil {
158+
return err
159+
}
160+
if tz == nil {
161+
return errors.New("trust zone not found")
162+
}
163+
trustZoneID = tz.GetId()
164+
165+
}
166+
if trustZoneID == "" {
167+
return errors.New("trust zone not found")
168+
}
169+
170+
policyID := opts.attestationPolicyID
171+
if policyID == "" {
172+
policies, err := ds.ListAttestationPolicies()
173+
if err != nil {
174+
return err
175+
}
176+
for _, policy := range policies {
177+
if policy.Name == opts.attestationPolicy {
178+
policyID = policy.GetId()
179+
break
180+
}
181+
}
182+
}
183+
if policyID == "" {
184+
return errors.New("attestation policy not found")
185+
}
186+
187+
federatesWith := opts.federatesWithIDs
188+
if len(opts.federatesWith) > 0 {
189+
federatesWith = []string{}
190+
tzs, err := ds.ListTrustZones()
191+
if err != nil {
192+
return err
193+
}
194+
for _, tz := range tzs {
195+
if slices.Contains(opts.federatesWith, tz.Name) {
196+
federatesWith = append(federatesWith, tz.GetId())
197+
}
198+
}
199+
}
200+
federations := []*ap_binding_proto.APBindingFederation{}
201+
for _, federate := range federatesWith {
202+
federations = append(federations, &ap_binding_proto.APBindingFederation{
203+
TrustZoneId: &federate,
204+
})
205+
}
206+
141207
binding := &ap_binding_proto.APBinding{
142-
TrustZone: opts.trustZone,
143-
Policy: opts.attestationPolicy,
144-
FederatesWith: opts.federatesWith,
208+
TrustZoneId: &trustZoneID,
209+
PolicyId: &policyID,
210+
Federations: federations,
145211
}
146212
_, err = ds.AddAPBinding(binding)
147213
return err
148214
},
149215
}
150216

151217
f := cmd.Flags()
152-
f.StringVar(&opts.trustZone, "trust-zone", "", "Trust zone name")
218+
f.StringVar(&opts.trustZonename, "trust-zone-name", "", "Trust zone name")
219+
f.StringVar(&opts.trustZoneID, "trust-zone-id", "", "Trust zone ID")
153220
f.StringVar(&opts.attestationPolicy, "attestation-policy", "", "Attestation policy name")
221+
f.StringVar(&opts.attestationPolicy, "attestation-policy-id", "", "Attestation policy ID")
154222
f.StringSliceVar(&opts.federatesWith, "federates-with", nil, "Defines a trust zone to federate identity with. May be specified multiple times")
223+
f.StringSliceVar(&opts.federatesWithIDs, "federates-with-id", nil, "Defines a trust zone to federate identity with. May be specified multiple times")
155224

156-
cobra.CheckErr(cmd.MarkFlagRequired("trust-zone"))
157-
cobra.CheckErr(cmd.MarkFlagRequired("attestation-policy"))
158-
225+
cmd.MarkFlagsOneRequired("trust-zone-name", "trust-zone-id")
226+
cmd.MarkFlagsOneRequired("attestation-policy", "attestation-policy-id")
227+
cmd.MarkFlagsMutuallyExclusive("trust-zone-name", "trust-zone-id")
228+
cmd.MarkFlagsMutuallyExclusive("attestation-policy", "attestation-policy-id")
229+
cmd.MarkFlagsMutuallyExclusive("federates-with", "federates-with-id")
159230
return cmd
160231
}
161232

162233
var apBindingDelCmdDesc = `
163234
This command will unbind an attestation policy from a trust zone.`
164235

165236
type DelOpts struct {
166-
trustZone string
237+
id string
238+
trustZoneName string
239+
trustZoneID string
167240
attestationPolicy string
168241
}
169242

@@ -180,20 +253,57 @@ func (c *APBindingCommand) GetDelCommand() *cobra.Command {
180253
return err
181254
}
182255

183-
binding := &ap_binding_proto.APBinding{
184-
TrustZone: opts.trustZone,
185-
Policy: opts.attestationPolicy,
256+
if opts.id != "" {
257+
return ds.DestroyAPBinding(opts.id)
258+
}
259+
260+
var trustZone *trust_zone_proto.TrustZone
261+
if opts.trustZoneName != "" {
262+
trustZone, err = ds.GetTrustZoneByName(opts.trustZoneName)
263+
if err != nil {
264+
return err
265+
}
266+
}
267+
if opts.trustZoneID != "" {
268+
trustZone, err = ds.GetTrustZone(opts.trustZoneID)
269+
if err != nil {
270+
return err
271+
}
272+
}
273+
policy, err := ds.GetAttestationPolicyByName(opts.attestationPolicy)
274+
if err != nil {
275+
return err
276+
}
277+
278+
bindings, err := ds.ListAPBindings(&datasourcepb.ListAPBindingsRequest_Filter{
279+
TrustZoneId: trustZone.Id,
280+
PolicyId: policy.Id,
281+
})
282+
if err != nil {
283+
return err
284+
}
285+
if len(bindings) == 0 {
286+
return errors.New("no binding found")
287+
}
288+
if len(bindings) > 1 {
289+
return errors.New("multiple bindings found")
186290
}
187-
return ds.DestroyAPBinding(binding)
291+
binding := bindings[0]
292+
return ds.DestroyAPBinding(binding.GetId())
188293
},
189294
}
190295

191296
f := cmd.Flags()
192-
f.StringVar(&opts.trustZone, "trust-zone", "", "Trust zone name")
297+
f.StringVar(&opts.trustZoneName, "trust-zone-name", "", "Trust zone name")
298+
f.StringVar(&opts.trustZoneID, "trust-zone-id", "", "Trust zone ID")
193299
f.StringVar(&opts.attestationPolicy, "attestation-policy", "", "Attestation policy name")
300+
f.StringVar(&opts.id, "id", "", "Binding ID")
194301

195-
cobra.CheckErr(cmd.MarkFlagRequired("trust-zone"))
196-
cobra.CheckErr(cmd.MarkFlagRequired("attestation-policy"))
302+
cmd.MarkFlagsOneRequired("trust-zone-id", "trust-zone-name", "id")
303+
cmd.MarkFlagsOneRequired("attestation-policy", "id")
304+
cmd.MarkFlagsMutuallyExclusive("trust-zone-name", "id")
305+
cmd.MarkFlagsMutuallyExclusive("trust-zone-id", "id")
306+
cmd.MarkFlagsMutuallyExclusive("attestation-policy", "id")
197307

198308
return cmd
199309
}

cmd/cofidectl/cmd/attestationpolicy/attestationpolicy.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,28 +385,53 @@ func parseSelectors(selectorStrings []string) ([]*types.Selector, error) {
385385
return selectors, nil
386386
}
387387

388+
type DelStaticOpts struct {
389+
name string
390+
id string
391+
}
392+
388393
var attestationPolicyDelCmdDesc = `
389394
This command will delete an attestation policy from the Cofide configuration state.
390395
`
391396

392397
func (c *AttestationPolicyCommand) getDelCommand() *cobra.Command {
398+
opts := DelStaticOpts{}
399+
393400
cmd := &cobra.Command{
394-
Use: "del [NAME]",
401+
Use: "del",
395402
Short: "Delete an attestation policy",
396403
Long: attestationPolicyDelCmdDesc,
397-
Args: cobra.ExactArgs(1),
398404
RunE: func(cmd *cobra.Command, args []string) error {
399-
return c.deletePolicy(cmd.Context(), args[0])
405+
return c.deletePolicy(cmd.Context(), opts)
400406
},
401407
}
408+
409+
f := cmd.Flags()
410+
f.StringVar(&opts.name, "name", "", "Name of the attestation policy")
411+
f.StringVar(&opts.id, "id", "", "ID of the attestation policy")
412+
413+
cmd.MarkFlagsOneRequired("name", "id")
414+
402415
return cmd
403416
}
404417

405-
func (c *AttestationPolicyCommand) deletePolicy(ctx context.Context, name string) error {
418+
func (c *AttestationPolicyCommand) deletePolicy(ctx context.Context, opts DelStaticOpts) error {
406419
ds, err := c.cmdCtx.PluginManager.GetDataSource(ctx)
407420
if err != nil {
408421
return err
409422
}
410423

411-
return ds.DestroyAttestationPolicy(name)
424+
id := opts.id
425+
if opts.name != "" {
426+
ap, err := ds.GetAttestationPolicyByName(opts.name)
427+
if err != nil {
428+
return err
429+
}
430+
if ap == nil {
431+
return fmt.Errorf("attestation policy %q not found", opts.name)
432+
}
433+
id = ap.GetId()
434+
}
435+
436+
return ds.DestroyAttestationPolicy(id)
412437
}

0 commit comments

Comments
 (0)