From fc27b775db634123f2a02ee5806c516a14b1428d Mon Sep 17 00:00:00 2001 From: Alejandro Roiz Walss Date: Tue, 30 Jan 2024 18:51:30 -0600 Subject: [PATCH 1/4] fix dynamodb saves --- confidant/routes/blind_credentials.py | 8 ++++---- confidant/routes/credentials.py | 15 ++++++++------- confidant/routes/services.py | 4 ++-- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/confidant/routes/blind_credentials.py b/confidant/routes/blind_credentials.py index 8e9ca4de..32696c83 100644 --- a/confidant/routes/blind_credentials.py +++ b/confidant/routes/blind_credentials.py @@ -188,7 +188,7 @@ def create_blind_credential(): if not isinstance(data.get('metadata', {}), dict): return jsonify({'error': 'metadata must be a dict'}), 400 for cred in BlindCredential.data_type_date_index.query( - 'blind-credential', name__eq=data['name']): + 'blind-credential'): # Conflict, the name already exists msg = 'Name already exists. See id: {0}'.format(cred.id) return jsonify({'error': msg, 'reference': cred.id}), 409 @@ -210,7 +210,7 @@ def create_blind_credential(): cipher_version=data['cipher_version'], modified_by=authnz.get_logged_in_user(), documentation=data.get('documentation') - ).save(id__null=True) + ).save() # Make this the current revision cred = BlindCredential( id=id, @@ -344,7 +344,7 @@ def update_blind_credential(id): cipher_version=update['cipher_version'], modified_by=authnz.get_logged_in_user(), documentation=update['documentation'] - ).save(id__null=True) + ).save() except PutError as e: logger.error(e) return jsonify( @@ -454,7 +454,7 @@ def revert_blind_credential_to_revision(id, to_revision): cipher_version=revert_credential.cipher_version, modified_by=authnz.get_logged_in_user(), documentation=revert_credential.documentation - ).save(id__null=True) + ).save() except PutError as e: logger.error(e) return jsonify( diff --git a/confidant/routes/credentials.py b/confidant/routes/credentials.py index 045170ba..c0498e3d 100644 --- a/confidant/routes/credentials.py +++ b/confidant/routes/credentials.py @@ -616,10 +616,11 @@ def create_credential(): if not _check: return jsonify(ret), 400 for cred in Credential.data_type_date_index.query( - 'credential', name__eq=data['name']): - # Conflict, the name already exists - msg = 'Name already exists. See id: {0}'.format(cred.id) - return jsonify({'error': msg, 'reference': cred.id}), 409 + 'credential'): + if cred.name == data['name']: + # Conflict, the name already exists + msg = 'Name already exists. See id: {0}'.format(cred.id) + return jsonify({'error': msg, 'reference': cred.id}), 409 # Generate an initial stable ID to allow name changes id = str(uuid.uuid4()).replace('-', '') # Try to save to the archive @@ -643,7 +644,7 @@ def create_credential(): documentation=data.get('documentation'), tags=data.get('tags', []), last_rotation_date=last_rotation_date, - ).save(id__null=True) + ).save() # Make this the current revision cred = Credential( id=id, @@ -882,7 +883,7 @@ def update_credential(id): documentation=update['documentation'], tags=update['tags'], last_rotation_date=update['last_rotation_date'], - ).save(id__null=True) + ).save() except PutError as e: logger.error(e) return jsonify({'error': 'Failed to add credential to archive.'}), 500 @@ -1056,7 +1057,7 @@ def revert_credential_to_revision(id, to_revision): documentation=revert_credential.documentation, tags=revert_credential.tags, last_rotation_date=revert_credential.last_rotation_date, - ).save(id__null=True) + ).save() except PutError as e: logger.error(e) return jsonify({'error': 'Failed to add credential to archive.'}), 500 diff --git a/confidant/routes/services.py b/confidant/routes/services.py index 3d585505..f8bc9823 100644 --- a/confidant/routes/services.py +++ b/confidant/routes/services.py @@ -650,7 +650,7 @@ def map_service_credentials(id): enabled=data.get('enabled'), revision=revision, modified_by=authnz.get_logged_in_user() - ).save(id__null=True) + ).save() except PutError as e: logger.error(e) return jsonify({'error': 'Failed to add service to archive.'}), 500 @@ -811,7 +811,7 @@ def revert_service_to_revision(id, to_revision): enabled=revert_service.enabled, revision=new_revision, modified_by=authnz.get_logged_in_user() - ).save(id__null=True) + ).save() except PutError as e: logger.error(e) return jsonify({'error': 'Failed to add service to archive.'}), 500 From 7051659c7cba98ce66f709f70b8dda3544f25861 Mon Sep 17 00:00:00 2001 From: Alejandro Roiz Walss Date: Tue, 30 Jan 2024 18:57:32 -0600 Subject: [PATCH 2/4] use filter_condition --- confidant/routes/credentials.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/confidant/routes/credentials.py b/confidant/routes/credentials.py index c0498e3d..489d8a45 100644 --- a/confidant/routes/credentials.py +++ b/confidant/routes/credentials.py @@ -616,11 +616,10 @@ def create_credential(): if not _check: return jsonify(ret), 400 for cred in Credential.data_type_date_index.query( - 'credential'): - if cred.name == data['name']: - # Conflict, the name already exists - msg = 'Name already exists. See id: {0}'.format(cred.id) - return jsonify({'error': msg, 'reference': cred.id}), 409 + 'credential', filter_condition=Credential.name == data['name']): + # Conflict, the name already exists + msg = 'Name already exists. See id: {0}'.format(cred.id) + return jsonify({'error': msg, 'reference': cred.id}), 409 # Generate an initial stable ID to allow name changes id = str(uuid.uuid4()).replace('-', '') # Try to save to the archive From 830064af53bbfd1ee489d1d0e015e9bfecead3b9 Mon Sep 17 00:00:00 2001 From: Alejandro Roiz Walss Date: Tue, 30 Jan 2024 18:59:12 -0600 Subject: [PATCH 3/4] use filter_condition for blind credentials --- confidant/routes/blind_credentials.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/confidant/routes/blind_credentials.py b/confidant/routes/blind_credentials.py index 32696c83..ed690660 100644 --- a/confidant/routes/blind_credentials.py +++ b/confidant/routes/blind_credentials.py @@ -188,7 +188,7 @@ def create_blind_credential(): if not isinstance(data.get('metadata', {}), dict): return jsonify({'error': 'metadata must be a dict'}), 400 for cred in BlindCredential.data_type_date_index.query( - 'blind-credential'): + 'blind-credential', filter_condition=BlindCredential.name == data['name']): # Conflict, the name already exists msg = 'Name already exists. See id: {0}'.format(cred.id) return jsonify({'error': msg, 'reference': cred.id}), 409 From e2108fdda54a78a095f889ce100147a5737b086b Mon Sep 17 00:00:00 2001 From: Alejandro Roiz Walss Date: Tue, 30 Jan 2024 19:02:57 -0600 Subject: [PATCH 4/4] fix lint --- confidant/routes/blind_credentials.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/confidant/routes/blind_credentials.py b/confidant/routes/blind_credentials.py index ed690660..5cbf6d37 100644 --- a/confidant/routes/blind_credentials.py +++ b/confidant/routes/blind_credentials.py @@ -188,7 +188,9 @@ def create_blind_credential(): if not isinstance(data.get('metadata', {}), dict): return jsonify({'error': 'metadata must be a dict'}), 400 for cred in BlindCredential.data_type_date_index.query( - 'blind-credential', filter_condition=BlindCredential.name == data['name']): + 'blind-credential', + filter_condition=BlindCredential.name == data['name'] + ): # Conflict, the name already exists msg = 'Name already exists. See id: {0}'.format(cred.id) return jsonify({'error': msg, 'reference': cred.id}), 409