Skip to content
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
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/PowerAuth-Server-1.6.0.md
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.
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" />
Copy link
Member

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?

Copy link
Member

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.

Copy link
Member

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?

Copy link
Member

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.

</changeSet>
</databaseChangeLog>
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>
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@

<include file="1.4.x/db.changelog-version.xml" relativeToChangelogFile="true" />
<include file="1.5.x/db.changelog-version.xml" relativeToChangelogFile="true" />
<include file="1.6.x/db.changelog-version.xml" relativeToChangelogFile="true" />

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class OperationTemplateEntity implements Serializable {
@Column(name = "id")
private Long id;

@Column(name = "template_name", nullable=false)
@Column(name = "template_name", nullable=false, unique = true)
private String templateName;

@Column(name = "operation_type", nullable=false)
Expand Down
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;
}

}
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;
}

}