(w http.ResponseWriter, r *http.Request)
| 81 | } |
| 82 | |
| 83 | func (a *API) loadInstanceConfig(w http.ResponseWriter, r *http.Request) (context.Context, error) { |
| 84 | ctx := r.Context() |
| 85 | |
| 86 | signature := getSignature(ctx) |
| 87 | if signature == "" { |
| 88 | return nil, badRequestError("Operator signature missing") |
| 89 | } |
| 90 | |
| 91 | claims := NetlifyMicroserviceClaims{} |
| 92 | p := jwt.Parser{ValidMethods: []string{jwt.SigningMethodHS256.Name}} |
| 93 | _, err := p.ParseWithClaims(signature, &claims, func(token *jwt.Token) (interface{}, error) { |
| 94 | return []byte(a.config.OperatorToken), nil |
| 95 | }) |
| 96 | if err != nil { |
| 97 | return nil, badRequestError("Operator microservice signature is invalid: %v", err) |
| 98 | } |
| 99 | |
| 100 | if claims.InstanceID == "" { |
| 101 | return nil, badRequestError("Instance ID is missing") |
| 102 | } |
| 103 | instanceID, err := uuid.FromString(claims.InstanceID) |
| 104 | if err != nil { |
| 105 | return nil, badRequestError("Instance ID is not a valid UUID") |
| 106 | } |
| 107 | |
| 108 | logEntrySetField(r, "instance_id", instanceID) |
| 109 | logEntrySetField(r, "netlify_id", claims.NetlifyID) |
| 110 | instance, err := models.GetInstance(a.db, instanceID) |
| 111 | if err != nil { |
| 112 | if models.IsNotFoundError(err) { |
| 113 | return nil, notFoundError("Unable to locate site configuration") |
| 114 | } |
| 115 | return nil, internalServerError("Database error loading instance").WithInternalError(err) |
| 116 | } |
| 117 | |
| 118 | config, err := instance.Config() |
| 119 | if err != nil { |
| 120 | return nil, internalServerError("Error loading environment config").WithInternalError(err) |
| 121 | } |
| 122 | |
| 123 | if claims.SiteURL != "" { |
| 124 | config.SiteURL = claims.SiteURL |
| 125 | } |
| 126 | logEntrySetField(r, "site_url", config.SiteURL) |
| 127 | |
| 128 | ctx = withNetlifyID(ctx, claims.NetlifyID) |
| 129 | ctx = withFunctionHooks(ctx, claims.FunctionHooks) |
| 130 | |
| 131 | ctx, err = WithInstanceConfig(ctx, config, instanceID) |
| 132 | if err != nil { |
| 133 | return nil, internalServerError("Error loading instance config").WithInternalError(err) |
| 134 | } |
| 135 | |
| 136 | return ctx, nil |
| 137 | } |
| 138 | |
| 139 | func (a *API) limitHandler(lmt *limiter.Limiter) middlewareHandler { |
| 140 | return func(w http.ResponseWriter, req *http.Request) (context.Context, error) { |
nothing calls this directly
no test coverage detected