Debian Codegraph @

creds/netrc.go

raw symbols SCIP
1 package creds
2
3 import (
4 "net"
5 "os"
6 "path/filepath"
7 "runtime"
8 "strings"
9 "sync"
10
11 "github.com/git-lfs/git-lfs/v3/config"
12 "github.com/git-lfs/go-netrc/netrc"
13 "github.com/rubyist/tracerx"
14 )
15
16 type NetrcFinder interface {
17 FindMachine(string, string) *netrc.Machine
18 }
19
20 const netrcBasename = ".netrc"
21 const netrcAltBasename = "_netrc"
22
23 func ParseNetrc(osEnv config.Environment) (NetrcFinder, string, error) {
24 home, _ := osEnv.Get("HOME")
25 if len(home) == 0 {
26 return &noFinder{}, "", nil
27 }
28
29 nrcfilename := filepath.Join(home, netrcBasename)
30 if _, err := os.Stat(nrcfilename); err != nil {
31 // If on Windows, try _netrc instead
32 if runtime.GOOS == "windows" {
33 altFilename := filepath.Join(home, netrcAltBasename)
34 if _, errAlt := os.Stat(altFilename); errAlt != nil {
35 return &noFinder{}, altFilename, nil
36 }
37 nrcfilename = altFilename
38 } else {
39 return &noFinder{}, nrcfilename, nil
40 }
41 }
42
43 f, err := netrc.ParseFile(nrcfilename)
44 return f, nrcfilename, err
45 }
46
47 type noFinder struct{}
48
49 func (f *noFinder) FindMachine(host string, loginName string) *netrc.Machine {
50 return nil
51 }
52
53 // NetrcCredentialHelper retrieves credentials from a .netrc file
54 type netrcCredentialHelper struct {
55 netrcFinder NetrcFinder
56 mu sync.Mutex
57 skip map[string]bool
58 }
59
60 var defaultNetrcFinder = &noFinder{}
61
62 // NewNetrcCredentialHelper creates a new netrc credential helper using a
63 // .netrc file gleaned from the OS environment
64 func newNetrcCredentialHelper(osEnv config.Environment) *netrcCredentialHelper {
65 netrcFinder, netrcfile, err := ParseNetrc(osEnv)
66 if err != nil {
67 tracerx.Printf("bad netrc file %s: %s", netrcfile, err)
68 return nil
69 }
70
71 if netrcFinder == nil {
72 netrcFinder = defaultNetrcFinder
73 }
74
75 return &netrcCredentialHelper{netrcFinder: netrcFinder, skip: make(map[string]bool)}
76 }
77
78 func (c *netrcCredentialHelper) Fill(what Creds) (Creds, error) {
79 host, err := getNetrcHostname(FirstEntryForKey(what, "host"))
80 if err != nil {
81 return nil, credHelperNoOp
82 }
83 c.mu.Lock()
84 defer c.mu.Unlock()
85 if c.skip[host] {
86 return nil, credHelperNoOp
87 }
88 if machine := c.netrcFinder.FindMachine(host, FirstEntryForKey(what, "username")); machine != nil {
89 creds := make(Creds)
90 creds["username"] = []string{machine.Login}
91 creds["password"] = []string{machine.Password}
92 creds["protocol"] = what["protocol"]
93 creds["host"] = what["host"]
94 creds["scheme"] = what["scheme"]
95 creds["path"] = what["path"]
96 creds["source"] = []string{"netrc"}
97 tracerx.Printf("netrc: git credential fill (%q, %q, %q, %q)",
98 FirstEntryForKey(what, "protocol"),
99 FirstEntryForKey(what, "host"), machine.Login,
100 FirstEntryForKey(what, "path"))
101 return creds, nil
102 }
103
104 return nil, credHelperNoOp
105 }
106
107 func getNetrcHostname(hostname string) (string, error) {
108 if strings.Contains(hostname, ":") {
109 host, _, err := net.SplitHostPort(hostname)
110 if err != nil {
111 tracerx.Printf("netrc: error parsing %q: %s", hostname, err)
112 return "", err
113 }
114 return host, nil
115 }
116
117 return hostname, nil
118 }
119
120 func (c *netrcCredentialHelper) Approve(what Creds) error {
121 if FirstEntryForKey(what, "source") == "netrc" {
122 host, err := getNetrcHostname(FirstEntryForKey(what, "host"))
123 if err != nil {
124 return credHelperNoOp
125 }
126 tracerx.Printf("netrc: git credential approve (%q, %q, %q)",
127 FirstEntryForKey(what, "protocol"),
128 FirstEntryForKey(what, "host"),
129 FirstEntryForKey(what, "path"))
130 c.mu.Lock()
131 c.skip[host] = false
132 c.mu.Unlock()
133 return nil
134 }
135 return credHelperNoOp
136 }
137
138 func (c *netrcCredentialHelper) Reject(what Creds) error {
139 if FirstEntryForKey(what, "source") == "netrc" {
140 host, err := getNetrcHostname(what["host"][0])
141 if err != nil {
142 return credHelperNoOp
143 }
144
145 tracerx.Printf("netrc: git credential reject (%q, %q, %q)",
146 what["protocol"], what["host"], what["path"])
147 c.mu.Lock()
148 c.skip[host] = true
149 c.mu.Unlock()
150 return nil
151 }
152 return credHelperNoOp
153 }