Island: Catch appropriate `marshmallow` error when loading config which could be invalid, in new configuration resource

This commit is contained in:
Shreya Malviya 2022-06-21 14:58:08 -07:00
parent 15615e08c4
commit 00e38391b4
1 changed files with 3 additions and 7 deletions

View File

@ -3,19 +3,17 @@ from dataclasses import dataclass
from itertools import chain from itertools import chain
from typing import Mapping from typing import Mapping
import marshmallow
from flask import jsonify, request from flask import jsonify, request
from common.configuration.agent_configuration import AgentConfigurationSchema from common.configuration.agent_configuration import AgentConfigurationSchema
from common.utils.exceptions import InvalidConfigurationError
from monkey_island.cc.repository import IAgentConfigurationRepository from monkey_island.cc.repository import IAgentConfigurationRepository
from monkey_island.cc.resources.AbstractResource import AbstractResource from monkey_island.cc.resources.AbstractResource import AbstractResource
from monkey_island.cc.resources.request_authentication import jwt_required from monkey_island.cc.resources.request_authentication import jwt_required
class ImportStatuses: class ImportStatuses:
UNSAFE_OPTION_VERIFICATION_REQUIRED = "unsafe_options_verification_required"
INVALID_CONFIGURATION = "invalid_configuration" INVALID_CONFIGURATION = "invalid_configuration"
INVALID_CREDENTIALS = "invalid_credentials"
IMPORTED = "imported" IMPORTED = "imported"
@ -51,11 +49,9 @@ class AgentConfiguration(AbstractResource):
try: try:
schema = AgentConfigurationSchema() schema = AgentConfigurationSchema()
configuration_object = schema.loads(configuration_json) configuration_object = schema.loads(configuration_json)
self._agent_configuration_repository.store_configuration( self._agent_configuration_repository.store_configuration(configuration_object)
configuration_object
) # check error handling
return ResponseContents().form_response() return ResponseContents().form_response()
except InvalidConfigurationError: # don't need this probably either; if invalid, schema should raise error (catch marshmallow exception and return 400) except marshmallow.exceptions.ValidationError:
return ResponseContents( return ResponseContents(
import_status=ImportStatuses.INVALID_CONFIGURATION, import_status=ImportStatuses.INVALID_CONFIGURATION,
message="Invalid configuration supplied. " message="Invalid configuration supplied. "