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

[datadog_synthetics_test] Fix multistep client certificate #2683

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AntoineDona
Copy link
Contributor

Description

This PR was created in response to this issue where applying a multistep test with a client certificate multiple times would result in the certificate being deleted in the backend.
The tricky thing here is that the certificate is never stored in the tf state for security reasons, and we pass an empty value to the backend if its value does not change. Because we don't have the step ids in terraform, the backend can't partially update the step and just overrides everything, thus the certificate is destroyed.

As a fix, we decided to pass the certificate on each tf update.

@AntoineDona AntoineDona marked this pull request as ready for review November 19, 2024 16:34
@AntoineDona AntoineDona requested review from a team as code owners November 19, 2024 16:34
@AntoineDona AntoineDona changed the title [SYNTH-17025] Fix multistep client certificate [datadog_synthetics_test] [SYNTH-17025] Fix multistep client certificate Nov 19, 2024
@AntoineDona AntoineDona changed the title [datadog_synthetics_test] [SYNTH-17025] Fix multistep client certificate [datadog_synthetics_test] Fix multistep client certificate Nov 19, 2024
Copy link
Contributor

@Drarig29 Drarig29 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Do you have an idea of how to capture this in unit tests?

datadog/resource_datadog_synthetics_test_.go Show resolved Hide resolved
datadog/resource_datadog_synthetics_test_.go Show resolved Hide resolved
datadog/resource_datadog_synthetics_test_.go Show resolved Hide resolved
return &certString, &keyString
}

func overrideStateCertificate(requestClientCertificates []interface{}, configCert, configKey string) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't do anything with the returned error - is it expected?

Copy link
Contributor

@etnbrd etnbrd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM, great work!
Left a few comments on structure to keep the provider implementation moving into the right direction.

datadog/resource_datadog_synthetics_test_.go Show resolved Hide resolved
datadog/resource_datadog_synthetics_test_.go Show resolved Hide resolved
Comment on lines +3831 to +3861
func overrideStateCertificate(requestClientCertificates []interface{}, configCert, configKey string) error {

if len(requestClientCertificates) == 0 {
return fmt.Errorf("requestClientCertificates is empty")
}
requestClientCertificate, ok := requestClientCertificates[0].(map[string]interface{})
if !ok {
return fmt.Errorf("requestClientCertificates[0] is not a map")
}
certList, ok := requestClientCertificate["cert"].([]interface{})
if !ok || len(certList) == 0 {
return fmt.Errorf("cert is not a valid list or is empty")
}
cert, ok := certList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("cert[0] is not a map")
}
cert["content"] = configCert

keyList, ok := requestClientCertificate["key"].([]interface{})
if !ok || len(keyList) == 0 {
return fmt.Errorf("key is not a valid list or is empty")
}
key, ok := keyList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("key[0] is not a map")
}
key["content"] = configKey

return nil
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mutation is to be avoided as much as possible. Instead, let's have this function return the requestClientCertificates interface, and override it in the caller.

Suggested change
func overrideStateCertificate(requestClientCertificates []interface{}, configCert, configKey string) error {
if len(requestClientCertificates) == 0 {
return fmt.Errorf("requestClientCertificates is empty")
}
requestClientCertificate, ok := requestClientCertificates[0].(map[string]interface{})
if !ok {
return fmt.Errorf("requestClientCertificates[0] is not a map")
}
certList, ok := requestClientCertificate["cert"].([]interface{})
if !ok || len(certList) == 0 {
return fmt.Errorf("cert is not a valid list or is empty")
}
cert, ok := certList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("cert[0] is not a map")
}
cert["content"] = configCert
keyList, ok := requestClientCertificate["key"].([]interface{})
if !ok || len(keyList) == 0 {
return fmt.Errorf("key is not a valid list or is empty")
}
key, ok := keyList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("key[0] is not a map")
}
key["content"] = configKey
return nil
}
func buildDatadogRequestClientCertificate(configCert, configKey string) {
requestClientCertificates := datadogV1. ... TODO
certList, ok := requestClientCertificate["cert"].([]interface{})
cert, ok := certList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("cert[0] is not a map")
}
cert["content"] = configCert
keyList, ok := requestClientCertificate["key"].([]interface{})
if !ok || len(keyList) == 0 {
return fmt.Errorf("key is not a valid list or is empty")
}
key, ok := keyList[0].(map[string]interface{})
if !ok {
return fmt.Errorf("key[0] is not a map")
}
key["content"] = configKey
return requestClientCertificates
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that we should avoid the mutation. However, completeSyntheticsTestRequest is not expecting a nicely formatted requestClientCertificates, but rather a raw stepMap["request_client_certificate"] with a more complexe structure. So the buildDatadogRequestClientCertificate does not really make sense, even more that we already have the buildDatadogRequestCertificates function which kinda does the same thing, inside of completeSyntheticsTestRequest.

rawConfig := d.GetRawConfig()
configCert, configKey := getConfigCertificate(rawConfig, i)
if configCert != nil && configKey != nil {
overrideStateCertificate(stepMap["request_client_certificate"].([]interface{}), *configCert, *configKey)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this function pure, just returning the right structure, and overriding here.

Copy link
Contributor Author

@AntoineDona AntoineDona Nov 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The overriding is quite complexe due to the nested structure of stepMap["request_client_certificate"] -> arrays of 1 object containing arrays of 1 object etc. completeSyntheticsTestRequest expects the weird structure as a param, and then calls buildDatadogRequestCertificates to create the right structure for the certificate, so we can't create it beforehand.
If we want to override without any mutation, it will be easier to create a deepCopy of stepMap["request_client_certificate"], then override it and return the finale clientCertificate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants