-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcertauthz.go
79 lines (70 loc) · 1.88 KB
/
certauthz.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
package traefik_certauthz
import (
"context"
"fmt"
"regexp"
"net/http"
"strings"
)
type Config struct {
Regex string
Domains []string
}
func CreateConfig() *Config {
return &Config{
Regex: "",
Domains: nil,
}
}
type CertAuthz struct {
next http.Handler
regex *regexp.Regexp
name string
}
func New(ctx context.Context, next http.Handler, config *Config, name string) (http.Handler, error) {
if config.Regex != "" && len(config.Domains) != 0 {
return nil, fmt.Errorf("You must specify either a regex or a domain list, not both")
}
if config.Regex == "" && len(config.Domains) == 0 {
return nil, fmt.Errorf("You must specify either a regex or a domain list")
}
var to_compile = config.Regex
if len(config.Domains) > 0 {
domains_regexes := []string{}
for _, domain := range config.Domains {
// Rudimentary check for invalid chars in domain
// Invalid domain names are still possible
var invalid = regexp.MustCompile(`(?i)[^a-z0-9\-\.\*]`)
if invalid.MatchString(domain) {
return nil, fmt.Errorf("Invalid characters in domain name: %v", domain)
}
domain = strings.Replace(domain, ".", "[.]", -1)
domain = strings.Replace(domain, "*", "[^.]+", -1)
domain = "(?i)^" + domain + "$"
domains_regexes = append(domains_regexes, domain)
}
to_compile = strings.Join(domains_regexes, "|")
}
var compiled, err = regexp.Compile(to_compile)
if err != nil {
return nil, err
}
return &CertAuthz{
regex: compiled,
next: next,
name: name,
}, nil
}
func (a *CertAuthz) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
if req.TLS != nil && len(req.TLS.PeerCertificates) != 0 {
var cert = req.TLS.PeerCertificates[0] // leaf certificate
for _, name := range cert.DNSNames {
if a.regex.MatchString(name) {
a.next.ServeHTTP(rw, req)
return
}
}
}
http.Error(rw, "No matching DNSNames", http.StatusForbidden)
return
}