Skip to content

Commit

Permalink
new: added policy ecc-aws-1005-ebs_volumes_too_old_snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
Vladyslav Yevsiukov authored and jamthehackerscat committed Aug 21, 2023
1 parent fc3909d commit 4d48faf
Show file tree
Hide file tree
Showing 18 changed files with 259 additions and 0 deletions.
23 changes: 23 additions & 0 deletions policies/ecc-aws-1005-ebs_volumes_too_old_snapshots.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright (c) 2023 EPAM Systems, Inc.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


policies:
- name: ecc-aws-1005-ebs_volumes_too_old_snapshots
description: |
EBS Snapshots older than 30 days
resource: aws.ebs-snapshot
filters:
- type: age
days: 30
op: ge
- type: value
key: State
value: completed
- type: unused
value: true
- type: skip-ami-snapshots
value: true
12 changes: 12 additions & 0 deletions terraform/epam-aws-1005-ebs_volumes_too_old_snapshots/green/ebs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "aws_ebs_volume" "this" {
availability_zone = var.default-az
size = 8

tags = {
Name = "1005_ebs_volume_Green"
}
}

resource "aws_ebs_snapshot" "this" {
volume_id = aws_ebs_volume.this.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4"
}
}
}

provider "aws"{
profile = var.profile
region = var.default-region

default_tags {
tags = {
CustodianRule = "ecc-aws-1005-ebs_volumes_too_old_snapshots"
ComplianceStatus = "Green"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
profile = "c7n"
default-region = "us-east-1"
default-az = "us-east-1a"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "default-region" {
type = string
description = "Default region for resources will be created"
}

variable "profile" {
type = string
description = "Profile name configured before running apply"
}

variable "default-az" {
type = string
description = "Default availability zone for resources"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "Stmt1684842287073",
"Action": [
"ec2:DescribeSnapshots",
"ec2:DescribeImages",
"autoscaling:DescribeAutoScalingGroups"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
12 changes: 12 additions & 0 deletions terraform/epam-aws-1005-ebs_volumes_too_old_snapshots/red/ebs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
resource "aws_ebs_volume" "this" {
availability_zone = var.default-az
size = 8

tags = {
Name = "1005_ebs_volume_Green"
}
}

resource "aws_ebs_snapshot" "this" {
volume_id = aws_ebs_volume.this.id
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4"
}
}
}

provider "aws"{
profile = var.profile
region = var.default-region

default_tags {
tags = {
CustodianRule = "ecc-aws-1005-ebs_volumes_too_old_snapshots"
ComplianceStatus = "Red"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
profile = "c7n"
default-region = "us-east-1"
default-az = "us-east-1a"
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
variable "default-region" {
type = string
description = "Default region for resources will be created"
}

variable "profile" {
type = string
description = "Profile name configured before running apply"
}

variable "default-az" {
type = string
description = "Default availability zone for resources"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PolicyTest(object):

def test_resources(self, base_test, resources):
base_test.assertEqual(len(resources), 0)

def mock_time(self):
return 2023, 7, 21
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"AutoScalingGroups": [],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"Images": [],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"status_code": 200,
"data": {
"Snapshots": [
{
"Description": "",
"Encrypted": false,
"OwnerId": "111111111111",
"Progress": "100%",
"SnapshotId": "snap-017930ee30036ecd6",
"StartTime": {
"__class__": "datetime",
"year": 2023,
"month": 7,
"day": 21,
"hour": 13,
"minute": 28,
"second": 11,
"microsecond": 792000
},
"State": "completed",
"VolumeId": "vol-086820785f5facb7b",
"VolumeSize": 8,
"Tags": [
{
"Key": "ComplianceStatus",
"Value": "Green"
},
{
"Key": "CustodianRule",
"Value": "ecc-aws-1005-ebs_volumes_too_old_snapshots"
}
],
"StorageTier": "standard"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"AutoScalingGroups": [],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"Images": [],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"status_code": 200,
"data": {
"Snapshots": [
{
"Description": "",
"Encrypted": false,
"OwnerId": "111111111111",
"Progress": "100%",
"SnapshotId": "snap-022dc2a681fa8a1d1",
"StartTime": {
"__class__": "datetime",
"year": 2023,
"month": 5,
"day": 21,
"hour": 13,
"minute": 27,
"second": 14,
"microsecond": 480000
},
"State": "completed",
"VolumeId": "vol-0590bbd50c29f927f",
"VolumeSize": 8,
"Tags": [
{
"Key": "ComplianceStatus",
"Value": "Red"
},
{
"Key": "CustodianRule",
"Value": "ecc-aws-1005-ebs_volumes_too_old_snapshots"
}
],
"StorageTier": "standard"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from datetime import datetime, timedelta

class PolicyTest(object):

def test_resources(self, base_test, resources):
base_test.assertEqual(len(resources), 1)
LastAccessedDate=datetime.fromisoformat(str(resources[0]['StartTime']))
time_now= datetime.fromisoformat('2023-07-21 13:27:14.283+00:00')
datatime30ago=time_now-timedelta(days=30)
base_test.assertFalse(LastAccessedDate>datatime30ago)

0 comments on commit 4d48faf

Please sign in to comment.