forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 589
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* make config a typed object to reduce the pain of far-nested dicts and do some validation on-load * cleanup * try to get tests to run without mysql * switch from union type
- Loading branch information
Showing
8 changed files
with
214 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,68 @@ | ||
import sys | ||
from typing import Dict | ||
from typing import Dict, Union | ||
|
||
import yaml | ||
from pydantic import BaseModel, Field | ||
|
||
from empire.server.common import helpers | ||
|
||
|
||
class EmpireConfig(object): | ||
def __init__(self): | ||
self.yaml: Dict = {} | ||
if "--config" in sys.argv: | ||
location = sys.argv[sys.argv.index("--config") + 1] | ||
print(f"Loading config from {location}") | ||
self.set_yaml(location) | ||
if len(self.yaml.items()) == 0: | ||
print(helpers.color("[*] Loading default config")) | ||
self.set_yaml("./empire/server/config.yaml") | ||
|
||
def set_yaml(self, location: str): | ||
try: | ||
with open(location, "r") as stream: | ||
self.yaml = yaml.safe_load(stream) | ||
except yaml.YAMLError as exc: | ||
print(exc) | ||
except FileNotFoundError as exc: | ||
print(exc) | ||
|
||
|
||
empire_config = EmpireConfig() | ||
class DatabaseConfig(BaseModel): | ||
type: str | ||
defaults: Dict[str, Union[bool, int, str]] | ||
|
||
# sqlite | ||
location: str = "empire/server/data/empire.db" | ||
|
||
# mysql | ||
url: str = "localhost:3306" | ||
username: str = "" | ||
password: str = "" | ||
|
||
|
||
class ModulesConfig(BaseModel): | ||
# todo vr In 5.0 we should pick a single naming convention for config. | ||
retain_last_value: bool = Field(alias="retain-last-value") | ||
|
||
|
||
class DirectoriesConfig(BaseModel): | ||
downloads: str | ||
module_source: str | ||
obfuscated_module_source: str | ||
|
||
|
||
class EmpireConfig(BaseModel): | ||
supress_self_cert_warning: bool = Field( | ||
alias="supress-self-cert-warning", default=True | ||
) | ||
database: DatabaseConfig | ||
modules: ModulesConfig | ||
plugins: Dict[str, Dict[str, str]] = {} | ||
directories: DirectoriesConfig | ||
|
||
# For backwards compatibility | ||
@property | ||
def yaml(self): | ||
return self.dict() | ||
|
||
|
||
def set_yaml(location: str): | ||
try: | ||
with open(location, "r") as stream: | ||
return yaml.safe_load(stream) | ||
except yaml.YAMLError as exc: | ||
print(exc) | ||
except FileNotFoundError as exc: | ||
print(exc) | ||
|
||
|
||
config_dict = {} | ||
if "--config" in sys.argv: | ||
location = sys.argv[sys.argv.index("--config") + 1] | ||
print(f"Loading config from {location}") | ||
config_dict = set_yaml(location) | ||
if len(config_dict.items()) == 0: | ||
print(helpers.color("[*] Loading default config")) | ||
config_dict = set_yaml("./empire/server/config.yaml") | ||
|
||
empire_config = EmpireConfig(**config_dict) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.