Skip to content

Commit 25e941e

Browse files
authored
chore: fix SSL configuration (#115)
* chore: fix SSL configuration * chore: update * chore: update * chore: update * chore: update * chore: update
1 parent e1d8049 commit 25e941e

File tree

4 files changed

+79
-22
lines changed

4 files changed

+79
-22
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,7 @@ override.tf.json
4242
terraform.rc
4343

4444
# Ignore docs overview
45-
docs/assets/overview.webp
45+
docs/assets/overview.webp
46+
47+
# Ignore compiled binary
48+
terraform-provider-bytebase

provider/data_source_instance.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ func dataSourceInstance() *schema.Resource {
109109
Description: "The connection user password used by Bytebase to perform DDL and DML operations.",
110110
},
111111
"external_secret": getExternalSecretSchema(),
112+
"use_ssl": {
113+
Type: schema.TypeBool,
114+
Computed: true,
115+
Description: "Enable SSL connection. Required to use SSL certificates.",
116+
},
112117
"ssl_ca": {
113118
Type: schema.TypeString,
114119
Computed: true,

provider/data_source_instance_list.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,11 @@ func dataSourceInstanceList() *schema.Resource {
165165
Description: "The connection user password used by Bytebase to perform DDL and DML operations.",
166166
},
167167
"external_secret": getExternalSecretSchema(),
168+
"use_ssl": {
169+
Type: schema.TypeBool,
170+
Computed: true,
171+
Description: "Enable SSL connection. Required to use SSL certificates.",
172+
},
168173
"ssl_ca": {
169174
Type: schema.TypeString,
170175
Computed: true,

provider/resource_instance.go

Lines changed: 65 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,12 @@ func resourceInstance() *schema.Resource {
116116
Description: "The connection user name used by Bytebase to perform DDL and DML operations.",
117117
},
118118
"password": {
119-
Type: schema.TypeString,
120-
Optional: true,
121-
Sensitive: true,
122-
Default: "",
123-
Description: "The connection user password used by Bytebase to perform DDL and DML operations.",
119+
Type: schema.TypeString,
120+
Optional: true,
121+
Sensitive: true,
122+
Computed: true,
123+
DiffSuppressFunc: suppressSensitiveFieldDiff,
124+
Description: "The connection user password used by Bytebase to perform DDL and DML operations.",
124125
},
125126
"external_secret": {
126127
Type: schema.TypeList,
@@ -234,26 +235,35 @@ func resourceInstance() *schema.Resource {
234235
},
235236
},
236237
},
237-
"ssl_ca": {
238-
Type: schema.TypeString,
238+
"use_ssl": {
239+
Type: schema.TypeBool,
239240
Optional: true,
240-
Default: "",
241-
Sensitive: true,
242-
Description: "The CA certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE.",
241+
Default: false,
242+
Description: "Enable SSL connection. Required to use SSL certificates.",
243+
},
244+
"ssl_ca": {
245+
Type: schema.TypeString,
246+
Optional: true,
247+
Sensitive: true,
248+
Computed: true,
249+
DiffSuppressFunc: suppressSensitiveFieldDiff,
250+
Description: "The CA certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE.",
243251
},
244252
"ssl_cert": {
245-
Type: schema.TypeString,
246-
Optional: true,
247-
Default: "",
248-
Sensitive: true,
249-
Description: "The client certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE.",
253+
Type: schema.TypeString,
254+
Optional: true,
255+
Sensitive: true,
256+
Computed: true,
257+
DiffSuppressFunc: suppressSensitiveFieldDiff,
258+
Description: "The client certificate. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE.",
250259
},
251260
"ssl_key": {
252-
Type: schema.TypeString,
253-
Optional: true,
254-
Default: "",
255-
Sensitive: true,
256-
Description: "The client key. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE.",
261+
Type: schema.TypeString,
262+
Optional: true,
263+
Sensitive: true,
264+
Computed: true,
265+
DiffSuppressFunc: suppressSensitiveFieldDiff,
266+
Description: "The client key. Optional, you can set this if the engine type is MYSQL, POSTGRES, TIDB or CLICKHOUSE.",
257267
},
258268
"host": {
259269
Type: schema.TypeString,
@@ -288,6 +298,17 @@ func resourceInstance() *schema.Resource {
288298
}
289299
}
290300

301+
// suppressSensitiveFieldDiff suppresses diffs for write-only sensitive fields.
302+
func suppressSensitiveFieldDiff(_ string, oldValue, newValue string, _ *schema.ResourceData) bool {
303+
// If the field was previously set (exists in state) and the new value is empty,
304+
// suppress the diff because the API doesn't return these fields
305+
if oldValue != "" && newValue == "" {
306+
return true
307+
}
308+
// If both are equal, suppress the diff
309+
return oldValue == newValue
310+
}
311+
291312
func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
292313
c := m.(api.Client)
293314

@@ -656,6 +677,7 @@ func flattenDataSourceList(d *schema.ResourceData, dataSourceList []*v1pb.DataSo
656677
raw["host"] = dataSource.Host
657678
raw["port"] = dataSource.Port
658679
raw["database"] = dataSource.Database
680+
raw["use_ssl"] = dataSource.UseSsl
659681

660682
// These sensitive fields won't returned in the API. Propagate state value.
661683
if ds, ok := oldDataSourceMap[dataSource.Id]; ok {
@@ -722,7 +744,26 @@ func flattenDataSourceList(d *schema.ResourceData, dataSourceList []*v1pb.DataSo
722744

723745
func dataSourceHash(rawDataSource interface{}) int {
724746
dataSource := rawDataSource.(map[string]interface{})
725-
return internal.ToHashcodeInt(dataSource["id"].(string))
747+
// Include id and SSL-related field presence to detect configuration changes
748+
hashStr := dataSource["id"].(string)
749+
750+
// Include use_ssl in hash to detect SSL enablement changes
751+
if v, ok := dataSource["use_ssl"].(bool); ok {
752+
hashStr = fmt.Sprintf("%s-ssl_%t", hashStr, v)
753+
}
754+
755+
// Include whether SSL certificates are present (not the values themselves)
756+
if v, ok := dataSource["ssl_ca"].(string); ok && v != "" {
757+
hashStr = fmt.Sprintf("%s-ca_present", hashStr)
758+
}
759+
if v, ok := dataSource["ssl_cert"].(string); ok && v != "" {
760+
hashStr = fmt.Sprintf("%s-cert_present", hashStr)
761+
}
762+
if v, ok := dataSource["ssl_key"].(string); ok && v != "" {
763+
hashStr = fmt.Sprintf("%s-key_present", hashStr)
764+
}
765+
766+
return internal.ToHashcodeInt(hashStr)
726767
}
727768

728769
func convertDataSourceCreateList(d *schema.ResourceData, validate bool) ([]*v1pb.DataSource, error) {
@@ -797,6 +838,9 @@ func convertDataSourceCreateList(d *schema.ResourceData, validate bool) ([]*v1pb
797838
return nil, errors.Errorf("cannot set both password and external_secret")
798839
}
799840

841+
if v, ok := obj["use_ssl"].(bool); ok {
842+
dataSource.UseSsl = v
843+
}
800844
if v, ok := obj["ssl_ca"].(string); ok {
801845
dataSource.SslCa = v
802846
}

0 commit comments

Comments
 (0)