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

PUB-2506 Remove inactive SSO admin accounts #444

Merged
merged 4 commits into from
Oct 25, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,20 @@ List<PiUser> findExistingByProvenanceId(@Param("provUserId") String provenanceUs
nativeQuery = true)
List<String> getAccManDataForMI();


@Query(value = "SELECT * FROM pi_user WHERE CAST(last_verified_date AS DATE) = CURRENT_DATE - (interval '1' day)"
+ " * :daysAgo AND user_provenance = 'PI_AAD' AND roles = 'VERIFIED'", nativeQuery = true)
List<PiUser> findVerifiedUsersByLastVerifiedDate(@Param("daysAgo") int daysSinceLastVerified);

@Query(value = "SELECT * FROM pi_user WHERE CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day)"
+ " * :daysAgo AND roles <> 'VERIFIED' AND user_provenance = 'PI_AAD'", nativeQuery = true)
List<PiUser> findAadAdminUsersByLastSignedInDate(@Param("daysAgo") int daysSinceLastSignedIn);
@Query(value = "SELECT * FROM pi_user WHERE user_provenance = 'PI_AAD' AND roles <> 'VERIFIED' AND "
+ "CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day) * :aadDays", nativeQuery = true)
List<PiUser> findAdminUsersFortNotificationByLastSignedInDate(@Param("aadDays") int aadNumberOfDays);

@Query(value = "SELECT * FROM pi_user WHERE (user_provenance = 'PI_AAD' AND roles <> 'VERIFIED' AND "
+ "CAST(last_signed_in_date AS DATE) <= CURRENT_DATE - (interval '1' day) * :aadDays) OR "
ChrisS1512 marked this conversation as resolved.
Show resolved Hide resolved
+ "(user_provenance = 'SSO' AND "
+ "CAST(last_signed_in_date AS DATE) <= CURRENT_DATE - (interval '1' day) * :ssoDays)", nativeQuery = true)
List<PiUser> findAdminUsersForDeletionByLastSignedInDate(@Param("aadDays") int aadNumberOfDays,
@Param("ssoDays") int ssoNumberOfDays);

@Query(value = "SELECT * FROM pi_user WHERE (CAST(last_signed_in_date AS DATE) = CURRENT_DATE - (interval '1' day)"
+ " * :cftDaysAgo AND user_provenance = 'CFT_IDAM') "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
@Slf4j
@Service
public class InactiveAccountManagementService {

private final UserRepository userRepository;
private final AzureUserService azureUserService;
private final PublicationService publicationService;
Expand All @@ -31,6 +30,9 @@ public class InactiveAccountManagementService {
@Value("${verification.aad-admin-account-deletion-days}")
private int aadAdminAccountDeletionDays;

@Value("${verification.sso-admin-account-deletion-days}")
private int ssoAdminAccountDeletionDays;

@Value("${verification.cft-idam-account-sign-in-notification-days}")
private int cftIdamAccountSignInNotificationDays;

Expand Down Expand Up @@ -74,7 +76,7 @@ public void sendMediaUsersForVerification() {
* Then send their details on to publication services to send them a notification email.
*/
public void notifyAdminUsersToSignIn() {
userRepository.findAadAdminUsersByLastSignedInDate(aadAdminAccountSignInNotificationDays)
userRepository.findAdminUsersFortNotificationByLastSignedInDate(aadAdminAccountSignInNotificationDays)
.forEach(user -> {
try {
publicationService.sendInactiveAccountSignInNotificationEmail(
Expand Down Expand Up @@ -119,7 +121,8 @@ public void findMediaAccountsForDeletion() {
* Account service handles the deletion of their AAD, P&I user and subscriptions.
*/
public void findAdminAccountsForDeletion() {
userRepository.findAadAdminUsersByLastSignedInDate(aadAdminAccountDeletionDays)
userRepository.findAdminUsersForDeletionByLastSignedInDate(aadAdminAccountDeletionDays,
ssoAdminAccountDeletionDays)
.forEach(user -> accountService.deleteAccount(user.getUserId()));
}

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ verification:
media-account-deletion-days: 365
aad-admin-account-sign-in-notification-days: 76
aad-admin-account-deletion-days: 90
sso-admin-account-deletion-days: 90
cft-idam-account-sign-in-notification-days: 118
cft-idam-account-deletion-days: 132
crime-idam-account-sign-in-notification-days: 180
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,11 @@ class AccountServiceTest {
private static final String USER_NOT_FOUND_EXCEPTION_MESSAGE =
"The exception when a user has not been found has been thrown";
private static final UUID VALID_USER_ID = UUID.randomUUID();
private static final UUID VALID_USER_ID_SSO = UUID.randomUUID();
private static final UUID VALID_USER_ID_IDAM = UUID.randomUUID();

private static final PiUser PI_USER = new PiUser();
private static final PiUser PI_USER_SSO = new PiUser();
private static final PiUser PI_USER_IDAM = new PiUser();
private static final AzureAccount AZURE_ACCOUNT = new AzureAccount();
private static final User EXPECTED_USER = new User();
Expand All @@ -118,6 +120,10 @@ void setup() {
PI_USER.setProvenanceUserId(ID);
PI_USER.setEmail(EMAIL);

PI_USER_SSO.setUserId(VALID_USER_ID_SSO);
PI_USER_SSO.setUserProvenance(UserProvenances.SSO);
PI_USER_SSO.setProvenanceUserId(ID);

PI_USER_IDAM.setUserId(VALID_USER_ID_IDAM);
PI_USER_IDAM.setUserProvenance(UserProvenances.CFT_IDAM);
PI_USER_IDAM.setProvenanceUserId(ID);
Expand Down Expand Up @@ -289,6 +295,21 @@ void testDeleteAadAccount() throws AzureCustomException {
verify(userRepository, times(1)).delete(PI_USER);
}

@Test
void testDeleteSsoAccount() {
when(userRepository.findByUserId(VALID_USER_ID_SSO)).thenReturn(Optional.of(PI_USER_SSO));

doNothing().when(userRepository).delete(PI_USER_SSO);
when(subscriptionService.sendSubscriptionDeletionRequest(VALID_USER_ID_SSO.toString()))
.thenReturn(SUBSCRIPTIONS_DELETED);

accountService.deleteAccount(VALID_USER_ID_SSO);

verifyNoInteractions(azureUserService);
verify(subscriptionService).sendSubscriptionDeletionRequest(VALID_USER_ID_SSO.toString());
verify(userRepository).delete(PI_USER_SSO);
}

@Test
void testDeleteIdamAccount() {
when(userRepository.findByUserId(VALID_USER_ID_IDAM)).thenReturn(Optional.of(PI_USER_IDAM));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class InactiveAccountManagementServiceTest {
private static final String MEDIA_USER_EMAIL = "media@test.com";
private static final UUID AAD_ADMIN_UUID = UUID.randomUUID();
private static final String AAD_ADMIN_USER_EMAIL = "aad_admin@test.com";
private static final UUID SSO_ADMIN_UUID = UUID.randomUUID();
private static final String SSO_ADMIN_USER_EMAIL = "sso_admin@test.com";
private static final UUID CFT_IDAM_UUID = UUID.randomUUID();
private static final String CFT_IDAM_USER_EMAIL = "cft_idam@test.com";
private static final UUID CRIME_IDAM_UUID = UUID.randomUUID();
Expand All @@ -47,11 +49,14 @@ class InactiveAccountManagementServiceTest {
private static final PiUser AAD_ADMIN_USER = new PiUser(AAD_ADMIN_UUID, UserProvenances.PI_AAD,
"2", AAD_ADMIN_USER_EMAIL, Roles.INTERNAL_SUPER_ADMIN_CTSC,
FORENAME, SURNAME, null, null, LAST_SIGNED_IN_DATE);
private static final PiUser SSO_ADMIN_USER = new PiUser(SSO_ADMIN_UUID, UserProvenances.SSO,
"3", SSO_ADMIN_USER_EMAIL, Roles.INTERNAL_SUPER_ADMIN_CTSC,
FORENAME, SURNAME, null, null, LAST_SIGNED_IN_DATE);
private static final PiUser CFT_IDAM_USER = new PiUser(CFT_IDAM_UUID, UserProvenances.CFT_IDAM,
"3", CFT_IDAM_USER_EMAIL, Roles.INTERNAL_ADMIN_CTSC,
"4", CFT_IDAM_USER_EMAIL, Roles.INTERNAL_ADMIN_CTSC,
FORENAME, SURNAME, null, null, LAST_SIGNED_IN_DATE);
private static final PiUser CRIME_IDAM_USER = new PiUser(CRIME_IDAM_UUID, UserProvenances.CRIME_IDAM,
"4", CRIME_IDAM_USER_EMAIL, Roles.INTERNAL_ADMIN_CTSC,
"5", CRIME_IDAM_USER_EMAIL, Roles.INTERNAL_ADMIN_CTSC,
FORENAME, SURNAME, null, null, LAST_SIGNED_IN_DATE);

private static User azureMediaUser = new User();
Expand Down Expand Up @@ -100,7 +105,7 @@ void testNoMediaUsersForVerification() {

@Test
void testNotifyAdminUsersToSignIn() throws AzureCustomException {
when(userRepository.findAadAdminUsersByLastSignedInDate(anyInt()))
when(userRepository.findAdminUsersFortNotificationByLastSignedInDate(anyInt()))
.thenReturn(Collections.singletonList(AAD_ADMIN_USER));
when(azureUserService.getUser(AAD_ADMIN_USER_EMAIL)).thenReturn(azureAdminUser);

Expand All @@ -112,7 +117,7 @@ void testNotifyAdminUsersToSignIn() throws AzureCustomException {

@Test
void testNoNotificationOfAdminUsersToSignIn() {
when(userRepository.findAadAdminUsersByLastSignedInDate(anyInt()))
when(userRepository.findAdminUsersFortNotificationByLastSignedInDate(anyInt()))
.thenReturn(Collections.emptyList());

inactiveAccountManagementService.notifyAdminUsersToSignIn();
Expand Down Expand Up @@ -163,16 +168,17 @@ void testNoMediaAccountDeletion() {

@Test
void testAdminAccountDeletion() {
when(userRepository.findAadAdminUsersByLastSignedInDate(anyInt()))
.thenReturn(Collections.singletonList(AAD_ADMIN_USER));
when(userRepository.findAdminUsersForDeletionByLastSignedInDate(anyInt(), anyInt()))
.thenReturn(List.of(AAD_ADMIN_USER, SSO_ADMIN_USER));

inactiveAccountManagementService.findAdminAccountsForDeletion();
verify(accountService).deleteAccount(AAD_ADMIN_UUID);
verify(accountService).deleteAccount(SSO_ADMIN_UUID);
}

@Test
void testNoAdminAccountDeletion() {
when(userRepository.findAadAdminUsersByLastSignedInDate(anyInt()))
when(userRepository.findAdminUsersForDeletionByLastSignedInDate(anyInt(), anyInt()))
.thenReturn(Collections.emptyList());

inactiveAccountManagementService.findAdminAccountsForDeletion();
Expand Down