Skip to content

Commit 70ed3ba

Browse files
Merge pull request #207 from RedisLabs/feat/get-tls-certificate-for-database
Get TLS certificate for database
2 parents 2fa07e7 + 9a5a5e8 commit 70ed3ba

File tree

5 files changed

+83
-1
lines changed

5 files changed

+83
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
All notable changes to this project will be documented in this file.
33
See updating [Changelog example here](https://keepachangelog.com/en/1.0.0/).
44

5+
## 0.28.0
6+
7+
### Added
8+
9+
* Adding an endpoint to get a TLS certificate for a database.
10+
511
## 0.27.0
612

713
### Updated

database_test.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,3 +543,19 @@ func TestDatabase_Import(t *testing.T) {
543543
})
544544
require.NoError(t, err)
545545
}
546+
547+
func TestDatabase_Certificate(t *testing.T) {
548+
s := httptest.NewServer(testServer("key", "secret", getRequest(t, "/subscriptions/42/databases/18/certificate",
549+
`{ "publicCertificatePEMString": "public-cert" }`)))
550+
551+
subject, err := clientFromTestServer(s, "key", "secret")
552+
require.NoError(t, err)
553+
554+
certificate, err := subject.Database.GetCertificate(context.TODO(), 42, 18)
555+
require.NoError(t, err)
556+
557+
assert.Equal(t, &databases.DatabaseCertificate{
558+
PublicCertificatePEMString: "public-cert",
559+
}, certificate)
560+
561+
}

service/databases/model.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,15 @@ func (o Import) String() string {
200200
return internal.ToString(o)
201201
}
202202

203+
// DatabaseCertificate represents the TLS certificate information for a database
204+
type DatabaseCertificate struct {
205+
PublicCertificatePEMString string `json:"publicCertificatePEMString"`
206+
}
207+
208+
func (o DatabaseCertificate) String() string {
209+
return internal.ToString(o)
210+
}
211+
203212
type listDatabaseResponse struct {
204213
Subscription []*listDbSubscription `json:"subscription,omitempty"`
205214
}

service/databases/service.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ func (a *API) Create(ctx context.Context, subscription int, db CreateDatabase) (
5656
return id, nil
5757
}
5858

59-
// List will return a ListDatabase that is capable of paging through all of the databases associated with a
59+
// List will return a ListDatabase that is capable of paging through all the databases associated with a
6060
// subscription.
6161
func (a *API) List(ctx context.Context, subscription int) *ListDatabase {
6262
return newListDatabase(ctx, a.client, subscription, 100)
@@ -125,6 +125,24 @@ func (a *API) Import(ctx context.Context, subscription int, database int, reques
125125
return a.taskWaiter.Wait(ctx, *task.ID)
126126
}
127127

128+
// GetCertificate retrieves the TLS certificate for the specified database within a subscription.
129+
func (a *API) GetCertificate(ctx context.Context, subscription int, database int) (*DatabaseCertificate, error) {
130+
var certificate DatabaseCertificate
131+
getCertificateUrl := "/subscriptions/%d/databases/%d/certificate"
132+
133+
path := fmt.Sprintf(getCertificateUrl, subscription, database)
134+
err := a.client.Get(
135+
ctx,
136+
fmt.Sprintf("get TLS certificate for database %d in subscription %d", database, subscription),
137+
path,
138+
&certificate,
139+
)
140+
if err != nil {
141+
return nil, wrap404Error(subscription, database, err)
142+
}
143+
return &certificate, nil
144+
}
145+
128146
type ListDatabase struct {
129147
client HttpClient
130148
subscription int

service/databases/service_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,39 @@ func TestListDatabase_recordsError(t *testing.T) {
8585
assert.Nil(t, subject.Value())
8686
}
8787

88+
func TestGetCertificate_success(t *testing.T) {
89+
client := &mockHttpClient{}
90+
api := NewAPI(client, nil, nil)
91+
92+
expected := &DatabaseCertificate{
93+
PublicCertificatePEMString: "test-certificate",
94+
}
95+
96+
client.On("Get", context.TODO(), "get TLS certificate for database 123 in subscription 456", "/subscriptions/456/databases/123/certificate", mock.AnythingOfType("*databases.DatabaseCertificate")).
97+
Run(func(args mock.Arguments) {
98+
cert := args.Get(3).(*DatabaseCertificate)
99+
cert.PublicCertificatePEMString = "test-certificate"
100+
}).Return(nil)
101+
102+
result, err := api.GetCertificate(context.TODO(), 456, 123)
103+
assert.NoError(t, err)
104+
assert.Equal(t, expected, result)
105+
}
106+
107+
func TestGetCertificate_error(t *testing.T) {
108+
client := &mockHttpClient{}
109+
api := NewAPI(client, nil, nil)
110+
111+
expected := fmt.Errorf("test error")
112+
client.On("Get", context.TODO(), "get TLS certificate for database 123 in subscription 456", "/subscriptions/456/databases/123/certificate", mock.AnythingOfType("*databases.DatabaseCertificate")).
113+
Return(expected)
114+
115+
result, err := api.GetCertificate(context.TODO(), 456, 123)
116+
assert.Error(t, err)
117+
assert.Equal(t, expected, err)
118+
assert.Nil(t, result)
119+
}
120+
88121
type mockHttpClient struct {
89122
mock.Mock
90123
}

0 commit comments

Comments
 (0)