azurerm_data_factory - customer-managed encryption key "cannot parse an empty string" bug fix#28621
Conversation
sreallymatt
left a comment
There was a problem hiding this comment.
Thanks @gerrytan, I've left some comments inline
| } | ||
|
|
||
| if encIdentity := enc.Identity; encIdentity != nil && encIdentity.UserAssignedIdentity != nil { | ||
| if encIdentity := enc.Identity; encIdentity != nil && encIdentity.UserAssignedIdentity != nil && *encIdentity.UserAssignedIdentity != "" { |
There was a problem hiding this comment.
We can make use of pointer.From here to simplify the condition
| if encIdentity := enc.Identity; encIdentity != nil && encIdentity.UserAssignedIdentity != nil && *encIdentity.UserAssignedIdentity != "" { | |
| if encIdentity := enc.Identity; encIdentity != nil && pointer.From(encIdentity.UserAssignedIdentity) != "" { |
There was a problem hiding this comment.
Thanks, will fix.
| parsed, err := commonids.ParseUserAssignedIdentityIDInsensitively(*encIdentity.UserAssignedIdentity) | ||
| if err != nil { | ||
| return fmt.Errorf("parsing %q: %+v", *encIdentity.UserAssignedIdentity, err) | ||
| return fmt.Errorf("parsing %q: %+v", "customer_managed_key_identity_id", err) |
There was a problem hiding this comment.
While here, we can remove the wrapped error, for ID parsing errors we generally just return the error directly
| return fmt.Errorf("parsing %q: %+v", "customer_managed_key_identity_id", err) | |
| return err |
|
This PR is being labeled as "stale" because it has not been updated for 30 or more days. If this PR is still valid, please remove the "stale" label. If this PR is blocked, please add it to the "Blocked" milestone. If you need some help completing this PR, please leave a comment letting us know. Thank you! |
|
@sreallymatt I have addressed all your feedbacks, and acctest has been run against latest commit (see description). Please have another look. |
sreallymatt
left a comment
There was a problem hiding this comment.
Thanks for adding the CustomizeDiff @gerrytan, I do think for safety it needs minor adjustments, would you mind taking a look at the comment I've left inline?
sreallymatt
left a comment
There was a problem hiding this comment.
Forgot to submit comment... 🙃
| func CMKIdentityIdRequiredAtCreation(ctx context.Context, d *pluginsdk.ResourceDiff, meta interface{}) error { | ||
| if d.Id() == "" && | ||
| d.Get("customer_managed_key_id").(string) != "" && | ||
| d.Get("customer_managed_key_identity_id").(string) == "" { |
There was a problem hiding this comment.
Since these 2 CMK properties are IDs, it's likely these will be unknown at initial plan time, while it won't cause issues if both are unknown/computed it will be problematic if only customer_managed_key_identity_id is.
To be safe, we should probably use RawConfig and .IsNull checks, e.g.
if d.Id() == "" {
rawConfig := d.GetRawConfig().AsValueMap()
rawCMK := rawConfig["customer_managed_key_id"]
rawCMKIdentity := rawConfig["customer_managed_key_identity_id"]
if !rawCMK.IsNull() && rawCMKIdentity.IsNull() {
return fmt.Errorf("`customer_managed_key_identity_id` is required when creating a new Data Factory with `customer_managed_key_id`")
}
}What do you think?
There was a problem hiding this comment.
Hey that is a good trick, thank you @sreallymatt I have pushed this change. Also I will document this in the best-practices.md in a separate PR.
There was a problem hiding this comment.
@sreallymatt can you please have a look at this follow up PR where I documented this in the best-practices.md: #31355 🙏
|
Thanks for the feedback @sreallymatt , good for you to take another look. I have triggered another acctest run: https://hashicorp.teamcity.com/buildConfiguration/TF_AzureRM_AZURERM_SERVICE_PUBLIC_DATAFACTORY/556638
Failures have been checked pre-existing on main |
sreallymatt
left a comment
There was a problem hiding this comment.
Thanks @gerrytan - LGTM ✅
|
I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active contributions. |

Community Note
Description
This PR fixes a bug where if
customer_managed_key_idis set on an existing data factory,customer_managed_key_identity_idis assumed to be present, but it could be an empty string when Managed Service Identity (MSI) is used. As a result user see a parsing error when the provider attempted to parse the json payload back into state.To reproduce the bug:
customer_managed_key_idset and withSystemAssignedidentitycustomer_managed_key_idto the URL of the key created in step 2 and perform another deployExpected: Terraform deploy completed with no errors
Actual: Terraform deploy failed with error:
Error: parsing "": parsing "": cannot parse an empty stringThe reproduction step above reflects the
TestAccDataFactory_keyVaultKeyEncryptionWithExistingDataFactoryAndManagedServiceIdentityI implemented in this PR.PR Checklist
For example: “
resource_name_here- description of change e.g. adding propertynew_property_name_here”Changes to existing Resource / Data Source
Testing
https://hashicorp.teamcity.com/buildConfiguration/TF_AzureRM_AZURERM_SERVICE_PUBLIC_DATAFACTORY/531907
Unfortunately there are 34 pre-existing failures in main, but I have diffed and checked there are no new regressions introduced by this change
Change Log
Below please provide what should go into the changelog (if anything) conforming to the Changelog Format documented here.
azurerm_data_factory- customer-managed encryption key "cannot parse an empty string" bug fix [azurerm_data_factory changes and corrupts customer_managed_key_id implicitly #27717]This is a (please select all that apply):
Related Issue(s)
Fixes #27717, #28256
Note
If this PR changes meaningfully during the course of review please update the title and description as required.