Add ability to configure secret keys
Change-Id: I119e00a4b7feaec9d93401ef9e825ee46d304be4
diff --git a/_modules/rundeck.py b/_modules/rundeck.py
index f47c837..5d9c7a3 100644
--- a/_modules/rundeck.py
+++ b/_modules/rundeck.py
@@ -251,6 +251,58 @@
.format(project_name, resp.status_code, resp.text))
+# Key Store
+
+def get_secret_metadata(path):
+ session, make_url = get_session()
+ resp = session.get(
+ make_url("/api/11/storage/keys/{}".format(path)),
+ allow_redirects=False,
+ )
+ if resp.status_code == 200:
+ return True, resp.json()
+ elif resp.status_code == 404:
+ return True, None
+ return False, (
+ "Could not retrieve metadata for the {} secret key: {}/{}"
+ .format(path, resp.status_code, resp.text))
+
+
+def upload_secret(path, type, content, update=False):
+ session, make_url = get_session()
+ session.headers['Content-Type'] = SECRET_CONTENT_TYPE[type]
+ method = session.put if update else session.post
+ resp = method(
+ make_url("/api/11/storage/keys/{}".format(path)),
+ data=content,
+ allow_redirects=False,
+ )
+ if resp.status_code in (200, 201):
+ return True, resp.json()
+ return False, (
+ "Could not create or update the {} secret key with the type {}: {}/{}"
+ .format(path, type, resp.status_code, resp.text))
+
+SECRET_CONTENT_TYPE = {
+ "private": "application/octet-stream",
+ "public": "application/pgp-keys",
+ "password": "application/x-rundeck-data-password",
+}
+
+
+def delete_secret(path):
+ session, make_url = get_session()
+ resp = session.delete(
+ make_url("/api/11/storage/keys/{}".format(path)),
+ allow_redirects=False,
+ )
+ if resp.status_code == 204:
+ return True, None
+ return False, (
+ "Could not delete the {} secret key: {}/{}"
+ .format(path, resp.status_code, resp.text))
+
+
# Utils
def create_project_config(project_name, params, config=None):