forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
import.go
62 lines (51 loc) · 1.14 KB
/
import.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
package systemtemplate
import (
"crypto/md5"
"encoding/base64"
"encoding/hex"
"text/template"
"io"
"crypto/sha256"
"strings"
"github.com/rancher/rancher/pkg/settings"
)
var (
t = template.Must(template.New("import").Parse(templateSource))
)
type context struct {
CAChecksum string
AgentImage string
AuthImage string
TokenKey string
Token string
URL string
URLPlain string
}
func SystemTemplate(resp io.Writer, agentImage, authImage, token, url string) error {
d := md5.Sum([]byte(token))
tokenKey := hex.EncodeToString(d[:])[:7]
if authImage == "fixed" {
authImage = settings.AuthImage.Get()
}
context := &context{
CAChecksum: CAChecksum(),
AgentImage: agentImage,
AuthImage: authImage,
TokenKey: tokenKey,
Token: base64.StdEncoding.EncodeToString([]byte(token)),
URL: base64.StdEncoding.EncodeToString([]byte(url)),
URLPlain: url,
}
return t.Execute(resp, context)
}
func CAChecksum() string {
ca := settings.CACerts.Get()
if ca != "" {
if !strings.HasSuffix(ca, "\n") {
ca += "\n"
}
digest := sha256.Sum256([]byte(ca))
return hex.EncodeToString(digest[:])
}
return ""
}