Skip to content

Commit 4aeccbd

Browse files
authored
Stable certificate data source ID (#222)
* fix: impossible condition specified * build: compute a stable id based on certificate chain * docs: update Changelog * ci: add 1.8 to test matrix
1 parent 913c4e0 commit 4aeccbd

File tree

4 files changed

+36
-6
lines changed

4 files changed

+36
-6
lines changed

.github/workflows/test.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,23 @@ jobs:
6060
matrix:
6161
terraform:
6262
- '1.3.*'
63-
- '1.5.*'
6463
- '1.6.*'
6564
- '1.7.*'
65+
- '1.8.*'
6666
include:
6767
- terraform: '1.3.*'
6868
domain: 'dnsimple-1-0-terraform.bio'
6969
registrant_contact_id: 10854
7070
registrant_change_domain: 'peoa1hvrl5s7q7os1bqadhd29uar81nnc4m0oyaloxex9kapsn20u6nr8z6l5h.eu'
71-
- terraform: '1.5.*'
71+
- terraform: '1.6.*'
7272
domain: 'dnsimple-1-1-terraform.bio'
7373
registrant_contact_id: 10169
7474
registrant_change_domain: '9qy9lpesl2f2o5ya45zyujrggori1mh8sl6k2oz37usv48lhn3ziistg3u5kgv.eu'
75-
- terraform: '1.6.*'
75+
- terraform: '1.7.*'
7676
domain: 'dnsimple-1-2-terraform.bio'
7777
registrant_contact_id: 10854
7878
registrant_change_domain: 'lqyivkga231hkiqihu0k7bjic2ixd01xs5vex8rmn2iaw0l7gxvhcbicigpfm3.eu'
79-
- terraform: '1.7.*'
79+
- terraform: '1.8.*'
8080
domain: 'dnsimple-1-4-terraform.bio'
8181
registrant_contact_id: 10169
8282
registrant_change_domain: 'z0u2w48bo5fzgdsh1g7zjpflbpt0tiyl6tmc75ltzzm6dbphghrgepbaxs6zrm.eu'

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## main
44

5+
ENHANCEMENTS:
6+
7+
- **Update Data Source:** `dnsimple_certificate` has been updated to have a stable ID. (dnsimple/terraform-provider-dnsimple#222)
8+
59
## 1.5.0
610

711
ENHANCEMENTS:

internal/framework/datasources/certificate_data_source.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package datasources
22

33
import (
44
"context"
5+
"crypto/sha1"
6+
"encoding/hex"
57
"fmt"
8+
"strings"
69
"time"
710

811
"github.com/hashicorp/terraform-plugin-framework-timeouts/datasource/timeouts"
@@ -149,7 +152,7 @@ func (d *CertificateDataSource) Read(ctx context.Context, req datasource.ReadReq
149152
data.ServerCertificate = types.StringValue(response.Data.ServerCertificate)
150153
data.RootCertificate = types.StringValue(response.Data.RootCertificate)
151154
chain, diag := types.ListValueFrom(ctx, types.StringType, response.Data.IntermediateCertificates)
152-
if err != nil {
155+
if diag.HasError() {
153156
resp.Diagnostics.Append(diag...)
154157
return
155158
}
@@ -166,7 +169,7 @@ func (d *CertificateDataSource) Read(ctx context.Context, req datasource.ReadReq
166169
}
167170

168171
data.PrivateKey = types.StringValue(response.Data.PrivateKey)
169-
data.Id = types.StringValue(time.Now().UTC().String())
172+
data.Id = types.StringValue(idFromCertificateChain(data.ServerCertificate.ValueString(), data.RootCertificate.ValueString(), response.Data.IntermediateCertificates))
170173

171174
// Save data into Terraform state
172175
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
@@ -227,3 +230,18 @@ func tryToConvergeCertificate(ctx context.Context, data *CertificateDataSourceMo
227230

228231
return CertificateConverged, nil
229232
}
233+
234+
// idFromCertificateChain generates a SHA1 hash from the certificate chain.
235+
func idFromCertificateChain(ServerCertificate, rootCertificate string, intermediateCertificateChain []string) string {
236+
// Concatenate all certificates into a single string
237+
certChain := ServerCertificate + rootCertificate + strings.Join(intermediateCertificateChain, "")
238+
239+
// Create a new SHA1 hash.
240+
h := sha1.New()
241+
242+
// Write the certificate chain string to the hash.
243+
h.Write([]byte(certChain))
244+
hashedCertChain := hex.EncodeToString(h.Sum(nil))
245+
246+
return hashedCertChain
247+
}

internal/framework/datasources/certificate_data_source_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,14 @@ func TestAccCertificateDataSource(t *testing.T) {
2828
resource.TestCheckResourceAttr("data.dnsimple_certificate.test", "certificate_id", certificateId),
2929
),
3030
},
31+
{
32+
Config: testAccCertificateDataSourceConfig(domain, certificateId),
33+
Check: resource.ComposeAggregateTestCheckFunc(
34+
resource.TestCheckResourceAttr("data.dnsimple_certificate.test", "domain", domain),
35+
resource.TestCheckResourceAttr("data.dnsimple_certificate.test", "certificate_id", certificateId),
36+
),
37+
ExpectNonEmptyPlan: false,
38+
},
3139
},
3240
})
3341
}

0 commit comments

Comments
 (0)