Browse Source

Implementing delete_password, get_url

master
Bernd Zeimetz 7 years ago
parent
commit
9196129ad0
3 changed files with 39 additions and 9 deletions
  1. 19
    5
      monkeystore/pwstore.py
  2. 7
    3
      monkeystore/web.py
  3. 13
    1
      testclient.py

+ 19
- 5
monkeystore/pwstore.py View File

@@ -226,6 +226,8 @@ class GPGpwstore(object):

def __get_url__(self, category, hostname, service, username):
pwdir = self.__get_directory__(category, hostname, service, username)
if not os.path.isdir(pwdir):
raise GPGpwstoreException("No password found!")
for url_file in glob.glob(pwdir + os.path.sep + '*.url'):
url = 'monkeystore://' + os.path.basename(url_file).replace('.url','')
return url
@@ -268,7 +270,7 @@ class GPGpwstore(object):
return ret


def del_password(self, pwstore_user, category, hostname, service,
def delete_password(self, pwstore_user, category, hostname, service,
username):
self.__check_access__(pwstore_user, category)
directory = self.__get_directory__(category, hostname, service, username)
@@ -276,7 +278,16 @@ class GPGpwstore(object):
raise GPGpwstoreException("No password stored for %s/%s/%s/%s - can't delete." %(
category, hostname, service,username))

print directory
shutil.rmtree(directory)
for p in ('..', '..'):
directory = os.path.realpath(os.path.join(directory,p))
# if directory is not empty, don't delete it
# (and don't try to delete the parent...)
if os.stat(directory).st_nlink > 2:
break
else:
shutil.rmtree(directory)
self.commit_messages.append('DELETE')
self.commit_messages.append('USER %s' %(pwstore_user,))
self.commit_messages.append('')
@@ -332,7 +343,9 @@ class GPGpwstore(object):

def get_url(self, pwstore_user, category, hostname, service, username):
self.__check_access__(pwstore_user, category)
return self.__get_url__(category, hostname, service, username)
url = self.__get_url__(category, hostname, service, username)
return self.crypt_data(pwstore_user, url)


def get_password_by_url(self, pwstore_user, url):
data = self.__get_data_by_url__(url)
@@ -352,15 +365,16 @@ class GPGpwstore(object):
self.repository.git.add(self.repository.untracked_files)
commit_details=""
if self.commit_messages:
commit_details='\n * '.join([''] + self.commit_messages)
self.commit_messages = [ '* %s' %(x,) if x else x for x in self.commit_messages ]
commit_details='\n'.join([''] + self.commit_messages)
self.repository.git.commit(m="monkeystore commit\n%s" %(commit_details,), a=True)
self.commit_messages = []


def crypt_token(self,pwstore_user, token):
def crypt_data(self,pwstore_user, data):
if not pwstore_user in self.users:
raise GPGpwstoreException("Unknown user: %s" %(pwstore_user,))
return self.__encrypt_data__(token, self.gpg_keys[pwstore_user])
return self.__encrypt_data__(data, self.gpg_keys[pwstore_user])

def __decrypt_with_token__(self,gpg_data, token):
return self.__decrypt_data__(gpg_data, passphrase=token)

+ 7
- 3
monkeystore/web.py View File

@@ -157,7 +157,7 @@ def generate_password(length=8):
def retrieve_token(pwstore_user):
token = pwgen(200, num_pw=1, numerals=True, capitalize=True, symbols=True)
try:
crypted_token = gpg_pwstore.crypt_token(pwstore_user, token)
crypted_token = gpg_pwstore.crypt_data(pwstore_user, token)
except GPGpwstoreException, e:
raise Fault('Failed to retrieve token', str(e))
__put_token__(pwstore_user, token)
@@ -168,8 +168,12 @@ def get_metadata(pwstore_user):
return gpg_pwstore.get_metadata(pwstore_user)

@api.register
def del_password(pwstore_user, token, category, hostname, service, username):
def delete_password(pwstore_user, token, category, hostname, service, username):
if token != __take_token__(pwstore_user):
raise Fault('Failed to delete', "Token invalid - no access!")
return gpg_pwstore.del_password(pwstore_user, category, hostname, service, username)
return gpg_pwstore.delete_password(pwstore_user, category, hostname, service, username)

@api.register
def get_url(pwstore_user, category, hostname, service, username):
return gpg_pwstore.get_url(pwstore_user, category, hostname, service, username)


+ 13
- 1
testclient.py View File

@@ -66,6 +66,18 @@ print description_crypt
print server.add_password('zebe', 'linux', 'zebe001', 'ssh', 'root',
password_crypt, description_crypt)



token=__decrypt_data__(server.retrieve_token('zebe'))
print "1 " + token
password_crypt=__encrypt_data__('f00bar!', None, passphrase=token, symmetric=True)
print password_crypt
description_crypt=__encrypt_data__('das ist eine beschreibung!', None, passphrase=token, symmetric=True)
print description_crypt
print server.add_password('zebe', 'linux', 'zebe001', 'sshasd', 'root',
password_crypt, description_crypt)

print __decrypt_data__(server.get_url('zebe', 'linux', 'zebe001', 'sshasd', 'root'))
token=__decrypt_data__(server.retrieve_token('zebe'))
server.del_password('zebe', token, 'linux', 'zebe001', 'ssh', 'root')
server.delete_password('zebe', token, 'linux', 'zebe001', 'ssh', 'root')


Loading…
Cancel
Save