2018-10-31 15:35:29 +01:00
#!/usr/bin/env python3
import argparse
2019-05-29 01:30:57 +02:00
import configparser
import os
from pprint import pprint
2018-10-31 15:35:29 +01:00
2019-05-29 01:30:57 +02:00
import redis
2018-10-31 15:35:29 +01:00
RED = " \033 [91m "
GREEN = " \033 [92m "
DEFAULT = " \033 [0m "
def clean ( brutal = False ) :
configfile = os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , ' config/config.cfg ' )
cfg = configparser . ConfigParser ( )
cfg . read ( configfile )
host = cfg . get ( " RedisGlobal " , " host " )
port = cfg . getint ( " RedisGlobal " , " port " )
servers = [ ]
for db in range ( 0 , 4 ) :
servers . append ( redis . Redis ( host , port , db = db , decode_responses = True ) )
if brutal :
print ( RED + ' Brutal mode ' + DEFAULT + ' selected ' )
2018-10-31 15:45:19 +01:00
print ( ' [ %s : %s ] Cleaning data... ' % ( host , port ) )
2018-10-31 15:35:29 +01:00
cleanBrutal ( servers [ 0 ] )
else :
print ( GREEN + ' Soft mode ' + DEFAULT + ' selected ' )
2018-10-31 15:45:19 +01:00
print ( ' [ %s : %s ] Cleaning data... ' % ( host , port ) )
2018-10-31 15:35:29 +01:00
cleanSoft ( servers )
# Perform a FLUSHALL
def cleanBrutal ( server ) :
server . flushall ( )
# Delete all keys independently
def cleanSoft ( servers ) :
prefix_keys_per_db = {
0 : [ ] , # publish only
1 : [ ] , # publish only (maps)
3 : [ ' bufferList ' ] , # queuing
2 : [
' GEO_COORD:* ' ,
' GEO_COUNTRY:* ' ,
' GEO_RAD:* ' ,
' CONTRIB_DAY:* ' ,
' CONTRIB_CATEG:* ' ,
' CONTRIB_LAST:* ' ,
' CONTRIB_ALL_ORG ' ,
' CONTRIB_ORG:* ' ,
' CONTRIB_TROPHY:* ' ,
' CONTRIB_LAST_AWARDS:* ' ,
' CONTRIB_ALL_ORG ' ,
' LOGIN_TIMESTAMP:* ' ,
' LOGIN_ORG:* ' ,
' LOGIN_ALL_ORG ' ,
' TRENDINGS_EVENTS:* ' ,
' TRENDINGS_CATEGS:* ' ,
' TRENDINGS_TAGS:* ' ,
' TRENDINGS_DISC:* ' ,
' TRENDINGS_SIGHT_sightings:* ' ,
' TRENDINGS_SIGHT_false_positive:* ' ,
' TEMP_CACHE_LIVE:* ' ,
]
}
for db , keys in prefix_keys_per_db . items ( ) :
serv = servers [ db ]
for k in keys :
2018-10-31 15:56:11 +01:00
# fetch all keys on the db
2018-10-31 15:35:29 +01:00
key_to_del = serv . keys ( k )
2018-10-31 15:56:11 +01:00
# delete all existing keys
2018-10-31 15:35:29 +01:00
if len ( key_to_del ) > 0 :
serv . delete ( * tuple ( key_to_del ) )
if __name__ == ' __main__ ' :
2018-10-31 15:45:19 +01:00
parser = argparse . ArgumentParser ( description = ' Clean data stored in the redis server specified in the configuration file ' )
parser . add_argument ( " -b " , " --brutal " , default = False , action = " store_true " , help = " Perfom a FLUSHALL on the redis database. If not set, will use a soft method to delete only keys used by MISP-Dashboard. " )
2018-10-31 15:35:29 +01:00
args = parser . parse_args ( )
clean ( args . brutal )
print ( GREEN + ' Done. ' + DEFAULT )