• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1833359013

23 May 2025 01:25PM UTC coverage: 66.318% (+0.5%) from 65.861%
1833359013

Pull #674

gitlab-ci

mzedel
fix(gui): prevented device tag editor to be shown when no tags exist

- this is to reduce confusion about tags defined async to the current session not being visible

Ticket: ME-528
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #674: ME-529, ME-528 - adjustments to device tag editing

29554 of 44564 relevant lines covered (66.32%)

1.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.38
/backend/pkg/requestid/middleware.go
1
// Copyright 2024 Northern.tech AS
2
//
3
//        Licensed under the Apache License, Version 2.0 (the "License");
4
//        you may not use this file except in compliance with the License.
5
//        You may obtain a copy of the License at
6
//
7
//            http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//        Unless required by applicable law or agreed to in writing, software
10
//        distributed under the License is distributed on an "AS IS" BASIS,
11
//        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//        See the License for the specific language governing permissions and
13
//        limitations under the License.
14
package requestid
15

16
import (
17
        "github.com/ant0ine/go-json-rest/rest"
18
        "github.com/gin-gonic/gin"
19
        "github.com/google/uuid"
20

21
        "github.com/mendersoftware/mender-server/pkg/log"
22
        "github.com/mendersoftware/mender-server/pkg/requestlog"
23
)
24

25
const RequestIdHeader = "X-MEN-RequestID"
26

27
type MiddlewareOptions struct {
28
        // GenerateRequestID decides whether a request ID should
29
        // be generated when none exists. (default: true)
30
        GenerateRequestID *bool
31
}
32

33
func NewMiddlewareOptions() *MiddlewareOptions {
4✔
34
        return new(MiddlewareOptions)
4✔
35
}
4✔
36

37
func (opt *MiddlewareOptions) SetGenerateRequestID(gen bool) *MiddlewareOptions {
4✔
38
        opt.GenerateRequestID = &gen
4✔
39
        return opt
4✔
40
}
4✔
41

42
// Middleware provides requestid middleware for the gin-gonic framework.
43
func Middleware(opts ...*MiddlewareOptions) gin.HandlerFunc {
4✔
44
        opt := NewMiddlewareOptions().
4✔
45
                SetGenerateRequestID(true)
4✔
46
        for _, o := range opts {
4✔
47
                if o == nil {
×
48
                        continue
×
49
                }
50
                if o.GenerateRequestID != nil {
×
51
                        opt.GenerateRequestID = o.GenerateRequestID
×
52
                }
×
53
        }
54
        return func(c *gin.Context) {
8✔
55
                ctx := c.Request.Context()
4✔
56

4✔
57
                requestID := c.GetHeader(RequestIdHeader)
4✔
58
                if requestID == "" && *opt.GenerateRequestID {
8✔
59
                        uid, _ := uuid.NewRandom()
4✔
60
                        requestID = uid.String()
4✔
61
                }
4✔
62
                ctx = WithContext(ctx, requestID)
4✔
63

4✔
64
                logger := log.FromContext(ctx)
4✔
65
                if logger != nil {
8✔
66
                        logger = logger.F(log.Ctx{"request_id": requestID})
4✔
67
                        ctx = log.WithContext(ctx, logger)
4✔
68
                }
4✔
69
                c.Header(RequestIdHeader, requestID)
4✔
70
                c.Request = c.Request.WithContext(ctx)
4✔
71
        }
72
}
73

74
// RequestIdMiddleware sets the X-MEN-RequestID header if it's not present,
75
// and adds the request id to the request logger's context.
76
type RequestIdMiddleware struct {
77
}
78

79
// MiddlewareFunc makes RequestIdMiddleware implement the Middleware interface.
80
func (mw *RequestIdMiddleware) MiddlewareFunc(h rest.HandlerFunc) rest.HandlerFunc {
5✔
81
        return func(w rest.ResponseWriter, r *rest.Request) {
10✔
82
                logger := requestlog.GetRequestLogger(r)
5✔
83

5✔
84
                reqId := r.Header.Get(RequestIdHeader)
5✔
85
                if reqId == "" {
10✔
86
                        uid, _ := uuid.NewRandom()
5✔
87
                        reqId = uid.String()
5✔
88
                }
5✔
89

90
                r = SetReqId(r, reqId)
5✔
91

5✔
92
                // enrich log context
5✔
93
                if logger != nil {
10✔
94
                        logger = logger.F(log.Ctx{"request_id": reqId})
5✔
95
                        r = requestlog.SetRequestLogger(r, logger)
5✔
96
                }
5✔
97

98
                //return the reuqest ID in response too, the client can log it
99
                //for end-to-end req tracing
100
                w.Header().Add(RequestIdHeader, reqId)
5✔
101

5✔
102
                h(w, r)
5✔
103
        }
104
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc