-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AM-87 Update the certificate used by applications after a default cer…
…t renewal
- Loading branch information
Showing
28 changed files
with
1,170 additions
and
8 deletions.
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
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
83 changes: 83 additions & 0 deletions
83
...gement-api-service/src/main/java/io/gravitee/am/management/service/tasks/TasksLoader.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,83 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.am.management.service.tasks; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.gravitee.am.repository.management.api.CertificateRepository; | ||
import io.gravitee.am.repository.management.api.SystemTaskRepository; | ||
import io.gravitee.am.service.ApplicationService; | ||
import io.gravitee.am.service.TaskManager; | ||
import io.gravitee.am.service.tasks.AssignSystemCertificate; | ||
import io.gravitee.am.service.tasks.AssignSystemCertificateDefinition; | ||
import io.gravitee.am.service.tasks.TaskType; | ||
import io.gravitee.common.component.LifecycleComponent; | ||
import io.gravitee.common.service.AbstractService; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.scheduling.TaskScheduler; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* This class is used to read tasks from the SystemTask that may be scheduled. | ||
* The goal is to ensure that scheduled task are executed if the Management instance | ||
* restart before the task execution. | ||
* | ||
* @author Eric LELEU (eric.leleu at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@Component | ||
public class TasksLoader extends AbstractService<TasksLoader> implements LifecycleComponent<TasksLoader> { | ||
|
||
private final Logger logger = LoggerFactory.getLogger(TaskManager.class); | ||
|
||
@Autowired | ||
private ObjectMapper mapper; | ||
@Autowired | ||
private TaskScheduler scheduler; | ||
@Autowired | ||
@Lazy | ||
private SystemTaskRepository taskRepository; | ||
@Autowired | ||
@Lazy | ||
private ApplicationService applicationService; | ||
@Autowired | ||
@Lazy | ||
private CertificateRepository certificateRepository; | ||
@Autowired | ||
private TaskManager taskManager; | ||
|
||
@Override | ||
protected void doStart() throws Exception { | ||
super.doStart(); | ||
this.logger.info("Load scheduled tasks"); | ||
this.taskRepository.findByType(TaskType.SIMPLE.name()) | ||
// currently only one kind of Simple tasks, so we simply filter on this value for safety | ||
.filter(systemTask -> AssignSystemCertificate.class.getSimpleName().equals(systemTask.getKind())) | ||
.map(systemTask -> { | ||
final var assignSystemCert = new AssignSystemCertificate(systemTask.getId(), this.applicationService, this.certificateRepository, this.taskManager); | ||
final var taskConfiguration = mapper.readValue(systemTask.getConfiguration(), AssignSystemCertificateDefinition.class); | ||
assignSystemCert.setDefinition(taskConfiguration); | ||
return assignSystemCert; | ||
}) | ||
.subscribe(task -> { | ||
logger.debug("Reschedule {} task of type {} with definition {}", task.type(), task.kind(), task.getDefinition()); | ||
task.registerScheduler(this.scheduler); | ||
task.schedule(); | ||
}); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...nt-api-service/src/test/java/io/gravitee/am/management/service/tasks/TasksLoaderTest.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,94 @@ | ||
/** | ||
* Copyright (C) 2015 The Gravitee team (http://gravitee.io) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.gravitee.am.management.service.tasks; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.gravitee.am.model.SystemTask; | ||
import io.gravitee.am.repository.management.api.SystemTaskRepository; | ||
import io.gravitee.am.service.tasks.AssignSystemCertificate; | ||
import io.gravitee.am.service.tasks.TaskType; | ||
import io.reactivex.Flowable; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Mockito; | ||
import org.mockito.Spy; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
import org.springframework.scheduling.TaskScheduler; | ||
|
||
import javax.annotation.OverridingMethodsMustInvokeSuper; | ||
import java.time.Instant; | ||
import java.util.ArrayList; | ||
import java.util.Date; | ||
import java.util.Random; | ||
|
||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.anyChar; | ||
import static org.mockito.ArgumentMatchers.argThat; | ||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static reactor.core.publisher.Mono.when; | ||
|
||
/** | ||
* @author Eric LELEU (eric.leleu at graviteesource.com) | ||
* @author GraviteeSource Team | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class TasksLoaderTest { | ||
|
||
@InjectMocks | ||
private TasksLoader tasksLoader; | ||
|
||
@Mock | ||
private SystemTaskRepository taskRepository; | ||
|
||
@Mock | ||
private TaskScheduler scheduler; | ||
|
||
@Spy | ||
private ObjectMapper mapper = new ObjectMapper(); | ||
|
||
@Test | ||
public void shouldSchedule_SimpleTask() throws Exception { | ||
var tasks = new ArrayList<>(); | ||
final int numberOfSimpleTasks = new Random().nextInt(10); | ||
for (int i = 0; i < numberOfSimpleTasks; ++i) { | ||
var task = new SystemTask(); | ||
task.setId("simple-task-"+i); | ||
task.setType(TaskType.SIMPLE.name()); | ||
task.setKind(AssignSystemCertificate.class.getSimpleName()); | ||
task.setConfiguration("{\"delay\": 1 , \"unit\": \"MINUTES\"}"); | ||
tasks.add(task); | ||
} | ||
|
||
final int numberOfOtherTasks = new Random().nextInt(10); | ||
for (int i = 0; i < numberOfOtherTasks; ++i) { | ||
var task = new SystemTask(); | ||
task.setId("other-task-"+i); | ||
task.setType(TaskType.SIMPLE.name()); | ||
task.setKind("other"); | ||
tasks.add(task); | ||
} | ||
|
||
doReturn(Flowable.fromIterable(tasks)).when(taskRepository).findByType(TaskType.SIMPLE.name()); | ||
|
||
tasksLoader.doStart(); | ||
|
||
verify(scheduler, times(numberOfSimpleTasks)).schedule(argThat(task -> task instanceof AssignSystemCertificate), any(Instant.class)); | ||
} | ||
} |
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
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
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
12 changes: 12 additions & 0 deletions
12
...jdbc/src/main/resources/liquibase/changelogs/v3_20_0/3.20.0-update-system-tasks-table.yml
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,12 @@ | ||
databaseChangeLog: | ||
- changeSet: | ||
id: 3.20.0-update-system-tasks-table | ||
author: GraviteeSource Team | ||
changes: | ||
## system task table | ||
##################### | ||
- addColumn: | ||
tableName: system_tasks | ||
columns: | ||
- column: { name: configuration, type: clob, constraints: { nullable: true } } | ||
- column: { name: kind, type: varchar(128), constraints: { nullable: true } } |
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
Oops, something went wrong.