Skip to content

Commit

Permalink
Allow hosts added as existing capacity when main environment is not f…
Browse files Browse the repository at this point in the history
…ound (#1672)

Hots added as existing capacities may not have an owning env. For those hosts, allow them to be added.
  • Loading branch information
tylerwowen authored Jul 25, 2024
1 parent cddbb72 commit ad27263
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,36 @@ void testGetMainEnvByHostName_noHostAgent() throws Exception {
sut.insert(expectedEnvBean);

setUpHostBean(TEST_CLUSTER);
setUpHostBean(TEST_CLUSTER + "2");
setUpHostBean(TEST_CLUSTER + "3");

EnvironBean actualEnvironBean = sut.getMainEnvByHostName(HOST_NAME);
assertEnvironBean(expectedEnvBean, actualEnvironBean);
}

@Test
void testGetMainEnvByHostId_noMainEnv() throws Exception {
EnvironBean envBean = EnvironBeanFixture.createRandomEnvironBean();
envBean.setCluster_name(null);
sut.insert(envBean);

setUpHostBean(TEST_CLUSTER);

EnvironBean actualEnvironBean = sut.getMainEnvByHostId(HOST_ID);
assertNull(actualEnvironBean);
}

@Test
void testGetMainEnvByHostName_noMainEnv() throws Exception {
EnvironBean envBean = EnvironBeanFixture.createRandomEnvironBean();
envBean.setCluster_name(null);
sut.insert(envBean);

setUpHostBean(TEST_CLUSTER);

EnvironBean actualEnvironBean = sut.getMainEnvByHostId(HOST_NAME);
assertNull(actualEnvironBean);
}

private void setUpHostAgentBean() throws Exception {
HostAgentBean hostAgentBean = new HostAgentBean();
hostAgentBean.setHost_id(HOST_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,7 @@ void authorize(
CapacityType capacityType,
List<String> capacities)
throws Exception {
if (targetEnvironBean.getSystem_priority() != null
&& targetEnvironBean.getSystem_priority() > 0) {
if (isSidecarEnvironment(targetEnvironBean)) {
// Allow sidecars to add capacity
return;
}
Expand Down Expand Up @@ -272,12 +271,16 @@ private HashSet<AuthZResource> getCapacityMainEnvironments(
throw new InternalServerErrorException(e);
}

if (envBean == null) {
throw new ForbiddenException(
"Failed to get the main environment with capacity name: " + capacity);
if (envBean != null) {
resources.add(new AuthZResource(envBean.getEnv_name(), envBean.getStage_name()));
} else {
LOG.info("Failed to find main environment for capacity {}, skip authorization", capacity);
}
resources.add(new AuthZResource(envBean.getEnv_name(), envBean.getStage_name()));
}
return resources;
}

private boolean isSidecarEnvironment(EnvironBean environBean) {
return environBean.getSystem_priority() != null && environBean.getSystem_priority() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,14 @@ void authorizeSuccess(CapacityType type) throws Exception {

@ParameterizedTest
@MethodSource("capacityTypes")
void authorizeThrowsExceptionWhenNoMainEnvFound(CapacityType type) throws Exception {
void authorizeSucceedsWhenNoMainEnvFound(CapacityType type) throws Exception {
EnvironBean envBean = EnvironBeanFixture.createRandomEnvironBean();

for (String capacity : capacities) {
when(environDAO.getMainEnvByHostName(capacity)).thenReturn(null);
when(environDAO.getByCluster(capacity)).thenReturn(null);
}
assertThrows(
ForbiddenException.class,
() -> sut.authorize(envBean, principal, type, capacities));
assertDoesNotThrow(() -> sut.authorize(envBean, principal, type, capacities));
}

@ParameterizedTest
Expand Down

0 comments on commit ad27263

Please sign in to comment.