use of com.netflix.spinnaker.front50.api.model.pipeline.Trigger in project front50 by spinnaker.
the class SharedManagedServiceAccountsMigration method migrate.
private void migrate(Pipeline pipeline, Map<String, ServiceAccount> serviceAccounts) {
log.info("Starting migration of pipeline '{}' with id '{}' for application '{}'", value("pipelineName", pipeline.getName()), value("application", pipeline.getApplication()), value("pipelineId", pipeline.getId()));
Set<String> newRoles = new HashSet<>();
List<String> existingRoles = (List) pipeline.getRoles();
if (existingRoles != null) {
existingRoles.stream().map(String::toLowerCase).forEach(newRoles::add);
}
List<Trigger> triggers = pipeline.getTriggers();
triggers.forEach(trigger -> {
String runAsUser = (String) trigger.get(RUN_AS_USER);
if (hasManagedServiceAccountUser(runAsUser)) {
ServiceAccount managedServiceAccount = serviceAccounts.get(runAsUser);
if (managedServiceAccount != null && !managedServiceAccount.getMemberOf().isEmpty()) {
managedServiceAccount.getMemberOf().stream().map(String::toLowerCase).forEach(newRoles::add);
}
}
});
String sharedManagedServiceAccountName = generatedSharedManagedServiceAccountName(newRoles);
ServiceAccount sharedManagedServiceAccount = new ServiceAccount();
sharedManagedServiceAccount.setName(sharedManagedServiceAccountName);
triggers.forEach(trigger -> {
String runAsUser = (String) trigger.get(RUN_AS_USER);
log.info("Replacing '{}' with automatic service user '{}' (application: '{}', pipelineName: '{}', " + "pipelineId: '{}')", value("oldServiceUser", runAsUser), value("newServiceUser", sharedManagedServiceAccountName), value("application", pipeline.getApplication()), value("pipelineName", pipeline.getName()), value("pipelineId", pipeline.getId()));
trigger.put(RUN_AS_USER, sharedManagedServiceAccountName);
});
log.info("Creating service user '{}' wih roles {}", sharedManagedServiceAccountName, newRoles);
sharedManagedServiceAccount.getMemberOf().addAll(newRoles);
pipeline.setRoles(new ArrayList(newRoles));
pipeline.setTriggers(triggers);
serviceAccountDAO.create(sharedManagedServiceAccount.getId(), sharedManagedServiceAccount);
pipelineDAO.update(pipeline.getId(), pipeline);
}
Aggregations