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

Fix pwd for service account, delete acl #1146

Merged
merged 2 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -281,7 +281,8 @@ List<Map<String, String>> getActivityLogForLastDays(

String updateConnectorRequestStatus(KafkaConnectorRequest topicRequest, String approver);

String updateAclRequest(AclRequests aclRequests, String approver, String jsonParams);
String updateAclRequest(
AclRequests aclRequests, String approver, String jsonParams, boolean saveReqOnly);

void updateNewUserRequest(String username, String approver, boolean isApprove);

Expand Down Expand Up @@ -385,4 +386,6 @@ List<Map<String, String>> getActivityLogForLastDays(
List<KwProperties> getKwProperties();

List<KwClusters> getClusters();

String updateJsonParams(String jsonParams, Integer req_no, int tenantId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -687,8 +687,9 @@ public String declineAclRequest(AclRequests aclReq, String approver) {
return jdbcUpdateHelper.declineAclRequest(aclReq, approver);
}

public String updateAclRequest(AclRequests aclReq, String approver, String jsonParams) {
return jdbcUpdateHelper.updateAclRequest(aclReq, approver, jsonParams);
public String updateAclRequest(
AclRequests aclReq, String approver, String jsonParams, boolean saveReqOnly) {
return jdbcUpdateHelper.updateAclRequest(aclReq, approver, jsonParams, saveReqOnly);
}

@Override
Expand Down Expand Up @@ -929,4 +930,9 @@ public List<KwProperties> getKwProperties() {
public List<KwClusters> getClusters() {
return jdbcSelectHelper.getClusters();
}

@Override
public String updateJsonParams(String jsonParams, Integer req_no, int tenantId) {
return jdbcUpdateHelper.updateJsonParams(jsonParams, req_no, tenantId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.springframework.beans.BeanUtils.copyProperties;

import io.aiven.klaw.dao.Acl;
import io.aiven.klaw.dao.AclID;
import io.aiven.klaw.dao.AclRequests;
import io.aiven.klaw.dao.KafkaConnectorRequest;
import io.aiven.klaw.dao.KwKafkaConnector;
Expand All @@ -23,6 +24,7 @@
import io.aiven.klaw.model.enums.NewUserStatus;
import io.aiven.klaw.model.enums.RequestOperationType;
import io.aiven.klaw.model.enums.RequestStatus;
import io.aiven.klaw.repository.AclRepo;
import io.aiven.klaw.repository.AclRequestsRepo;
import io.aiven.klaw.repository.KwKafkaConnectorRepo;
import io.aiven.klaw.repository.KwKafkaConnectorRequestsRepo;
Expand Down Expand Up @@ -63,6 +65,9 @@ public class UpdateDataJdbc {
@Autowired(required = false)
private AclRequestsRepo aclRequestsRepo;

@Autowired(required = false)
private AclRepo aclRepo;

@Autowired(required = false)
private UserInfoRepo userInfoRepo;

Expand Down Expand Up @@ -249,12 +254,31 @@ private void updateConnectorSOT(List<KwKafkaConnector> topics, String topicId) {
});
}

public String updateAclRequest(AclRequests aclReq, String approver, String jsonParams) {
public String updateJsonParams(String jsonParams, int reqNo, int tenantId) {
AclID aclID = new AclID();
aclID.setReq_no(reqNo);
aclID.setTenantId(tenantId);

Optional<Acl> acl = aclRepo.findById(aclID);
acl.ifPresent(
value -> {
value.setJsonParams(jsonParams);
aclRepo.save(acl.get());
});

return ApiResultStatus.SUCCESS.value;
}

public String updateAclRequest(
AclRequests aclReq, String approver, String jsonParams, boolean saveReqOnly) {
log.debug("updateAclRequest {} {}", aclReq.getTopicname(), approver);
aclReq.setApprover(approver);
aclReq.setRequestStatus(RequestStatus.APPROVED.value);
aclReq.setApprovingtime(new Timestamp(System.currentTimeMillis()));
aclRequestsRepo.save(aclReq);
if (saveReqOnly) {
return ApiResultStatus.SUCCESS.value;
}

return processMultipleAcls(aclReq, jsonParams);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private String handleAclRequestClusterApiResponse(
updateServiceAccountsForTeam(aclReq, tenantId);
}
}
updateAclReqStatus = dbHandle.updateAclRequest(aclReq, userDetails, jsonParams);
updateAclReqStatus = dbHandle.updateAclRequest(aclReq, userDetails, jsonParams, false);
} else {
updateAclReqStatus = ApiResultStatus.FAILURE.value;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,31 @@ private void approveSyncBackAcls(
} else if (resultAclNullCheck.contains("Acl already exists")) {
logUpdateSyncBackTopics.add(String.format(ACL_SYNC_ERR_103, aclFound.getTopicname()));
} else {
Env env =
manageDatabase
.getHandleDbRequests()
.getEnvDetails(syncBackAcls.getSourceEnv(), tenantId);
KwClusters kwClusters =
manageDatabase
.getClusters(KafkaClustersType.of(env.getType()), tenantId)
.get(env.getClusterId());
// Update aivenaclid in klaw metadata
if (kwClusters.getKafkaFlavor().equals(KafkaFlavors.AIVEN_FOR_APACHE_KAFKA.value)) {
String jsonParams = "{}", aivenAclIdKey = "aivenaclid";
if (Objects.requireNonNull(responseBody).isSuccess()) {
Object responseData = responseBody.getData();
if (responseData instanceof Map) {
Map<String, String> dataMap = (Map<String, String>) responseData;
if (dataMap.containsKey(aivenAclIdKey)) {
jsonParams = "{\"" + aivenAclIdKey + "\":\"" + dataMap.get(aivenAclIdKey) + "\"}";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would we be better creating a POJO for this and converting it using an object mapper?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

}
}
}

manageDatabase
.getHandleDbRequests()
.updateJsonParams(jsonParams, aclFound.getReq_no(), tenantId);
}
if (!Objects.equals(syncBackAcls.getSourceEnv(), syncBackAcls.getTargetEnv())) {
logUpdateSyncBackTopics.add(String.format(ACL_SYNC_ERR_104, aclFound.getTopicname()));
// Create request
Expand All @@ -228,7 +253,7 @@ private void approveSyncBackAcls(
String emptyJsonParams = "{}";
manageDatabase
.getHandleDbRequests()
.updateAclRequest(aclReq, userName, emptyJsonParams);
.updateAclRequest(aclReq, userName, emptyJsonParams, true);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/resources/static/js/browseAcls.js
Original file line number Diff line number Diff line change
Expand Up @@ -822,7 +822,7 @@ app.controller("browseAclsCtrl", function($scope, $http, $location, $window) {
title: "Success: ",
text: "Password is displayed above !!"
});
$scope.alert = "Password : " + output.data.password;
$scope.alert = "Password : " + output.password;
}

}).error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,16 @@ public void updateAclRequest() {
when(insertDataJdbcHelper.insertIntoAclsSOT(any(), eq(false)))
.thenReturn(ApiResultStatus.SUCCESS.value);
String result =
updateData.updateAclRequest(utilMethods.getAclRequestCreate("testtopic"), "uiuser2", "{}");
updateData.updateAclRequest(
utilMethods.getAclRequestCreate("testtopic"), "uiuser2", "{}", false);
assertThat(result).isEqualTo(ApiResultStatus.SUCCESS.value);
}

@Test
public void updateAclRequest1() {
when(deleteDataJdbcHelper.deletePrevAclRecs(any())).thenReturn(ApiResultStatus.SUCCESS.value);
String result =
updateData.updateAclRequest(utilMethods.getAclRequest("testtopic"), "uiuser2", "{}");
updateData.updateAclRequest(utilMethods.getAclRequest("testtopic"), "uiuser2", "{}", false);
assertThat(result).isEqualTo(ApiResultStatus.SUCCESS.value);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public void approveAclRequests() throws KlawException {
ApiResponse.builder().success(true).message(ApiResultStatus.SUCCESS.value).build();
when(clusterApiService.approveAclRequests(any(), anyInt()))
.thenReturn(new ResponseEntity<>(apiResponse, HttpStatus.OK));
when(handleDbRequests.updateAclRequest(any(), any(), anyString()))
when(handleDbRequests.updateAclRequest(any(), any(), anyString(), anyBoolean()))
.thenReturn(ApiResultStatus.SUCCESS.value);
when(commonUtilsService.getEnvsFromUserId(anyString()))
.thenReturn(new HashSet<>(Collections.singletonList("1")));
Expand Down Expand Up @@ -518,7 +518,7 @@ public void approveAclRequestsWithAivenAcl() throws KlawException {
.build();
when(clusterApiService.approveAclRequests(any(), anyInt()))
.thenReturn(new ResponseEntity<>(apiResponse, HttpStatus.OK));
when(handleDbRequests.updateAclRequest(any(), any(), anyString()))
when(handleDbRequests.updateAclRequest(any(), any(), anyString(), anyBoolean()))
.thenReturn(ApiResultStatus.SUCCESS.value);
when(commonUtilsService.getEnvsFromUserId(anyString()))
.thenReturn(new HashSet<>(Collections.singletonList("1")));
Expand Down Expand Up @@ -581,7 +581,7 @@ public void approveAclRequestsFailure2() throws KlawException {
ApiResponse.builder().success(true).message(ApiResultStatus.SUCCESS.value).build();
when(clusterApiService.approveAclRequests(any(), anyInt()))
.thenReturn(new ResponseEntity<>(apiResponse, HttpStatus.OK));
when(handleDbRequests.updateAclRequest(any(), any(), anyString()))
when(handleDbRequests.updateAclRequest(any(), any(), anyString(), anyBoolean()))
.thenThrow(new RuntimeException("Error"));

ApiResponse apiResp = aclControllerService.approveAclRequests(req_no);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@
import io.aiven.klaw.model.AclInfo;
import io.aiven.klaw.model.ApiResponse;
import io.aiven.klaw.model.SyncAclUpdates;
import io.aiven.klaw.model.SyncBackAcls;
import io.aiven.klaw.model.enums.AclType;
import io.aiven.klaw.model.enums.ApiResultStatus;
import io.aiven.klaw.model.enums.KafkaClustersType;
import io.aiven.klaw.model.enums.KafkaFlavors;
import io.aiven.klaw.model.enums.KafkaSupportedProtocol;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -36,6 +39,8 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
Expand Down Expand Up @@ -203,6 +208,56 @@ public void getAclsSyncTrue2() throws KlawException {
assertThat(aclList).isEmpty();
}

@Test
@Order(8)
public void updateSyncBackAcls() throws KlawException {
String envSelected = "1";
Map<String, String> aivenAclId = new HashMap<>();
aivenAclId.put("aivenaclid", "test1234");
ApiResponse apiResponse =
ApiResponse.builder()
.success(true)
.data(aivenAclId)
.message(ApiResultStatus.SUCCESS.value)
.build();
ResponseEntity<ApiResponse> apiResponseResponseEntity =
new ResponseEntity<>(apiResponse, HttpStatus.OK);
stubUserInfo();
when(commonUtilsService.getTenantId(anyString())).thenReturn(101);
when(manageDatabase.getKafkaEnvList(anyInt())).thenReturn(utilMethods.getEnvLists());
Env env = utilMethods.getEnvLists().get(0);
env.setType("kafka");
when(handleDbRequests.getEnvDetails(anyString(), anyInt())).thenReturn(env);

when(manageDatabase.getClusters(any(KafkaClustersType.class), anyInt()))
.thenReturn(clustersHashMap);
when(clustersHashMap.get(any())).thenReturn(kwClusters);
when(kwClusters.getKafkaFlavor()).thenReturn(KafkaFlavors.AIVEN_FOR_APACHE_KAFKA.value);
when(commonUtilsService.isNotAuthorizedUser(any(), any())).thenReturn(false);
when(handleDbRequests.getSyncAclsFromReqNo(anyInt(), anyInt()))
.thenReturn(getAclsSOT0().get(0));
when(clusterApiService.approveAclRequests(any(), anyInt()))
.thenReturn(apiResponseResponseEntity);
when(handleDbRequests.updateJsonParams(anyString(), anyInt(), anyInt()))
.thenReturn(ApiResultStatus.SUCCESS.value);

SyncBackAcls syncBackAcls = getSyncBackAcls(envSelected);

ApiResponse apiResponseActual = aclSyncControllerService.updateSyncBackAcls(syncBackAcls);
assertThat(apiResponseActual.isSuccess()).isTrue();
}

private static SyncBackAcls getSyncBackAcls(String envSelected) {
SyncBackAcls syncBackAcls = new SyncBackAcls();
String[] aclIds = new String[1];
aclIds[0] = "1001";
syncBackAcls.setAclIds(aclIds);
syncBackAcls.setTypeOfSync("SELECTED_ACLS");
syncBackAcls.setSourceEnv(envSelected);
syncBackAcls.setTargetEnv(envSelected);
return syncBackAcls;
}

private List<Team> getAvailableTeams() {

Team team1 = new Team();
Expand Down