private void handlePluginNotifyError(String pluginId, HealthStateScope scope, String errorDescription, Exception e) { String message = "Notification update failed for plugin: " + pluginId; serverHealthService.update(ServerHealthState.error(message, errorDescription, HealthStateType.general(scope))); LOGGER.warn(message, e); }
private boolean hasOperatePermission(Username username, OperationResult operationResult) { if (!securityService.hasOperatePermissionForAgents(username)) { String message = "Unauthorized to operate on agent"; operationResult.forbidden(message, message, HealthStateType.general(HealthStateScope.GLOBAL)); return false; } return true; }
private boolean isUnknownAgent(AgentInstance agentInstance, OperationResult operationResult) { if (agentInstance.isNullAgent()) { String agentNotFoundMessage = String.format("Agent '%s' not found", agentInstance.getUuid()); operationResult.notFound("Agent not found.", agentNotFoundMessage, HealthStateType.general(HealthStateScope.GLOBAL)); return true; } return false; }
public void check(OperationResult result) { HealthStateType id = HealthStateType.general(HealthStateScope.forPipeline(pipelineName)); if (pipelinePauseService.isPaused(pipelineName)) { String message = String.format("Failed to trigger pipeline [%s]", pipelineName); result.conflict(message, String.format("Pipeline %s is paused", pipelineName), id); } else { result.success(id); } } }
public Stage cancelAndTriggerRelevantStages(String pipelineName, String stageName, Username userName, LocalizedOperationResult result) throws Exception { Stage stage = stageService.findLatestStage(pipelineName, stageName); if (stage == null) { String stageLocator = String.format("(pipeline name: %s, stage name %s)", pipelineName, stageName); LOGGER.warn("[Stage Cancellation] Failed to retrieve stage{}", stageLocator); result.notFound("Stage '" + stageLocator + "' not found.", HealthStateType.general(HealthStateScope.GLOBAL)); return null; } return cancelAndTriggerRelevantStages(stage.getId(), userName, result); }
private boolean materialNotFound(String pipelineName, Map<String, String> revisions, OperationResult result) { for (String pipelineFingerprint : revisions.keySet()) { if (goConfigService.findMaterial(new CaseInsensitiveString(pipelineName), pipelineFingerprint) == null) { String message = String.format("material with fingerprint [%s] not found in pipeline [%s]", pipelineFingerprint, pipelineName); result.notFound(message, message, HealthStateType.general(HealthStateScope.forPipeline(pipelineName))); return true; } } return false; }
public boolean validate(String pipelineName, Username username, OperationResult result) { if (!goConfigService.hasPipelineNamed(new CaseInsensitiveString(pipelineName))) { String pipelineNotKnown = String.format("Pipeline named [%s] is not known.", pipelineName); result.notFound(pipelineNotKnown, pipelineNotKnown, HealthStateType.general(HealthStateScope.GLOBAL)); return false; } if (!securityService.hasViewPermissionForPipeline(username, pipelineName)) { result.forbidden(NOT_AUTHORIZED_TO_VIEW_PIPELINE, NOT_AUTHORIZED_TO_VIEW_PIPELINE, HealthStateType.general(HealthStateScope.forPipeline(pipelineName))); return false; } return true; }
public void check(OperationResult result) { HealthStateType id = HealthStateType.general(HealthStateScope.forPipeline(pipelineName)); if (!securityService.hasOperatePermissionForStage(pipelineName, stageName, username)) { String message = String.format("Failed to trigger pipeline [%s]", pipelineName); result.forbidden(message, "User " + username + " does not have permission to schedule " + pipelineName + "/" + stageName, id); } else { result.success(id); } } }
private void setServerHealthError(String message, String description) { serverHealthService.update( ServerHealthState.error(message, description, HealthStateType.general(HealthStateScope.forBackupCron())) ); }
private void clearServerHealthError() { serverHealthService.update( ServerHealthState.success(HealthStateType.general(HealthStateScope.forBackupCron())) ); }
public void check(OperationResult result) { HealthStateType type = HealthStateType.general(HealthStateScope.forPipeline(CaseInsensitiveString.str(pipelineConfig.name()))); if (pipelineConfig.isFirstStageManualApproval()) { String message = String.format("Failed to trigger pipeline [%s]", pipelineConfig.name()); result.error(message, String.format("The first stage of pipeline \"%s\" has manual approval", pipelineConfig.name()), type); } else { result.success(type); } } }
@Test public void shouldUnderstandEquality() { ServerHealthState fooError = ServerHealthState.error("my message", "my description", HealthStateType.general(HealthStateScope.forPipeline("foo"))); ServerHealthState fooErrorCopy = ServerHealthState.error("my message", "my description", HealthStateType.general(HealthStateScope.forPipeline("foo"))); assertThat(fooError, is(fooErrorCopy)); }
private boolean updateConfig(final PartialConfig newPart, final String fingerprint, ConfigRepoConfig repoConfig) { try { goConfigService.updateConfig(buildUpdateCommand(newPart, fingerprint)); return true; } catch (Exception e) { if (repoConfig != null) { String description = String.format("%s- For Config Repo: %s", GoConfigValidity.invalid(e).errorMessage(), newPart.getOrigin().displayName()); ServerHealthState state = ServerHealthState.error(INVALID_CRUISE_CONFIG_MERGE, description, HealthStateType.general(HealthStateScope.forPartialConfigRepo(repoConfig))); serverHealthService.update(state); } return false; } }
@Test public void successShouldReturnTrueIfStatusIs2xx(){ assertThat(httpOperationResult.isSuccess(), is(true)); httpOperationResult.notAcceptable("not acceptable", HealthStateType.general(HealthStateScope.GLOBAL)); assertThat(httpOperationResult.isSuccess(), is(false)); httpOperationResult.ok("message"); assertThat(httpOperationResult.isSuccess(), is(true)); httpOperationResult.error("message", "desc", HealthStateType.general(HealthStateScope.GLOBAL)); assertThat(httpOperationResult.isSuccess(), is(false)); httpOperationResult.accepted("Request to schedule pipeline 'baboon' accepted", "blah blah", HealthStateType.general(HealthStateScope.forPipeline("baboon"))); assertThat(httpOperationResult.isSuccess(), is(true)); }
@Test public void shouldDifferentiateTypeUsingSubkey() { HealthStateScope scope = HealthStateScope.fromPlugin("plugin-id"); assertEquals(HealthStateType.general(scope), HealthStateType.general(scope)); assertEquals(HealthStateType.withSubkey(scope, "subkey1"), HealthStateType.withSubkey(scope, "subkey1")); assertNotEquals(HealthStateType.withSubkey(scope, "subkey1"), HealthStateType.withSubkey(scope, "subkey2")); assertNotEquals(HealthStateType.withSubkey(scope, "subkey1"), HealthStateType.general(scope)); } }
@Test public void shouldEscapeWarningMessageAndDescriptionByDefault() { ServerHealthState warningStateWithoutTimeout = ServerHealthState.warning("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo"))); ServerHealthState warningStateWithTimeout = ServerHealthState.warning("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo")), Timeout.TEN_SECONDS); ServerHealthState warningState = ServerHealthState.warning("\"<message1 & message2>\"", "\"<message1 & message2>\"", HealthStateType.general(HealthStateScope.forPipeline("foo")), 15L); assertThat(warningStateWithoutTimeout.getMessage(), is(""<message1 & message2>"")); assertThat(warningStateWithoutTimeout.getDescription(), is(""<message1 & message2>"")); assertThat(warningStateWithTimeout.getMessage(), is("\"<message1 & message2>\"")); assertThat(warningStateWithTimeout.getDescription(), is("\"<message1 & message2>\"")); assertThat(warningState.getMessage(), is(""<message1 & message2>"")); assertThat(warningState.getDescription(), is(""<message1 & message2>"")); }
@Test public void shouldReturn406ForNotAcceptable() { httpOperationResult.notAcceptable("not acceptable", HealthStateType.general(HealthStateScope.GLOBAL)); assertThat(httpOperationResult.httpCode(), is(406)); assertThat(httpOperationResult.canContinue(), is(false)); assertThat(httpOperationResult.message(), is("not acceptable")); }
@Test public void shouldNotReturnSuccessfulIfUnauthorized() { LocalizedOperationResult result = new HttpLocalizedOperationResult(); result.forbidden(LocalizedMessage.forbiddenToViewPipeline("whateva"), HealthStateType.general(HealthStateScope.GLOBAL)); assertThat(result.isSuccessful(), is(false)); }
@Test public void shouldReturn403WhenUserDoesNotHavePermissionToAccessResource() { String message = LocalizedMessage.forbiddenToViewPipeline("whateva"); HttpLocalizedOperationResult result = new HttpLocalizedOperationResult(); result.forbidden(message, HealthStateType.general(HealthStateScope.GLOBAL)); assertThat(result.httpCode(), is(403)); }
@Test public void shouldRemoveErrorLogWhenCorrespondingMaterialIsMissing() throws Exception { serverHealthService.update(ServerHealthState.error("hg-message", "description", HealthStateType.general(forMaterial(MaterialsMother.hgMaterial())))); SvnMaterialConfig svnMaterialConfig = MaterialConfigsMother.svnMaterialConfig(); serverHealthService.update(ServerHealthState.error("svn-message", "description", HealthStateType.general(forMaterialConfig(svnMaterialConfig)))); CruiseConfig cruiseConfig = new BasicCruiseConfig(); cruiseConfig.addPipeline("defaultGroup", new PipelineConfig(new CaseInsensitiveString("dev"), new MaterialConfigs(svnMaterialConfig), new StageConfig(new CaseInsensitiveString("first"), new JobConfigs()))); serverHealthService.purgeStaleHealthMessages(cruiseConfig); assertThat(serverHealthService.logs().size(), is(1)); }