Search in sources :

Example 6 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project che-server by eclipse-che.

the class OpenShiftProjectTest method testOpenShiftProjectPreparingRoleBindingWhenProjectDoesNotExistWithCheServerSA.

@Test(dependsOnMethods = "testOpenShiftProjectPreparingWhenProjectDoesNotExistWithCheServerSA")
public void testOpenShiftProjectPreparingRoleBindingWhenProjectDoesNotExistWithCheServerSA() throws Exception {
    // given
    prepareNamespaceGet(PROJECT_NAME);
    Resource resource = prepareProjectResource(PROJECT_NAME);
    doThrow(new KubernetesClientException("error", 403, null)).when(resource).get();
    final MixedOperation mixedOperation = mock(MixedOperation.class);
    final NonNamespaceOperation namespaceOperation = mock(NonNamespaceOperation.class);
    doReturn(mixedOperation).when(openShiftCheServerClient).serviceAccounts();
    when(mixedOperation.inNamespace(anyString())).thenReturn(namespaceOperation);
    when(namespaceOperation.withName(anyString())).thenReturn(serviceAccountResource);
    when(serviceAccountResource.get()).thenReturn(mock(ServiceAccount.class));
    doReturn(projectRequestOperation).when(openShiftCheServerClient).projectrequests();
    // doReturn(metadataNested).when(metadataNested).withName(anyString());
    when(openShiftCheServerClient.roleBindings()).thenReturn(mixedRoleBindingOperation);
    lenient().when(mixedRoleBindingOperation.inNamespace(anyString())).thenReturn(nonNamespaceRoleBindingOperation);
    when(openShiftClient.currentUser()).thenReturn(new UserBuilder().withNewMetadata().withName("jdoe").endMetadata().build());
    // when
    openShiftProject.prepare(true, true, Map.of(), Map.of());
    // then
    ArgumentCaptor<RoleBinding> roleBindingArgumentCaptor = ArgumentCaptor.forClass(RoleBinding.class);
    verify(nonNamespaceRoleBindingOperation).createOrReplace(roleBindingArgumentCaptor.capture());
    RoleBinding roleBinding = roleBindingArgumentCaptor.getValue();
    assertNotNull(roleBinding);
    assertEquals(roleBinding.getMetadata().getName(), "admin");
    assertEquals(roleBinding.getRoleRef().getName(), "admin");
    assertEquals(roleBinding.getUserNames(), ImmutableList.of("jdoe"));
}
Also used : ServiceAccount(io.fabric8.kubernetes.api.model.ServiceAccount) Resource(io.fabric8.kubernetes.client.dsl.Resource) RoleBinding(io.fabric8.openshift.api.model.RoleBinding) MixedOperation(io.fabric8.kubernetes.client.dsl.MixedOperation) NonNamespaceOperation(io.fabric8.kubernetes.client.dsl.NonNamespaceOperation) KubernetesClientException(io.fabric8.kubernetes.client.KubernetesClientException) UserBuilder(io.fabric8.openshift.api.model.UserBuilder) Test(org.testng.annotations.Test)

Example 7 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project kubernetes-client by fabric8io.

the class RoleBindingCrudTest method crudTest.

@Test
public void crudTest() {
    RoleBinding roleBinding = new RoleBindingBuilder().withNewMetadata().withName("read-jobs").endMetadata().addToSubjects(0, new SubjectBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("User").withName("jane").withNamespace("default").build()).withRoleRef(new RoleRefBuilder().withApiGroup("rbac.authorization.k8s.io").withKind("Role").withName("job-reader").build()).build();
    // test of creation
    roleBinding = client.rbac().roleBindings().create(roleBinding);
    assertNotNull(roleBinding);
    assertEquals("RoleBinding", roleBinding.getKind());
    assertEquals("rbac.authorization.k8s.io/v1", roleBinding.getApiVersion());
    assertNotNull(roleBinding.getMetadata());
    assertEquals("read-jobs", roleBinding.getMetadata().getName());
    assertNotNull(roleBinding.getSubjects());
    assertEquals(1, roleBinding.getSubjects().size());
    assertEquals("rbac.authorization.k8s.io", roleBinding.getSubjects().get(0).getApiGroup());
    assertEquals("User", roleBinding.getSubjects().get(0).getKind());
    assertEquals("jane", roleBinding.getSubjects().get(0).getName());
    assertEquals("default", roleBinding.getSubjects().get(0).getNamespace());
    assertNotNull(roleBinding.getRoleRef());
    assertEquals("Role", roleBinding.getRoleRef().getKind());
    assertEquals("job-reader", roleBinding.getRoleRef().getName());
    assertEquals("rbac.authorization.k8s.io", roleBinding.getRoleRef().getApiGroup());
    // test of list
    RoleBindingList kubernetesRoleBindingList = client.rbac().roleBindings().list();
    assertNotNull(kubernetesRoleBindingList);
    assertNotNull(kubernetesRoleBindingList.getItems());
    assertEquals(1, kubernetesRoleBindingList.getItems().size());
    assertNotNull(kubernetesRoleBindingList.getItems().get(0));
    assertEquals("RoleBinding", kubernetesRoleBindingList.getItems().get(0).getKind());
    assertEquals("rbac.authorization.k8s.io/v1", kubernetesRoleBindingList.getItems().get(0).getApiVersion());
    assertNotNull(kubernetesRoleBindingList.getItems().get(0).getMetadata());
    assertEquals("read-jobs", kubernetesRoleBindingList.getItems().get(0).getMetadata().getName());
    assertNotNull(kubernetesRoleBindingList.getItems().get(0).getSubjects());
    assertEquals(1, kubernetesRoleBindingList.getItems().get(0).getSubjects().size());
    assertEquals("rbac.authorization.k8s.io", kubernetesRoleBindingList.getItems().get(0).getSubjects().get(0).getApiGroup());
    assertEquals("User", kubernetesRoleBindingList.getItems().get(0).getSubjects().get(0).getKind());
    assertEquals("jane", kubernetesRoleBindingList.getItems().get(0).getSubjects().get(0).getName());
    assertEquals("default", kubernetesRoleBindingList.getItems().get(0).getSubjects().get(0).getNamespace());
    assertNotNull(kubernetesRoleBindingList.getItems().get(0).getRoleRef());
    assertEquals("Role", kubernetesRoleBindingList.getItems().get(0).getRoleRef().getKind());
    assertEquals("job-reader", kubernetesRoleBindingList.getItems().get(0).getRoleRef().getName());
    assertEquals("rbac.authorization.k8s.io", kubernetesRoleBindingList.getItems().get(0).getRoleRef().getApiGroup());
    // test of updation
    roleBinding = client.rbac().roleBindings().withName("read-jobs").edit(r -> new RoleBindingBuilder(r).editSubject(0).withName("jane-new").endSubject().build());
    assertNotNull(roleBinding);
    assertEquals("RoleBinding", roleBinding.getKind());
    assertEquals("rbac.authorization.k8s.io/v1", roleBinding.getApiVersion());
    assertNotNull(roleBinding.getMetadata());
    assertEquals("read-jobs", roleBinding.getMetadata().getName());
    assertNotNull(roleBinding.getSubjects());
    assertEquals(1, roleBinding.getSubjects().size());
    assertEquals("rbac.authorization.k8s.io", roleBinding.getSubjects().get(0).getApiGroup());
    assertEquals("User", roleBinding.getSubjects().get(0).getKind());
    assertEquals("jane-new", roleBinding.getSubjects().get(0).getName());
    assertEquals("default", roleBinding.getSubjects().get(0).getNamespace());
    assertNotNull(roleBinding.getRoleRef());
    assertEquals("Role", roleBinding.getRoleRef().getKind());
    assertEquals("job-reader", roleBinding.getRoleRef().getName());
    assertEquals("rbac.authorization.k8s.io", roleBinding.getRoleRef().getApiGroup());
    // test of deletion
    boolean deleted = client.rbac().roleBindings().delete();
    assertTrue(deleted);
    kubernetesRoleBindingList = client.rbac().roleBindings().list();
    assertEquals(0, kubernetesRoleBindingList.getItems().size());
}
Also used : Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Logger(org.slf4j.Logger) RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) SubjectBuilder(io.fabric8.kubernetes.api.model.rbac.SubjectBuilder) RoleBindingList(io.fabric8.kubernetes.api.model.rbac.RoleBindingList) LoggerFactory(org.slf4j.LoggerFactory) RoleRefBuilder(io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder) Test(org.junit.jupiter.api.Test) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) EnableKubernetesMockClient(io.fabric8.kubernetes.client.server.mock.EnableKubernetesMockClient) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) KubernetesClient(io.fabric8.kubernetes.client.KubernetesClient) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) RoleBindingList(io.fabric8.kubernetes.api.model.rbac.RoleBindingList) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) SubjectBuilder(io.fabric8.kubernetes.api.model.rbac.SubjectBuilder) RoleRefBuilder(io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder) Test(org.junit.jupiter.api.Test)

Example 8 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project kubernetes-client by fabric8io.

the class RoleBindingIT method get.

@Test
public void get() {
    RoleBinding roleBinding = client.rbac().roleBindings().inNamespace(session.getNamespace()).withName("rb-get").get();
    assertNotNull(roleBinding);
    assertEquals("RoleBinding", roleBinding.getKind());
    assertNotNull(roleBinding.getMetadata());
    assertEquals("rb-get", roleBinding.getMetadata().getName());
    assertNotNull(roleBinding.getSubjects());
    assertEquals(1, roleBinding.getSubjects().size());
    assertEquals("rbac.authorization.k8s.io", roleBinding.getSubjects().get(0).getApiGroup());
    assertEquals("User", roleBinding.getSubjects().get(0).getKind());
    assertEquals("jane", roleBinding.getSubjects().get(0).getName());
    assertEquals("default", roleBinding.getSubjects().get(0).getNamespace());
    assertNotNull(roleBinding.getRoleRef());
    assertEquals("Role", roleBinding.getRoleRef().getKind());
    assertEquals("job-reader", roleBinding.getRoleRef().getName());
    assertEquals("rbac.authorization.k8s.io", roleBinding.getRoleRef().getApiGroup());
}
Also used : RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) Test(org.junit.Test)

Example 9 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project kubernetes-client by fabric8io.

the class RoleBindingIT method load.

@Test
public void load() {
    RoleBinding aRoleBinding = client.rbac().roleBindings().inNamespace(session.getNamespace()).load(getClass().getResourceAsStream("/test-kubernetesrolebinding.yml")).get();
    assertNotNull(aRoleBinding);
    assertEquals("RoleBinding", aRoleBinding.getKind());
    assertNotNull(aRoleBinding.getMetadata());
    assertEquals("read-jobs", aRoleBinding.getMetadata().getName());
    assertNotNull(aRoleBinding.getSubjects());
    assertEquals(1, aRoleBinding.getSubjects().size());
    assertEquals("rbac.authorization.k8s.io", aRoleBinding.getSubjects().get(0).getApiGroup());
    assertEquals("User", aRoleBinding.getSubjects().get(0).getKind());
    assertEquals("jane", aRoleBinding.getSubjects().get(0).getName());
    assertEquals("default", aRoleBinding.getSubjects().get(0).getNamespace());
    assertNotNull(aRoleBinding.getRoleRef());
    assertEquals("Role", aRoleBinding.getRoleRef().getKind());
    assertEquals("job-reader", aRoleBinding.getRoleRef().getName());
    assertEquals("rbac.authorization.k8s.io", aRoleBinding.getRoleRef().getApiGroup());
}
Also used : RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) Test(org.junit.Test)

Example 10 with RoleBinding

use of io.fabric8.kubernetes.api.model.rbac.RoleBinding in project kubernetes-client by fabric8io.

the class RoleBindingIT method list.

@Test
public void list() {
    RoleBindingList roleBindingList = client.rbac().roleBindings().inNamespace(session.getNamespace()).withLabels(Collections.singletonMap("type", "io.fabric8.roleBindingIT")).list();
    assertNotNull(roleBindingList);
    assertNotNull(roleBindingList.getItems());
    assertEquals(1, roleBindingList.getItems().size());
    assertNotNull(roleBindingList.getItems().get(0));
    assertEquals("RoleBinding", roleBindingList.getItems().get(0).getKind());
    assertNotNull(roleBindingList.getItems().get(0).getMetadata());
    assertEquals("rb-list", roleBindingList.getItems().get(0).getMetadata().getName());
    assertNotNull(roleBindingList.getItems().get(0).getSubjects());
    assertEquals(1, roleBindingList.getItems().get(0).getSubjects().size());
    assertEquals("rbac.authorization.k8s.io", roleBindingList.getItems().get(0).getSubjects().get(0).getApiGroup());
    assertEquals("User", roleBindingList.getItems().get(0).getSubjects().get(0).getKind());
    assertEquals("jane", roleBindingList.getItems().get(0).getSubjects().get(0).getName());
    assertEquals("default", roleBindingList.getItems().get(0).getSubjects().get(0).getNamespace());
    assertNotNull(roleBindingList.getItems().get(0).getRoleRef());
    assertEquals("Role", roleBindingList.getItems().get(0).getRoleRef().getKind());
    assertEquals("job-reader", roleBindingList.getItems().get(0).getRoleRef().getName());
}
Also used : RoleBindingList(io.fabric8.kubernetes.api.model.rbac.RoleBindingList) Test(org.junit.Test)

Aggregations

RoleBinding (io.fabric8.kubernetes.api.model.rbac.RoleBinding)33 RoleRefBuilder (io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder)20 RoleBinding (io.fabric8.openshift.api.model.RoleBinding)19 Test (org.junit.jupiter.api.Test)19 RoleBindingBuilder (io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder)16 RoleRef (io.fabric8.kubernetes.api.model.rbac.RoleRef)16 SubjectBuilder (io.fabric8.kubernetes.api.model.rbac.SubjectBuilder)14 RoleBindingBuilder (io.fabric8.openshift.api.model.RoleBindingBuilder)13 Role (io.fabric8.kubernetes.api.model.rbac.Role)10 Subject (io.fabric8.kubernetes.api.model.rbac.Subject)10 KubernetesClientException (io.fabric8.kubernetes.client.KubernetesClientException)10 OpenShiftClient (io.fabric8.openshift.client.OpenShiftClient)10 List (java.util.List)8 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ServiceAccount (io.fabric8.kubernetes.api.model.ServiceAccount)6 Kafka (io.strimzi.api.kafka.model.Kafka)6 KafkaBuilder (io.strimzi.api.kafka.model.KafkaBuilder)6 CertManager (io.strimzi.certs.CertManager)6 KubernetesVersion (io.strimzi.operator.KubernetesVersion)6 PlatformFeaturesAvailability (io.strimzi.operator.PlatformFeaturesAvailability)6