Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Synthetics] Prevent certificate update with wrong value #997

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions datadog/resource_datadog_synthetics_test_.go
Original file line number Diff line number Diff line change
Expand Up @@ -938,14 +938,23 @@ func buildSyntheticsTestStruct(d *schema.ResourceData) *datadogV1.SyntheticsTest
cert.SetFilename(attr.(string))
}
if attr, ok := d.GetOk("request_client_certificate.0.cert.0.content"); ok {
cert.SetContent(attr.(string))
// only set the certificate content if it is not an already hashed string
// this is needed for the update function that receives the data from the state
// and not from the config. So we get a hash of the certificate and not it's real
// value.
if isHash := isCertHash(attr.(string)); isHash == false {
cert.SetContent(attr.(string))
}
}

if attr, ok := d.GetOk("request_client_certificate.0.key.0.filename"); ok {
key.SetFilename(attr.(string))
}
if attr, ok := d.GetOk("request_client_certificate.0.key.0.content"); ok {
key.SetContent(attr.(string))
// only set the key content if it is not an already hashed string
if isHash := isCertHash(attr.(string)); isHash == false {
key.SetContent(attr.(string))
}
}

clientCertificate := datadogV1.SyntheticsTestRequestCertificate{
Expand Down Expand Up @@ -1655,15 +1664,19 @@ func validateSyntheticsAssertionOperator(val interface{}, key string) (warns []s
return
}

func isCertHash(content string) bool {
// a sha256 hash consists of 64 hexadecimal characters
isHash, _ := regexp.MatchString("^[A-Fa-f0-9]{64}$", content)

return isHash
}

// get the sha256 of a client certificate content
// in some case where Terraform compares the state value
// we already get the hashed value so we don't need to
// hash it again
func getCertificateStateValue(content string) string {
contentBytes := []byte(content)

// hacky way to detect if the value is already a sha256 hash
if len(contentBytes) == 64 {
if isHash := isCertHash(content); isHash {
return content
}

Expand Down