Ejemplo n.º 1
0
def importKey(task):
	task_tag = "IMPORTING KEY"
	print "\n\n************** %s [START] ******************\n" % task_tag
	print "importing gpg key for %s" % task.doc_id
	task.setStatus(302)
	
	import os
	from conf import DEBUG, ANNEX_DIR
	from vars import ASSET_TAGS
	
	source = None
	
	if hasattr(task, "doc_id"):
		from lib.Worker.Models.ic_source import InformaCamSource
	
		source = InformaCamSource(_id=task.doc_id)
		if source is None:
			print "DOC IS NONE"
			print "\n\n************** %s [ERROR] ******************\n" % task_tag
			task.fail()
			return
	
		try:
			pgp_key = source.getAssetsByTagName(ASSET_TAGS['PGP_KEY'])[0]
		except Exception as e:
			print "NO PGP KEY FOR SOURCE: %s" % e
			pgp_key = os.path.join(ANNEX_DIR, source.base_path, "publicKey")
			print "trying to use %s instead" % pgp_key
			
			if not os.path.exists(pgp_key):
				print "STILL COULD NOT FIND A PGP KEY AT %s" % pgp_key
				print "\n\n************** %s [ERROR] ******************\n" % task_tag
				task.fail()
				return
			
	elif hasattr(task, "pgp_file"):
		pgp_key = task.pgp_file

	if pgp_key is None:
		print "NO PGP KEY HERE."
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		task.fail()
		return
	
	import gnupg, re
	from conf import getConfig
	gpg = gnupg.GPG(homedir=getConfig('gpg_homedir'))
	
	print "NOW IMPORTING PGP KEY"
	with open(pgp_key, 'rb') as PGP_KEY:	
		import_result = gpg.import_keys(PGP_KEY.read())
		if DEBUG: print import_result.results
	
	try:
		fingerprint = import_result.results[0]['fingerprint']
	except Exception as e:
		print "THIS FINGERPRINT IS F*****G NULL WHAT DO YOU THINK THIS IS?"
		print e
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		task.fail()
		return
	
	if source is not None:
		from vars import MIME_TYPES

		source.fingerprint = fingerprint
		source.original_mime_type = source.mime_type
		source.mime_type = MIME_TYPES['pgp']	
		source.save()

		source.addCompletedTask(task.task_path)
	
		'''
		from lib.Worker.Models.uv_task import UnveillanceTask
		next_task = UnveillanceTask(inflate={
			'task_path' : "Source.reverify_media.reverifyMedia",
			'queue' : task.queue,
			'doc_id' : source._id
		})
		next_task.run()
		'''
	
	task.routeNext()
	task.finish()
	print "\n\n************** %s [END] ******************\n" % task_tag
Ejemplo n.º 2
0
def initSource(task):
	task_tag = "INITING SOURCE"
	print "\n\n************** %s [START] ******************\n" % task_tag
	task.setStatus(302)
	
	from lib.Worker.Models.ic_source import InformaCamSource
	from conf import DEBUG
	from vars import ASSET_TAGS
	
	source = InformaCamSource(_id=task.doc_id)
	if source is None:
		print "SOURCE DOCUMENT DOES NOT EXIST"
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		task.fail()
		return
	
	if not hasattr(task, "assets"):
		print "NO ASSETS FOR THIS SOURCE"
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		task.fail()
		return
	
	import re, json, os
	from conf import ANNEX_DIR
	
	next_task = None
	for asset in task.assets:
		description = None
		tags = None
		sync = False
		
		if re.match(r'publicKey', asset):
			# import key
			description = "Source's public pgp key"
			tags = [ASSET_TAGS['PGP_KEY']]
			
			from lib.Worker.Models.uv_task import UnveillanceTask
		
			next_task = UnveillanceTask(inflate={
				'doc_id' : source._id,
				'task_path' : "PGP.import_key.importKey",
				'queue' : task.queue
			})
			sync = True
		elif re.match(r'credentials', asset):
			# parse creds
			with open(os.path.join(ANNEX_DIR, source.base_path, asset), 'rb') as C:
				try:
					credentials = json.loads(C.read())
					if DEBUG: print credentials
					for field in ['email','alias']:
						if field in credentials.keys() and credentials[field] != "":
							setattr(source, field, credentials[field])
					
					source.save()
				except Exception as e:
					if DEBUG: print e
					pass
			
					
		asset_path = source.addAsset(None, asset, description=description, tags=tags)
		print "ASSET PATH: %s" % asset_path
		
		if asset_path is None: continue
		if sync:
			print "ADDING %s AS FILE AS WELL:" % asset_path
			source.addFile(asset_path, None)
			
	if next_task is None:
		print "NO PUBLIC KEY FOR SOURCE."
		print "\n\n************** %s [ERROR] ******************\n" % task_tag
		task.fail()
		return
	
	source.addCompletedTask(task.task_path)
	from time import sleep
	sleep(10)
	
	next_task.run()
	task.finish()
	print "\n\n************** %s [END] ******************\n" % task_tag