Skip to content

Commit 3db39b3

Browse files
author
Jonathan Visser
committed
add bin/add_whitelist_rule
1 parent 26e5387 commit 3db39b3

5 files changed

Lines changed: 161 additions & 0 deletions

File tree

bin/add_whitelist_rule

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
command

hypernode_api_python/client.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,36 @@ def get_whitelist_rules(self, app_name, filter_data=None):
594594
"GET", HYPERNODE_API_WHITELIST_ENDPOINT.format(app_name), filter_data
595595
)
596596

597+
def add_whitelist_rule(self, app_name, data):
598+
"""
599+
Add a whitelist rule for the specified app. See get_whitelist_options
600+
for the available options.
601+
Example:
602+
> client.add_whitelist_rule(
603+
> 'yourhypernodeappname',
604+
> data={'ip': '1.2.3.4', 'type': 'database', 'description': 'my description'}
605+
> ).json()
606+
> {
607+
> 'id': 1234,
608+
> 'created': '2024-05-25T13:39:48Z',
609+
> 'domainname': 'yourhypernodeappname.hypernode.io',
610+
> 'ip': '1.2.3.4',
611+
> 'type': 'database',
612+
> 'description': 'my description'
613+
> }
614+
615+
:param str app_name: The name of the Hypernode to add the whitelist
616+
rule for
617+
:param dict data: Data describing the whitelist rule to add. An
618+
example could be: {'ip': '1.2.3.4', 'type': 'database'}. The type can
619+
be one of 'waf', 'database' or 'ftp', and optionally a 'description'
620+
can be specified.
621+
:return obj response: The request response object
622+
"""
623+
return self.requests(
624+
"POST", HYPERNODE_API_WHITELIST_ENDPOINT.format(app_name), data=data
625+
)
626+
597627
def get_current_product_for_app(self, app_name):
598628
"""
599629
Retrieve information about the product the specified App is currently on.

hypernode_api_python/commands.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,46 @@ def get_whitelist_rules(args=None):
501501
print_response(client.get_whitelist_rules(app_name))
502502

503503

504+
def add_whitelist_rule(args=None):
505+
parser = ArgumentParser(
506+
description="""
507+
Add a WAF whitelist rule for the Hypernode.
508+
509+
Example:
510+
$ ./bin/add_whitelist_rule 1.2.3.4 --type database --description "my description"
511+
{
512+
"id": 1234,
513+
"created": "2024-05-25T13:39:48Z",
514+
"domainname": "yourhypernodeappname.hypernode.io",
515+
"ip": "1.2.3.4",
516+
"type": "database",
517+
"description": "my description"
518+
}
519+
""",
520+
formatter_class=RawTextHelpFormatter,
521+
)
522+
parser.add_argument(
523+
"ip",
524+
help="The IP address to whitelist",
525+
)
526+
parser.add_argument(
527+
"--type",
528+
help="The type of whitelist rule to add",
529+
choices=["waf", "database", "ftp"],
530+
default="database",
531+
)
532+
parser.add_argument(
533+
"--description",
534+
help="An optional description for the whitelist rule",
535+
default="",
536+
)
537+
args = parser.parse_args(args=args)
538+
client = get_client()
539+
app_name = get_app_name()
540+
data = {"ip": args.ip, "type": args.type, "description": args.description}
541+
print_response(client.add_whitelist_rule(app_name, data=data))
542+
543+
504544
def get_current_product_for_app(args=None):
505545
parser = ArgumentParser(
506546
description="""
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from unittest import TestCase
2+
from unittest.mock import Mock
3+
4+
from hypernode_api_python.client import (
5+
HYPERNODE_API_WHITELIST_ENDPOINT,
6+
HypernodeAPIPython,
7+
)
8+
9+
10+
class TestAddWhitelistRule(TestCase):
11+
def setUp(self):
12+
self.client = HypernodeAPIPython(token="my_token")
13+
self.mock_request = Mock()
14+
self.client.requests = self.mock_request
15+
self.app_name = "my_app"
16+
17+
def test_whitelist_endpoint_is_correct(self):
18+
self.assertEqual("/v2/whitelist/{}/", HYPERNODE_API_WHITELIST_ENDPOINT)
19+
20+
def test_calls_add_whitelist_rule_endpoint_properly(self):
21+
data = {"ip": "1.2.3.4", "type": "database", "description": "my description"}
22+
self.client.add_whitelist_rule(self.app_name, data)
23+
24+
self.mock_request.assert_called_once_with(
25+
"POST", f"/v2/whitelist/{self.app_name}/", data=data
26+
)
27+
28+
def test_returns_add_whitelist_rule_response(self):
29+
response = Mock()
30+
response.status_code = 200
31+
self.mock_request.return_value = response
32+
33+
self.assertEqual(
34+
self.client.add_whitelist_rule(self.app_name, {"ip": "1.2.3.4"}),
35+
self.mock_request.return_value,
36+
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
from hypernode_api_python.commands import add_whitelist_rule
2+
from tests.testcase import TestCase
3+
4+
5+
class TestAddWhitelistRule(TestCase):
6+
def setUp(self):
7+
self.print_response = self.set_up_patch(
8+
"hypernode_api_python.commands.print_response"
9+
)
10+
self.get_client = self.set_up_patch("hypernode_api_python.commands.get_client")
11+
self.client = self.get_client.return_value
12+
self.get_app_name = self.set_up_patch(
13+
"hypernode_api_python.commands.get_app_name"
14+
)
15+
self.get_app_name.return_value = "myappname"
16+
17+
def test_add_whitelist_rule_gets_client(self):
18+
add_whitelist_rule(["1.2.3.4"])
19+
20+
self.get_client.assert_called_once_with()
21+
22+
def test_add_whitelist_rule_gets_app_name(self):
23+
add_whitelist_rule(["1.2.3.4"])
24+
25+
self.get_app_name.assert_called_once_with()
26+
27+
def test_add_whitelist_rule_adds_whitelist_rule_with_default_type(self):
28+
add_whitelist_rule(["1.2.3.4"])
29+
30+
expected_data = {"ip": "1.2.3.4", "type": "database", "description": ""}
31+
self.client.add_whitelist_rule.assert_called_once_with(
32+
"myappname", data=expected_data
33+
)
34+
35+
def test_add_whitelist_rule_adds_whitelist_rule_with_specified_arguments(self):
36+
add_whitelist_rule(
37+
["1.2.3.4", "--type", "waf", "--description", "my description"]
38+
)
39+
40+
expected_data = {
41+
"ip": "1.2.3.4",
42+
"type": "waf",
43+
"description": "my description",
44+
}
45+
self.client.add_whitelist_rule.assert_called_once_with(
46+
"myappname", data=expected_data
47+
)
48+
49+
def test_add_whitelist_rule_prints_response(self):
50+
add_whitelist_rule(["1.2.3.4"])
51+
52+
self.print_response.assert_called_once_with(
53+
self.client.add_whitelist_rule.return_value
54+
)

0 commit comments

Comments
 (0)