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

mendersoftware / useradm / 1325239969

08 Dec 2023 03:39PM UTC coverage: 87.019%. Remained the same
1325239969

push

gitlab-ci

web-flow
Merge pull request #401 from mendersoftware/dependabot/go_modules/go.mongodb.org/mongo-driver-1.13.1

chore: bump go.mongodb.org/mongo-driver from 1.12.1 to 1.13.1

2869 of 3297 relevant lines covered (87.02%)

130.99 hits per line

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

100.0
/middleware.go
1
// Copyright 2021 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 main
15

16
import (
17
        "fmt"
18

19
        "github.com/ant0ine/go-json-rest/rest"
20
        "github.com/mendersoftware/go-lib-micro/accesslog"
21
        "github.com/mendersoftware/go-lib-micro/identity"
22
        log "github.com/mendersoftware/go-lib-micro/log"
23
        "github.com/mendersoftware/go-lib-micro/requestid"
24
        "github.com/mendersoftware/go-lib-micro/requestlog"
25

26
        api_http "github.com/mendersoftware/useradm/api/http"
27
        "github.com/mendersoftware/useradm/authz"
28
        "github.com/mendersoftware/useradm/jwt"
29
)
30

31
const (
32
        EnvProd = "prod"
33
        EnvDev  = "dev"
34
)
35

36
var (
37
        commonLoggingAccessStack = []rest.Middleware{
38

39
                // logging
40
                &requestlog.RequestLogMiddleware{},
41
                &accesslog.AccessLogMiddleware{Format: accesslog.SimpleLogFormat},
42
                &rest.TimerMiddleware{},
43
                &rest.RecorderMiddleware{},
44
        }
45

46
        defaultDevStack = []rest.Middleware{
47
                // catches the panic errors that occur with stack trace
48
                &rest.RecoverMiddleware{
49
                        EnableResponseStackTrace: true,
50
                },
51

52
                // json pretty print
53
                &rest.JsonIndentMiddleware{},
54
        }
55

56
        defaultProdStack = []rest.Middleware{
57
                // catches the panic errors
58
                &rest.RecoverMiddleware{},
59
        }
60

61
        commonStack = []rest.Middleware{
62
                // verifies the request Content-Type header
63
                // The expected Content-Type is 'application/json'
64
                // if the content is non-null
65
                &rest.ContentTypeCheckerMiddleware{},
66
                &requestid.RequestIdMiddleware{},
67
                &identity.IdentityMiddleware{
68
                        UpdateLogger: true,
69
                },
70
        }
71

72
        middlewareMap = map[string][]rest.Middleware{
73
                EnvProd: defaultProdStack,
74
                EnvDev:  defaultDevStack,
75
        }
76
)
77

78
func SetupMiddleware(
79
        api *rest.Api,
80
        mwtype string,
81
        authorizer authz.Authorizer,
82
        jwth map[int]jwt.Handler,
83
        jwthFallback jwt.Handler,
84
) error {
7✔
85

7✔
86
        l := log.New(log.Ctx{})
7✔
87

7✔
88
        l.Infof("setting up %s middleware", mwtype)
7✔
89

7✔
90
        api.Use(commonLoggingAccessStack...)
7✔
91

7✔
92
        mwstack, ok := middlewareMap[mwtype]
7✔
93
        if !ok {
9✔
94
                return fmt.Errorf("incorrect middleware type: %s", mwtype)
2✔
95
        }
2✔
96

97
        api.Use(mwstack...)
5✔
98

5✔
99
        api.Use(commonStack...)
5✔
100

5✔
101
        authzmw := &authz.AuthzMiddleware{
5✔
102
                Authz:              authorizer,
5✔
103
                ResFunc:            api_http.ExtractResourceAction,
5✔
104
                JWTHandlers:        jwth,
5✔
105
                JWTFallbackHandler: jwthFallback,
5✔
106
        }
5✔
107

5✔
108
        //force authz only on verification endpoint
5✔
109
        ifmw := &rest.IfMiddleware{
5✔
110
                Condition: api_http.IsVerificationEndpoint,
5✔
111
                IfTrue:    authzmw,
5✔
112
        }
5✔
113

5✔
114
        api.Use(ifmw)
5✔
115

5✔
116
        return nil
5✔
117
}
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