summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Heimes <christian@python.org>2015-10-16 13:21:22 +0200
committerChristian Heimes <christian@python.org>2015-10-16 13:21:22 +0200
commit91e382abd6a6b527ca3a1a8e6e83f4f21a115a75 (patch)
tree2dd097c12e39fb62cef66445ca44368933aa835c
parente2eb7931187dd8fd6580ef18027a9fe209b57dab (diff)
downloadcustodia_pwmgr-91e382abd6a6b527ca3a1a8e6e83f4f21a115a75.tar.gz
custodia_pwmgr-91e382abd6a6b527ca3a1a8e6e83f4f21a115a75.tar.xz
custodia_pwmgr-91e382abd6a6b527ca3a1a8e6e83f4f21a115a75.zip
Retrieve password
Handle exceptions and error conditions
-rw-r--r--.dockerignore2
-rwxr-xr-xcustodia_pwmgr/custodia_pwmgr.py43
-rw-r--r--custodia_pwmgr/static/style.css3
-rw-r--r--custodia_pwmgr/templates/canvas.html13
-rw-r--r--custodia_pwmgr/templates/index.html14
-rw-r--r--custodia_pwmgr/templates/secret.html11
-rwxr-xr-xpush.sh5
7 files changed, 72 insertions, 19 deletions
diff --git a/.dockerignore b/.dockerignore
index 0bdbbbf..9c891de 100644
--- a/.dockerignore
+++ b/.dockerignore
@@ -2,4 +2,6 @@
.*swp
__pycache__
+push.sh
kubernetes
+.git
diff --git a/custodia_pwmgr/custodia_pwmgr.py b/custodia_pwmgr/custodia_pwmgr.py
index 6bc3681..d0ca754 100755
--- a/custodia_pwmgr/custodia_pwmgr.py
+++ b/custodia_pwmgr/custodia_pwmgr.py
@@ -28,7 +28,7 @@ from urllib import quote
from flask import Flask, flash, render_template, redirect, request, url_for
from custodia.client import CustodiaClient
-from requests.exceptions import HTTPError
+from requests.exceptions import RequestException
# get Unix socket from env
CUSTODIA_SOCKET = os.environ.get('CUSTODIA_SOCKET')
@@ -60,7 +60,7 @@ class FlaskCustodia(object):
# timeout = app.config.get('custodia_timeout')
self._container = app.config['CUSTODIA_CONTAINER']
self._client = CustodiaClient(url)
- #self._client.headers['REMOTE_USER'] = self._container
+ self._client.headers['REMOTE_USER'] = self._container
#self.mkcontainer()
def _genpath(self, key):
@@ -71,7 +71,7 @@ class FlaskCustodia(object):
def mkcontainer(self):
try:
self._client.create_container(self._container)
- except HTTPError as e:
+ except RequestException as e:
if e.response.status_code != 409:
raise
return False
@@ -99,10 +99,18 @@ class FlaskCustodia(object):
flaskcustodia = FlaskCustodia(app)
+def flash_exception(msg, exc):
+ return flash("%s: %s" % (msg, exc), 'error')
+
+
@app.route('/')
def index():
# flaskcustodia.set_simple('key', 'password')
- items = flaskcustodia.items()
+ try:
+ items = flaskcustodia.items()
+ except RequestException as e:
+ flash_exception("Unable to retrieve secrets", e)
+ items = []
return render_template('index.html', items=items)
@@ -110,16 +118,35 @@ def index():
def add_password():
name = request.form['name']
password = request.form['password']
- flaskcustodia.set_simple(name, password)
- flash('New entry was successfully stored')
+ try:
+ flaskcustodia.set_simple(name, password)
+ except RequestException as e:
+ flash_exception("Unable to add new entry", e)
+ else:
+ flash('New entry was successfully stored')
return redirect(url_for('index'))
+@app.route('/get/<name>')
+def get_password(name):
+ try:
+ value = flaskcustodia.get_simple(name)
+ except RequestException as e:
+ flash_exception("Cannot retrieve entry '%s'" % name, e)
+ return redirect(url_for('index'))
+ else:
+ return render_template('secret.html', name=name, value=value)
+
+
@app.route('/delete', methods=['POST'])
def delete_password():
name = request.form['name']
- flaskcustodia.delete(name)
- flash('Entry was successfully deleted')
+ try:
+ flaskcustodia.delete(name)
+ except RequestException as e:
+ flash_exception("Unable to delete entry", e)
+ else:
+ flash('Entry was successfully deleted')
return redirect(url_for('index'))
if __name__ == '__main__':
diff --git a/custodia_pwmgr/static/style.css b/custodia_pwmgr/static/style.css
index 211e307..45c7e98 100644
--- a/custodia_pwmgr/static/style.css
+++ b/custodia_pwmgr/static/style.css
@@ -3,6 +3,7 @@ a, h1, h2 { color: #377BA8; }
h1, h2 { font-family: 'Georgia', serif; margin: 0; }
h1 { border-bottom: 2px solid #eee; }
h2 { font-size: 1.2em; }
+dt { font-weight: bold; }
.page { margin: 2em auto; width: 35em; border: 5px solid #ccc;
padding: 0.8em; background: white; }
@@ -13,7 +14,7 @@ h2 { font-size: 1.2em; }
.add-entry dl { font-weight: bold; }
.metanav { text-align: right; font-size: 0.8em; padding: 0.3em;
margin-bottom: 1em; background: #fafafa; }
-.flash { background: #CEE5F5; padding: 0.5em;
+.flashes { background: #CEE5F5; padding: 0.5em;
border: 1px solid #AACBE2; }
.error { background: #F0D6D6; padding: 0.5em; }
diff --git a/custodia_pwmgr/templates/canvas.html b/custodia_pwmgr/templates/canvas.html
index 136606d..cb9d805 100644
--- a/custodia_pwmgr/templates/canvas.html
+++ b/custodia_pwmgr/templates/canvas.html
@@ -3,9 +3,16 @@
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css') }}">
<div class="page">
<h1>Custodia Example App</h1>
- {% for message in get_flashed_messages() %}
- <div class="flash">{{ message }}</div>
- {% endfor %}
+ <h2>Password Manager Example</h2>
+{% with messages = get_flashed_messages(with_categories=true) %}
+ {% if messages %}
+ <div class="flashes">
+ {% for category, message in messages %}
+ <div class="{{ category }}">{{ message }}</div>
+ {% endfor %}
+ </div>
+ {% endif %}
+{% endwith %}
{% block body %}{% endblock %}
</div>
diff --git a/custodia_pwmgr/templates/index.html b/custodia_pwmgr/templates/index.html
index e37ed82..7dc00f7 100644
--- a/custodia_pwmgr/templates/index.html
+++ b/custodia_pwmgr/templates/index.html
@@ -1,22 +1,22 @@
{% extends "canvas.html" %}
{% block body %}
-<h2>Password Manager Example</h2>
<h3>Passwords</h3>
-<ul class="entries">
{% for item in items %}
- <li>{{ item }}
- <form action="{{ url_for('delete_password') }}" method="post">
+<ul class="entries">
+ <li><a href="{{ url_for('get_password', name=item) }}">{{ item }}</a>
+ <form action="{{ url_for('delete_password') }}" method="post" style="display: inline;">
<input type="hidden" name="name" value="{{ item }}" />
<input type="submit" value="Delete">
</form>
</li>
+</ul>
{% else %}
- <li>No passwords</li>
+ <em>No password</em>
{% endfor %}
-</ul>
+
<h3>Add Password</h3>
-<form action="{{ url_for('add_password') }}" method="post">
+<form action="{{ url_for('add_password') }}" method="post" autocomplete="off">
<dl>
<dt>Name:
<dd><input type="text" name="name">
diff --git a/custodia_pwmgr/templates/secret.html b/custodia_pwmgr/templates/secret.html
new file mode 100644
index 0000000..b463c31
--- /dev/null
+++ b/custodia_pwmgr/templates/secret.html
@@ -0,0 +1,11 @@
+{% extends "canvas.html" %}
+{% block body %}
+<h3>Password</h3>
+<dl>
+ <dt>Name:
+ <dd>{{ name }}
+ <dt>Secret:
+ <dd>{{ value }}
+</dl>
+<a href="{{ url_for('index') }}">back</a>
+{% endblock %}
diff --git a/push.sh b/push.sh
new file mode 100755
index 0000000..a767741
--- /dev/null
+++ b/push.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+set -e
+sudo docker build -t 10.34.78.249:5000/custodia-pwmgr .
+sudo docker push 10.34.78.249:5000/custodia-pwmgr
+