Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 21bc67d

Browse files
authored
ref(certs): unexport methods on cert manager (#4742)
There are 2 exported methods on certmanager that are only used in unit tests in other packages. This removes that need by leveraging interfaces in the appropriate places where a test may be needed. The 2 methods are GetRootCertificate and GetCertificate. This is especially useful for both upcoming changes, and when installing certmanager, as it removes the burdensome step of needing to place the Root CA in a k8s secret Signed-off-by: Sean Teeling <[email protected]>
1 parent ec9b9f9 commit 21bc67d

27 files changed

+127
-573
lines changed

DESIGN.md

-6
Original file line numberDiff line numberDiff line change
@@ -458,15 +458,9 @@ type Manager interface {
458458
// IssueCertificate issues a new certificate.
459459
IssueCertificate(CommonName, time.Duration) (*Certificate, error)
460460
461-
// GetCertificate returns a certificate given its Common Name (CN)
462-
GetCertificate(CommonName) (*Certificate, error)
463-
464461
// RotateCertificate rotates an existing certificate.
465462
RotateCertificate(CommonName) (*Certificate, error)
466463
467-
// GetRootCertificate returns the root certificate.
468-
GetRootCertificate() (*Certificate, error)
469-
470464
// ListCertificates lists all certificates issued
471465
ListCertificates() ([]*Certificate, error)
472466

cmd/osm-bootstrap/osm-bootstrap.go

-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ func getCertOptions() (providers.Options, error) {
121121
case providers.VaultKind:
122122
return vaultOptions, nil
123123
case providers.CertManagerKind:
124-
certManagerOptions.SecretName = caBundleSecretName
125124
return certManagerOptions, nil
126125
}
127126
return nil, fmt.Errorf("unknown certificate provider kind: %s", certProviderKind)

cmd/osm-controller/osm-controller.go

-1
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ func getCertOptions() (providers.Options, error) {
130130
case providers.VaultKind:
131131
return vaultOptions, nil
132132
case providers.CertManagerKind:
133-
certManagerOptions.SecretName = caBundleSecretName
134133
return certManagerOptions, nil
135134
}
136135
return nil, fmt.Errorf("unknown certificate provider kind: %s", certProviderKind)

cmd/osm-injector/osm-injector.go

-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@ func getCertOptions() (providers.Options, error) {
119119
case providers.VaultKind:
120120
return vaultOptions, nil
121121
case providers.CertManagerKind:
122-
certManagerOptions.SecretName = caBundleSecretName
123122
return certManagerOptions, nil
124123
}
125124
return nil, fmt.Errorf("unknown certificate provider kind: %s", certProviderKind)

docs/certificate_management.md

+3-19
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,13 @@ Currently there are three supported certificate managers:
88
- [cert-manager](https://cert-manager.io/)
99
- [HashiCorp Vault](https://www.hashicorp.com/products/vault)
1010

11-
All certificate managers implement the `Manager` interface (located in `pkg/certificate`). Currently this interface is defined as:
11+
All certificate managers implement the `Issuer` interface (located in `pkg/certificate`). Currently this interface is defined as:
1212

1313
```go
14-
// Manager is the interface declaring the methods for the Certificate Manager.
15-
type Manager interface {
14+
// Issuer is the interface declaring the methods for the Certificate Manager.
15+
type Issuer interface {
1616
// IssueCertificate issues a new certificate.
1717
IssueCertificate(CommonName, time.Duration) (*Certificate, error)
18-
19-
// GetCertificate returns a certificate given its Common Name (CN)
20-
GetCertificate(CommonName) (*Certificate, error)
21-
22-
// RotateCertificate rotates an existing certificate.
23-
RotateCertificate(CommonName) (*Certificate, error)
24-
25-
// GetRootCertificate returns the root certificate in PEM format and its expiration.
26-
GetRootCertificate() (*Certificate, error)
27-
28-
// ListCertificates lists all certificates issued
29-
ListCertificates() ([]*Certificate, error)
30-
31-
// ReleaseCertificate informs the underlying certificate issuer that the given cert will no longer be needed.
32-
// This method could be called when a given payload is terminated. Calling this should remove certs from cache and free memory if possible.
33-
ReleaseCertificate(CommonName)
3418
}
3519
```
3620

pkg/certificate/fake_manager.go

-8
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ import (
1010
)
1111

1212
var (
13-
caCert = &Certificate{
14-
CommonName: "Test CA",
15-
Expiration: time.Now().Add(time.Hour * 24),
16-
}
1713
validity = time.Hour
1814
)
1915

@@ -43,10 +39,6 @@ func (i *fakeIssuer) IssueCertificate(cn CommonName, validityPeriod time.Duratio
4339
}, nil
4440
}
4541

46-
func (i *fakeIssuer) GetRootCertificate() *Certificate {
47-
return caCert
48-
}
49-
5042
// FakeCertManager is a testing helper that returns a *certificate.Manager
5143
func FakeCertManager() (*Manager, error) {
5244
cm, err := NewManager(

pkg/certificate/manager.go

+1-32
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import (
1212
"github.com/openservicemesh/osm/pkg/messaging"
1313
)
1414

15-
var errCertNotFound = errors.New("certificate not found")
16-
1715
// NewManager creates a new CertManager with the passed CA and CA Private Key
1816
func NewManager(mrcClient MRCClient, serviceCertValidityDuration time.Duration, msgBroker *messaging.Broker) (*Manager, error) {
1917
// TODO(#4502): transition this call to a watch function that knows how to handle multiple MRC and can react to changes.
@@ -61,12 +59,7 @@ func (m *Manager) Start(checkInterval time.Duration, certRotation <-chan struct{
6159
}
6260

6361
func (m *Manager) checkAndRotate() {
64-
certs, err := m.ListCertificates()
65-
if err != nil {
66-
log.Error().Err(err).Msgf("Error listing all certificates")
67-
}
68-
69-
for _, cert := range certs {
62+
for _, cert := range m.ListIssuedCertificates() {
7063
shouldRotate := cert.ShouldRotate()
7164

7265
word := map[bool]string{true: "will", false: "will not"}[shouldRotate]
@@ -131,14 +124,6 @@ func (m *Manager) ReleaseCertificate(cn CommonName) {
131124
m.cache.Delete(cn)
132125
}
133126

134-
// GetCertificate returns a certificate given its Common Name (CN)
135-
func (m *Manager) GetCertificate(cn CommonName) (*Certificate, error) {
136-
if cert := m.getFromCache(cn); cert != nil {
137-
return cert, nil
138-
}
139-
return nil, errCertNotFound
140-
}
141-
142127
// RotateCertificate implements Manager and rotates an existing
143128
func (m *Manager) RotateCertificate(cn CommonName) (*Certificate, error) {
144129
start := time.Now()
@@ -171,22 +156,6 @@ func (m *Manager) RotateCertificate(cn CommonName) (*Certificate, error) {
171156
return newCert, nil
172157
}
173158

174-
// ListCertificates lists all certificates issued
175-
func (m *Manager) ListCertificates() ([]*Certificate, error) {
176-
var certs []*Certificate
177-
m.cache.Range(func(_ interface{}, certInterface interface{}) bool {
178-
certs = append(certs, certInterface.(*Certificate))
179-
return true // continue the iteration
180-
})
181-
return certs, nil
182-
}
183-
184-
// GetRootCertificate returns the root
185-
func (m *Manager) GetRootCertificate() *Certificate {
186-
// TODO(#4502): determine client(s) to use based on the rotation stage(s) of the client(s)
187-
return m.clients[0].GetRootCertificate()
188-
}
189-
190159
// ListIssuedCertificates implements CertificateDebugger interface and returns the list of issued certificates.
191160
func (m *Manager) ListIssuedCertificates() []*Certificate {
192161
var certs []*Certificate

pkg/certificate/manager_test.go

+5-74
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ var _ = Describe("Test Certificate Manager", func() {
4646
Expect(issueCertificateError).ToNot(HaveOccurred())
4747
Expect(cert.GetCommonName()).To(Equal(CommonName(serviceFQDN)))
4848

49-
cachedCert, getCertificateError := m.GetCertificate(serviceFQDN)
50-
Expect(getCertificateError).ToNot(HaveOccurred())
49+
cachedCert := m.getFromCache(serviceFQDN)
5150
Expect(cachedCert).To(Equal(cert))
5251
})
5352
})
@@ -110,70 +109,14 @@ func TestReleaseCertificate(t *testing.T) {
110109
assert := tassert.New(t)
111110

112111
manager.ReleaseCertificate(tc.commonName)
113-
_, err := manager.GetCertificate(tc.commonName)
112+
cert := manager.getFromCache(tc.commonName)
114113

115-
assert.ErrorIs(err, errCertNotFound)
114+
assert.Nil(cert)
116115
})
117116
}
118117
}
119118

120-
func TestGetCertificate(t *testing.T) {
121-
cn := CommonName("Test Cert")
122-
cert := &Certificate{
123-
CommonName: cn,
124-
Expiration: time.Now().Add(1 * time.Hour),
125-
}
126-
127-
expiredCn := CommonName("Expired Test Cert")
128-
expiredCert := &Certificate{
129-
CommonName: expiredCn,
130-
Expiration: time.Now().Add(-1 * time.Hour),
131-
}
132-
133-
manager := &Manager{}
134-
manager.cache.Store(cn, cert)
135-
manager.cache.Store(expiredCn, expiredCert)
136-
137-
testCases := []struct {
138-
name string
139-
commonName CommonName
140-
expectedCertificate *Certificate
141-
expectedErr error
142-
}{
143-
{
144-
name: "cache hit",
145-
commonName: cn,
146-
expectedCertificate: cert,
147-
},
148-
{
149-
name: "cache miss",
150-
commonName: CommonName("Wrong Cert"),
151-
expectedErr: errCertNotFound,
152-
},
153-
{
154-
name: "certificate expiration",
155-
commonName: expiredCn,
156-
expectedErr: errCertNotFound,
157-
},
158-
}
159-
160-
for _, tc := range testCases {
161-
t.Run(tc.name, func(t *testing.T) {
162-
assert := tassert.New(t)
163-
164-
c, err := manager.GetCertificate(tc.commonName)
165-
if tc.expectedErr != nil {
166-
assert.ErrorIs(err, tc.expectedErr)
167-
return
168-
}
169-
170-
assert.Nil(err)
171-
assert.Equal(tc.expectedCertificate, c)
172-
})
173-
}
174-
}
175-
176-
func TestListCertificate(t *testing.T) {
119+
func TestListIssuedCertificate(t *testing.T) {
177120
assert := tassert.New(t)
178121

179122
cn := CommonName("Test Cert")
@@ -192,9 +135,7 @@ func TestListCertificate(t *testing.T) {
192135
manager.cache.Store(cn, cert)
193136
manager.cache.Store(anotherCn, anotherCert)
194137

195-
cs, err := manager.ListCertificates()
196-
197-
assert.Nil(err)
138+
cs := manager.ListIssuedCertificates()
198139
assert.Len(cs, 2)
199140

200141
for i, c := range cs {
@@ -212,13 +153,3 @@ func TestListCertificate(t *testing.T) {
212153
}
213154
}
214155
}
215-
216-
func TestGetRootCertificate(t *testing.T) {
217-
assert := tassert.New(t)
218-
219-
manager := &Manager{clients: []Issuer{&fakeIssuer{}}}
220-
221-
got := manager.GetRootCertificate()
222-
223-
assert.Equal(caCert, got)
224-
}

pkg/certificate/mock_certificate_generated.go

-122
This file was deleted.

0 commit comments

Comments
 (0)