Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional: Update API response status code #1576

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ public class EnvironBean implements Updatable, Serializable {
@JsonProperty("terminationLimit")
private Integer termination_limit;

public void validate() throws Exception {
public void validate() throws IllegalArgumentException {
// A bunch of these fields will always be alphanumeric (with _ and -)
String envRegEx = "^[A-Za-z0-9_\\-]*$";
if (this.env_name != null && !this.env_name.matches(envRegEx)) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import com.pinterest.deployservice.dao.TagDAO;
import com.pinterest.deployservice.db.DatabaseUtil;
import com.pinterest.deployservice.db.DeployQueryFilter;
import com.pinterest.deployservice.exception.TeletaanInternalException;
import com.pinterest.deployservice.scm.SourceControlManagerProxy;

import com.google.common.base.Joiner;
Expand All @@ -76,6 +75,7 @@
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import javax.ws.rs.core.Response;
import javax.ws.rs.WebApplicationException;

public class DeployHandler implements DeployHandlerInterface{
private static final Logger LOG = LoggerFactory.getLogger(DeployHandler.class);
Expand Down Expand Up @@ -386,7 +386,7 @@ public void disableAutoPromote(EnvironBean envBean, String operator, boolean for
*/
private void validateBuild(EnvironBean envBean, String buildId) throws Exception {
if(StringUtils.isEmpty(buildId)) {
throw new TeletaanInternalException(Response.Status.BAD_REQUEST, "Build id can not be empty.");
throw new WebApplicationException("Build id can not be empty.", Response.Status.BAD_REQUEST);
}
BuildBean buildBean = buildDAO.getById(buildId);

Expand All @@ -398,23 +398,30 @@ private void validateBuild(EnvironBean envBean, String buildId) throws Exception
// only allow a non-private deploy if the build is from a trusted artifact url
if(envBean.getEnsure_trusted_build() && !buildBean.getScm_branch().equals("private") &&
buildAllowlist != null && !buildAllowlist.trusted(buildBean.getArtifact_url())) {
throw new TeletaanInternalException(Response.Status.BAD_REQUEST, String.format("Non-private build url points to an untrusted location (%s). Please Contact #teletraan to ensure the build artifact is published to a trusted url.",
buildBean.getArtifact_url()));
throw new WebApplicationException(String.format(
"Non-private build url points to an untrusted location (%s). Please Contact #teletraan to ensure the build artifact is published to a trusted url.",
buildBean.getArtifact_url()), Response.Status.FORBIDDEN);
}
// if the stage is not allowed (allow_private_build)
if(!envBean.getAllow_private_build()) {
// only allow deploy if it is not private build
if (buildBean.getScm_branch().equals("private")) {
throw new TeletaanInternalException(Response.Status.BAD_REQUEST, "This stage does not allow deploying a private build. Please Contact #teletraan to allow your stage for deploying private build.");
throw new WebApplicationException(
"This stage does not allow deploying a private build. Please Contact #teletraan to allow your stage for deploying private build.",
Response.Status.FORBIDDEN);
}
}
// disallow sox deploy if the build artifact is private
if(envBean.getIs_sox() && buildBean.getScm_branch().equals("private")) {
throw new TeletaanInternalException(Response.Status.BAD_REQUEST, "This stage requires SOX builds. A private build cannot be used in a sox-compliant stage.");
throw new WebApplicationException(
"This stage requires SOX builds. A private build cannot be used in a sox-compliant stage.",
Response.Status.FORBIDDEN);
}
// disallow sox deploy if the build artifact is not from a sox source url
if(envBean.getIs_sox() && buildAllowlist != null && !buildAllowlist.sox_compliant(buildBean.getArtifact_url())) {
throw new TeletaanInternalException(Response.Status.BAD_REQUEST, "This stage requires SOX builds. The build must be from a sox-compliant source. Contact your sox administrators.");
throw new WebApplicationException(
"This stage requires SOX builds. The build must be from a sox-compliant source. Contact your sox administrators.",
Response.Status.FORBIDDEN);
}
}

Expand All @@ -437,7 +444,7 @@ public String deploy(EnvironBean envBean, String buildId, String desc, String de
"The delivery type %s is different with the stage type %s for %s/%s",
deliveryType, envBean.getStage_type(), envBean.getEnv_name(), envBean.getStage_name());
LOG.error(errorMessage);
throw new TeletaanInternalException(Response.Status.CONFLICT, errorMessage);
throw new WebApplicationException(errorMessage, Response.Status.CONFLICT);
} else {
EnvType type = EnvType.valueOf(deliveryType);
envBean.setStage_type(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.pinterest.deployservice.common.EncryptionUtils;
import com.pinterest.deployservice.common.HTTPClient;
import com.pinterest.deployservice.common.KnoxKeyReader;
import com.pinterest.deployservice.exception.TeletaanInternalException;

import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
Expand All @@ -40,6 +39,7 @@
import java.util.Map;
import java.util.Queue;
import javax.ws.rs.core.Response;
import javax.ws.rs.WebApplicationException;

public class GithubManager extends BaseManager {
private static final Logger LOG = LoggerFactory.getLogger(GithubManager.class);
Expand Down Expand Up @@ -84,7 +84,7 @@ private void setHeaders() throws Exception {
// get installation token using the jwt token
GitHub gitHubApp = new GitHubBuilder().withJwtToken(jwtToken).build();
GHAppInstallation appInstallation = gitHubApp.getApp().getInstallationByOrganization(this.githubAppOrganization);
GHAppInstallationToken appInstallationToken = appInstallation.createToken().create();
GHAppInstallationToken appInstallationToken = appInstallation.createToken().create();

// always use the newly created GitHub app token as the token will expire
this.headers.put("Authorization", String.format("Token %s", appInstallationToken.getToken()));
Expand Down Expand Up @@ -181,7 +181,8 @@ public CommitBean getCommit(String repo, String sha) throws Exception {
// an IOException (and its subclasses) in this case indicates that the commit hash is not found in the repo.
// e.g. java.io.IOException: Server returned HTTP response code: 422
LOG.warn("GitHub commit hash {} is not found in repo {}.", sha, repo);
throw new TeletaanInternalException(Response.Status.NOT_FOUND, String.format("Commit hash %s is not found in repo %s", sha, repo));
throw new WebApplicationException(String.format("Commit hash %s is not found in repo %s", sha, repo),
Response.Status.NOT_FOUND);
}

GsonBuilder builder = new GsonBuilder();
Expand Down
4 changes: 2 additions & 2 deletions deploy-service/teletraanservice/bin/server.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ server:
customFieldNames:
requestAttributes: principal
includes: [timestamp, remoteAddress, protocol, method, requestUri, statusCode]
registerDefaultExceptionMappers: false
detailedJsonProcessingExceptionMapper: true
registerDefaultExceptionMappers: true

#
# Logging settings. refer
Expand Down Expand Up @@ -159,7 +160,6 @@ system:
# Teletraan needs to refer Deploy Dashboard url in its notify etc.
dashboardUrl: ${TELETRAAN_DASHBOARD_URL:-http://localhost:8888}
# Display full stack trace or just the error to user.
#clientError: long|short

#External Alerts
externalAlerts:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.pinterest.teletraan;

import com.pinterest.teletraan.exception.GenericExceptionMapper;
import com.pinterest.teletraan.health.GenericHealthCheck;
import com.pinterest.teletraan.resource.*;
import com.pinterest.teletraan.universal.security.PrincipalNameInjector;
Expand All @@ -24,8 +23,6 @@
import io.dropwizard.auth.AuthDynamicFeature;
import io.dropwizard.configuration.EnvironmentVariableSubstitutor;
import io.dropwizard.configuration.SubstitutingSourceProvider;
import io.dropwizard.jersey.jackson.JsonProcessingExceptionMapper;
import io.dropwizard.jersey.validation.JerseyViolationExceptionMapper;
import io.dropwizard.setup.Bootstrap;
import io.dropwizard.setup.Environment;
import io.swagger.jaxrs.config.BeanConfig;
Expand Down Expand Up @@ -108,13 +105,6 @@ public void run(TeletraanServiceConfiguration configuration, Environment environ

environment.healthChecks().register("generic", new GenericHealthCheck(context));

// Exception handlers
// Constrains validation exceptions, returns 4xx
environment.jersey().register(JerseyViolationExceptionMapper.class);
// Jackson Json parsing exceptions
environment.jersey().register(new JsonProcessingExceptionMapper(true));
environment.jersey().register(new GenericExceptionMapper(configuration.getSystemFactory().getClientError()));

environment.jersey().register(PrincipalNameInjector.class);

// Swagger API docs generation related
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.pinterest.teletraan.config;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.pinterest.deployservice.common.Constants;

public class SystemFactory {
private static final String DEFAULT_DASHBOARD_URL = "http://localhost:8888";
Expand All @@ -27,9 +26,6 @@ public class SystemFactory {
@JsonProperty
private String changeFeedUrl;

@JsonProperty
private String clientError = Constants.CLIENT_ERROR_SHORT;

@JsonProperty
private long agentCountCacheTtl = 10 * 1000;

Expand All @@ -52,14 +48,6 @@ public void setChangeFeedUrl(String changeFeedUrl) {
this.changeFeedUrl = changeFeedUrl;
}

public String getClientError() {
return clientError;
}

public void setClientError(String clientError) {
this.clientError = clientError;
}

public Long getAgentCountCacheTtl() {
return agentCountCacheTtl;
}
Expand Down

This file was deleted.

Loading