Search in sources :

Example 11 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser in project cloud-pipeline by epam.

the class UserDaoTest method testUserCRUDWithAttributes.

@Test
public void testUserCRUDWithAttributes() {
    Map<String, String> attributes = new HashMap<>();
    attributes.put(ATTRIBUTES_KEY, ATTRIBUTES_VALUE);
    PipelineUser user = new PipelineUser();
    user.setUserName(TEST_USER1);
    user.setAttributes(attributes);
    PipelineUser savedUser = userDao.createUser(user, Arrays.asList(DefaultRoles.ROLE_ADMIN.getId(), DefaultRoles.ROLE_USER.getId()));
    Assert.assertNotNull(savedUser);
    Assert.assertNotNull(savedUser.getId());
    Assert.assertNotNull(savedUser.getAttributes());
    Collection<PipelineUser> users = userDao.loadAllUsers();
    Assert.assertFalse(users.isEmpty());
    Assert.assertTrue(users.stream().anyMatch(u -> u.getId().equals(savedUser.getId()) && assertUserAttributes(attributes, u.getAttributes())));
    PipelineUser userById = userDao.loadUserById(savedUser.getId());
    Assert.assertEquals(savedUser.getId(), userById.getId());
    Assert.assertTrue(assertUserAttributes(attributes, userById.getAttributes()));
    PipelineUser userByName = userDao.loadUserByName(TEST_USER1.toUpperCase());
    Assert.assertEquals(savedUser.getId(), userByName.getId());
    Assert.assertTrue(assertUserAttributes(attributes, userByName.getAttributes()));
    savedUser.setUserName(TEST_USER2);
    userDao.updateUser(savedUser);
    PipelineUser userByChangedName = userDao.loadUserByName(TEST_USER2);
    Assert.assertEquals(savedUser.getId(), userByChangedName.getId());
    Assert.assertTrue(assertUserAttributes(attributes, userByChangedName.getAttributes()));
    userDao.deleteUserRoles(savedUser.getId());
    userDao.deleteUser(savedUser.getId());
    Assert.assertNull(userDao.loadUserById(savedUser.getId()));
    Collection<PipelineUser> usersAfterDeletion = userDao.loadAllUsers();
    Assert.assertTrue(usersAfterDeletion.stream().noneMatch(u -> u.getId().equals(savedUser.getId())));
}
Also used : Arrays(java.util.Arrays) DataStorageDao(com.epam.pipeline.dao.datastorage.DataStorageDao) Collection(java.util.Collection) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) HashMap(java.util.HashMap) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) Map(java.util.Map) ObjectCreatorUtils(com.epam.pipeline.manager.ObjectCreatorUtils) S3bucketDataStorage(com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage) Assert(org.junit.Assert) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Collections(java.util.Collections) Role(com.epam.pipeline.entity.user.Role) Transactional(org.springframework.transaction.annotation.Transactional) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) HashMap(java.util.HashMap) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest)

Example 12 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser in project cloud-pipeline by epam.

the class UserDaoTest method testUserCRUD.

@Test
public void testUserCRUD() {
    PipelineUser user = new PipelineUser();
    user.setUserName(TEST_USER1);
    PipelineUser savedUser = userDao.createUser(user, Arrays.asList(DefaultRoles.ROLE_ADMIN.getId(), DefaultRoles.ROLE_USER.getId()));
    Assert.assertNotNull(savedUser);
    Assert.assertNotNull(savedUser.getId());
    Collection<PipelineUser> users = userDao.loadAllUsers();
    Assert.assertFalse(users.isEmpty());
    Assert.assertTrue(users.stream().anyMatch(u -> u.getId().equals(savedUser.getId())));
    PipelineUser userById = userDao.loadUserById(savedUser.getId());
    Assert.assertEquals(savedUser.getId(), userById.getId());
    PipelineUser userByName = userDao.loadUserByName(TEST_USER1.toUpperCase());
    Assert.assertEquals(savedUser.getId(), userByName.getId());
    savedUser.setUserName(TEST_USER2);
    userDao.updateUser(savedUser);
    PipelineUser userByChangedName = userDao.loadUserByName(TEST_USER2);
    Assert.assertEquals(savedUser.getId(), userByChangedName.getId());
    List<PipelineUser> loadedUsers = userDao.loadUsersByNames(Collections.singletonList(TEST_USER2));
    Assert.assertFalse(loadedUsers.isEmpty());
    userDao.updateUserRoles(savedUser, Collections.singletonList(DefaultRoles.ROLE_USER.getId()));
    PipelineUser userUpdatedRoles = userDao.loadUserByName(TEST_USER2);
    Assert.assertEquals(1, userUpdatedRoles.getRoles().size());
    Assert.assertEquals(DefaultRoles.ROLE_USER.name(), userUpdatedRoles.getRoles().get(0).getName());
    userDao.deleteUserRoles(savedUser.getId());
    userDao.deleteUser(savedUser.getId());
    Assert.assertNull(userDao.loadUserById(savedUser.getId()));
    Collection<PipelineUser> usersAfterDeletion = userDao.loadAllUsers();
    Assert.assertTrue(usersAfterDeletion.stream().noneMatch(u -> u.getId().equals(savedUser.getId())));
}
Also used : Arrays(java.util.Arrays) DataStorageDao(com.epam.pipeline.dao.datastorage.DataStorageDao) Collection(java.util.Collection) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) Autowired(org.springframework.beans.factory.annotation.Autowired) Test(org.junit.Test) HashMap(java.util.HashMap) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) CollectionUtils(org.apache.commons.collections4.CollectionUtils) ArrayList(java.util.ArrayList) IsCollectionWithSize.hasSize(org.hamcrest.collection.IsCollectionWithSize.hasSize) Value(org.springframework.beans.factory.annotation.Value) List(java.util.List) Map(java.util.Map) ObjectCreatorUtils(com.epam.pipeline.manager.ObjectCreatorUtils) S3bucketDataStorage(com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage) Assert(org.junit.Assert) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Collections(java.util.Collections) Role(com.epam.pipeline.entity.user.Role) Transactional(org.springframework.transaction.annotation.Transactional) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Test(org.junit.Test) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest)

Example 13 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser in project cloud-pipeline by epam.

the class ToolSecurityPolicyAspect method checkToolBySecurityPolicy.

@Before("@annotation(com.epam.pipeline.manager.docker.scan.ToolSecurityPolicyCheck) && args(runVO,..)")
public void checkToolBySecurityPolicy(JoinPoint joinPoint, PipelineStart runVO) {
    if (runVO.isForce()) {
        PipelineUser user = authManager.getCurrentUser();
        if (user != null && user.isAdmin()) {
            return;
        }
    }
    PipelineConfiguration configuration = configurationManager.getPipelineConfiguration(runVO);
    String tag = toolManager.getTagFromImageName(configuration.getDockerImage());
    Tool tool = toolManager.loadByNameOrId(configuration.getDockerImage());
    if (!clairToolScanManager.checkTool(tool, tag)) {
        throw new ToolExecutionDeniedException(messageHelper.getMessage(MessageConstants.ERROR_TOOL_SECURITY_POLICY_VIOLATION));
    }
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) ToolExecutionDeniedException(com.epam.pipeline.exception.ToolExecutionDeniedException) PipelineConfiguration(com.epam.pipeline.entity.configuration.PipelineConfiguration) Tool(com.epam.pipeline.entity.pipeline.Tool) Before(org.aspectj.lang.annotation.Before)

Example 14 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser in project cloud-pipeline by epam.

the class NotificationManagerTest method setUp.

@Before
@SuppressWarnings("unchecked")
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    admin = new PipelineUser("admin");
    userDao.createUser(admin, Collections.singletonList(DefaultRoles.ROLE_ADMIN.getId()));
    testOwner = new PipelineUser("testOwner");
    userDao.createUser(testOwner, Collections.emptyList());
    testUser1 = new PipelineUser("TestUser1");
    userDao.createUser(testUser1, Collections.emptyList());
    testUser2 = new PipelineUser("TestUser2");
    userDao.createUser(testUser2, Collections.emptyList());
    longRunningTemplate = createTemplate(1L, "testTemplate");
    longRunningSettings = createSettings(NotificationType.LONG_RUNNING, longRunningTemplate.getId(), 1L, 1L);
    issueTemplate = createTemplate(3L, "issueTemplate");
    issueSettings = createSettings(NotificationType.NEW_ISSUE, issueTemplate.getId(), -1L, -1L);
    issueCommentTemplate = createTemplate(4L, "issueCommentTemplate");
    issueCommentSettings = createSettings(NotificationType.NEW_ISSUE_COMMENT, issueCommentTemplate.getId(), -1L, -1L);
    createTemplate(NotificationType.IDLE_RUN.getId(), "idle-run-template");
    createSettings(NotificationType.IDLE_RUN, NotificationType.IDLE_RUN.getId(), -1, -1);
    longRunnging = new PipelineRun();
    DateTime date = DateTime.now(DateTimeZone.UTC).minus(Duration.standardMinutes(6));
    longRunnging.setStartDate(date.toDate());
    longRunnging.setStatus(TaskStatus.RUNNING);
    longRunnging.setOwner(admin.getUserName());
    longRunnging.setPodId("longRunning");
    when(pipelineRunManager.loadRunningAndTerminatedPipelineRuns()).thenReturn(Collections.singletonList(longRunnging));
    when(pipelineRunManager.loadPipelineRun(org.mockito.Matchers.any())).thenReturn(longRunnging);
    when(kubernetesManager.getKubernetesClient()).thenReturn(mockClient);
    ExtendedRole noAdmins = new ExtendedRole();
    noAdmins.setUsers(Collections.emptyList());
    Pod mockPod = mock(Pod.class);
    PodStatus podStatus = new PodStatus(null, null, "hostIp", "", "", "podIp", "bla-bla", "5 o'clock");
    podMetadata = new ObjectMeta();
    podMetadata.setLabels(Collections.emptyMap());
    when(mockPod.getStatus()).thenReturn(podStatus);
    when(mockPod.getMetadata()).thenReturn(podMetadata);
    MixedOperation<Pod, PodList, DoneablePod, PodResource<Pod, DoneablePod>> mockPods = new KubernetesTestUtils.MockPods().mockNamespace(Matchers.any(String.class)).mockWithName(Matchers.any(String.class)).mockPod(mockPod).and().getMockedEntity();
    when(mockClient.pods()).thenReturn(mockPods);
}
Also used : PipelineRun(com.epam.pipeline.entity.pipeline.PipelineRun) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) ObjectMeta(io.fabric8.kubernetes.api.model.ObjectMeta) PodList(io.fabric8.kubernetes.api.model.PodList) PodResource(io.fabric8.kubernetes.client.dsl.PodResource) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) Pod(io.fabric8.kubernetes.api.model.Pod) DoneablePod(io.fabric8.kubernetes.api.model.DoneablePod) DateTime(org.joda.time.DateTime) PodStatus(io.fabric8.kubernetes.api.model.PodStatus) KubernetesTestUtils(com.epam.pipeline.util.KubernetesTestUtils) Before(org.junit.Before)

Example 15 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser in project cloud-pipeline by epam.

the class SMTPNotificationManagerTest method testEmailSending.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testEmailSending() {
    PipelineUser user = new PipelineUser();
    user.setUserName(USER_NAME);
    user.setAdmin(true);
    user.setAttributes(Collections.singletonMap(EMAIL_KEY, EMAIL));
    userRepository.save(user);
    NotificationMessage message = new NotificationMessage();
    NotificationTemplate template = new NotificationTemplate();
    template.setSubject(MESSAGE_SUBJECT);
    template.setBody(MESSAGE_BODY);
    message.setTemplate(template);
    message.setTemplateParameters(Collections.emptyMap());
    message.setToUserId(user.getId());
    message.setCopyUserIds(Collections.singletonList(user.getId()));
    smtpNotificationManager.notifySubscribers(message);
    MimeMessage[] receivedMessages = greenMail.getReceivedMessages();
    assertTrue(receivedMessages.length == 2);
    assertTrue(GreenMailUtil.getBody(receivedMessages[0]).contains(MESSAGE_BODY));
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) MimeMessage(javax.mail.internet.MimeMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Test(org.junit.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest) AbstractSpringTest(com.epam.pipeline.notifier.AbstractSpringTest) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

PipelineUser (com.epam.pipeline.entity.user.PipelineUser)44 Transactional (org.springframework.transaction.annotation.Transactional)23 Test (org.junit.Test)19 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)13 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)13 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)12 ArrayList (java.util.ArrayList)8 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)7 Collections (java.util.Collections)7 List (java.util.List)7 Map (java.util.Map)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 Role (com.epam.pipeline.entity.user.Role)6 AbstractSpringTest (com.epam.pipeline.notifier.AbstractSpringTest)6 Arrays (java.util.Arrays)6 S3bucketDataStorage (com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage)5 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)5 Collection (java.util.Collection)5 CollectionUtils (org.apache.commons.collections4.CollectionUtils)5 DataStorageDao (com.epam.pipeline.dao.datastorage.DataStorageDao)4