Fix or noqa some python linter errors

Also, replace os.path with Path
This commit is contained in:
Shay Nehmad 2020-08-31 14:13:56 +03:00
parent d9dbb6fcfa
commit 910e8355f9
19 changed files with 40 additions and 47 deletions

View File

@ -91,16 +91,14 @@ class MonkeyIslandRequests(object):
return requests.patch(self.addr + url, # noqa: DUO123
data=data,
headers=self.get_jwt_header(),
verify=False
)
verify=False)
@_Decorators.refresh_jwt_token
def delete(self, url):
return requests.delete( # noqa: DOU123
self.addr + url,
headers=self.get_jwt_header(),
verify=False
)
verify=False)
@_Decorators.refresh_jwt_token
def get_jwt_header(self):

View File

@ -28,7 +28,7 @@ class NetworkRange(object, metaclass=ABCMeta):
"""
base_range = self.get_range()
if self._shuffle:
random.shuffle(base_range)
random.shuffle(base_range) # noqa: DUO102
for x in base_range:
yield self._number_to_ip(x)

View File

@ -89,7 +89,7 @@ class InfectionMonkey(object):
if self._opts.depth is not None:
WormConfiguration._depth_from_commandline = True
WormConfiguration.depth = self._opts.depth
LOG.debug(f"Setting propagation depth from command line")
LOG.debug("Setting propagation depth from command line")
LOG.debug(f"Set propagation depth to {WormConfiguration.depth}")
self._keep_running = True

View File

@ -1,8 +1,9 @@
import base64
import os
from Crypto import Random
from Crypto.Cipher import AES
# PyCrypto is deprecated, but we use pycryptodome, which uses the exact same imports but it maintained
from Crypto import Random # noqa: DOU133
from Crypto.Cipher import AES # noqa: DOU133
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH

View File

@ -1,6 +1,5 @@
from common.cloud.aws.aws_instance import AwsInstance
from monkey_island.cc.environment import Environment
from monkey_island.cc.resources.auth.auth_user import User
__author__ = 'itay.mizeretz'

View File

@ -1,7 +1,5 @@
import logging
env = None
import monkey_island.cc.resources.auth.user_store as user_store
from monkey_island.cc.environment import (EnvironmentConfig, aws, password,
standard, testing)
@ -22,6 +20,8 @@ ENV_DICT = {
TESTING: testing.TestingEnvironment
}
env = None
def set_env(env_type: str, env_config: EnvironmentConfig):
global env

View File

@ -112,4 +112,3 @@ class TestEnvironment(TestCase):
self.assertTrue(method())
else:
self.assertFalse(method())

View File

@ -57,7 +57,7 @@ class TestEnvironmentConfig(TestCase):
def test_get_server_config_file_path(self):
if platform.system() == "Windows":
server_file_path = MONKEY_ISLAND_ABS_PATH + "\cc\server_config.json"
server_file_path = MONKEY_ISLAND_ABS_PATH + r"\cc\server_config.json"
else:
server_file_path = MONKEY_ISLAND_ABS_PATH + "/cc/server_config.json"
self.assertEqual(EnvironmentConfig.get_config_file_path(), server_file_path)

View File

@ -1,35 +1,35 @@
import logging
import os
import os.path
import sys
import time
from pathlib import Path
from threading import Thread
MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0"
# Add the monkey_island directory to the path, to make sure imports that don't start with "monkey_island." work.
MONKEY_ISLAND_DIR_BASE_PATH = str(Path(__file__).parent.parent)
if str(MONKEY_ISLAND_DIR_BASE_PATH) not in sys.path:
sys.path.insert(0, MONKEY_ISLAND_DIR_BASE_PATH)
BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if BASE_PATH not in sys.path:
sys.path.insert(0, BASE_PATH)
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH
from monkey_island.cc.island_logger import json_setup_logging
from monkey_island.cc.consts import MONKEY_ISLAND_ABS_PATH # noqa: E402
from monkey_island.cc.island_logger import json_setup_logging # noqa: E402
# This is here in order to catch EVERYTHING, some functions are being called on imports the log init needs to be on top.
json_setup_logging(default_path=os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'island_logger_default_config.json'),
json_setup_logging(default_path=Path(MONKEY_ISLAND_ABS_PATH, 'cc', 'island_logger_default_config.json'),
default_level=logging.DEBUG)
logger = logging.getLogger(__name__)
import monkey_island.cc.environment.environment_singleton as env_singleton
from common.version import get_version
from monkey_island.cc.app import init_app
from monkey_island.cc.bootloader_server import BootloaderHttpServer
from monkey_island.cc.database import get_db_version, is_db_server_up
from monkey_island.cc.network_utils import local_ip_addresses
from monkey_island.cc.resources.monkey_download import MonkeyDownload
from monkey_island.cc.services.reporting.exporter_init import \
populate_exporter_list
from monkey_island.cc.setup import setup
import monkey_island.cc.environment.environment_singleton as env_singleton # noqa: E402
from common.version import get_version # noqa: E402
from monkey_island.cc.app import init_app # noqa: E402
from monkey_island.cc.bootloader_server import BootloaderHttpServer # noqa: E402
from monkey_island.cc.database import get_db_version, is_db_server_up # noqa: E402
from monkey_island.cc.network_utils import local_ip_addresses # noqa: E402
from monkey_island.cc.resources.monkey_download import MonkeyDownload # noqa: E402
from monkey_island.cc.services.reporting.exporter_init import populate_exporter_list # noqa: E402
from monkey_island.cc.setup import setup # noqa: E402
MINIMUM_MONGO_DB_VERSION_REQUIRED = "4.2.0"
def main(should_setup_only=False):
@ -54,8 +54,8 @@ def start_island_server(should_setup_only):
populate_exporter_list()
app = init_app(mongo_url)
crt_path = os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'server.crt')
key_path = os.path.join(MONKEY_ISLAND_ABS_PATH, 'cc', 'server.key')
crt_path = str(Path(MONKEY_ISLAND_ABS_PATH, 'cc', 'server.crt'))
key_path = str(Path(MONKEY_ISLAND_ABS_PATH, 'cc', 'server.key'))
setup()

View File

@ -143,7 +143,7 @@ class Monkey(Document):
try:
_ = Monkey.get_single_monkey_by_id(object_id)
return True
except:
except: # noqa: E722
return False
@staticmethod

View File

@ -77,7 +77,7 @@ class TestMonkey(IslandTestCase):
self.assertIsNotNone(Monkey.get_single_monkey_by_id(a_monkey.id))
# Raise on non-existent monkey
with pytest.raises(MonkeyNotFoundError) as e_info:
with pytest.raises(MonkeyNotFoundError) as _:
_ = Monkey.get_single_monkey_by_id("abcdefabcdefabcdefabcdef")
def test_get_os(self):

View File

@ -15,5 +15,5 @@ class IslandLog(flask_restful.Resource):
def get(self):
try:
return IslandLogService.get_log_file()
except Exception as e:
except Exception:
logger.error('Monkey Island logs failed to download', exc_info=True)

View File

@ -217,7 +217,8 @@ class ConfigService:
@staticmethod
def set_server_ips_in_config(config):
ips = local_ip_addresses()
config["internal"]["island_server"]["command_servers"] = ["%s:%d" % (ip, env_singleton.env.get_island_port()) for ip in ips]
config["internal"]["island_server"]["command_servers"] = \
["%s:%d" % (ip, env_singleton.env.get_islaned_port()) for ip in ips]
config["internal"]["island_server"]["current_server"] = "%s:%d" % (ips[0], env_singleton.env.get_island_port())
@staticmethod

View File

@ -1,6 +1,5 @@
from bson import ObjectId
from monkey_island.cc.models.edge import Edge
from monkey_island.cc.services.edge.displayed_edge import DisplayedEdgeService
from monkey_island.cc.services.edge.edge import RIGHT_ARROW, EdgeService
from monkey_island.cc.testing.IslandTestCase import IslandTestCase

View File

@ -1,7 +1,6 @@
from bson import ObjectId
from monkey_island.cc.models import Monkey
from monkey_island.cc.models.edge import Edge
from monkey_island.cc.services.edge.displayed_edge import DisplayedEdgeService
from monkey_island.cc.services.edge.edge import EdgeService
from monkey_island.cc.services.node import NodeService

View File

@ -3,13 +3,11 @@ from datetime import datetime, timedelta
from typing import Dict
from bson import ObjectId
from mongoengine import DoesNotExist
import monkey_island.cc.services.log
from monkey_island.cc import models
from monkey_island.cc.database import mongo
from monkey_island.cc.models import Monkey
from monkey_island.cc.models.edge import Edge
from monkey_island.cc.network_utils import is_local_ips, local_ip_addresses
from monkey_island.cc.services.edge.displayed_edge import DisplayedEdgeService
from monkey_island.cc.services.edge.edge import EdgeService

View File

@ -299,7 +299,7 @@ class AWSExporter(Exporter):
title="Machines are accessible using passwords supplied by the user during the Monkey's configuration.",
description="Change {0}'s password to a complex one-use password that is not shared with other computers on the "
"network.",
recommendation="The machine machine ({ip_address}) is vulnerable to a WMI attack. The Monkey authenticated over "
recommendation="The machine {machine} ({ip_address}) is vulnerable to a WMI attack. The Monkey authenticated over "
"the WMI protocol with user {username} and its password.".format(
machine=issue['machine'],
ip_address=issue['ip_address'],
@ -316,7 +316,7 @@ class AWSExporter(Exporter):
title="Machines are accessible using passwords supplied by the user during the Monkey's configuration.",
description="Change {0}'s password to a complex one-use password that is not shared with other computers on the "
"network.".format(issue['username']),
recommendation="The machine machine ({ip_address}) is vulnerable to a WMI attack. The Monkey used a "
recommendation="The machine {machine} ({ip_address}) is vulnerable to a WMI attack. The Monkey used a "
"pass-the-hash attack over WMI protocol with user {username}".format(
machine=issue['machine'],
ip_address=issue['ip_address'],

View File

@ -4,7 +4,6 @@ import dateutil
from monkey_island.cc.encryptor import encryptor
from monkey_island.cc.models import Monkey
from monkey_island.cc.models.edge import Edge
from monkey_island.cc.services.edge.displayed_edge import EdgeService
from monkey_island.cc.services.node import NodeService
from monkey_island.cc.services.telemetry.processing.utils import \

View File

@ -7,7 +7,7 @@ for more details.
import argparse
from Crypto.Hash import SHA3_512
from Crypto.Hash import SHA3_512 # noqa: DUO133
def main():