2022-05-05 13:25:50 +00:00
|
|
|
// Copyright 2022 Cloudbase Solutions SRL
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
|
// a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
2022-04-22 14:46:27 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
|
|
import (
|
2024-01-05 23:32:16 +00:00
|
|
|
"context"
|
2022-04-22 14:46:27 +00:00
|
|
|
"encoding/json"
|
2025-08-16 19:31:58 +00:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2023-01-20 21:54:59 +02:00
|
|
|
"io"
|
2024-01-05 23:32:16 +00:00
|
|
|
"log/slog"
|
2022-04-22 14:46:27 +00:00
|
|
|
"net/http"
|
2025-08-12 09:28:21 +00:00
|
|
|
"net/url"
|
2023-01-17 17:32:28 +01:00
|
|
|
"strings"
|
2022-04-22 14:46:27 +00:00
|
|
|
|
2024-02-22 07:31:51 +01:00
|
|
|
"github.com/gorilla/mux"
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
|
|
2023-07-22 22:26:47 +00:00
|
|
|
gErrors "github.com/cloudbase/garm-provider-common/errors"
|
2023-07-22 22:39:17 +00:00
|
|
|
"github.com/cloudbase/garm-provider-common/util"
|
2023-03-12 16:01:49 +02:00
|
|
|
"github.com/cloudbase/garm/apiserver/params"
|
|
|
|
|
"github.com/cloudbase/garm/auth"
|
2025-08-12 09:28:21 +00:00
|
|
|
"github.com/cloudbase/garm/config"
|
2023-03-12 16:01:49 +02:00
|
|
|
"github.com/cloudbase/garm/metrics"
|
|
|
|
|
runnerParams "github.com/cloudbase/garm/params"
|
2024-02-22 17:20:05 +01:00
|
|
|
"github.com/cloudbase/garm/runner" //nolint:typecheck
|
2025-08-12 09:28:21 +00:00
|
|
|
garmUtil "github.com/cloudbase/garm/util"
|
2023-03-12 16:01:49 +02:00
|
|
|
wsWriter "github.com/cloudbase/garm/websocket"
|
2025-08-27 00:25:17 +00:00
|
|
|
"github.com/cloudbase/garm/workers/websocket/events"
|
2022-04-22 14:46:27 +00:00
|
|
|
)
|
|
|
|
|
|
2025-08-12 09:28:21 +00:00
|
|
|
func NewAPIController(r *runner.Runner, authenticator *auth.Authenticator, hub *wsWriter.Hub, apiCfg config.APIServer) (*APIController, error) {
|
2024-02-20 16:42:10 +01:00
|
|
|
controllerInfo, err := r.GetControllerInfo(auth.GetAdminContext(context.Background()))
|
2023-08-13 06:01:28 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("failed to get controller info: %w", err)
|
2023-08-13 06:01:28 +00:00
|
|
|
}
|
2025-08-12 09:28:21 +00:00
|
|
|
var checkOrigin func(r *http.Request) bool
|
|
|
|
|
if len(apiCfg.CORSOrigins) > 0 {
|
|
|
|
|
checkOrigin = func(r *http.Request) bool {
|
|
|
|
|
origin := r.Header["Origin"]
|
|
|
|
|
if len(origin) == 0 {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
u, err := url.Parse(origin[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
for _, val := range apiCfg.CORSOrigins {
|
|
|
|
|
corsVal, err := url.Parse(val)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if garmUtil.ASCIIEqualFold(u.Host, corsVal.Host) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-04-22 14:46:27 +00:00
|
|
|
return &APIController{
|
2022-04-28 16:13:20 +00:00
|
|
|
r: r,
|
2023-01-27 12:35:51 +02:00
|
|
|
auth: authenticator,
|
2022-10-21 02:49:53 +03:00
|
|
|
hub: hub,
|
|
|
|
|
upgrader: websocket.Upgrader{
|
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
|
WriteBufferSize: 16384,
|
2025-08-12 09:28:21 +00:00
|
|
|
CheckOrigin: checkOrigin,
|
2022-10-21 02:49:53 +03:00
|
|
|
},
|
2023-08-15 17:19:06 +00:00
|
|
|
controllerID: controllerInfo.ControllerID.String(),
|
2022-04-22 14:46:27 +00:00
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type APIController struct {
|
2023-08-13 06:01:28 +00:00
|
|
|
r *runner.Runner
|
|
|
|
|
auth *auth.Authenticator
|
|
|
|
|
hub *wsWriter.Hub
|
|
|
|
|
upgrader websocket.Upgrader
|
|
|
|
|
controllerID string
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-01-05 23:32:16 +00:00
|
|
|
func handleError(ctx context.Context, w http.ResponseWriter, err error) {
|
2023-08-16 10:48:25 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-04-22 14:46:27 +00:00
|
|
|
apiErr := params.APIErrorResponse{
|
2025-08-16 19:31:58 +00:00
|
|
|
Details: err.Error(),
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, gErrors.ErrNotFound):
|
2022-04-22 14:46:27 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
|
|
|
|
apiErr.Error = "Not Found"
|
2025-08-16 19:31:58 +00:00
|
|
|
case errors.Is(err, gErrors.ErrUnauthorized):
|
2022-04-22 14:46:27 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
|
apiErr.Error = "Not Authorized"
|
2022-07-07 16:48:00 +00:00
|
|
|
// Don't include details on 401 errors.
|
|
|
|
|
apiErr.Details = ""
|
2025-08-16 19:31:58 +00:00
|
|
|
case errors.Is(err, gErrors.ErrBadRequest):
|
2022-04-22 14:46:27 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
apiErr.Error = "Bad Request"
|
2025-08-16 19:31:58 +00:00
|
|
|
case errors.Is(err, gErrors.ErrDuplicateEntity), errors.Is(err, &gErrors.ConflictError{}):
|
2022-04-22 14:46:27 +00:00
|
|
|
w.WriteHeader(http.StatusConflict)
|
|
|
|
|
apiErr.Error = "Conflict"
|
|
|
|
|
default:
|
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
|
apiErr.Error = "Server error"
|
2022-07-07 16:48:00 +00:00
|
|
|
// Don't include details on server error.
|
|
|
|
|
apiErr.Details = ""
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 21:54:59 +02:00
|
|
|
if err := json.NewEncoder(w).Encode(apiErr); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-05 23:32:16 +00:00
|
|
|
func (a *APIController) handleWorkflowJobEvent(ctx context.Context, w http.ResponseWriter, r *http.Request) {
|
2022-04-22 14:46:27 +00:00
|
|
|
defer r.Body.Close()
|
2023-01-20 21:54:59 +02:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2022-04-22 14:46:27 +00:00
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, gErrors.NewBadRequestError("invalid post body: %s", err))
|
2022-04-22 14:46:27 +00:00
|
|
|
return
|
|
|
|
|
}
|
2024-04-19 08:47:44 +00:00
|
|
|
|
2022-04-22 14:46:27 +00:00
|
|
|
signature := r.Header.Get("X-Hub-Signature-256")
|
|
|
|
|
hookType := r.Header.Get("X-Github-Hook-Installation-Target-Type")
|
2025-05-14 21:09:02 +00:00
|
|
|
giteaTargetType := r.Header.Get("X-Gitea-Hook-Installation-Target-Type")
|
2022-04-22 14:46:27 +00:00
|
|
|
|
2025-05-14 21:09:02 +00:00
|
|
|
forgeType := runnerParams.GithubEndpointType
|
|
|
|
|
if giteaTargetType != "" {
|
|
|
|
|
forgeType = runnerParams.GiteaEndpointType
|
|
|
|
|
hookType = giteaTargetType
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := a.r.DispatchWorkflowJob(hookType, signature, forgeType, body); err != nil {
|
2024-02-22 09:38:00 +01:00
|
|
|
switch {
|
|
|
|
|
case errors.Is(err, gErrors.ErrNotFound):
|
2024-02-19 16:22:32 +01:00
|
|
|
metrics.WebhooksReceived.WithLabelValues(
|
2024-02-20 14:27:27 +01:00
|
|
|
"false", // label: valid
|
|
|
|
|
"owner_unknown", // label: reason
|
2024-02-19 16:22:32 +01:00
|
|
|
).Inc()
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "got not found error from DispatchWorkflowJob. webhook not meant for us?")
|
2022-10-04 10:31:23 +02:00
|
|
|
return
|
2024-02-22 09:38:00 +01:00
|
|
|
case strings.Contains(err.Error(), "signature"):
|
|
|
|
|
// nolint:golangci-lint,godox TODO: check error type
|
2024-02-19 16:22:32 +01:00
|
|
|
metrics.WebhooksReceived.WithLabelValues(
|
2024-02-20 14:27:27 +01:00
|
|
|
"false", // label: valid
|
|
|
|
|
"signature_invalid", // label: reason
|
2024-02-19 16:22:32 +01:00
|
|
|
).Inc()
|
2024-02-22 09:38:00 +01:00
|
|
|
default:
|
2024-02-19 16:22:32 +01:00
|
|
|
metrics.WebhooksReceived.WithLabelValues(
|
2024-02-20 14:27:27 +01:00
|
|
|
"false", // label: valid
|
|
|
|
|
"unknown", // label: reason
|
2024-02-19 16:22:32 +01:00
|
|
|
).Inc()
|
2022-10-04 10:31:23 +02:00
|
|
|
}
|
2023-01-17 17:32:28 +01:00
|
|
|
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-22 14:46:27 +00:00
|
|
|
return
|
|
|
|
|
}
|
2024-02-19 16:22:32 +01:00
|
|
|
metrics.WebhooksReceived.WithLabelValues(
|
2024-02-20 14:27:27 +01:00
|
|
|
"true", // label: valid
|
|
|
|
|
"", // label: reason
|
2024-02-19 16:22:32 +01:00
|
|
|
).Inc()
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-13 06:01:28 +00:00
|
|
|
func (a *APIController) WebhookHandler(w http.ResponseWriter, r *http.Request) {
|
2024-01-05 23:32:16 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
|
|
2023-08-13 06:01:28 +00:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
controllerID, ok := vars["controllerID"]
|
|
|
|
|
// If the webhook URL includes a controller ID, we validate that it's meant for us. We still
|
|
|
|
|
// support bare webhook URLs, which are tipically configured manually by the user.
|
|
|
|
|
// The controllerID suffixed webhook URL is useful when configuring the webhook for an entity
|
|
|
|
|
// via garm. We cannot tag a webhook URL on github, so there is no way to determine ownership.
|
|
|
|
|
// Using a controllerID suffix is a simple way to denote ownership.
|
|
|
|
|
if ok && controllerID != a.controllerID {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.InfoContext(ctx, "ignoring webhook meant for foreign controller", "req_controller_id", controllerID)
|
2023-08-13 06:01:28 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-22 14:46:27 +00:00
|
|
|
headers := r.Header.Clone()
|
2022-05-03 12:40:59 +00:00
|
|
|
|
2022-04-23 13:05:40 +00:00
|
|
|
event := runnerParams.Event(headers.Get("X-Github-Event"))
|
2022-04-22 14:46:27 +00:00
|
|
|
switch event {
|
2022-04-23 13:05:40 +00:00
|
|
|
case runnerParams.WorkflowJobEvent:
|
2024-01-05 23:32:16 +00:00
|
|
|
a.handleWorkflowJobEvent(ctx, w, r)
|
2025-02-10 13:14:22 +00:00
|
|
|
case runnerParams.PingEvent:
|
|
|
|
|
// Ignore ping event. We may want to save the ping in the github entity table in the future.
|
2022-04-22 14:46:27 +00:00
|
|
|
default:
|
2025-02-10 13:14:22 +00:00
|
|
|
slog.DebugContext(ctx, "ignoring unknown event", "gh_event", util.SanitizeLogEntry(string(event)))
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
Add events websocket endpoint
This change adds a new websocket endpoint for database events. The events
endpoint allows clients to stream events as they happen in GARM. Events
are defined as a structure containning the event type (create, update, delete),
the database entity involved (instances, pools, repos, etc) and the payload
consisting of the object involved in the event. The payload translates
to the types normally returned by the API and can be deserialized as one
of the types present in the params package.
The events endpoint is a websocket endpoint and it accepts filters as
a simple json send over the websocket connection. The filters allows the
user to specify which entities are of interest, and which operations should
be returned. For example, you may be interested in changes made to pools
or runners, in which case you could create a filter that only returns
update operations for pools. Or update and delete operations.
The filters can be defined as:
{
"filters": [
{
"entity_type": "instance",
"operations": ["update", "delete"]
},
{
"entity_type": "pool"
},
],
"send_everything": false
}
This would return only update and delete events for instances and all events
for pools. Alternatively you can ask GARM to send you everything:
{
"send_everything": true
}
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
2024-07-03 22:30:41 +00:00
|
|
|
func (a *APIController) EventsHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
|
|
|
|
if _, err := w.Write([]byte("events are available to admin users")); err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := a.upgrader.Upgrade(w, r, nil)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "error upgrading to websockets")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
|
|
wsClient, err := wsWriter.NewClient(ctx, conn)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to create new client")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
defer wsClient.Stop()
|
|
|
|
|
|
|
|
|
|
eventHandler, err := events.NewHandler(ctx, wsClient)
|
|
|
|
|
if err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to create new event handler")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := eventHandler.Start(); err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to start event handler")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
<-eventHandler.Done()
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-21 02:49:53 +03:00
|
|
|
func (a *APIController) WSHandler(writer http.ResponseWriter, req *http.Request) {
|
|
|
|
|
ctx := req.Context()
|
|
|
|
|
if !auth.IsAdmin(ctx) {
|
|
|
|
|
writer.WriteHeader(http.StatusForbidden)
|
2023-01-20 21:54:59 +02:00
|
|
|
if _, err := writer.Write([]byte("you need admin level access to view logs")); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-01-20 21:54:59 +02:00
|
|
|
}
|
2022-10-21 02:49:53 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if a.hub == nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, writer, gErrors.NewBadRequestError("log streamer is disabled"))
|
2022-10-21 02:49:53 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
conn, err := a.upgrader.Upgrade(writer, req, nil)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "error upgrading to websockets")
|
2022-10-21 02:49:53 +03:00
|
|
|
return
|
|
|
|
|
}
|
2024-07-02 22:26:12 +00:00
|
|
|
defer conn.Close()
|
2022-10-21 02:49:53 +03:00
|
|
|
|
2024-07-02 22:26:12 +00:00
|
|
|
client, err := wsWriter.NewClient(ctx, conn)
|
2022-10-21 02:49:53 +03:00
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to create new client")
|
2022-10-21 02:49:53 +03:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err := a.hub.Register(client); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to register new client")
|
2022-10-21 02:49:53 +03:00
|
|
|
return
|
|
|
|
|
}
|
2024-07-02 22:26:12 +00:00
|
|
|
defer a.hub.Unregister(client)
|
|
|
|
|
|
|
|
|
|
if err := client.Start(); err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to start client")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
<-client.Done()
|
|
|
|
|
slog.Info("client disconnected", "client_id", client.ID())
|
2022-10-21 02:49:53 +03:00
|
|
|
}
|
|
|
|
|
|
2022-04-22 14:46:27 +00:00
|
|
|
// NotFoundHandler is returned when an invalid URL is acccessed
|
|
|
|
|
func (a *APIController) NotFoundHandler(w http.ResponseWriter, r *http.Request) {
|
2024-01-05 23:32:16 +00:00
|
|
|
ctx := r.Context()
|
2022-04-22 14:46:27 +00:00
|
|
|
apiErr := params.APIErrorResponse{
|
|
|
|
|
Details: "Resource not found",
|
|
|
|
|
Error: "Not found",
|
|
|
|
|
}
|
2023-08-16 10:48:25 +00:00
|
|
|
|
2022-05-03 12:40:59 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-08-16 10:48:25 +00:00
|
|
|
w.WriteHeader(http.StatusNotFound)
|
2023-01-20 21:54:59 +02:00
|
|
|
if err := json.NewEncoder(w).Encode(apiErr); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failet to write response")
|
2023-01-20 21:54:59 +02:00
|
|
|
}
|
2022-04-22 14:46:27 +00:00
|
|
|
}
|
2022-04-28 16:13:20 +00:00
|
|
|
|
2023-07-18 18:47:25 +03:00
|
|
|
// swagger:route GET /metrics-token metrics-token GetMetricsToken
|
2023-07-18 15:19:53 +03:00
|
|
|
//
|
|
|
|
|
// Returns a JWT token that can be used to access the metrics endpoint.
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: JWTResponse
|
|
|
|
|
// 401: APIErrorResponse
|
2023-01-17 17:32:28 +01:00
|
|
|
func (a *APIController) MetricsTokenHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
2023-01-26 14:02:53 +01:00
|
|
|
if !auth.IsAdmin(ctx) {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, gErrors.ErrUnauthorized)
|
2023-01-26 14:02:53 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-17 17:32:28 +01:00
|
|
|
token, err := a.auth.GetJWTMetricsToken(ctx)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2023-01-17 17:32:28 +01:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-01-26 14:02:53 +01:00
|
|
|
err = json.NewEncoder(w).Encode(runnerParams.JWTResponse{Token: token})
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-01-26 14:02:53 +01:00
|
|
|
}
|
2023-01-17 17:32:28 +01:00
|
|
|
}
|
|
|
|
|
|
2023-07-10 17:16:17 +03:00
|
|
|
// swagger:route POST /auth/login login Login
|
|
|
|
|
//
|
|
|
|
|
// Logs in a user and returns a JWT token.
|
|
|
|
|
//
|
|
|
|
|
// Parameters:
|
|
|
|
|
// + name: Body
|
|
|
|
|
// description: Login information.
|
|
|
|
|
// type: PasswordLoginParams
|
|
|
|
|
// in: body
|
|
|
|
|
// required: true
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: JWTResponse
|
|
|
|
|
// 400: APIErrorResponse
|
|
|
|
|
//
|
2022-04-28 16:13:20 +00:00
|
|
|
// LoginHandler returns a jwt token
|
|
|
|
|
func (a *APIController) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
2024-01-05 23:32:16 +00:00
|
|
|
ctx := r.Context()
|
2022-04-28 16:13:20 +00:00
|
|
|
var loginInfo runnerParams.PasswordLoginParams
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&loginInfo); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, gErrors.ErrBadRequest)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := loginInfo.Validate(); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx, err := a.auth.AuthenticateUser(ctx, loginInfo)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tokenString, err := a.auth.GetJWTToken(ctx)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-01-20 21:54:59 +02:00
|
|
|
if err := json.NewEncoder(w).Encode(runnerParams.JWTResponse{Token: tokenString}); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-01-20 21:54:59 +02:00
|
|
|
}
|
2022-04-28 16:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-10 17:16:17 +03:00
|
|
|
// swagger:route POST /first-run first-run FirstRun
|
|
|
|
|
//
|
|
|
|
|
// Initialize the first run of the controller.
|
|
|
|
|
//
|
|
|
|
|
// Parameters:
|
|
|
|
|
// + name: Body
|
|
|
|
|
// description: Create a new user.
|
|
|
|
|
// type: NewUserParams
|
|
|
|
|
// in: body
|
|
|
|
|
// required: true
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: User
|
|
|
|
|
// 400: APIErrorResponse
|
2022-04-28 16:13:20 +00:00
|
|
|
func (a *APIController) FirstRunHandler(w http.ResponseWriter, r *http.Request) {
|
2024-01-05 23:32:16 +00:00
|
|
|
ctx := r.Context()
|
2022-04-28 16:13:20 +00:00
|
|
|
if a.auth.IsInitialized() {
|
|
|
|
|
err := gErrors.NewConflictError("already initialized")
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newUserParams runnerParams.NewUserParams
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&newUserParams); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, gErrors.ErrBadRequest)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newUser, err := a.auth.InitController(ctx, newUserParams)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-01-20 21:54:59 +02:00
|
|
|
if err := json.NewEncoder(w).Encode(newUser); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-01-20 21:54:59 +02:00
|
|
|
}
|
2022-04-28 16:13:20 +00:00
|
|
|
}
|
|
|
|
|
|
2023-07-10 17:16:17 +03:00
|
|
|
// swagger:route GET /providers providers ListProviders
|
|
|
|
|
//
|
|
|
|
|
// List all providers.
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: Providers
|
|
|
|
|
// 400: APIErrorResponse
|
2022-04-28 16:13:20 +00:00
|
|
|
func (a *APIController) ListProviders(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
providers, err := a.r.ListProviders(ctx)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2022-04-28 16:13:20 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-03 12:40:59 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2023-01-20 21:54:59 +02:00
|
|
|
if err := json.NewEncoder(w).Encode(providers); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-01-20 21:54:59 +02:00
|
|
|
}
|
2022-04-28 16:13:20 +00:00
|
|
|
}
|
2023-06-23 21:15:46 +00:00
|
|
|
|
2023-07-18 15:19:53 +03:00
|
|
|
// swagger:route GET /jobs jobs ListJobs
|
|
|
|
|
//
|
|
|
|
|
// List all jobs.
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: Jobs
|
|
|
|
|
// 400: APIErrorResponse
|
2023-06-23 21:15:46 +00:00
|
|
|
func (a *APIController) ListAllJobs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
jobs, err := a.r.ListAllJobs(ctx)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2023-06-23 21:15:46 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
if err := json.NewEncoder(w).Encode(jobs); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-06-23 21:15:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-08-12 22:41:00 +00:00
|
|
|
|
|
|
|
|
// swagger:route GET /controller-info controllerInfo ControllerInfo
|
|
|
|
|
//
|
|
|
|
|
// Get controller info.
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: ControllerInfo
|
|
|
|
|
// 409: APIErrorResponse
|
|
|
|
|
func (a *APIController) ControllerInfoHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
info, err := a.r.GetControllerInfo(ctx)
|
|
|
|
|
if err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
handleError(ctx, w, err)
|
2023-08-12 22:41:00 +00:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
if err := json.NewEncoder(w).Encode(info); err != nil {
|
2024-01-05 23:32:16 +00:00
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
2023-08-12 22:41:00 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-05 06:41:16 +00:00
|
|
|
|
|
|
|
|
// swagger:route PUT /controller controller UpdateController
|
|
|
|
|
//
|
|
|
|
|
// Update controller.
|
|
|
|
|
//
|
|
|
|
|
// Parameters:
|
|
|
|
|
// + name: Body
|
|
|
|
|
// description: Parameters used when updating the controller.
|
|
|
|
|
// type: UpdateControllerParams
|
|
|
|
|
// in: body
|
|
|
|
|
// required: true
|
|
|
|
|
//
|
|
|
|
|
// Responses:
|
|
|
|
|
// 200: ControllerInfo
|
|
|
|
|
// 400: APIErrorResponse
|
|
|
|
|
func (a *APIController) UpdateControllerHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
ctx := r.Context()
|
|
|
|
|
var updateParams runnerParams.UpdateControllerParams
|
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&updateParams); err != nil {
|
|
|
|
|
handleError(ctx, w, gErrors.ErrBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := updateParams.Validate(); err != nil {
|
|
|
|
|
handleError(ctx, w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
info, err := a.r.UpdateController(ctx, updateParams)
|
|
|
|
|
if err != nil {
|
|
|
|
|
handleError(ctx, w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
|
if err := json.NewEncoder(w).Encode(info); err != nil {
|
|
|
|
|
slog.With(slog.Any("error", err)).ErrorContext(ctx, "failed to encode response")
|
|
|
|
|
}
|
|
|
|
|
}
|