...
1 package middleware
2
3 import (
4 "errors"
5 "fmt"
6 "net/http"
7
8 "github.com/gin-gonic/gin"
9 "github.com/hashicorp/go-version"
10
11 "edge-infra.dev/pkg/lib/fog"
12 "edge-infra.dev/pkg/sds/emergencyaccess/apierror"
13 errorhandler "edge-infra.dev/pkg/sds/emergencyaccess/apierror/handler"
14 "edge-infra.dev/pkg/sds/emergencyaccess/eaconst"
15 )
16
17
18
19
20
21
22
23
24
25
26
27
28 func HealthCheck(checks ...func() error) gin.HandlerFunc {
29 return func(ctx *gin.Context) {
30 for _, check := range checks {
31 if err := check(); err != nil {
32 log := fog.FromContext(ctx).WithName("health")
33 log.Error(err, "failed health check")
34 ctx.String(http.StatusServiceUnavailable, "failed health check: %s", err)
35 return
36 }
37 }
38 ctx.String(http.StatusOK, "ok")
39 }
40 }
41
42
43
44
45
46
47
48
49
50 func VerifyAPIVersion(currentAPIVersionStr string) gin.HandlerFunc {
51
52 currentAPIVersion, err := version.NewVersion(currentAPIVersionStr)
53 if err != nil {
54 panic(fmt.Errorf("unable to parse current API version: %w", err))
55 }
56 currentMajorAPIVersion := currentAPIVersion.Segments()[0]
57 constraint := fmt.Sprintf("~>%d.0", currentMajorAPIVersion)
58 versionConstraint, err := version.NewConstraint(constraint)
59 if err != nil {
60 panic(fmt.Errorf("unable to parse versionConstraint: %w", err))
61 }
62
63 return func(c *gin.Context) {
64 c.Header(eaconst.APIServerVersionKey, currentAPIVersionStr)
65
66 v := c.Request.Header.Get(eaconst.APIVersionKey)
67 if v == "" {
68
69 c.Next()
70 return
71 }
72 incomingVersion, err := version.NewVersion(v)
73 if err != nil {
74 errorhandler.ErrorHandler(c, apierror.E(apierror.ErrAPIVersion))
75 return
76 }
77
78 if !versionConstraint.Check(incomingVersion) {
79 errorhandler.ErrorHandler(
80 c,
81 apierror.E(
82 apierror.ErrAPIVersion,
83 fmt.Sprintf("The current RemoteCLI version is incompatible with the server version. Please download the appropriate remotecli binary for the environment. Server API Version %s", currentAPIVersion),
84 errors.New("remotecli API version is not compatible with server"),
85 ),
86 )
87 return
88 }
89 c.Next()
90 }
91 }
92
View as plain text