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

mendersoftware / iot-manager / 1401702106

05 Aug 2024 08:32PM UTC coverage: 87.577%. Remained the same
1401702106

push

gitlab-ci

web-flow
Merge pull request #295 from mendersoftware/dependabot/docker/docker-dependencies-03b04ac819

chore: bump golang from 1.22.4-alpine3.19 to 1.22.5-alpine3.19 in the docker-dependencies group

3264 of 3727 relevant lines covered (87.58%)

11.44 hits per line

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

100.0
/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/iot-manager/crypto"
25
        inet "github.com/mendersoftware/iot-manager/internal/net"
26

27
        validation "github.com/go-ozzo/ozzo-validation/v4"
28
)
29

30
type HexSecret crypto.String
31

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

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

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

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

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

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

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

74
func (cred HTTPCredentials) validateURL(interface{}) error {
18✔
75
        uu, err := url.Parse(cred.URL)
18✔
76
        if err != nil {
19✔
77
                return err
1✔
78
        }
1✔
79
        if !cred.validateAddr {
25✔
80
                return nil
8✔
81
        }
8✔
82
        ips, err := net.LookupIP(uu.Hostname())
9✔
83
        if err != nil {
10✔
84
                return err
1✔
85
        }
1✔
86
        for _, ip := range ips {
17✔
87
                if !inet.IsGlobalUnicast(ip) {
10✔
88
                        return net.InvalidAddrError(
1✔
89
                                "hostname resolves to reserved address",
1✔
90
                        )
1✔
91
                }
1✔
92
        }
93
        return nil
7✔
94
}
95

96
func (cred HTTPCredentials) Validate() error {
18✔
97
        return validation.ValidateStruct(&cred,
18✔
98
                validation.Field(&cred.URL,
18✔
99
                        validation.Required,
18✔
100
                        validation.By(cred.validateURL),
18✔
101
                ),
18✔
102
        )
18✔
103
}
18✔
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