-
Notifications
You must be signed in to change notification settings - Fork 0
/
cert.go
79 lines (74 loc) · 1.67 KB
/
cert.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 main
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"strings"
)
func normalizeCertURL(inURL string) (outURL *url.URL, err error) {
if !strings.HasPrefix(inURL, "http://") && !strings.HasPrefix(inURL, "https://") {
err = fmt.Errorf("Provided value must be URL starting with 'http(s)://'. Got: %s", inURL)
return
}
outURL, err = url.Parse(inURL)
if err != nil {
return
}
outURL.Scheme = "https"
return
}
func CertLoad(location string) ([]byte, error) {
if strings.HasPrefix(location, "http://") || strings.HasPrefix(location, "https://") {
return CertLoadFromURL(location)
}
return CertLoadFromFile(location)
}
func CertLoadFromFile(filename string) (cert []byte, err error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("cannot open cert file '%s': %s", filename, err)
}
defer file.Close()
cert, err = io.ReadAll(file)
if err != nil {
return nil, fmt.Errorf("cannot read cert file '%s': %s", filename, err)
}
return
}
func CertLoadFromURL(inURL string) (cert []byte, err error) {
u, err := normalizeCertURL(inURL)
if err != nil {
return
}
isAccessURL, appInfo, err := getCloudflareAccessAppInfo(u)
if err != nil {
return
}
var accessAuthToken string
if isAccessURL {
accessAuthToken, err = getCloudflareAccessToken(u, appInfo)
if err != nil {
return
}
}
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return
}
if isAccessURL && accessAuthToken != "" {
req.Header.Add("cf-access-token", accessAuthToken)
}
client := http.Client{}
resp, err := client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
cert, err = io.ReadAll(resp.Body)
if err != nil {
return
}
return
}