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);
}
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);
}
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);
}
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());
}
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());
}
Aggregations