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

feat: add remove token functionality for the security server admin in the ui #2576

Merged
merged 14 commits into from
Feb 4, 2025
Merged
Prev Previous commit
Next Next commit
fix: fixes of sonarqube issues
Refs: XRDDEV-2622
  • Loading branch information
enelir committed Jan 27, 2025

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 910365e181b68184abfb60b4b65f9dfd106f3995
Original file line number Diff line number Diff line change
@@ -31,6 +31,7 @@

public enum CommonDeviationMessage implements DeviationProvider {
INTERNAL_ERROR("internal_error", "Internal error. See server logs for more details"),
ACTION_NOT_POSSIBLE("action_not_possible", "Action not possible"),
GENERIC_VALIDATION_FAILURE("invalid_parameters", "Validation failure"),
SECURITY_SERVER_NOT_FOUND("security_server_not_found", "Security server not found"),
INVALID_ENCODED_ID("invalid_encoded_id", "Invalid encoded id"),
@@ -76,7 +77,8 @@ public enum CommonDeviationMessage implements DeviationProvider {

TOKEN_FETCH_FAILED("token_fetch_failed", "Error getting tokens"),
TOKEN_PIN_INCORRECT("pin_incorrect", "Entered PIN was incorrect"),
TOKEN_NOT_ACTIVE("token_not_active", "Token is not active");
TOKEN_NOT_ACTIVE("token_not_active", "Token is not active"),
TOKEN_NOT_FOUND("token_not_found", "Token not found");

@Getter
private final String code;
Original file line number Diff line number Diff line change
@@ -179,14 +179,8 @@ public ResponseEntity<Token> updateToken(String id, TokenName tokenName) {
@AuditEventMethod(event = RestApiAuditEvent.DELETE_TOKEN)
@Override
public ResponseEntity<Void> deleteToken(String id) {
try {
tokenService.deleteToken(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
} catch (TokenNotFoundException e) {
throw new ResourceNotFoundException(e);
} catch (ActionNotPossibleException e) {
throw new ConflictException(e);
}
tokenService.deleteToken(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@PreAuthorize("hasAuthority('GENERATE_KEY')")
Original file line number Diff line number Diff line change
@@ -29,6 +29,8 @@

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.niis.xroad.common.exception.DataIntegrityException;
import org.niis.xroad.common.exception.NotFoundException;
import org.niis.xroad.common.exception.ServiceException;
import org.niis.xroad.restapi.config.audit.AuditDataHelper;
import org.niis.xroad.restapi.config.audit.RestApiAuditProperty;
@@ -50,8 +52,10 @@
import java.util.function.Predicate;

import static java.util.stream.Collectors.toList;
import static org.niis.xroad.common.exception.util.CommonDeviationMessage.ACTION_NOT_POSSIBLE;
import static org.niis.xroad.common.exception.util.CommonDeviationMessage.INTERNAL_ERROR;
import static org.niis.xroad.common.exception.util.CommonDeviationMessage.TOKEN_FETCH_FAILED;
import static org.niis.xroad.common.exception.util.CommonDeviationMessage.TOKEN_NOT_FOUND;
import static org.niis.xroad.common.exception.util.CommonDeviationMessage.TOKEN_PIN_INCORRECT;

/**
@@ -403,24 +407,18 @@ public void updateSoftwareTokenPin(String tokenId, String oldPin, String newPin)
* Delete inactive token
*
* @param id ID of the token
* @throws TokenNotFoundException token not found
* @throws ActionNotPossibleException if deletion was not possible
*/
public void deleteToken(String id) throws TokenNotFoundException, ActionNotPossibleException {
TokenInfo tokenInfo = getToken(id);

auditDataHelper.put(tokenInfo);

possibleActionsRuleEngine.requirePossibleTokenAction(PossibleActionEnum.DELETE_TOKEN, tokenInfo);

public void deleteToken(String id) {
try {
TokenInfo tokenInfo = getToken(id);
auditDataHelper.put(tokenInfo);

possibleActionsRuleEngine.requirePossibleTokenAction(PossibleActionEnum.DELETE_TOKEN, tokenInfo);
signerProxyFacade.deleteToken(id);
} catch (SignerException e) {
if (e.isCausedByTokenNotFound()) {
throw new TokenNotFoundException(e);
} else {
throw e;
}
} catch (TokenNotFoundException e) {
throw new NotFoundException(TOKEN_NOT_FOUND, e);
} catch (ActionNotPossibleException e) {
throw new DataIntegrityException(ACTION_NOT_POSSIBLE, e);
} catch (CodedException ce) {
throw ce;
} catch (Exception other) {
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.niis.xroad.common.exception.NotFoundException;
import org.niis.xroad.restapi.config.audit.AuditDataHelper;
import org.niis.xroad.securityserver.restapi.dto.TokenInitStatusInfo;
import org.niis.xroad.securityserver.restapi.util.TokenTestUtils;
@@ -83,26 +84,21 @@ public void setup() throws Exception {
doAnswer(invocation -> {
Object[] args = invocation.getArguments();
String tokenId = (String) args[0];
if (WRONG_SOFTTOKEN_PIN_TOKEN_ID.equals(tokenId)) {
throw new SignerException(ErrorCodes.X_PIN_INCORRECT);
} else if (WRONG_HSM_PIN_TOKEN_ID.equals(tokenId)) {
throw new SignerException(ErrorCodes.X_LOGIN_FAILED, SignerException.CKR_PIN_INCORRECT_MESSAGE);
} else if (UNKNOWN_LOGIN_FAIL_TOKEN_ID.equals(tokenId)) {
throw new SignerException(ErrorCodes.X_LOGIN_FAILED, "dont know what happened");
} else if (TOKEN_NOT_FOUND_TOKEN_ID.equals(tokenId)) {
throw new SignerException(ErrorCodes.X_TOKEN_NOT_FOUND, "did not find it");
} else if (UNRECOGNIZED_FAULT_CODE_TOKEN_ID.equals(tokenId)) {
throw new SignerException("foo", "bar");
} else {
log.debug("activate successful");
switch (tokenId) {
case WRONG_SOFTTOKEN_PIN_TOKEN_ID -> throw new SignerException(ErrorCodes.X_PIN_INCORRECT);
case UNKNOWN_LOGIN_FAIL_TOKEN_ID ->
throw new SignerException(ErrorCodes.X_LOGIN_FAILED, "dont know what happened");
case TOKEN_NOT_FOUND_TOKEN_ID ->
throw new SignerException(ErrorCodes.X_TOKEN_NOT_FOUND, "did not find it");
case UNRECOGNIZED_FAULT_CODE_TOKEN_ID -> throw new SignerException("foo", "bar");
case null, default -> log.debug("activate successful");
}
return null;
}).when(signerProxyFacade).activateToken(any(), any());

doAnswer(invocation -> {
Object[] args = invocation.getArguments();
String oldPin = new String((char[]) args[1]);
String newPin = new String((char[]) args[2]);
if (WRONG_SOFTTOKEN_PIN_TOKEN_ID.equals(oldPin)) {
throw new SignerException(ErrorCodes.X_PIN_INCORRECT);
} else {
@@ -228,13 +224,25 @@ public void updateTokenFriendlyName() throws Exception {
assertEquals("friendly-neighborhood", token.getFriendlyName());
}

@Test
public void deleteToken() throws Exception {
TokenInfo token = tokenService.getToken(GOOD_TOKEN_ID);
assertEquals(GOOD_TOKEN_NAME, token.getFriendlyName());
tokenService.deleteToken(GOOD_TOKEN_ID);
}

@Test(expected = TokenNotFoundException.class)
public void updateNonExistingTokenFriendlyName() throws Exception {
tokenService.updateTokenFriendlyName(TOKEN_NOT_FOUND_TOKEN_ID, "new-name");
}

@Test(expected = NotFoundException.class)
public void deleteNonExistingToken() {
tokenService.deleteToken(TOKEN_NOT_FOUND_TOKEN_ID);
}

@Test
public void getUnknownSoftwareTokenInitStatus() throws Exception {
public void getUnknownSoftwareTokenInitStatus() {
when(signerProxyFacade.getTokens()).thenThrow(new SignerException("Error"));
TokenInitStatusInfo tokenStatus = tokenService.getSoftwareTokenInitStatus();
assertEquals(TokenInitStatusInfo.UNKNOWN, tokenStatus);
Original file line number Diff line number Diff line change
@@ -163,7 +163,7 @@ public static List<TokenInfo> getTokens() throws SignerException {
() -> RpcSignerClient.execute(ctx -> ctx.getBlockingTokenService().listTokens(Empty.newBuilder().build()))
.getTokensList().stream()
.map(TokenInfo::new)
.collect(Collectors.toList())
.toList()
);
}

@@ -929,7 +929,7 @@ public static List<CertificateInfo> getMemberCerts(ClientId memberId) throws Sig
.build()))
.getCertsList().stream()
.map(CertificateInfo::new)
.collect(Collectors.toList())
.toList()
);
}

Loading