Skip to content

Commit 22dfd61

Browse files
authored
Merge pull request #48 from mitre/emu-overwrite
Avoid emu data overwrite
2 parents 9c961a3 + c6edc5f commit 22dfd61

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

app/emu_svc.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ def _copy_planner(self, source_path, target_filename):
159159
if not os.path.exists(planner_dir):
160160
os.makedirs(planner_dir)
161161
target_path = os.path.join(planner_dir, target_filename)
162-
shutil.copyfile(source_path, target_path)
163-
self.log.debug('Copied planner to %s', target_path)
162+
if os.path.exists(target_path):
163+
self.log.debug('Planner %s already exists. Skipping.', target_path)
164+
else:
165+
shutil.copyfile(source_path, target_path)
166+
self.log.debug('Copied planner to %s', target_path)
164167

165168
@staticmethod
166169
def _is_planner(data):
@@ -226,8 +229,11 @@ async def _write_adversary(self, data):
226229
os.makedirs(d)
227230

228231
file_path = os.path.join(d, '%s.yml' % data['id'])
229-
with open(file_path, 'w') as f:
230-
f.write(yaml.dump(data))
232+
if os.path.exists(file_path):
233+
self.log.debug('Adversary profile %s already exists. Skipping.', file_path)
234+
else:
235+
with open(file_path, 'w') as f:
236+
f.write(yaml.dump(data))
231237

232238
async def _save_adversary(self, id, name, description, abilities):
233239
adversary = dict(
@@ -249,8 +255,11 @@ async def _write_ability(self, data):
249255
if not os.path.exists(d):
250256
os.makedirs(d)
251257
file_path = os.path.join(d, '%s.yml' % data['id'])
252-
with open(file_path, 'w') as f:
253-
f.write(yaml.dump([data]))
258+
if os.path.exists(file_path):
259+
self.log.debug('Ability file %s already exists. Skipping.', file_path)
260+
else:
261+
with open(file_path, 'w') as f:
262+
f.write(yaml.dump([data]))
254263

255264
@staticmethod
256265
def get_privilege(executors):
@@ -344,5 +353,8 @@ async def _write_source(self, data):
344353
os.makedirs(d)
345354

346355
file_path = os.path.join(d, '%s.yml' % data['id'])
347-
with open(file_path, 'w') as f:
348-
f.write(yaml.dump(data))
356+
if os.path.exists(file_path):
357+
self.log.debug('Fact source file %s already exists. Skipping.', file_path)
358+
else:
359+
with open(file_path, 'w') as f:
360+
f.write(yaml.dump(data))

hook.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import os
2-
import shutil
32

43
from app.utility.base_world import BaseWorld
54
from plugins.emu.app.emu_svc import EmuService
@@ -22,10 +21,5 @@ async def enable(services):
2221
if not os.path.isdir(plugin_svc.repo_dir):
2322
await plugin_svc.clone_repo()
2423

25-
for directory in ['abilities', 'adversaries', 'sources', 'planners']:
26-
full_path = os.path.join(data_dir, directory)
27-
if os.path.isdir(full_path):
28-
shutil.rmtree(full_path)
29-
3024
await plugin_svc.decrypt_payloads()
3125
await plugin_svc.populate_data_directory()

0 commit comments

Comments
 (0)