chaosmonkey.api package

Submodules

chaosmonkey.api.api_errors module

This module contains an object that represent an API Error

Any APIError thrown in an endpoint is handled to return to the user a proper json error with custom status code and message

exception chaosmonkey.api.api_errors.APIError(message, status_code=None, payload=None)[source]

Bases: Exception

Represents an API Error

Parameters:
  • message – message to be returned to the user
  • status_code – response status code (defaults to 400)
  • payload – custom payload to give extra info in the response
Example:
>>> raise APIError("Error detected", 500, {"extra": "extra_info"})
to_dict()[source]

Convert exception to dict

chaosmonkey.api.request_validator module

chaosmonkey.api.request_validator.validate_payload(request, schema)[source]

validates a request payload against a json schema

Parameters:
  • request – request received with valid json body
  • schema – schema to validate the request payload
Returns:

True

Raises:

chaosmonkey.api.api_errors()

chaosmonkey.api.attacks_blueprint module

Base path: /api/1/attacks

Attacks are python modules (located in /attacks folder) that are executed to perform actual attacks.

Each attack has three main properties represented in the API:

  1. example: a JSON example for the attack. Use it as a template to call /plans endpoints
  2. ref: its unique identifier. module_name:AttackClass
  3. schema: json schema that validates the json representation for the attack
chaosmonkey.api.attacks_blueprint.list_attacks()[source]

Return a list with the available attacks and its configuration.

Example:

{
    "attacks": [
        {
            "example": {
                "args": {
                    "filters": {
                        "tag:Name": "playground-asg"
                    },
                    "region": "eu-west-1"
                },
                "ref": "terminate_ec2_instance:TerminateEC2Instance"
            },
            "ref": "terminate_ec2_instance:TerminateEC2Instance",
            "schema": {
                "type": "object",
                "properties": {
                    "args": {
                        "type": "object",
                        "properties": {
                            "filters": {
                                "type": "object",
                                "properties": {
                                    "tag:Name": {
                                        "type": "string"
                                    }
                                },
                                "required": [
                                    "tag:Name"
                                ]
                            },
                            "region": {
                                "optional": true,
                                "type": "string"
                            }
                        },
                        "required": [
                            "region",
                            "filters"
                        ]
                    },
                    "ref": {
                        "type": "string"
                    }
                }
            }
        }
    ],
    "_links": {
        "self": {
            "href": "/api/1/attacks/"
        }
    }
}
Returns:chaosmonkey.api.hal.document()

chaosmonkey.api.executors_blueprint module

Base path: /api/1/executors

Executors are scheduled jobs that are related with an attack, so in the given date the job will execute the attack. The only way to create executors is through chaosmonkey.api.plans_blueprint()

Evey executor has 4 main properties:

  1. id: unique identifier
  2. next_run_time: The time and date that the executor is going to be executed
  3. plan_id: id of the plan that created the executor
  4. executed: if the executor has been executed

Example:

{
    "id": "3b373155577b4d1bbc62216ffea013a4",
    "plan_id": "3ec72048cab04b76bdf2cfd4bc81cd1e",
    "next_run_time": "2017-01-25T10:12:1485339145",
    "executed": false
}
chaosmonkey.api.executors_blueprint.delete_executor(executor_id)[source]

Delete an executor

Example request:

DEL /api/1/executors/6890192d8b6c40e5af16f13aa036c7dc
chaosmonkey.api.executors_blueprint.dict_to_trigger(trigger_dict)[source]

Returns a trigger version of the trigger json

chaosmonkey.api.executors_blueprint.get_executors()[source]

Get a list of scheduled executors

Example response:

{
    "executors":[
        {
            "_links":{
                "self":{
                    "href":"/api/1/executors/3b373155577b4d1bbc62216ffea013a4"
                },
                "update":{
                    "href":"/api/1/executors/3b373155577b4d1bbc62216ffea013a4"
                },
                "delete":{
                    "href":"/api/1/executors/3b373155577b4d1bbc62216ffea013a4"
                }
            },
            "id":"3b373155577b4d1bbc62216ffea013a4",
            "plan_id":"3ec72048cab04b76bdf2cfd4bc81cd1e",
            "next_run_time":"2017-01-25T10:12:1485339145",
            "executed": false
        }
    ]
}
Param:executed. Control when to show all executors (true) or only not executed (false). Defaults to false
Returns:chaosmonkey.api.hal.document()
chaosmonkey.api.executors_blueprint.put_executor(executor_id)[source]

Update a executor to change its date. To provide a new date use the format in the example bellow. The format is used to create a DateTrigger from the apscheduler.

TODO: create more Triggers

Example request:

PUT /api/1/executors/3b373155577b4d1bbc62216ffea013a4
Body:
    {
      "type" : "date",
      "args" : {
        "date": "2017-10-23T19:19"
      }
    }

Example response:

{
  "id": "3b373155577b4d1bbc62216ffea013a4",
  "plan_id": "3ec72048cab04b76bdf2cfd4bc81cd1e",
  "next_run_time": "2017-10-23T19:19:1508786354",
  "executed": false,
  "_links": {
    "self": {
      "href": "/api/1/executors/3b373155577b4d1bbc62216ffea013a4"
    },
    "update":{
      "href":"/api/1/executors/3b373155577b4d1bbc62216ffea013a4"
    },
    "delete":{
      "href":"/api/1/executors/3b373155577b4d1bbc62216ffea013a4"
    }
  }
}
Returns:chaosmonkey.api.hal.document()
chaosmonkey.api.executors_blueprint.trigger_to_dict(trigger)[source]

Returns a dict version of the trigger

chaosmonkey.api.planners_blueprint module

Base path: /api/1/planners

Planners are python modules (located in /planners folder). Planners are responsible of create executors.

Planners has three main properties represented in the API:

  1. example: a JSON example for the planner
  2. ref: its unique identifier. module_name:PlannerClass
  3. schema: json schema that validates the planner
chaosmonkey.api.planners_blueprint.list_planners()[source]

Return a list with the available planners and its configuration.

Example response:

{
    "_links": {
        "self": {
            "href": "/api/1/planners/"
        }
    },
    "planners": [
        {
            "example": {
                "args": {
                    "times": 4,
                    "max_time": "15:00",
                    "min_time": "10:00"
                },
                "ref": "simple_planner:SimplePlanner"
            },
            "ref": "simple_planner:SimplePlanner",
            "schema": {
                "type": "object",
                "properties": {
                    "args": {
                        "type": "object",
                        "properties": {
                            "times": {
                                "type": "number"
                            },
                            "max_time": {
                                "type": "string"
                            },
                            "min_time": {
                                "type": "string"
                            }
                        }
                    },
                    "ref": {
                        "type": "string"
                    }
                }
            }
        }
    ]
}
Returns:chaosmonkey.api.hal.document()

chaosmonkey.api.plans_blueprint module

Base path: /api/1/plans

Plans receive a planner and an attack and create executors calling the corresponding planner with the given attack.

Each plan creates N executors related to an attack to be executed in the future.

Plans has the following properties

  • id: unique identifier for a plan
  • created: creation date
  • next_execution: execution date for the next executor
  • name: plan name
  • executors_count: number of executors in the plan
  • executed: if all the executors in the plan has been executed
chaosmonkey.api.plans_blueprint.add_plan()[source]

Add a plan.

Example request:

PUT /api/1/executors/3b373155577b4d1bbc62216ffea013a4
Body:
    {
        "name": "Terminate instances in Playground",
        "attack": {
            "args": {
                "region": "eu-west-1",
                "filters": {
                    "tag:Name": "playground-asg"
                }
            },
            "ref": "terminate_ec2_instance:TerminateEC2Instance"
        },
        "planner": {
            "ref": "simple_planner:SimplePlanner",
            "args": {
                "min_time" : "10:00",
                "max_time" : "19:00",
                "times": 4
            }
        }
    }
chaosmonkey.api.plans_blueprint.delete_plan(plan_id)[source]

Delete a plan

Example request:

DEL /api/1/plans/6890192d8b6c40e5af16f13aa036c7dc
chaosmonkey.api.plans_blueprint.get_plan(plan_id)[source]

Get a plan with all related executors

Example request:

GET /api/1/plans/6890192d8b6c40e5af16f13aa036c7dc

Example response:

{
    "id": "6890192d8b6c40e5af16f13aa036c7dc",
    "_embedded": {
        "executors": [
            {
                "plan_id": "6890192d8b6c40e5af16f13aa036c7dc",
                "_links": {
                    "self": {
                        "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dcdd2530572fd04c5aa061f261f82743d3"
                    },
                    "update": {
                        "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dcdd2530572fd04c5aa061f261f82743d3"
                    },
                    "delete": {
                        "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dcdd2530572fd04c5aa061f261f82743d3"
                    }
                },
                "next_run_time": "2017-01-26T13:14:1485436447",
                "id": "dd2530572fd04c5aa061f261f82743d3"
            },
            {
                "plan_id": "6890192d8b6c40e5af16f13aa036c7dc",
                "_links": {
                    "self": {
                        "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc1dd3f0d392e545808edb74852213c1ae"
                    },
                    "update": {
                        "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc1dd3f0d392e545808edb74852213c1ae"
                    },
                    "delete": {
                        "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc1dd3f0d392e545808edb74852213c1ae"
                    }
                },
                "next_run_time": "2017-01-26T18:24:1485455082",
                "id": "1dd3f0d392e545808edb74852213c1ae"
            }
        ]
    },
    "created": "2017-01-26T10:41:1485427282",
    "next_execution": null,
    "name": "Terminate instances in Playground",
    "executors_count": null,
    "_links": {
        "self": {
            "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc"
        }
    }
}
Returns:chaosmonkey.api.hal.document()
chaosmonkey.api.plans_blueprint.list_plans()[source]

List all plans created

Example request:

GET /api/1/plans/?all=true

Example response:

{
    "_links": {
        "self": {
            "href": "/api/1/plans/"
        }
    },
    "plans": [
        {
            "id": "6890192d8b6c40e5af16f13aa036c7dc",
            "created": "2017-01-26T10:41:1485427282",
            "next_execution": "2017-01-26 13:14:07.583372",
            "name": "Terminate instances in Playground",
            "executors_count": 2,
            "_links": {
                "self": {
                    "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc"
                },
                "update": {
                    "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc"
                },
                "delete": {
                    "href": "/api/1/plans/6890192d8b6c40e5af16f13aa036c7dc"
                }
            }
        }
    ]
}
Param:all. Control when to show all plans (true) or only not executed (false). Defaults to false
Returns:chaosmonkey.api.hal.document()

Module contents

API Package.

This package creates a flask application with the API to interact with the CME.