|
| 1 | +package acm |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/stretchr/testify/require" |
| 8 | + |
| 9 | + "github.com/infracost/go-proto/pkg/tree/aws/acmpca" |
| 10 | + "github.com/infracost/go-proto/pkg/tree/resource" |
| 11 | + "github.com/infracost/go-proto/pkg/tree/value" |
| 12 | +) |
| 13 | + |
| 14 | +func TestCertificateManager_AddCertificateAuthorities(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + certificates []Certificate |
| 18 | + cas []acmpca.CertificateAuthority |
| 19 | + wantLinkedCA []string // expected linked CA ID per cert, empty string means no link |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "no certificates or CAs", |
| 23 | + certificates: nil, |
| 24 | + cas: nil, |
| 25 | + wantLinkedCA: nil, |
| 26 | + }, |
| 27 | + { |
| 28 | + name: "certificate with empty ARN is not linked", |
| 29 | + certificates: []Certificate{ |
| 30 | + {CertificateAuthorityARN: value.EmptyString}, |
| 31 | + }, |
| 32 | + cas: []acmpca.CertificateAuthority{ |
| 33 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}}, |
| 34 | + }, |
| 35 | + wantLinkedCA: []string{""}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "certificate with matching ARN is linked", |
| 39 | + certificates: []Certificate{ |
| 40 | + {CertificateAuthorityARN: value.New("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123", 0, "", nil)}, |
| 41 | + }, |
| 42 | + cas: []acmpca.CertificateAuthority{ |
| 43 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}}, |
| 44 | + }, |
| 45 | + wantLinkedCA: []string{"arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "certificate with non-matching ARN is not linked", |
| 49 | + certificates: []Certificate{ |
| 50 | + {CertificateAuthorityARN: value.New("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/xyz-999", 0, "", nil)}, |
| 51 | + }, |
| 52 | + cas: []acmpca.CertificateAuthority{ |
| 53 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}}, |
| 54 | + }, |
| 55 | + wantLinkedCA: []string{""}, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "multiple certificates with mixed matching", |
| 59 | + certificates: []Certificate{ |
| 60 | + {CertificateAuthorityARN: value.New("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123", 0, "", nil)}, |
| 61 | + {CertificateAuthorityARN: value.EmptyString}, |
| 62 | + {CertificateAuthorityARN: value.New("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/def-456", 0, "", nil)}, |
| 63 | + }, |
| 64 | + cas: []acmpca.CertificateAuthority{ |
| 65 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}}, |
| 66 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/def-456"}}, |
| 67 | + }, |
| 68 | + wantLinkedCA: []string{ |
| 69 | + "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123", |
| 70 | + "", |
| 71 | + "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/def-456", |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "existing relationships are reset", |
| 76 | + certificates: []Certificate{ |
| 77 | + { |
| 78 | + CertificateAuthorityARN: value.EmptyString, |
| 79 | + Relationships: CertificateRelationships{ |
| 80 | + CertificateAuthority: &acmpca.CertificateAuthority{ |
| 81 | + Resource: resource.Resource{ID: "stale-ca"}, |
| 82 | + }, |
| 83 | + }, |
| 84 | + }, |
| 85 | + }, |
| 86 | + cas: []acmpca.CertificateAuthority{ |
| 87 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}}, |
| 88 | + }, |
| 89 | + wantLinkedCA: []string{""}, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "linked CA is a copy not a reference to the slice element", |
| 93 | + certificates: []Certificate{ |
| 94 | + {CertificateAuthorityARN: value.New("arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123", 0, "", nil)}, |
| 95 | + }, |
| 96 | + cas: []acmpca.CertificateAuthority{ |
| 97 | + {Resource: resource.Resource{ID: "arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}}, |
| 98 | + }, |
| 99 | + wantLinkedCA: []string{"arn:aws:acm-pca:us-east-1:123456789012:certificate-authority/abc-123"}, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + cm := &CertificateManager{Certificates: tt.certificates} |
| 106 | + pca := &acmpca.PCACertificateAuthority{CertificateAuthorities: tt.cas} |
| 107 | + |
| 108 | + cm.AddCertificateAuthorities(pca) |
| 109 | + |
| 110 | + require.Len(t, cm.Certificates, len(tt.wantLinkedCA)) |
| 111 | + for i, wantID := range tt.wantLinkedCA { |
| 112 | + cert := cm.Certificates[i] |
| 113 | + if wantID == "" { |
| 114 | + assert.Nil(t, cert.Relationships.CertificateAuthority, "cert[%d] should not have a linked CA", i) |
| 115 | + } else { |
| 116 | + require.NotNil(t, cert.Relationships.CertificateAuthority, "cert[%d] should have a linked CA", i) |
| 117 | + assert.Equal(t, wantID, cert.Relationships.CertificateAuthority.ID, "cert[%d] linked to wrong CA", i) |
| 118 | + } |
| 119 | + } |
| 120 | + }) |
| 121 | + } |
| 122 | +} |
0 commit comments