-
Notifications
You must be signed in to change notification settings - Fork 381
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
base: master
Are you sure you want to change the base?
[datadog_synthetics_test] Fix multistep client certificate #2683
Conversation
There was a problem hiding this 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?
return &certString, &keyString | ||
} | ||
|
||
func overrideStateCertificate(requestClientCertificates []interface{}, configCert, configKey string) error { |
There was a problem hiding this comment.
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?
There was a problem hiding this 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.
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 | ||
} |
There was a problem hiding this comment.
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.
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 | |
} |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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.