Skip to content

Commit 829c7ea

Browse files
authored
feat: support ssl enforcement config (#4700)
2 parents 9b21164 + b5abdaf commit 829c7ea

4 files changed

Lines changed: 73 additions & 0 deletions

File tree

pkg/config/db.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ type (
7575
AllowedCidrsV6 []string `toml:"allowed_cidrs_v6"`
7676
}
7777

78+
sslEnforcement struct {
79+
Enabled bool `toml:"enabled"`
80+
}
81+
7882
db struct {
7983
Image string `toml:"-"`
8084
Port uint16 `toml:"port"`
@@ -88,6 +92,7 @@ type (
8892
Seed seed `toml:"seed"`
8993
Settings settings `toml:"settings"`
9094
NetworkRestrictions networkRestrictions `toml:"network_restrictions"`
95+
SslEnforcement *sslEnforcement `toml:"ssl_enforcement"`
9196
Vault map[string]Secret `toml:"vault"`
9297
}
9398

@@ -233,3 +238,31 @@ func (n *networkRestrictions) DiffWithRemote(remoteConfig v1API.NetworkRestricti
233238
}
234239
return diff.Diff("remote[db.network_restrictions]", remoteCompare, "local[db.network_restrictions]", currentValue), nil
235240
}
241+
242+
func (s sslEnforcement) ToUpdateSslEnforcementBody() v1API.V1UpdateSslEnforcementConfigJSONRequestBody {
243+
body := v1API.V1UpdateSslEnforcementConfigJSONRequestBody{}
244+
body.RequestedConfig.Database = s.Enabled
245+
return body
246+
}
247+
248+
func (s *sslEnforcement) FromRemoteSslEnforcement(remoteConfig v1API.SslEnforcementResponse) {
249+
if s == nil {
250+
return
251+
}
252+
s.Enabled = remoteConfig.CurrentConfig.Database
253+
}
254+
255+
func (s *sslEnforcement) DiffWithRemote(remoteConfig v1API.SslEnforcementResponse) ([]byte, error) {
256+
copy := *s
257+
// Convert the config values into easily comparable remoteConfig values
258+
currentValue, err := ToTomlBytes(copy)
259+
if err != nil {
260+
return nil, err
261+
}
262+
copy.FromRemoteSslEnforcement(remoteConfig)
263+
remoteCompare, err := ToTomlBytes(copy)
264+
if err != nil {
265+
return nil, err
266+
}
267+
return diff.Diff("remote[db.ssl_enforcement]", remoteCompare, "local[db.ssl_enforcement]", currentValue), nil
268+
}

pkg/config/templates/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ allowed_cidrs = ["0.0.0.0/0"]
7474
# Defaults to allow all IPv6 connections. Set empty array to block all IPs.
7575
allowed_cidrs_v6 = ["::/0"]
7676

77+
# Uncomment to reject non-secure connections to the database.
78+
# [db.ssl_enforcement]
79+
# enabled = true
80+
7781
[realtime]
7882
enabled = true
7983
# Bind realtime via either IPv4 or IPv6. (default: IPv4)

pkg/config/testdata/config.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ allowed_cidrs = ["0.0.0.0/0"]
7474
# Defaults to allow all IPv6 connections. Set empty array to block all IPs.
7575
allowed_cidrs_v6 = ["::/0"]
7676

77+
# Uncomment to reject non-secure connections to the database.
78+
[db.ssl_enforcement]
79+
enabled = true
80+
7781
[realtime]
7882
enabled = true
7983
# Bind realtime via either IPv4 or IPv6. (default: IPv6)

pkg/config/updater.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ func (u *ConfigUpdater) UpdateDbConfig(ctx context.Context, projectRef string, c
100100
if err := u.UpdateDbNetworkRestrictionsConfig(ctx, projectRef, c.NetworkRestrictions, filter...); err != nil {
101101
return err
102102
}
103+
if c.SslEnforcement != nil {
104+
return u.UpdateSslEnforcement(ctx, projectRef, *c.SslEnforcement, filter...)
105+
}
103106
return nil
104107
}
105108

@@ -132,6 +135,35 @@ func (u *ConfigUpdater) UpdateDbNetworkRestrictionsConfig(ctx context.Context, p
132135
return nil
133136
}
134137

138+
func (u *ConfigUpdater) UpdateSslEnforcement(ctx context.Context, projectRef string, s sslEnforcement, filter ...func(string) bool) error {
139+
sslEnforcementConfig, err := u.client.V1GetSslEnforcementConfigWithResponse(ctx, projectRef)
140+
if err != nil {
141+
return errors.Errorf("failed to read SSL enforcement config: %w", err)
142+
} else if sslEnforcementConfig.JSON200 == nil {
143+
return errors.Errorf("unexpected status %d: %s", sslEnforcementConfig.StatusCode(), string(sslEnforcementConfig.Body))
144+
}
145+
sslEnforcementDiff, err := s.DiffWithRemote(*sslEnforcementConfig.JSON200)
146+
if err != nil {
147+
return err
148+
} else if len(sslEnforcementDiff) == 0 {
149+
fmt.Fprintln(os.Stderr, "Remote DB SSL enforcement config is up to date.")
150+
return nil
151+
}
152+
fmt.Fprintln(os.Stderr, "Updating SSL enforcement with config:", string(sslEnforcementDiff))
153+
for _, keep := range filter {
154+
if !keep("db") {
155+
return nil
156+
}
157+
}
158+
updateBody := s.ToUpdateSslEnforcementBody()
159+
if resp, err := u.client.V1UpdateSslEnforcementConfigWithResponse(ctx, projectRef, updateBody); err != nil {
160+
return errors.Errorf("failed to update SSL enforcement config: %w", err)
161+
} else if resp.JSON200 == nil {
162+
return errors.Errorf("unexpected status %d: %s", resp.StatusCode(), string(resp.Body))
163+
}
164+
return nil
165+
}
166+
135167
func (u *ConfigUpdater) UpdateAuthConfig(ctx context.Context, projectRef string, c auth, filter ...func(string) bool) error {
136168
if !c.Enabled {
137169
return nil

0 commit comments

Comments
 (0)