-
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.
Merge pull request #87 from kanton-bern/feature/HELLODATA-1689_user_d…
…eactivation Feature/hellodata 1689 user deactivation
- Loading branch information
Showing
9 changed files
with
361 additions
and
179 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
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
72 changes: 72 additions & 0 deletions
72
...java/ch/bedag/dap/hellodata/sidecars/airflow/service/user/AirflowDisableUserConsumer.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,72 @@ | ||
package ch.bedag.dap.hellodata.sidecars.airflow.service.user; | ||
|
||
import ch.bedag.dap.hellodata.commons.nats.annotation.JetStreamSubscribe; | ||
import ch.bedag.dap.hellodata.commons.sidecars.resources.v1.user.data.SubsystemUserUpdate; | ||
import ch.bedag.dap.hellodata.sidecars.airflow.client.AirflowClient; | ||
import ch.bedag.dap.hellodata.sidecars.airflow.client.user.response.AirflowRole; | ||
import ch.bedag.dap.hellodata.sidecars.airflow.client.user.response.AirflowUserResponse; | ||
import ch.bedag.dap.hellodata.sidecars.airflow.client.user.response.AirflowUsersResponse; | ||
import ch.bedag.dap.hellodata.sidecars.airflow.service.provider.AirflowClientProvider; | ||
import ch.bedag.dap.hellodata.sidecars.airflow.service.resource.AirflowUserResourceProviderService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.apache.commons.collections4.CollectionUtils; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.net.URISyntaxException; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
|
||
import static ch.bedag.dap.hellodata.commons.sidecars.events.HDEvent.DISABLE_USER; | ||
import static ch.bedag.dap.hellodata.sidecars.airflow.service.user.AirflowUserUtil.*; | ||
|
||
@Log4j2 | ||
@Service | ||
@RequiredArgsConstructor | ||
@SuppressWarnings("java:S3516") | ||
public class AirflowDisableUserConsumer { | ||
|
||
private final AirflowUserResourceProviderService userResourceProviderService; | ||
private final AirflowClientProvider airflowClientProvider; | ||
private final AirflowUserResourceProviderService airflowUserResourceProviderService; | ||
|
||
|
||
@SuppressWarnings("unused") | ||
@JetStreamSubscribe(event = DISABLE_USER) | ||
public CompletableFuture<Void> disableUser(SubsystemUserUpdate supersetUserUpdate) { | ||
try { | ||
log.info("------- Received airflow user disable request {}", supersetUserUpdate); | ||
|
||
AirflowClient airflowClient = airflowClientProvider.getAirflowClientInstance(); | ||
AirflowUsersResponse users = airflowClient.users(); | ||
List<AirflowRole> allAirflowRoles = CollectionUtils.emptyIfNull(airflowClient.roles().getRoles()).stream().toList(); | ||
|
||
// Airflow only allows unique username and email, so we make sure there is nobody with either of these already existing, before creating a new one | ||
Optional<AirflowUserResponse> userResult = users.getUsers() | ||
.stream() | ||
.filter(user -> user.getEmail().equalsIgnoreCase(supersetUserUpdate.getEmail()) || | ||
user.getUsername().equalsIgnoreCase(supersetUserUpdate.getUsername())) | ||
.findFirst(); | ||
|
||
if (userResult.isPresent()) { | ||
AirflowUserResponse airflowUser = userResult.get(); | ||
removeRoleFromUser(airflowUser, ADMIN_ROLE_NAME, allAirflowRoles); | ||
removeRoleFromUser(airflowUser, VIEWER_ROLE_NAME, allAirflowRoles); | ||
removeRoleFromUser(airflowUser, AF_OPERATOR_ROLE_NAME, allAirflowRoles); | ||
removeAllDataDomainRolesFromUser(airflowUser); | ||
addRoleToUser(airflowUser, PUBLIC_ROLE_NAME, allAirflowRoles); | ||
updateUser(airflowUser, airflowClient, airflowUserResourceProviderService); | ||
userResourceProviderService.publishUsers(); | ||
log.info("User with email: {} disabled", supersetUserUpdate.getEmail()); | ||
} else { | ||
log.warn("User with email: {} not found", supersetUserUpdate.getEmail()); | ||
} | ||
} catch (URISyntaxException | IOException e) { | ||
log.error("Could not disable user {}", supersetUserUpdate.getEmail(), e); | ||
} | ||
return null;//NOSONAR | ||
} | ||
|
||
} |
Oops, something went wrong.