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

mendersoftware / mender-server / 1686000914

24 Feb 2025 01:34PM UTC coverage: 76.203%. Remained the same
1686000914

Pull #416

gitlab-ci

mzedel
test: added e2e tests for webhook functionality

Ticket: MEN-7926
Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #416: MEN-7926

4 of 8 new or added lines in 2 files covered. (50.0%)

4 existing lines in 2 files now uncovered.

36860 of 48371 relevant lines covered (76.2%)

1.49 hits per line

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

96.49
/backend/services/iot-manager/model/credentials_http.go
1
// Copyright 2022 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

15
package model
16

17
import (
18
        "encoding/hex"
19
        "encoding/json"
20
        "fmt"
21
        "net"
22
        "net/url"
23

24
        "github.com/mendersoftware/mender-server/pkg/config"
25
        dconfig "github.com/mendersoftware/mender-server/services/iot-manager/config"
26
        "github.com/mendersoftware/mender-server/services/iot-manager/crypto"
27
        inet "github.com/mendersoftware/mender-server/services/iot-manager/internal/net"
28

29
        validation "github.com/go-ozzo/ozzo-validation/v4"
30
)
31

32
type HexSecret crypto.String
33

34
func (sec *HexSecret) UnmarshalText(b []byte) error {
2✔
35
        dst := make([]byte, hex.DecodedLen(len(b)))
2✔
36
        n, err := hex.Decode(dst, b)
2✔
37
        if err != nil {
3✔
38
                return fmt.Errorf("value error: '%s' is not a hexadecimal string", string(b))
1✔
39
        }
1✔
40
        *sec = HexSecret(dst[:n])
2✔
41
        return nil
2✔
42
}
43

44
func (sec HexSecret) MarshalText() ([]byte, error) {
1✔
45
        return []byte("<omitted>"), nil
1✔
46
}
1✔
47

48
func (sec HexSecret) MarshalBSON() ([]byte, error) {
2✔
49
        cStr := crypto.String(sec)
2✔
50
        return (&cStr).MarshalBSON()
2✔
51
}
2✔
52

53
func (sec *HexSecret) UnmarshalBSON(b []byte) error {
2✔
54
        cStr := (*crypto.String)(sec)
2✔
55
        return cStr.UnmarshalBSON(b)
2✔
56
}
2✔
57

58
type HTTPCredentials struct {
59
        URL    string     `json:"url,omitempty" bson:"url,omitempty"`
60
        Secret *HexSecret `json:"secret,omitempty" bson:"secret,omitempty"`
61

62
        // private field toggling validation verbosity
63
        // - only set if unmarshaled from JSON
64
        validateAddr bool
65
}
66

67
func (cred *HTTPCredentials) UnmarshalJSON(b []byte) error {
3✔
68
        type creds HTTPCredentials
3✔
69
        if err := json.Unmarshal(b, (*creds)(cred)); err != nil {
4✔
70
                return err
1✔
71
        }
1✔
72
        cred.validateAddr = true
3✔
73
        return nil
3✔
74
}
75

76
func (cred HTTPCredentials) validateURL(interface{}) error {
3✔
77
        uu, err := url.Parse(cred.URL)
3✔
78
        if err != nil {
4✔
79
                return err
1✔
80
        }
1✔
81
        c := config.Config
3✔
82
        if c.GetBool(dconfig.SettingDomainSkipVerify) {
3✔
NEW
UNCOV
83
                return nil
×
NEW
UNCOV
84
        }
×
85
        if !cred.validateAddr {
6✔
86
                return nil
3✔
87
        }
3✔
88
        ips, err := net.LookupIP(uu.Hostname())
3✔
89
        if err != nil {
4✔
90
                return err
1✔
91
        }
1✔
92
        for _, ip := range ips {
6✔
93
                if !inet.IsGlobalUnicast(ip) {
4✔
94
                        return net.InvalidAddrError(
1✔
95
                                "hostname resolves to reserved address",
1✔
96
                        )
1✔
97
                }
1✔
98
        }
99
        return nil
3✔
100
}
101

102
func (cred HTTPCredentials) Validate() error {
3✔
103
        return validation.ValidateStruct(&cred,
3✔
104
                validation.Field(&cred.URL,
3✔
105
                        validation.Required,
3✔
106
                        validation.By(cred.validateURL),
3✔
107
                ),
3✔
108
        )
3✔
109
}
3✔
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