-
Notifications
You must be signed in to change notification settings - Fork 2
/
tismd.go
127 lines (115 loc) · 2.75 KB
/
tismd.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
// main package for the Immutable Secrets Managements API. This service will decrypt and return a pgp encrypted messages if the requester has a valid jwt token that lists the pgp key's id in it's scope.
package main
import (
"crypto/tls"
"github.com/deuscapturus/tism/config"
"github.com/deuscapturus/tism/encryption"
"github.com/deuscapturus/tism/mytls"
"github.com/deuscapturus/tism/randid"
"github.com/deuscapturus/tism/request"
"github.com/deuscapturus/tism/token"
"log"
"net/http"
)
func init() {
// Generate admin token if requested
if config.Config.GenAdminToken {
adminToken, err := token.GenerateToken(
[]string{"ALL"},
99999999999,
randid.Generate(32),
1,
)
if err != nil {
log.Println(err)
}
log.Println(adminToken)
}
// Generate new TLS cert if requested
if config.Config.GenCert {
mytls.Generate(config.Config.TLSDir)
}
}
// main function.
func main() {
if !config.Config.NoServerStart {
server()
}
}
// server Start http server and provide routes.
func server() {
server := http.Server{
Addr: config.Config.Address + ":" + config.Config.Port,
TLSConfig: mytls.TLSConfig,
TLSNextProto: make(map[string]func(*http.Server, *tls.Conn, http.Handler), 0),
}
//Routes
http.Handle("/decrypt", Handle(
request.Parse,
token.Parse,
encryption.SetMyKeyRing,
encryption.Decrypt,
))
http.Handle("/encrypt", Handle(
request.Parse,
token.Parse,
encryption.SetMyKeyRing,
encryption.Encrypt,
))
http.Handle("/token/info", Handle(
request.Parse,
token.Parse,
token.Info,
))
http.Handle("/token/new", Handle(
request.Parse,
token.Parse,
token.IsAdmin,
token.New,
))
http.Handle("/key/new", Handle(
request.Parse,
token.Parse,
token.IsAdmin,
encryption.NewKey,
))
http.Handle("/key/list", Handle(
request.Parse,
token.Parse,
encryption.SetMyKeyRing,
encryption.ListKeys,
))
http.Handle("/key/get", Handle(
request.Parse,
token.Parse,
encryption.SetMyKeyRing,
encryption.GetKey,
))
http.Handle("/key/delete", Handle(
request.Parse,
token.Parse,
token.IsAdmin,
encryption.SetMyKeyRing,
encryption.DeleteKey,
))
http.Handle("/", http.FileServer(http.Dir(config.Config.WebClientPath)))
log.Fatal(server.ListenAndServeTLS(
config.Config.TLSDir+"/"+config.Config.TLSCertFile,
config.Config.TLSDir+"/"+config.Config.TLSKeyFile,
))
}
type Handler func(w http.ResponseWriter, rc http.Request) (error, http.Request)
// Handle is middleware chain handler
func Handle(handlers ...Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rc := *r.WithContext(r.Context())
var err error
for _, handler := range handlers {
err, rc = handler(w, rc)
if err != nil {
w.Write([]byte(err.Error()))
return
}
}
})
}