Skip to content

Commit 2f89504

Browse files
authored
chore: add checks for server ca certificates in refresh.go (GoogleCloudPlatform#1037)
1 parent 46b8af2 commit 2f89504

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

internal/cloudsql/refresh.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ func fetchMetadata(
139139

140140
// parse the server-side CA certificate
141141
caCerts := []*x509.Certificate{}
142+
if db.ServerCaCert == nil {
143+
return metadata{}, errtype.NewRefreshError("instance does not have a server CA certificate", inst.String(), nil)
144+
}
142145
for b, rest := pem.Decode([]byte(db.ServerCaCert.Cert)); b != nil; b, rest = pem.Decode(rest) {
143146
if b == nil {
144147
return metadata{}, errtype.NewRefreshError("failed to decode valid PEM cert", inst.String(), nil)

internal/cloudsql/refresh_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,16 @@ func TestRefreshMetadataRefreshError(t *testing.T) {
401401
wantErr: &errtype.RefreshError{},
402402
desc: "When the server cert does not decode",
403403
},
404+
{
405+
req: mock.InstanceGetSuccess(
406+
mock.NewFakeCSQLInstance(
407+
cn.Project(), cn.Region(), cn.Name(),
408+
mock.WithRegion("my-region"),
409+
mock.WithServerCaCert(nil),
410+
), 1),
411+
wantErr: &errtype.RefreshError{},
412+
desc: "When the server CA cert is nil",
413+
},
404414
{
405415
req: mock.InstanceGetSuccess(
406416
mock.NewFakeCSQLInstance(

internal/mock/cloudsql.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ type FakeCSQLInstance struct {
7474
Cert *x509.Certificate
7575
// certs holds all of the certificates for this instance
7676
certs *TLSCertificates
77+
// ServerCaCert is the server CA certificate for the mock instance.
78+
ServerCaCert *sqladmin.SslCert
7779
}
7880

7981
// String returns the instance connection name for the
@@ -226,6 +228,13 @@ func WithServerCAMode(serverCAMode string) FakeCSQLInstanceOption {
226228
}
227229
}
228230

231+
// WithServerCaCert sets the ServerCaCert of the instance.
232+
func WithServerCaCert(c *sqladmin.SslCert) FakeCSQLInstanceOption {
233+
return func(i *FakeCSQLInstance) {
234+
i.ServerCaCert = c
235+
}
236+
}
237+
229238
// NewFakeCSQLInstance returns a CloudSQLInst object for configuring mocks.
230239
func NewFakeCSQLInstance(project, region, name string, opts ...FakeCSQLInstanceOption) FakeCSQLInstance {
231240

0 commit comments

Comments
 (0)