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

[HPR-1606] Add support specifying disk encryption set keys across replicated regions #371

Merged
merged 4 commits into from
Feb 16, 2024

Conversation

nywilken
Copy link
Contributor

@nywilken nywilken commented Feb 5, 2024

Add support for target_region block in SharedImageGalleryDestination

As an alternative to specifying replication_regions for a Shared Image
Gallery Destination, users can not use one or more target_region block
attributes to define the target regions for a shared image version. The
target_region block attribute supports the setting of individual disk
encryption key id for multi-region replication of encrypted
disks using CMKs.

The target_region attribute is meant to replace the now deprecated
replication_regions attribute. Internally, any values set for
replication_regions will be transferred into a set of target_region
blocks to provide the same level of functionality. The one difference being that
Packer will no longer add a default entry for the SIG build location
when using target_region blocks.

Closes: #264

Example Config

variable "resource_group" {
  type    = string
  default = "${env("ARM_RESOURCE_GROUP)}"
}

source "azure-arm" "autogenerated_1" {
  use_azure_cli_auth                = true
  image_offer                       = "UbuntuServer"
  image_publisher                   = "Canonical"
  image_sku                         = "16.04-LTS"
  location                          = "East US"
  os_type                           = "Linux"
  vm_size         = "Standard_DS2_v2"
  shared_image_gallery_destination {
    gallery_name        = "PackerSigGallery"
    image_name          = "PackerSigImageDefinition"
    image_version       = "1.0.1"
    resource_group      = var.resource_group
    target_region {
        name = "West US 2"
        disk_encryption_set_id = "KEY"
    }
    target_region {
        name = "East US"
        disk_encryption_set_id = "KEY"
    }
  }
}

build {
  sources = ["source.azure-arm.autogenerated_1"]

  provisioner "shell" {
    execute_command = "chmod +x {{ .Path }}; {{ .Vars }} sudo -E sh '{{ .Path }}'"
    inline          = ["apt-get update", "apt-get upgrade -y", "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"]
    inline_shebang  = "/bin/sh -x"
  }

}

@nywilken nywilken changed the title nywilken/target regions for sig [HPR-1606] Add support specifying DES across replicated regions Feb 5, 2024
@nywilken nywilken force-pushed the nywilken/target-regions-for-sig branch 3 times, most recently from 1e4d28b to 89dcd8b Compare February 5, 2024 20:28
@nywilken nywilken changed the title [HPR-1606] Add support specifying DES across replicated regions [HPR-1606] Add support specifying disk encryption set keys across replicated regions Feb 8, 2024
builder/azure/arm/builder.go Outdated Show resolved Hide resolved
builder/azure/arm/builder.go Outdated Show resolved Hide resolved
@nywilken nywilken marked this pull request as ready for review February 8, 2024 18:09
@nywilken nywilken requested a review from a team as a code owner February 8, 2024 18:09
}

// TODO It would be better if validation could be handled in a central location
Copy link
Contributor

Choose a reason for hiding this comment

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

This code looks good but I wonder if we should add this validation to its own function or its own step, that way we could more easily unit test this logic in isolation, feel free to take that feedback or leave it, but I think it'd be good to add some sort of unit test coverage to ensure that we don't break the backwards compatibility you've established here in future updates to this code

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved this into config.go and added a few tests for it. Let me know what you think.

if foundMandatoryReplicationRegion == false {
b.config.SharedGalleryDestination.SigDestinationReplicationRegions = append(normalizedReplicationRegions, buildLocation)
// Validate target region settings; it can be the deprecated replicated_regsions attribute or multiple target_region blocks
if (len(b.config.SharedGalleryDestination.SigDestinationReplicationRegions) > 0) && (len(b.config.SharedGalleryDestination.SigDestinationTargetRegions) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we check for this before we compute the regions? Like in the config validation we could just check if target_regions and replicated_regions are set together, that way we fail a bit earlier when the config is invalid, what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah we can do that. This checks seems like it is bit late!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Contributor

@JenGoldstrich JenGoldstrich left a comment

Choose a reason for hiding this comment

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

This largely LGTM, nice work on this issue Wilken, I left a thought about a block in builder.go I think we could break off into a function and unit test in isolation, but I'm happy to do that refactoring in a follow up PR if you'd prefer, lemme know what you think!

@@ -1362,6 +1362,116 @@ func TestConfigShouldAcceptShallowReplicationWithWithUnsetReplicaCount(t *testin
t.Fatalf("expected config to accept shallow replication with unset replica count build: %v", err)
}
}

func TestConfigValidateShallowReplicationRegion(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome! thanks for adding these

return nil, fmt.Errorf("when `use_shallow_replication` is enabled the value of `replicated_regions` must match the build region specified by `location` or match the region of `build_resource_group_name`.")

// Convert deprecated replication_regions to []TargetRegion
if len(b.config.SharedGalleryDestination.SigDestinationReplicationRegions) > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not blocking and I think this could be done in a follow up PR but I think we should move this block as well, into a new function called ConvertReplicatedRegions in can still exist in the builder but that way we can unit test it in isolation so that we don't break backwards compatibility in the future with this unintentionally.

Copy link
Contributor Author

@nywilken nywilken Feb 13, 2024

Choose a reason for hiding this comment

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

FWIW I started down this path, as I agree this could help with testing but there is a bunch of nuance logic happening there that I wouldn't want to hide in a function. Breaking out the for loop is straight forward but we would run it after normalization. There are a few tests already that if given replication_regions asserts that the the correct number of target_regions are crated. If that helps.

Copy link
Contributor

Choose a reason for hiding this comment

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

That makes sense, thanks for clarifying!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about how to move this stuff into a step_* but 😵 - maybe after a day something comes up. Thanks for this suggestion.

As an alternative to specifying replication_regions for a Shared Image
Gallery Destination, users can not use one or more `target_region` block
attributes to define the target regions for a shared image version. The
target_region block attribute supports the setting of individual disk
encryption key id for multi-region replication of encrypted
disks using CMKs.

The `target_region` attribute is meant to replace the now deprecated
`replication_regions` attribute. Internally, any values set for
replication_regions will be transferred into a set of target_region
blocks to provide the same level of functionality. The one difference being that
Packer will no longer add a default entry for the SIG build location
when using target_region blocks.
@nywilken nywilken force-pushed the nywilken/target-regions-for-sig branch from baf7527 to e7dc560 Compare February 16, 2024 17:22
@nywilken nywilken merged commit 66da753 into main Feb 16, 2024
12 checks passed
@nywilken nywilken deleted the nywilken/target-regions-for-sig branch February 16, 2024 17:36
julianmaze pushed a commit to julianmaze/packer-plugin-azure that referenced this pull request Feb 21, 2024
…licated regions (hashicorp#371)

* Add support for target_region block in SharedImageGalleryDestination

As an alternative to specifying replication_regions for a Shared Image
Gallery Destination, users can not use one or more `target_region` block
attributes to define the target regions for a shared image version. The
target_region block supports attributes for setting individual disk
encryption key id to support multi-region replication of encrypted
disks using CMKs.

The `target_region` block attribute is meant to replace the now deprecated
`replication_regions` attributed. Internally, any values set for
replication_regions will be transferred into a set of target_regions to
provide the same level of functionality. The one difference being that
Packer will no longer add a default entry for the SIG build location
when using target_region blocks.
julianmaze pushed a commit to julianmaze/packer-plugin-azure that referenced this pull request Feb 21, 2024
…licated regions (hashicorp#371)

* Add support for target_region block in SharedImageGalleryDestination

As an alternative to specifying replication_regions for a Shared Image
Gallery Destination, users can not use one or more `target_region` block
attributes to define the target regions for a shared image version. The
target_region block supports attributes for setting individual disk
encryption key id to support multi-region replication of encrypted
disks using CMKs.

The `target_region` block attribute is meant to replace the now deprecated
`replication_regions` attributed. Internally, any values set for
replication_regions will be transferred into a set of target_regions to
provide the same level of functionality. The one difference being that
Packer will no longer add a default entry for the SIG build location
when using target_region blocks.
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.

Disk Encryption Set ID does not work for Replicated Regions
2 participants