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

m-lab / uuid / #622601771

06 Jun 2024 01:19AM UTC coverage: 91.765%. First build
#622601771

Pull #15

travis-ci

Pull Request #15: Allow prefix file to be passed in via function call

0 of 7 new or added lines in 1 file covered. (0.0%)

78 of 85 relevant lines covered (91.76%)

3.32 hits per line

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

75.86
/uuid.go
1
// Package uuid provides functions that create a consistent globally unique
2
// UUID for a given TCP socket.  The package defines a new command-line flag
3
// `-uuid-prefix-file`, and that file and its contents should be set up prior
4
// to invoking any command which uses this library.
5
//
6
// This implementation only works reliably on Linux systems.
7
package uuid
8

9
import (
10
        "flag"
11
        "fmt"
12
        "io/ioutil"
13
        "net"
14
        "os"
15

16
        "github.com/m-lab/uuid/prefix"
17
        "github.com/m-lab/uuid/socookie"
18

19
        "github.com/m-lab/go/flagx"
20
)
21

22
const (
23
        // Whenever there is an error We return this value instead of the empty
24
        // string. We do this in an effort to detect when client code
25
        // accidentally uses the returned UUID even when it should not have.
26
        //
27
        // This is borne out of past experience, most notably an incident where
28
        // returning an empty string and an error condition caused the
29
        // resulting code to create a file named ".gz", which was (thanks to
30
        // shell-filename-hiding rules) a very hard bug to uncover.  If a file
31
        // is ever named "INVALID_UUID.gz", it will be much easier to detect
32
        // that there is a problem versus just ".gz"
33
        invalidUUID = "INVALID_UUID"
34
)
35

36
var (
37
        // uuidPrefix is the UUID prefix derived from a command-line flag specifying
38
        // the file which contains the UUID prefix. Ideally the file will be something
39
        // like "/var/local/uuid/prefix", and the contents of the file will be bytes
40
        // that resemble "host.example.com_45353453".
41
        //
42
        // By default it is a string generated in an unsafe manner that contains the
43
        // substring "_unsafe_" in it. This is not great, but it is better than the
44
        // default of the empty string, and it allows people to use code which uses
45
        // this library without having to set up a well-known location.
46
        uuidPrefix = flagx.FileBytes(prefix.UnsafeString())
47
)
48

49
func init() {
1✔
50
        flag.Var(&uuidPrefix, "uuid-prefix-file", "The file holding the UUID prefix for sockets created in this network namespace.")
1✔
51
}
1✔
52

53
// SetUUIDPrefixFile allows the prefix filename to be passed in via a function
54
// call instead of via the command line. This function is useful for programs
55
// with custom command lines that want to use this package.
NEW
56
func SetUUIDPrefixFile(filename string) error {
×
NEW
57
        fileContents, err := ioutil.ReadFile(filename)
×
NEW
58
        if err != nil {
×
NEW
59
                return err
×
NEW
60
        }
×
NEW
61
        uuidPrefix = fileContents
×
NEW
62
        return nil
×
63
}
64

65
// getCookie returns the cookie (the UUID) associated with a socket. For a given
66
// boot of a given hostname, this UUID is guaranteed to be unique (until the
67
// host receives more than 2^64 connections without rebooting).
68
//
69
// This implementation only works reliably and correctly on Linux systems.
70
func getCookie(file *os.File) (uint64, error) {
3✔
71
        return socookie.Get(file)
3✔
72
}
3✔
73

74
// FromTCPConn returns a string that is a globally unique identifier for the
75
// socket held by the passed-in TCPConn (assuming hostnames are unique).
76
//
77
// This function will never return the empty string, but the returned string
78
// value should only be used if the error is nil.
79
func FromTCPConn(t *net.TCPConn) (string, error) {
3✔
80
        file, err := t.File()
3✔
81
        if err != nil {
4✔
82
                return invalidUUID, err
1✔
83
        }
1✔
84
        defer file.Close()
2✔
85
        return FromFile(file)
2✔
86
}
87

88
// FromFile returns a string that is a globally unique identifier for the socket
89
// represented by the os.File pointer.
90
//
91
// This function will never return the empty string, but the returned string
92
// value should only be used if the error is nil.
93
func FromFile(file *os.File) (string, error) {
3✔
94
        cookie, err := getCookie(file)
3✔
95
        if err != nil {
4✔
96
                return invalidUUID, err
1✔
97
        }
1✔
98
        return FromCookie(cookie), nil
2✔
99
}
100

101
// FromCookie returns a string that is a globally unique identifier for the
102
// passed-in socket cookie.
103
func FromCookie(cookie uint64) string {
2✔
104
        return fmt.Sprintf("%s_%016X", string(uuidPrefix), cookie)
2✔
105
}
2✔
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