Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

Commit

Permalink
Merge branch 'heliocorreia-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateus Pontes committed Feb 14, 2020
2 parents 70d47c6 + 262419f commit d074d4d
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 5 deletions.
28 changes: 28 additions & 0 deletions fastlane/worker/docker/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

# 3rd Party
from flask import Blueprint, current_app, g, make_response, request
from validators import domain as validate_domain

bp = Blueprint( # pylint: disable=invalid-name
"docker", __name__, url_prefix="/docker-executor"
Expand Down Expand Up @@ -31,6 +32,15 @@ def add_to_blacklist():

host = data["host"]

if not validate_hostname(host):
msg = (
"Failed to add host to blacklist because 'host'"
" attribute had an invalid value."
)
g.logger.warn(msg)

return make_response(msg, 400)

redis.sadd(BLACKLIST_KEY, host)

return ""
Expand Down Expand Up @@ -59,6 +69,15 @@ def remove_from_blacklist():

host = data["host"]

if not validate_hostname(host):
msg = (
"Failed to remove host to blacklist because 'host'"
" attribute had an invalid value."
)
g.logger.warn(msg)

return make_response(msg, 400)

redis.srem(BLACKLIST_KEY, host)

return ""
Expand All @@ -71,3 +90,12 @@ def get_details():
details = loads(request.get_data())

return details


def validate_hostname(value):
values = value.split(':')

if len(values) != 2:
return False

return validate_domain(values[0]) and values[1].isdigit()
19 changes: 16 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Redis-Sentinel-Url = "^1.0"
croniter = "^0.3.29"
Flask-BasicAuth = "^0.2.0"
gevent-websocket = {git = "https://github.com/lrezek/gevent-websocket.git", "rev" = "902d82b4de6e369e7ad2626d8853f7e1c7bc3e7c"}
validators = "^0.14.0"

[tool.poetry.dev-dependencies]
pytest = "^3.0"
Expand Down
11 changes: 9 additions & 2 deletions tests/unit/api/test_docker_blacklist.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Standard Library
from json import dumps
from random import randrange
from uuid import uuid4

# 3rd Party
Expand All @@ -13,7 +14,10 @@ def test_docker_blacklist1(client):
"""Test blacklisting a docker server"""

def ensure_blacklist(method):
docker_host = str(uuid4())
docker_host = "{domain}:{port}".format(
domain=str(uuid4()).replace("-", "."),
port=int(randrange(10, 99999)) # nosec
)

data = {"host": docker_host}
response = getattr(client, method)(
Expand Down Expand Up @@ -62,7 +66,10 @@ def ensure_blacklist(method):

def test_docker_blacklist3(client):
"""Test removing from blacklist a docker server"""
docker_host = str(uuid4())
docker_host = "{domain}:{port}".format(
domain=str(uuid4()).replace("-", "."),
port=int(randrange(10, 99999)) # nosec
)

data = {"host": docker_host}
response = client.post(
Expand Down
120 changes: 120 additions & 0 deletions tests/unit/worker/test_docker_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Standard Library
from json import dumps

# 3rd Party
from preggy import expect

# Fastlane
from fastlane.worker.docker.api import validate_hostname


def test_validate_hostname1():
expect(validate_hostname("example.com")).to_be_false()


def test_validate_hostname2():
expect(validate_hostname("example.com/")).to_be_false()


def test_validate_hostname3():
expect(validate_hostname("example.com:abcd")).to_be_false()


def test_validate_hostname4():
expect(validate_hostname("example.com:1234")).to_be_true()


def test_add_to_blacklist1(client):
"""Test adding to blacklist without payload returns 400"""
with client.application.app_context():
resp = client.post(
f"/docker-executor/blacklist"
)
expect(resp.status_code).to_equal(400)


def test_add_to_blacklist2(client):
"""Test adding to blacklist without host on payload returns 400"""

with client.application.app_context():
resp = client.post(
f"/docker-executor/blacklist",
data=dumps({
"myparam": "abc"
})
)
expect(resp.status_code).to_equal(400)


def test_add_to_blacklist3(client):
"""Test adding to blacklist with valid host on payload returns 400"""

with client.application.app_context():
resp = client.post(
f"/docker-executor/blacklist",
data=dumps({
"host": "example.com"
})
)
expect(resp.status_code).to_equal(400)


def test_add_to_blacklist4(client):
"""Test adding to blacklist with valid host on payload returns 200"""

with client.application.app_context():
resp = client.post(
f"/docker-executor/blacklist",
data=dumps({
"host": "example.com:1234"
})
)
expect(resp.status_code).to_equal(200)


def test_remove_from_blacklist1(client):
"""Test adding to blacklist without payload returns 400"""
with client.application.app_context():
resp = client.delete(
f"/docker-executor/blacklist"
)
expect(resp.status_code).to_equal(400)


def test_remove_from_blacklist2(client):
"""Test adding to blacklist without host on payload returns 400"""

with client.application.app_context():
resp = client.delete(
f"/docker-executor/blacklist",
data=dumps({
"myparam": "abc"
})
)
expect(resp.status_code).to_equal(400)


def test_remove_from_blacklist3(client):
"""Test adding to blacklist with valid host on payload returns 400"""

with client.application.app_context():
resp = client.delete(
f"/docker-executor/blacklist",
data=dumps({
"host": "example.com"
})
)
expect(resp.status_code).to_equal(400)


def test_remove_from_blacklist4(client):
"""Test adding to blacklist with valid host on payload returns 200"""

with client.application.app_context():
resp = client.delete(
f"/docker-executor/blacklist",
data=dumps({
"host": "example.com:1234"
})
)
expect(resp.status_code).to_equal(200)

0 comments on commit d074d4d

Please sign in to comment.