Skip to content

Commit 6d8a0c8

Browse files
cisco-nxos-provider: refactor Trustpoint configuration (#45)
This patch rewrites the implementation for configuring PKI trustpoints on Cisco NX-OS devices. It supports the following configuration: ``` crypto ca trustpoint <name> revocation-check crl ! crypto ca import <name> pkcs12 <file> ``` Certificates are imported using the gNOI Cert service.
1 parent bdc24c5 commit 6d8a0c8

File tree

3 files changed

+25
-49
lines changed

3 files changed

+25
-49
lines changed

internal/provider/cisco/nxos/crypto/trustpoint.go

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,42 +10,28 @@ import (
1010
"github.com/ironcore-dev/network-operator/internal/provider/cisco/nxos/gnmiext"
1111
)
1212

13+
var _ gnmiext.DeviceConf = (*Trustpoint)(nil)
14+
1315
type Trustpoint struct {
1416
ID string
1517
}
1618

17-
var _ gnmiext.DeviceConf = Trustpoints{}
18-
19-
type Trustpoints []*Trustpoint
20-
21-
func (t Trustpoints) ToYGOT(_ gnmiext.Client) ([]gnmiext.Update, error) {
22-
items := &nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems{TPList: make(map[string]*nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems_TPList, len(t))}
23-
for _, tp := range t {
24-
list := &nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems_TPList{
25-
Name: ygot.String(tp.ID),
26-
}
27-
list.PopulateDefaults()
28-
29-
if err := items.AppendTPList(list); err != nil {
30-
return nil, err
31-
}
32-
}
33-
19+
func (t *Trustpoint) ToYGOT(_ gnmiext.Client) ([]gnmiext.Update, error) {
20+
v := &nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems_TPList{}
21+
v.PopulateDefaults()
22+
v.Name = ygot.String(t.ID)
3423
return []gnmiext.Update{
3524
gnmiext.ReplacingUpdate{
36-
XPath: "System/userext-items/pkiext-items/tp-items",
37-
Value: items,
25+
XPath: "System/userext-items/pkiext-items/tp-items/TP-list[name=" + t.ID + "]",
26+
Value: v,
3827
},
3928
}, nil
4029
}
4130

42-
func (t Trustpoints) Reset(_ gnmiext.Client) ([]gnmiext.Update, error) {
43-
items := &nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems{}
44-
items.PopulateDefaults()
31+
func (t *Trustpoint) Reset(_ gnmiext.Client) ([]gnmiext.Update, error) {
4532
return []gnmiext.Update{
46-
gnmiext.ReplacingUpdate{
47-
XPath: "System/userext-items/pkiext-items/tp-items",
48-
Value: items,
33+
gnmiext.DeletingUpdate{
34+
XPath: "System/userext-items/pkiext-items/tp-items/TP-list[name=" + t.ID + "]",
4935
},
5036
}, nil
5137
}

internal/provider/cisco/nxos/crypto/trustpoint_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
func Test_Trustpoint(t *testing.T) {
17-
tp := &Trustpoints{{ID: "mytrustpoint"}}
17+
tp := &Trustpoint{ID: "mytrustpoint"}
1818

1919
got, err := tp.ToYGOT(&gnmiext.ClientMock{})
2020
if err != nil {
@@ -30,26 +30,22 @@ func Test_Trustpoint(t *testing.T) {
3030
t.Errorf("expected value to be of type ReplacingUpdate")
3131
}
3232

33-
if update.XPath != "System/userext-items/pkiext-items/tp-items" {
34-
t.Errorf("expected key 'System/userext-items/pkiext-items/tp-items' to be present")
33+
if update.XPath != "System/userext-items/pkiext-items/tp-items/TP-list[name=mytrustpoint]" {
34+
t.Errorf("expected key 'System/userext-items/pkiext-items/tp-items/TP-list[name=mytrustpoint]' to be present")
3535
}
3636

37-
ti, ok := update.Value.(*nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems)
37+
ti, ok := update.Value.(*nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems_TPList)
3838
if !ok {
3939
t.Errorf("expected value to be of type *nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems")
4040
}
4141

42-
want := &nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems{
43-
TPList: map[string]*nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems_TPList{
44-
"mytrustpoint": {
45-
Name: ygot.String("mytrustpoint"),
46-
KeyType: nxos.Cisco_NX_OSDevice_Pki_KeyType_Type_RSA,
47-
RevokeCheckConf: nxos.Cisco_NX_OSDevice_Pki_CertRevokeCheck_crl,
48-
EnrollmentType: nxos.Cisco_NX_OSDevice_Pki_CertEnrollType_none,
49-
},
50-
},
42+
want := &nxos.Cisco_NX_OSDevice_System_UserextItems_PkiextItems_TpItems_TPList{
43+
Name: ygot.String("mytrustpoint"),
44+
KeyType: nxos.Cisco_NX_OSDevice_Pki_KeyType_Type_RSA,
45+
RevokeCheckConf: nxos.Cisco_NX_OSDevice_Pki_CertRevokeCheck_crl,
46+
EnrollmentType: nxos.Cisco_NX_OSDevice_Pki_CertEnrollType_none,
5147
}
5248
if !reflect.DeepEqual(ti, want) {
53-
t.Errorf("unexpected value for 'System/userext-items/pkiext-items/tp-items': got=%+v, want=%+v", ti, want)
49+
t.Errorf("unexpected value for 'System/userext-items/pkiext-items/tp-items/TP-list[name=mytrustpoint]': got=%+v, want=%+v", ti, want)
5450
}
5551
}

internal/provider/cisco/nxos/provider.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -413,17 +413,11 @@ func (step *Trustpoints) Exec(ctx context.Context, s *Scope) error {
413413
if step.Spec == nil {
414414
return nil
415415
}
416-
t := make(crypto.Trustpoints, 0, len(step.Spec.Certificates))
417-
for _, trustpoint := range step.Spec.Certificates {
418-
t = append(t, &crypto.Trustpoint{ID: trustpoint.Name})
419-
}
420-
if err := s.GNMI.Update(ctx, t); err != nil {
421-
return err
422-
}
423-
if step.DryRun {
424-
return nil
425-
}
426416
for _, trustpoint := range step.Spec.Certificates {
417+
tp := &crypto.Trustpoint{ID: trustpoint.Name}
418+
if err := s.GNMI.Update(ctx, tp); err != nil {
419+
return fmt.Errorf("failed to get trustpoint %s: %w", trustpoint.Name, err)
420+
}
427421
cert, err := s.Client.Certificate(ctx, trustpoint.Source.SecretRef)
428422
if err != nil {
429423
return fmt.Errorf("failed to get trustpoint certificate from secret: %w", err)

0 commit comments

Comments
 (0)