-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
180 lines (146 loc) · 3.8 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"log/syslog"
"net/url"
"os"
"os/user"
"strings"
"gopkg.in/ini.v1"
)
type SshAuthResponse struct {
Href string `json:"href"`
Header map[string]string `json:"header"`
}
type LdapConfiguration struct {
Urls []string
Groups []string
BaseDn string
Cacert string
}
type LfsConfiguration struct {
Url string
User string
Password string
}
type Configuration struct {
Lfs *LfsConfiguration
Ldap *LdapConfiguration
}
var errNotAuthorised = errors.New("You're not authorised for this operation")
func main() {
op, ns, repo, err := figureOutArguments(os.Args[1:])
if err != nil {
errOut(err.Error())
}
configFile := "/etc/git-lfs-authenticate.conf"
if configOverride := os.Getenv("GIT_LFS_AUTHENTICATE_CONFIG"); configOverride != "" {
configFile = configOverride
}
cfg, err := readConfig(configFile)
if err != nil {
errOutf("Failed to read %q: %s", configFile, err)
}
user, err := user.Current()
if err != nil {
errOut(err.Error())
}
ok, err := checkMembership(cfg.Ldap, user.Username)
if err != nil {
errOut(err.Error())
}
slg, _ := syslog.New(syslog.LOG_INFO|syslog.LOG_LOCAL0, os.Args[0])
if !ok {
if slg != nil {
slg.Write([]byte(fmt.Sprintf("user %q not authorised to %q from %s/%s", user.Username, op, ns, repo)))
}
errOut(errNotAuthorised.Error())
}
u, err := url.Parse(cfg.Lfs.Url)
if err != nil {
errOut(err.Error())
}
u.Path = "/" + ns + "/" + repo
res := &SshAuthResponse{
Href: u.String(),
Header: map[string]string{
"Authorization": "Basic " + httpBasicAuth(cfg.Lfs.User, cfg.Lfs.Password),
"X-Lfs-From-Ssh": "yes",
"X-Lfs-On-Behalf-Of": user.Username,
},
}
b, err := json.Marshal(res)
if err != nil {
errOut(err.Error())
}
os.Stdout.Write(b)
if slg != nil {
slg.Write([]byte(fmt.Sprintf("user %q authorised to %q from %s/%s", user.Username, op, ns, repo)))
}
}
func readConfig(file string) (*Configuration, error) {
cfg, err := ini.Load(file)
if err != nil {
return nil, err
}
section := cfg.Section("Ldap")
ldap := &LdapConfiguration{
Groups: section.Key("Groups").Strings(","),
Urls: section.Key("Urls").Strings(","),
BaseDn: section.Key("Base").String(),
Cacert: section.Key("Cacert").String(),
}
section = cfg.Section("Lfs")
lfs := &LfsConfiguration{
Url: section.Key("Url").String(),
User: section.Key("User").String(),
Password: section.Key("Password").String(),
}
err = cfg.Section("Ldap").MapTo(ldap)
if err != nil {
return nil, err
}
err = cfg.Section("Lfs").MapTo(lfs)
if err != nil {
return nil, err
}
return &Configuration{
Ldap: ldap,
Lfs: lfs,
}, nil
}
func figureOutArguments(args []string) (string, string, string, error) {
if len(args) < 2 {
return "", "", "", fmt.Errorf("Expected at least 2 arguments, got %d", len(args))
}
if len(args) > 3 {
// legacy API also passes OID but we don't need it here
return "", "", "", fmt.Errorf("Expected no more than 3 arguments, got %d", len(args))
}
path, operation := args[0], args[1]
if operation != "upload" && operation != "download" {
err := fmt.Sprintf("Unknown LFS operation: %q, expected %q or %q", operation, "download", "upload")
return "", "", "", errors.New(err)
}
parts := strings.Split(path, "/")
if len(parts) != 2 {
err := fmt.Sprintf("Cannot figure out namespace and repository from path: %q", path)
return "", "", "", errors.New(err)
}
return operation, parts[0], parts[1], nil
}
func httpBasicAuth(user, password string) string {
pair := user + ":" + password
return base64.StdEncoding.EncodeToString([]byte(pair))
}
func errOutf(format string, args ...interface{}) {
fmt.Fprintf(os.Stderr, format+"\n", args...)
os.Exit(1)
}
func errOut(msg string) {
fmt.Fprint(os.Stderr, msg+"\n")
os.Exit(1)
}