|
| 1 | +package porter_app |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "connectrpc.com/connect" |
| 7 | + porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1" |
| 8 | + "github.com/porter-dev/porter/api/server/handlers" |
| 9 | + "github.com/porter-dev/porter/api/server/shared" |
| 10 | + "github.com/porter-dev/porter/api/server/shared/apierrors" |
| 11 | + "github.com/porter-dev/porter/api/server/shared/config" |
| 12 | + "github.com/porter-dev/porter/api/server/shared/requestutils" |
| 13 | + "github.com/porter-dev/porter/api/types" |
| 14 | + "github.com/porter-dev/porter/internal/models" |
| 15 | + "github.com/porter-dev/porter/internal/telemetry" |
| 16 | +) |
| 17 | + |
| 18 | +// AppEnvVariablesHandler is the handler for the /apps/{porter_app_name}/env-variables endpoint |
| 19 | +type AppEnvVariablesHandler struct { |
| 20 | + handlers.PorterHandlerReadWriter |
| 21 | +} |
| 22 | + |
| 23 | +// NewAppEnvVariablesHandler handles GET requests to /apps/{porter_app_name}/env-variables |
| 24 | +func NewAppEnvVariablesHandler( |
| 25 | + config *config.Config, |
| 26 | + decoderValidator shared.RequestDecoderValidator, |
| 27 | + writer shared.ResultWriter, |
| 28 | +) *AppEnvVariablesHandler { |
| 29 | + return &AppEnvVariablesHandler{ |
| 30 | + PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer), |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// EnvVariables is a struct containing maps of the app's env variables and secrets |
| 35 | +type EnvVariables struct { |
| 36 | + Variables map[string]string `json:"variables"` |
| 37 | + Secrets map[string]string `json:"secrets"` |
| 38 | +} |
| 39 | + |
| 40 | +// AppEnvVariablesRequest is the request object for the /apps/{porter_app_name}/env-variables endpoint |
| 41 | +type AppEnvVariablesRequest struct { |
| 42 | + DeploymentTargetID string `schema:"deployment_target_id"` |
| 43 | +} |
| 44 | + |
| 45 | +// AppEnvVariablesResponse is the response object for the /apps/{porter_app_name}/env-variables endpoint |
| 46 | +type AppEnvVariablesResponse struct { |
| 47 | + EnvVariables EnvVariables `json:"env_variables"` |
| 48 | +} |
| 49 | + |
| 50 | +// ServeHTTP retrieves the app's env variables from CCP and writes them to the response |
| 51 | +func (c *AppEnvVariablesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 52 | + ctx, span := telemetry.NewSpan(r.Context(), "serve-get-app-env-variables") |
| 53 | + defer span.End() |
| 54 | + |
| 55 | + project, _ := ctx.Value(types.ProjectScope).(*models.Project) |
| 56 | + cluster, _ := ctx.Value(types.ClusterScope).(*models.Cluster) |
| 57 | + |
| 58 | + appName, reqErr := requestutils.GetURLParamString(r, types.URLParamPorterAppName) |
| 59 | + if reqErr != nil { |
| 60 | + e := telemetry.Error(ctx, span, reqErr, "error parsing stack name from url") |
| 61 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(e, http.StatusBadRequest)) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "app-name", Value: appName}) |
| 66 | + |
| 67 | + request := &LatestAppRevisionRequest{} |
| 68 | + if ok := c.DecodeAndValidate(w, r, request); !ok { |
| 69 | + err := telemetry.Error(ctx, span, nil, "error decoding request") |
| 70 | + c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(err, http.StatusBadRequest)) |
| 71 | + return |
| 72 | + } |
| 73 | + |
| 74 | + // optional deployment target id - if not provided, use the cluster's default |
| 75 | + deploymentTargetID := request.DeploymentTargetID |
| 76 | + telemetry.WithAttributes(span, telemetry.AttributeKV{Key: "deployment-target-id", Value: deploymentTargetID}) |
| 77 | + |
| 78 | + var deploymentTargetIdentifer *porterv1.DeploymentTargetIdentifier |
| 79 | + if deploymentTargetID != "" { |
| 80 | + deploymentTargetIdentifer = &porterv1.DeploymentTargetIdentifier{ |
| 81 | + Id: deploymentTargetID, |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + appEnvVariablesReq := connect.NewRequest(&porterv1.AppEnvVariablesRequest{ |
| 86 | + ProjectId: int64(project.ID), |
| 87 | + ClusterId: int64(cluster.ID), |
| 88 | + AppName: appName, |
| 89 | + DeploymentTargetIdentifier: deploymentTargetIdentifer, |
| 90 | + }) |
| 91 | + |
| 92 | + ccpResp, err := c.Config().ClusterControlPlaneClient.AppEnvVariables(ctx, appEnvVariablesReq) |
| 93 | + if err != nil { |
| 94 | + err := telemetry.Error(ctx, span, err, "error getting app env variables from ccp") |
| 95 | + c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) |
| 96 | + return |
| 97 | + } |
| 98 | + |
| 99 | + if ccpResp == nil || ccpResp.Msg == nil { |
| 100 | + err := telemetry.Error(ctx, span, nil, "ccp returned nil response") |
| 101 | + c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) |
| 102 | + return |
| 103 | + } |
| 104 | + if ccpResp.Msg.EnvVariables == nil { |
| 105 | + err := telemetry.Error(ctx, span, nil, "no env variables returned") |
| 106 | + c.HandleAPIError(w, r, apierrors.NewErrInternal(err)) |
| 107 | + return |
| 108 | + } |
| 109 | + |
| 110 | + envVars := EnvVariables{ |
| 111 | + Variables: ccpResp.Msg.EnvVariables.Normal, |
| 112 | + Secrets: ccpResp.Msg.EnvVariables.Secret, |
| 113 | + } |
| 114 | + |
| 115 | + // write the app to the response |
| 116 | + c.WriteResult(w, r, &AppEnvVariablesResponse{ |
| 117 | + EnvVariables: envVars, |
| 118 | + }) |
| 119 | +} |
0 commit comments