Search in sources :

Example 1 with RoleBindingBuilder

use of io.fabric8.openshift.api.model.RoleBindingBuilder in project che-server by eclipse-che.

the class KubernetesWorkspaceServiceAccountTest method shouldProvisionSARolesEvenIfItAlreadyExists.

@Test
public void shouldProvisionSARolesEvenIfItAlreadyExists() throws Exception {
    ServiceAccountBuilder serviceAccountBuilder = new ServiceAccountBuilder().withNewMetadata().withName(SA_NAME).endMetadata();
    RoleBuilder roleBuilder = new RoleBuilder().withNewMetadata().withName("foo").endMetadata();
    RoleBindingBuilder roleBindingBuilder = new RoleBindingBuilder().withNewMetadata().withName("foo-builder").endMetadata();
    // pre-create SA and some roles
    k8sClient.serviceAccounts().inNamespace(NAMESPACE).createOrReplace(serviceAccountBuilder.build());
    k8sClient.rbac().roles().inNamespace(NAMESPACE).create(roleBuilder.build());
    k8sClient.rbac().roleBindings().inNamespace(NAMESPACE).create(roleBindingBuilder.build());
    // when
    serviceAccount.prepare();
    // then
    // make sure more roles added
    RoleList rl = k8sClient.rbac().roles().inNamespace(NAMESPACE).list();
    assertTrue(rl.getItems().size() > 1);
    RoleBindingList rbl = k8sClient.rbac().roleBindings().inNamespace(NAMESPACE).list();
    assertTrue(rbl.getItems().size() > 1);
}
Also used : RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) RoleList(io.fabric8.kubernetes.api.model.rbac.RoleList) RoleBindingList(io.fabric8.kubernetes.api.model.rbac.RoleBindingList) ServiceAccountBuilder(io.fabric8.kubernetes.api.model.ServiceAccountBuilder) RoleBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBuilder) Test(org.testng.annotations.Test)

Example 2 with RoleBindingBuilder

use of io.fabric8.openshift.api.model.RoleBindingBuilder in project che-server by eclipse-che.

the class UserPermissionConfiguratorTest method keepOtherClusterRoles.

@Test
public void keepOtherClusterRoles() throws InfrastructureException {
    // given - some other binding in place
    client.rbac().roleBindings().inNamespace(TEST_NAMESPACE_NAME).create(new RoleBindingBuilder().withNewMetadata().withName("othercr").endMetadata().withSubjects(new Subject("blabol", "blabol", "blabol", "blabol")).withNewRoleRef().withName("blabol").endRoleRef().build());
    // when
    configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
    // then
    var roleBindings = client.rbac().roleBindings().inNamespace(TEST_NAMESPACE_NAME);
    Assert.assertEquals(roleBindings.list().getItems().size(), 3);
}
Also used : RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) Subject(io.fabric8.kubernetes.api.model.rbac.Subject) Test(org.testng.annotations.Test)

Example 3 with RoleBindingBuilder

use of io.fabric8.openshift.api.model.RoleBindingBuilder in project che-server by eclipse-che.

the class UserPermissionConfiguratorTest method replaceExistingBindingsWithSameName.

@Test
public void replaceExistingBindingsWithSameName() throws InfrastructureException {
    // given - cr1 binding already exists
    client.rbac().roleBindings().inNamespace(TEST_NAMESPACE_NAME).create(new RoleBindingBuilder().withNewMetadata().withName("cr1").endMetadata().withSubjects(new Subject("blabol", "blabol", "blabol", "blabol")).withNewRoleRef().withName("blabol").endRoleRef().build());
    // when
    configurator.configure(namespaceResolutionContext, TEST_NAMESPACE_NAME);
    // then
    var roleBindings = client.rbac().roleBindings().inNamespace(TEST_NAMESPACE_NAME);
    Assert.assertEquals(roleBindings.list().getItems().size(), 2);
    var cr1 = roleBindings.withName("cr1").get();
    Assert.assertEquals(cr1.getRoleRef().getName(), "cr1");
    Assert.assertEquals(cr1.getSubjects().size(), 1);
    Assert.assertEquals(cr1.getSubjects().get(0).getName(), TEST_USERNAME);
    Assert.assertEquals(cr1.getSubjects().get(0).getNamespace(), TEST_NAMESPACE_NAME);
}
Also used : RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) Subject(io.fabric8.kubernetes.api.model.rbac.Subject) Test(org.testng.annotations.Test)

Example 4 with RoleBindingBuilder

use of io.fabric8.openshift.api.model.RoleBindingBuilder 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 5 with RoleBindingBuilder

use of io.fabric8.openshift.api.model.RoleBindingBuilder in project kubernetes-client by fabric8io.

the class RoleBindingIT method update.

@Test
public void update() {
    RoleBinding roleBinding = client.rbac().roleBindings().inNamespace(session.getNamespace()).withName("rb-update").edit(r -> new RoleBindingBuilder(r).editSubject(0).withName("jane-new").endSubject().build());
    assertNotNull(roleBinding);
    assertEquals("RoleBinding", roleBinding.getKind());
    assertNotNull(roleBinding.getMetadata());
    assertEquals("rb-update", 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());
}
Also used : RoleBindingBuilder(io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder) RoleBinding(io.fabric8.kubernetes.api.model.rbac.RoleBinding) Test(org.junit.Test)

Aggregations

RoleBindingBuilder (io.fabric8.kubernetes.api.model.rbac.RoleBindingBuilder)23 RoleBindingBuilder (io.fabric8.openshift.api.model.RoleBindingBuilder)13 Test (org.junit.jupiter.api.Test)13 RoleRefBuilder (io.fabric8.kubernetes.api.model.rbac.RoleRefBuilder)10 Subject (io.fabric8.kubernetes.api.model.rbac.Subject)10 SubjectBuilder (io.fabric8.kubernetes.api.model.rbac.SubjectBuilder)10 RoleBinding (io.fabric8.openshift.api.model.RoleBinding)10 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)7 RoleBinding (io.fabric8.kubernetes.api.model.rbac.RoleBinding)7 RoleRef (io.fabric8.kubernetes.api.model.rbac.RoleRef)6 Test (org.testng.annotations.Test)6 RoleBuilder (io.fabric8.kubernetes.api.model.rbac.RoleBuilder)4 RoleBindingList (io.fabric8.kubernetes.api.model.rbac.RoleBindingList)3 KubernetesClient (io.fabric8.kubernetes.client.KubernetesClient)3 ConfigMap (io.fabric8.kubernetes.api.model.ConfigMap)2 ConfigMapBuilder (io.fabric8.kubernetes.api.model.ConfigMapBuilder)2 HasMetadata (io.fabric8.kubernetes.api.model.HasMetadata)2 ServiceAccountBuilder (io.fabric8.kubernetes.api.model.ServiceAccountBuilder)2 ClusterRoleBindingBuilder (io.fabric8.kubernetes.api.model.rbac.ClusterRoleBindingBuilder)2 Role (io.fabric8.kubernetes.api.model.rbac.Role)2