4
4
package apbinding
5
5
6
6
import (
7
+ "errors"
7
8
"os"
8
9
"strings"
9
10
10
11
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"
12
14
cmdcontext "github.com/cofide/cofidectl/pkg/cmd/context"
13
15
"github.com/cofide/cofidectl/pkg/plugin/datasource"
14
16
17
+ "slices"
18
+
15
19
"github.com/olekukonko/tablewriter"
16
20
"github.com/spf13/cobra"
17
21
)
@@ -52,8 +56,8 @@ This command will list attestation policy bindings in the Cofide configuration s
52
56
`
53
57
54
58
type ListOpts struct {
55
- trustZone string
56
- attestationPolicy string
59
+ trustZoneID string
60
+ attestationPolicyID string
57
61
}
58
62
59
63
func (c * APBindingCommand ) GetListCommand () * cobra.Command {
@@ -79,33 +83,39 @@ func (c *APBindingCommand) GetListCommand() *cobra.Command {
79
83
}
80
84
81
85
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" )
84
88
85
89
return cmd
86
90
}
87
91
88
92
func (c * APBindingCommand ) list (source datasource.DataSource , opts ListOpts ) ([]* ap_binding_proto.APBinding , error ) {
89
93
filter := & datasourcepb.ListAPBindingsRequest_Filter {}
90
- if opts .trustZone != "" {
91
- filter .TrustZoneName = & opts .trustZone
94
+ if opts .trustZoneID != "" {
95
+ filter .TrustZoneId = & opts .trustZoneID
92
96
}
93
- if opts .attestationPolicy != "" {
94
- filter .PolicyName = & opts .attestationPolicy
97
+ if opts .attestationPolicyID != "" {
98
+ filter .PolicyId = & opts .attestationPolicyID
95
99
}
96
100
return source .ListAPBindings (filter )
97
101
}
98
102
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
+
99
112
func renderList (bindings []* ap_binding_proto.APBinding ) {
100
113
data := make ([][]string , len (bindings ))
101
114
for i , binding := range bindings {
102
115
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 ()),
109
119
}
110
120
}
111
121
@@ -120,9 +130,12 @@ var apBindingAddCmdDesc = `
120
130
This command will bind an attestation policy to a trust zone.`
121
131
122
132
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
126
139
}
127
140
128
141
func (c * APBindingCommand ) GetAddCommand () * cobra.Command {
@@ -138,32 +151,92 @@ func (c *APBindingCommand) GetAddCommand() *cobra.Command {
138
151
return err
139
152
}
140
153
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
+
141
207
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 ,
145
211
}
146
212
_ , err = ds .AddAPBinding (binding )
147
213
return err
148
214
},
149
215
}
150
216
151
217
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" )
153
220
f .StringVar (& opts .attestationPolicy , "attestation-policy" , "" , "Attestation policy name" )
221
+ f .StringVar (& opts .attestationPolicy , "attestation-policy-id" , "" , "Attestation policy ID" )
154
222
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" )
155
224
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" )
159
230
return cmd
160
231
}
161
232
162
233
var apBindingDelCmdDesc = `
163
234
This command will unbind an attestation policy from a trust zone.`
164
235
165
236
type DelOpts struct {
166
- trustZone string
237
+ id string
238
+ trustZoneName string
239
+ trustZoneID string
167
240
attestationPolicy string
168
241
}
169
242
@@ -180,20 +253,57 @@ func (c *APBindingCommand) GetDelCommand() *cobra.Command {
180
253
return err
181
254
}
182
255
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" )
186
290
}
187
- return ds .DestroyAPBinding (binding )
291
+ binding := bindings [0 ]
292
+ return ds .DestroyAPBinding (binding .GetId ())
188
293
},
189
294
}
190
295
191
296
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" )
193
299
f .StringVar (& opts .attestationPolicy , "attestation-policy" , "" , "Attestation policy name" )
300
+ f .StringVar (& opts .id , "id" , "" , "Binding ID" )
194
301
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" )
197
307
198
308
return cmd
199
309
}
0 commit comments