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

r/resource_aws_ssm_patch_baseline - add support for ApproveUntilDate attribute #13850

Merged
merged 6 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 4 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
49 changes: 42 additions & 7 deletions aws/resource_aws_ssm_patch_baseline.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,16 @@ func resourceAwsSsmPatchBaseline() *schema.Resource {
Schema: map[string]*schema.Schema{
"approve_after_days": {
Type: schema.TypeInt,
Required: true,
Optional: true,
ValidateFunc: validation.IntBetween(0, 100),
},

"approve_until_date": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))`), "must be formatted YYYY-MM-DD"),
},

"compliance_level": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -235,7 +241,11 @@ func resourceAwsSsmPatchBaselineCreate(d *schema.ResourceData, meta interface{})
}

if _, ok := d.GetOk("approval_rule"); ok {
params.ApprovalRules = expandAwsSsmPatchRuleGroup(d)
rules, err := expandAwsSsmPatchRuleGroup(d)
if err != nil {
return err
}
params.ApprovalRules = rules
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

if _, ok := d.GetOk("source"); ok {
Expand Down Expand Up @@ -288,7 +298,11 @@ func resourceAwsSsmPatchBaselineUpdate(d *schema.ResourceData, meta interface{})
}

if d.HasChange("approval_rule") {
params.ApprovalRules = expandAwsSsmPatchRuleGroup(d)
rules, err := expandAwsSsmPatchRuleGroup(d)
if err != nil {
return err
}
params.ApprovalRules = rules
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

if d.HasChange("global_filter") {
Expand Down Expand Up @@ -441,7 +455,7 @@ func flattenAwsSsmPatchFilterGroup(group *ssm.PatchFilterGroup) []map[string]int
return result
}

func expandAwsSsmPatchRuleGroup(d *schema.ResourceData) *ssm.PatchRuleGroup {
func expandAwsSsmPatchRuleGroup(d *schema.ResourceData) (*ssm.PatchRuleGroup, error) {
bflad marked this conversation as resolved.
Show resolved Hide resolved
var rules []*ssm.PatchRule

ruleConfig := d.Get("approval_rule").([]interface{})
Expand All @@ -468,18 +482,31 @@ func expandAwsSsmPatchRuleGroup(d *schema.ResourceData) *ssm.PatchRuleGroup {
}

rule := &ssm.PatchRule{
ApproveAfterDays: aws.Int64(int64(rCfg["approve_after_days"].(int))),
PatchFilterGroup: filterGroup,
ComplianceLevel: aws.String(rCfg["compliance_level"].(string)),
EnableNonSecurity: aws.Bool(rCfg["enable_non_security"].(bool)),
}

// Verify that at least one of approve_after_days or approve_until_date is set
approveAfterDays, _ := rCfg["approve_after_days"].(int)
approveUntilDate, _ := rCfg["approve_until_date"].(string)

if approveAfterDays > 0 && len(approveUntilDate) > 0 {
return nil, fmt.Errorf("Only one of approve_after_days or approve_until_date must be configured")
}

if len(approveUntilDate) > 0 {
rule.ApproveUntilDate = aws.String(approveUntilDate)
} else {
rule.ApproveAfterDays = aws.Int64(int64(approveAfterDays))
}
bflad marked this conversation as resolved.
Show resolved Hide resolved

rules = append(rules, rule)
}

return &ssm.PatchRuleGroup{
PatchRules: rules,
}
}, nil
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

func flattenAwsSsmPatchRuleGroup(group *ssm.PatchRuleGroup) []map[string]interface{} {
Expand All @@ -491,10 +518,18 @@ func flattenAwsSsmPatchRuleGroup(group *ssm.PatchRuleGroup) []map[string]interfa

for _, rule := range group.PatchRules {
r := make(map[string]interface{})
r["approve_after_days"] = aws.Int64Value(rule.ApproveAfterDays)
r["compliance_level"] = aws.StringValue(rule.ComplianceLevel)
r["enable_non_security"] = aws.BoolValue(rule.EnableNonSecurity)
r["patch_filter"] = flattenAwsSsmPatchFilterGroup(rule.PatchFilterGroup)

if rule.ApproveAfterDays != nil {
r["approve_after_days"] = aws.Int64Value(rule.ApproveAfterDays)
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

if rule.ApproveUntilDate != nil {
r["approve_until_date"] = aws.StringValue(rule.ApproveUntilDate)
bflad marked this conversation as resolved.
Show resolved Hide resolved
}

result = append(result, r)
}

Expand Down
109 changes: 109 additions & 0 deletions aws/resource_aws_ssm_patch_baseline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,54 @@ func TestAccAWSSSMPatchBaseline_OperatingSystem(t *testing.T) {
})
}

func TestAccAWSSSMPatchBaseline_ApproveUntilDateParam(t *testing.T) {
var before, after ssm.PatchBaselineIdentity
name := acctest.RandString(10)
resourceName := "aws_ssm_patch_baseline.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSSSMPatchBaselineDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSSSMPatchBaselineConfigWithApproveUntilDate(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMPatchBaselineExists(resourceName, &before),
resource.TestCheckResourceAttr(resourceName, "approval_rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.approve_until_date", "2020-01-01"),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.patch_filter.#", "2"),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.compliance_level", ssm.PatchComplianceLevelCritical),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.enable_non_security", "true"),
resource.TestCheckResourceAttr(resourceName, "operating_system", "AMAZON_LINUX"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAWSSSMPatchBaselineConfigWithApproveUntilDateUpdated(name),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSSSMPatchBaselineExists(resourceName, &after),
resource.TestCheckResourceAttr(resourceName, "approval_rule.#", "1"),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.approve_until_date", "2020-02-02"),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.patch_filter.#", "2"),
resource.TestCheckResourceAttr(resourceName, "approval_rule.0.compliance_level", ssm.PatchComplianceLevelCritical),
resource.TestCheckResourceAttr(resourceName, "operating_system", "AMAZON_LINUX"),
func(*terraform.State) error {
if aws.StringValue(before.BaselineId) != aws.StringValue(after.BaselineId) {
t.Fatal("Baseline IDs changed unexpectedly")
}
return nil
},
),
},
},
})
}

func TestAccAWSSSMPatchBaseline_Sources(t *testing.T) {
var before, after ssm.PatchBaselineIdentity
name := acctest.RandString(10)
Expand Down Expand Up @@ -252,6 +300,7 @@ func TestAccAWSSSMPatchBaseline_RejectPatchesAction(t *testing.T) {
var ssmPatch ssm.PatchBaselineIdentity
name := acctest.RandString(10)
resourceName := "aws_ssm_patch_baseline.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down Expand Up @@ -462,6 +511,66 @@ resource "aws_ssm_patch_baseline" "test" {
`, rName)
}

func testAccAWSSSMPatchBaselineConfigWithApproveUntilDate(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_patch_baseline" "test" {
name = %[1]q
operating_system = "AMAZON_LINUX"
description = "Baseline containing all updates approved for production systems"

tags = {
Name = "My Patch Baseline"
}

approval_rule {
approve_until_date = "2020-01-01"
enable_non_security = true
compliance_level = "CRITICAL"

patch_filter {
key = "PRODUCT"
values = ["AmazonLinux2016.03", "AmazonLinux2016.09", "AmazonLinux2017.03", "AmazonLinux2017.09"]
}

patch_filter {
key = "SEVERITY"
values = ["Critical", "Important"]
}
}
}
`, rName)
}

func testAccAWSSSMPatchBaselineConfigWithApproveUntilDateUpdated(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_patch_baseline" "test" {
name = %[1]q
operating_system = "AMAZON_LINUX"
description = "Baseline containing all updates approved for production systems"

tags = {
Name = "My Patch Baseline"
}

approval_rule {
approve_until_date = "2020-02-02"
enable_non_security = true
compliance_level = "CRITICAL"

patch_filter {
key = "PRODUCT"
values = ["AmazonLinux2016.03", "AmazonLinux2016.09", "AmazonLinux2017.03", "AmazonLinux2017.09"]
}

patch_filter {
key = "SEVERITY"
values = ["Critical", "Important"]
}
}
}
`, rName)
}

func testAccAWSSSMPatchBaselineConfigWithSource(rName string) string {
return fmt.Sprintf(`
resource "aws_ssm_patch_baseline" "test" {
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/ssm_patch_baseline.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ The following arguments are supported:

The `approval_rule` block supports:

* `approve_after_days` - (Required) The number of days after the release date of each patch matched by the rule the patch is marked as approved in the patch baseline. Valid Range: 0 to 100.
* `approve_after_days` - (Optional) The number of days after the release date of each patch matched by the rule the patch is marked as approved in the patch baseline. Valid Range: 0 to 100. Conflicts with `approve_until_date`
* `approve_until_date` - (Optional) The cutoff date for auto approval of released patches. Any patches released on or before this date are installed automatically. Date is formatted as `YYYY-MM-DD`. Conflicts with `approve_after_days`
* `patch_filter` - (Required) The patch filter group that defines the criteria for the rule. Up to 5 patch filters can be specified per approval rule using Key/Value pairs. Valid Keys are `PATCH_SET | PRODUCT | CLASSIFICATION | MSRC_SEVERITY | PATCH_ID`. Valid combinations of these Keys and the `operating_system` value can be found in the [SSM DescribePatchProperties API Reference](https://docs.aws.amazon.com/systems-manager/latest/APIReference/API_DescribePatchProperties.html). Valid Values are exact values for the patch property given as the key, or a wildcard `*`, which matches all values.
* `PATCH_SET` defaults to `OS` if unspecified
* `compliance_level` - (Optional) Defines the compliance level for patches approved by this rule. Valid compliance levels include the following: `CRITICAL`, `HIGH`, `MEDIUM`, `LOW`, `INFORMATIONAL`, `UNSPECIFIED`. The default value is `UNSPECIFIED`.
Expand Down