Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the endpoint for custom fields addition and add option to allow custom fields with non-ascii characters #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions contrib/ManageConfig/submit_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,19 @@ def insert_case_templates(thehive_url, api_key, case_templates):
raise Exception("{} : {}".format(response.status_code, response.text))


def insert_custom_fields(thehive_url, api_key, custom_fields):
def insert_custom_fields(thehive_url, api_key, custom_fields, utf_json):
for cf in custom_fields:
response = requests.post('{}/api/list/custom_fields'.format(thehive_url),
headers={"Authorization": "Bearer {}".format(api_key),
'Content-Type': 'application/json'},
data=json.dumps({"value": cf}))
response = requests.post(
'{}/api/customField'.format(thehive_url),
headers={
"Authorization": "Bearer {}".format(api_key),
'Content-Type': 'application/json'
},
data=json.dumps(cf, ensure_ascii=not utf_json)
)
if response.status_code == 400:
print(response.text)
elif response.status_code != 200:
elif response.status_code != 201:
raise Exception("{} : {}".format(response.status_code, response.text))


Expand All @@ -58,13 +62,13 @@ def insert_metrics(thehive_url, api_key, metrics):
raise Exception("{} : {}".format(response.status_code, response.text))


def submit_config(thehive_url, api_key, conf_path):
def submit_config(thehive_url, api_key, conf_path, utf_json):
with open(conf_path, 'r') as infile:
config = json.loads(infile.read())
print("Inserting observable datatypes...")
insert_datatypes(thehive_url, api_key, config["datatypes"])
print("Inserting custom fields...")
insert_custom_fields(thehive_url, api_key, config["custom_fields"])
insert_custom_fields(thehive_url, api_key, config["custom_fields"], utf_json)
print("Inserting metrics...")
insert_metrics(thehive_url, api_key, config["metrics"])
print("Inserting case templates...")
Expand All @@ -78,13 +82,19 @@ def parse_args():
help=("TheHive server url."),required=True)
parser.add_argument("-c", "--config_path",
help=("Configuration file path."), required=True)
parser.add_argument(
'--utf-json',
dest='utf_json',
help='Do not force ASCII decoding of the configuration file (only for custom fields)',
action=argparse.BooleanOptionalAction
)
return parser.parse_args()


def main():
args = parse_args()
print("Submitting config to {}".format(args.url))
submit_config(args.url, args.key, args.config_path)
submit_config(args.url, args.key, args.config_path, args.utf_json)


if __name__ == "__main__" :
Expand Down