New CLI API: secrets API improvements.
* Added more conditions and appropriate messages
on secrets deletion to check:
- if provided secret_id is a valid uuid4;
- if secret with provided secret_id is exists.
* Stylystic fixes.
@@ -2,6 +2,7 @@ from flask import request, g, abort
from flask_restplus import Resource, fields
import re
+from pyinfrabox.utils import validate_uuid4
from pyinfraboxutils.ibflask import auth_required, OK
from pyinfraboxutils.ibrestplus import api
from api.namespaces import project as ns
@@ -39,24 +40,24 @@ class Secrets(Resource):
if not Secrets.name_pattern.match(b['name']):
abort(400, 'Secret name must be not empty alphanumeric string.')
- result = g.db.execute_one_dict('''
+ result = g.db.execute_one_dict("""
SELECT COUNT(*) as cnt FROM secret WHERE project_id = %s
- ''', [project_id])
+ """, [project_id])
if result['cnt'] > 50:
abort(400, 'Too many secrets.')
- r = g.db.execute_one('''
+ r = g.db.execute_one("""
SELECT count(*) FROM secret
WHERE project_id = %s AND name = %s
- ''', [project_id, b['name']])
+ """, [project_id, b['name']])
if r[0] > 0:
abort(400, 'Secret with this name already exist.')
- g.db.execute('''
+ g.db.execute("""
INSERT INTO secret (project_id, name, value) VALUES(%s, %s, %s)
- ''', [project_id, b['name'], b['value']])
+ """, [project_id, b['name'], b['value']])
g.db.commit()
@@ -67,9 +68,20 @@ class Secrets(Resource):
class Secret(Resource):
@auth_required(['user'])
def delete(self, project_id, secret_id):
- g.db.execute('''
+ if not validate_uuid4(secret_id):
+ abort(400, "Invalid secret uuid.")
+
+ num_secrets = g.db.execute_one("""
+ SELECT COUNT(*) FROM secret
+ WHERE project_id = %s and id = %s
+ """, [project_id, secret_id])[0]
+
+ if num_secrets == 0:
+ return abort(400, 'Such secret does not exist.')
+
+ g.db.execute("""
DELETE FROM secret WHERE project_id = %s and id = %s
- ''', [project_id, secret_id])
+ """, [project_id, secret_id])
g.db.commit()
return OK('Successfully deleted secret.')
