/
cert-manager.go
177 lines (137 loc) · 3.27 KB
/
cert-manager.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
package pbc
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path/filepath"
)
type Profile struct {
Name string `json:"name"`
}
func (profile Profile) getBasePath() string {
return filepath.Join(getDataDir(), "profiles", profile.Name)
}
func (profile Profile) getKeyPath() string {
return filepath.Join(profile.getBasePath(), "key.pem")
}
func (profile Profile) getCertPath() string {
return filepath.Join(profile.getBasePath(), "cert.pem")
}
func ListProfiles() []Profile {
indexPath := filepath.Join(getDataDir(), "index.json")
file, err := os.Open(indexPath)
if err != nil {
file, err := os.Create(indexPath)
if err != nil {
return []Profile{}
}
json.NewEncoder(file).Encode([]Profile{})
return []Profile{}
}
data := []Profile{}
err = json.NewDecoder(file).Decode(&data)
if err != nil {
return []Profile{}
}
return data
}
func SaveProfiles(profiles []Profile) {
file, err := os.Create(filepath.Join(getDataDir(), "index.json"))
if err != nil {
fmt.Println(err)
return
}
json.NewEncoder(file).Encode(profiles)
}
func AddProfile(profile Profile) {
SaveProfiles(append(ListProfiles(), profile))
}
func DelProfile(name string) {
profiles := ListProfiles()
mfd := -1
for i, p := range profiles {
if p.Name == name {
mfd = i
}
}
if mfd != -1 {
os.RemoveAll(profiles[mfd].getBasePath())
SaveProfiles(append(profiles[:mfd], profiles[mfd+1:]...))
}
}
func GetProfile(name string) (Profile, error) {
profiles := ListProfiles()
for _, prof := range profiles {
if prof.Name == name {
return prof, nil
}
}
return Profile{}, errors.New("Profile not found")
}
func CreateProfile(name, p12path string) (Profile, error) {
for _, p := range ListProfiles() {
if p.Name == name {
return Profile{}, errors.New("Profile exists")
}
}
profile := Profile{
Name: name,
}
dir := filepath.Join(getDataDir(), "profiles", profile.Name)
os.MkdirAll(dir, 0755)
os.Chmod(dir, 0755)
createCertKey(p12path, profile.getCertPath(), profile.getKeyPath())
return profile, nil
}
func copyWWDR() error {
if _, err := os.Stat(filepath.Join(getDataDir(), "wwdr.pem")); err != nil {
wwdr, err := os.Open(ExpandPath("${GOPATH}/src/stash.tsrapplabs.com/ut/pbc/wwdr.pem"))
if err != nil {
return err
}
defer wwdr.Close()
file, err := os.Create(filepath.Join(getDataDir(), "wwdr.pem"))
if err != nil {
return err
}
defer file.Close()
_, err = io.Copy(file, wwdr)
return err
}
return nil
}
func InitDataDir() {
if !exists(getDataDir()) {
fmt.Println("Adding datadir")
os.MkdirAll(getDataDir(), 0750)
os.Chmod(getDataDir(), 0750)
}
if !exists(filepath.Join(getDataDir(), "index.json")) {
fmt.Println("Adding index.json")
file, err := os.Create(filepath.Join(getDataDir(), "index.json"))
if err == nil {
_, err := file.WriteString("{}")
if err != nil {
fmt.Println(err)
}
file.Close()
}
}
if !exists(filepath.Join(getDataDir(), "wwdr.pem")) {
fmt.Println("Copying wwdr.pem")
if err := copyWWDR(); err != nil {
fmt.Println(err)
}
}
if !exists(filepath.Join(getDataDir(), "profiles")) {
fmt.Println("Adding profiles")
os.MkdirAll(filepath.Join(getDataDir(), "profiles"), 0750)
os.Chmod(getDataDir(), 0750)
}
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}