Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Commit

Permalink
Fix Minor Regression with TSDB plugin, Expose the plugin in controller (
Browse files Browse the repository at this point in the history
#110)

* Fix Minor Regression with TSDB plugin, Expose the plugin in controller

* Changelog

* Code Formatting

* Minor renames

* Fix Rest Issues

* Use Gender Neutral comment
  • Loading branch information
shashankrnr32 committed Jan 17, 2022
1 parent c3651ca commit d3c61f7
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 12 deletions.
4 changes: 4 additions & 0 deletions docs/releases.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Version 0.x.x

### Next Version (0.6.0)

1. Fix Regression with OpenTSDB Verification Plugin by [Shashank Sharma](https://github.com/shashankrnr32)

### Version 0.5.0
1. Add `MachineTargetExecutor` support for contrib agents by [Alfin S Thomas](https://github.com/AlfinST)

Expand Down
4 changes: 2 additions & 2 deletions src/ychaos/agents/network/iptables.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def iptables_command_builder(


class IPTablesBlockConfig(TimedAgentConfig):
name = "block_ports"
name = "iptables_block"
desc = "This agent modifies the iptables rules to block traffic to specified ports or endpoint"
is_sudo = True
incoming_ports: Optional[List[int]] = Field(
Expand Down Expand Up @@ -290,7 +290,7 @@ def teardown(self) -> None:


class DNSBlockConfig(TimedAgentConfig):
name = "block_dns"
name = "dns_block"
desc = "This agent modifies the iptables rules to block traffic to DNS ports"

is_sudo = True
Expand Down
3 changes: 1 addition & 2 deletions src/ychaos/agents/system/icmp.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# Copyright 2021, Yahoo
# Licensed under the terms of the Apache 2.0 license. See the LICENSE file in the project root for terms

# TODO: PingDisable Agent
import warnings
from queue import LifoQueue

Expand All @@ -16,7 +15,7 @@


class PingDisableConfig(TimedAgentConfig):
name = "ping_disable"
name = "disable_ping"
description = "This agent disables the system to respond to ping/icmp packets"

priority = AgentPriority.MODERATE_PRIORITY
Expand Down
5 changes: 3 additions & 2 deletions src/ychaos/core/executor/MachineTargetExecutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
# Licensed under the terms of the Apache 2.0 license. See the LICENSE file in the project root for terms
import json
import random
from types import SimpleNamespace
from pathlib import Path
from ...app_logger import AppLogger
from types import SimpleNamespace

from ...agents.index import AgentType
from ...app_logger import AppLogger
from ...testplan.attack import MachineTargetDefinition
from ...testplan.schema import TestPlan
from ...utils.dependency import DependencyUtils
Expand Down
2 changes: 2 additions & 0 deletions src/ychaos/core/verification/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from .plugins.HTTPRequestVerificationPlugin import (
HTTPRequestVerificationPlugin,
)
from .plugins.OpenTSDBVerificationPlugin import OpenTSDBVerificationPlugin
from .plugins.PythonModuleVerificationPlugin import (
PythonModuleVerificationPlugin,
)
Expand All @@ -27,6 +28,7 @@
"python_module": PythonModuleVerificationPlugin,
"http_request": HTTPRequestVerificationPlugin,
"sdv4": SDv4VerificationPlugin,
"tsdb": OpenTSDBVerificationPlugin,
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def _build_session(self):
session.headers = self.config.headers

if not self.config.verify:
# Disable Insecure Request Warning if verify=False (User knows what he is doing)
# Disable Insecure Request Warning if verify=False (Users know what they are doing)
from requests.packages.urllib3.exceptions import (
InsecureRequestWarning,
)
Expand All @@ -58,7 +58,7 @@ def _build_session(self):
"Bearer " + self.config.bearer_token.get_secret_value()
)

session.cert = self.config.cert
session.cert = self.config.get_request_cert()

return session

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from pydantic import BaseModel, validate_arguments

from ....testplan.verification import OpenTSDBVerification
from ....utils.types import Json
from ..data import VerificationData, VerificationStateData
from .BaseVerificationPlugin import RequestVerificationPlugin

Expand All @@ -15,6 +16,7 @@ class OpenTSDBVerificationData(BaseModel):
url: str # Making this string since this is built with only trusted data
status_code: Optional[int]
data: Dict[datetime, float]
response: Optional[Json]

__verification_type__ = "tsdb"

Expand All @@ -34,6 +36,7 @@ def run_verification(self) -> VerificationStateData:
self.config.method,
url=str(self.config.url),
timeout=self.config.timeout / 1000,
json=self.config.query,
)

if tsdb_response.status_code != 200:
Expand All @@ -44,6 +47,7 @@ def run_verification(self) -> VerificationStateData:
url=self.config.url,
status_code=tsdb_response.status_code,
data=dict(),
response=tsdb_response.json(),
).dict(),
)

Expand Down
13 changes: 12 additions & 1 deletion src/ychaos/testplan/verification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class HTTPRequestSchema(SchemaModel):
)

# Certificate
cert: Tuple[Path, Path] = Field(
cert: Optional[Tuple[Path, Path]] = Field(
default=None,
description="The certificate to be sent for HTTP call. The tuple should contain Certificate and Key File path",
)
Expand All @@ -85,6 +85,17 @@ class HTTPRequestSchema(SchemaModel):
BuiltinUtils.Request.validate_method
)

def get_request_cert(self) -> Optional[Tuple[Path, Path]]:
"""
Returns the resolved Certificate Paths by expanding the user path (~).
"""
if self.cert:
return (
self.cert[0].expanduser().resolve(),
self.cert[1].expanduser().resolve(),
)
return None


class HTTPRequestVerification(HTTPRequestSchema):
"""
Expand Down
10 changes: 10 additions & 0 deletions src/ychaos/utils/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from typing import Dict, List, TypeVar, Union

JsonTypeVar = TypeVar("JsonTypeVar")

JsonPrimitive = Union[str, float, int, bool, None]

JsonDict = Dict[str, JsonTypeVar]
JsonArray = List[JsonTypeVar]

Json = Union[JsonPrimitive, JsonDict, JsonArray]
2 changes: 1 addition & 1 deletion tests/core/executor/test_MachineTargetExecutor.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Copyright 2021, Yahoo
# Licensed under the terms of the Apache 2.0 license. See the LICENSE file in the project root for terms
import json
import yaml
from pathlib import Path
from unittest import TestCase

import yaml
from mockito import ANY, mock, unstub, when

from ychaos.core.exceptions.executor_errors import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Copyright 2021, Yahoo
# Licensed under the terms of the Apache 2.0 license. See the LICENSE file in the project root for terms
from tempfile import NamedTemporaryFile
from unittest import TestCase

from mockito import mock, unstub, when
Expand All @@ -15,11 +16,13 @@
from ychaos.testplan.verification.plugins.metrics import ComparisonCondition


class TestHTTPVerificationPlugin(TestCase):
class TestOpenTSDBVerificationPlugin(TestCase):
def setUp(self) -> None:
file = NamedTemporaryFile()
self.verification_config = OpenTSDBVerification(
url="https://tsdb.ychaos.yahoo.com",
method="POST",
cert=(file.name, file.name),
query=dict(), # For testing only
criteria=[
MultipleConditionalsMetricsVerificationCriteria(
Expand Down Expand Up @@ -68,7 +71,7 @@ def setUp(self) -> None:
def test_plugin_init(self):
verification_plugin = OpenTSDBVerificationPlugin(self.verification_config)
self.assertDictEqual(verification_plugin._session.headers, dict())
self.assertIsNone(verification_plugin._session.cert)
self.assertIsNotNone(verification_plugin._session.cert)
self.assertDictEqual(verification_plugin._session.params, dict())

def test_plugin_run_verification_for_success_response_from_tsdb(self):
Expand All @@ -81,6 +84,7 @@ def test_plugin_run_verification_for_success_response_from_tsdb(self):
"POST",
url="https://tsdb.ychaos.yahoo.com",
timeout=self.verification_config.timeout / 1000,
json=dict(),
).thenReturn(mock_response)

state_data = verification_plugin.run_verification()
Expand All @@ -99,6 +103,7 @@ def test_plugin_run_verification_for_success_response_from_tsdb_fails_validation
"POST",
url="https://tsdb.ychaos.yahoo.com",
timeout=self.verification_config.timeout / 1000,
json=dict(),
).thenReturn(mock_response)

state_data = verification_plugin.run_verification()
Expand All @@ -115,6 +120,7 @@ def test_plugin_run_verification_for_failure_response(self):
"POST",
url="https://tsdb.ychaos.yahoo.com",
timeout=self.verification_config.timeout / 1000,
json=dict(),
).thenReturn(mock_response)

state_data = verification_plugin.run_verification()
Expand All @@ -134,6 +140,7 @@ def test_plugin_run_verification_for_success_response_for_no_criteria(
"POST",
url="https://tsdb.ychaos.yahoo.com",
timeout=self.verification_config.timeout / 1000,
json=dict(),
).thenReturn(mock_response)

state_data = verification_plugin.run_verification()
Expand Down

0 comments on commit d3c61f7

Please sign in to comment.