-
Notifications
You must be signed in to change notification settings - Fork 11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #1080: Protection against duplicate entries in the pa_operation_template table #1091
Merged
jnpsk
merged 3 commits into
develop
from
issues/1080-operation-template-table-duplicate-entries
Oct 31, 2023
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Migration from 1.5.x to 1.6.0 | ||
|
||
This guide contains instructions for migration from PowerAuth Server version `1.5.x` to version `1.6.0`. | ||
|
||
## Database Changes | ||
|
||
### Forbid name duplication for operation templates. | ||
|
||
Add unique constraint to `templateName` column in `pa_operation_template` table. |
15 changes: 15 additions & 0 deletions
15
...hangesets/powerauth-java-server/1.6.x/20231018-add-constraint-operation-template-name.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd"> | ||
|
||
<changeSet id="1" logicalFilePath="powerauth-java-server/1.6.x/20231018-add-constraint-operation-template-name.xml" author="Jan Pesek"> | ||
<preConditions onFail="MARK_RAN"> | ||
<not> | ||
<uniqueConstraintExists tableName="pa_operation_template" columnNames="template_name" constraintName="pa_operation_template_template_name_uk" /> | ||
</not> | ||
</preConditions> | ||
<comment>Add unique constraint to pa_operation_template.template_name</comment> | ||
<addUniqueConstraint tableName="pa_operation_template" columnNames="template_name" constraintName="pa_operation_template_template_name_uk" /> | ||
</changeSet> | ||
</databaseChangeLog> |
8 changes: 8 additions & 0 deletions
8
docs/db/changelog/changesets/powerauth-java-server/1.6.x/db.changelog-version.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.9.xsd"> | ||
|
||
<include file="20231018-add-constraint-operation-template-name.xml" relativeToChangelogFile="true" /> | ||
|
||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...me/security/powerauth/app/server/database/repository/OperationTemplateRepositoryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* PowerAuth Server and related software components | ||
* Copyright (C) 2023 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.getlime.security.powerauth.app.server.database.repository; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.getlime.security.powerauth.app.server.database.model.entity.OperationTemplateEntity; | ||
import io.getlime.security.powerauth.crypto.lib.enums.PowerAuthSignatureTypes; | ||
import org.hibernate.exception.ConstraintViolationException; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager; | ||
import org.springframework.context.annotation.Import; | ||
|
||
import java.util.Optional; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* Test for {@link OperationTemplateRepository}. | ||
* | ||
* @author Jan Pesek, [email protected] | ||
*/ | ||
@DataJpaTest | ||
@Import(ObjectMapper.class) | ||
class OperationTemplateRepositoryTest { | ||
|
||
@Autowired | ||
private OperationTemplateRepository repository; | ||
|
||
@Autowired | ||
private TestEntityManager entityManager; | ||
|
||
@Test | ||
void testDuplicateOperationTemplateCreation() { | ||
final String templateName = "login"; | ||
|
||
repository.save(createOperationTemplateEntity(templateName)); | ||
banterCZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
entityManager.flush(); | ||
|
||
Optional<OperationTemplateEntity> entity = repository.findTemplateByName(templateName); | ||
assertTrue(entity.isPresent()); | ||
assertEquals(templateName, entity.get().getTemplateName()); | ||
|
||
repository.save(createOperationTemplateEntity(templateName)); | ||
assertThrows(ConstraintViolationException.class, () -> entityManager.flush()); | ||
} | ||
|
||
private static OperationTemplateEntity createOperationTemplateEntity(String templateName) { | ||
final OperationTemplateEntity entity = new OperationTemplateEntity(); | ||
entity.setTemplateName(templateName); | ||
entity.setOperationType(templateName); | ||
entity.setDataTemplate("A2"); | ||
PowerAuthSignatureTypes[] signatureTypes = {PowerAuthSignatureTypes.POSSESSION}; | ||
entity.setSignatureType(signatureTypes); | ||
entity.setMaxFailureCount(5L); | ||
entity.setExpiration(300L); | ||
return entity; | ||
} | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
...ity/powerauth/app/server/service/behavior/tasks/OperationTemplateServiceBehaviorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* PowerAuth Server and related software components | ||
* Copyright (C) 2023 Wultra s.r.o. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published | ||
* by the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package io.getlime.security.powerauth.app.server.service.behavior.tasks; | ||
|
||
import com.wultra.security.powerauth.client.model.enumeration.SignatureType; | ||
import com.wultra.security.powerauth.client.model.request.OperationTemplateCreateRequest; | ||
import io.getlime.security.powerauth.app.server.service.exceptions.GenericServiceException; | ||
import io.getlime.security.powerauth.app.server.service.model.ServiceError; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* Test for {@link OperationTemplateServiceBehavior}. | ||
* | ||
* @author Jan Pesek, [email protected] | ||
*/ | ||
@SpringBootTest | ||
class OperationTemplateServiceBehaviorTest { | ||
|
||
@Autowired | ||
private OperationTemplateServiceBehavior service; | ||
|
||
@Test | ||
void testDuplicateOperationTemplateCreation() throws Exception { | ||
final String templateName = "login"; | ||
|
||
service.createOperationTemplate(createOperationTemplateCreateRequest(templateName)); | ||
assertFalse(service.getAllTemplates().isEmpty()); | ||
|
||
final GenericServiceException exception = assertThrows(GenericServiceException.class, () -> | ||
service.createOperationTemplate(createOperationTemplateCreateRequest(templateName))); | ||
assertEquals(ServiceError.OPERATION_TEMPLATE_ALREADY_EXISTS, exception.getCode()); | ||
} | ||
|
||
private static OperationTemplateCreateRequest createOperationTemplateCreateRequest(String templateName) { | ||
final OperationTemplateCreateRequest request = new OperationTemplateCreateRequest(); | ||
request.setTemplateName(templateName); | ||
request.setOperationType(templateName); | ||
request.setDataTemplate("A2"); | ||
request.getSignatureType().add(SignatureType.POSSESSION_KNOWLEDGE); | ||
request.setMaxFailureCount(5L); | ||
request.setExpiration(300L); | ||
return request; | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zcgandcomp Should we keep recording the db changes also in the migration guides or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add information into the migration guide, please add this information in the usual way as seen in previous migration guides.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be concrete about the changes or just mention that some changes are needed and link a script, which will be generated during the release?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/wultra/development-internal/wiki/Using-Liquibase#13-migration-between-versions. Yes please add a migration guide and mention the change. In theory it is possible to have (invalid) duplicities in the DB. The migration script in SQL will be generated during release procedure.