Newer
Older
package qkdnmanager
import (
"encoding/json"
"errors"
"net/http"
"time"
"code.fbi.h-da.de/danet/quant/goKMS/config"
"code.fbi.h-da.de/danet/quant/goKMS/kms"
"code.fbi.h-da.de/danet/quant/goKMS/kms/peers"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
const APIPrefix = "/api/v1"
const operationalState = "operationalState"
const qkdInterface = "qkdInterface"
const interCKMSInterface = "interCKMSInterface"
const akmsInterface = "akmsInterface"
const setKeyStore = "setKeyStore"
var endpoints = map[string]string{
operationalState: "/operationalState",
qkdInterface: "/qkdInterface",
interCKMSInterface: "/interCKMSInterface",
akmsInterface: "/akmsInterface",
setKeyStore: "/setKeyStore",
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
}
type QkdnManagerServer struct {
Server *http.Server
kms *kms.KMS
}
type KmsData struct {
KMS_ID uuid.UUID `json:"KMS_ID"`
Peers []Peer `json:"peers,omitempty"`
Running bool `json:"running,omitempty"`
}
type Peer struct {
Peer_ID uuid.UUID `json:"peer_ID"`
Running bool `json:"running"`
}
func NewQkdnManagerServer(kms *kms.KMS) *QkdnManagerServer {
return &QkdnManagerServer{
Server: &http.Server{
ReadHeaderTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 30 * time.Second,
},
kms: kms,
}
}
func (qs *QkdnManagerServer) InitHTTPServer(conf config.QkdnManagerServer) {
logrus.Info("Setting up server for management information...")
mux := http.NewServeMux()
qs.addHandlersToMux(mux)
qs.Server.Addr = conf.Address
qs.Server.Handler = mux
go qs.startServer()
logrus.Infof("QKDN manager server running on %s ...", conf.Address)
}
func (qs *QkdnManagerServer) addHandlersToMux(mux *http.ServeMux) {
// add additional handlers if necessary
mux.HandleFunc(APIPrefix+endpoints[operationalState], qs.handleHttpOperationalState)
mux.HandleFunc(APIPrefix+endpoints[qkdInterface], qs.handleHttpQkdInterface)
mux.HandleFunc(APIPrefix+endpoints[interCKMSInterface], qs.handleHttpInterCKMSInterface)
mux.HandleFunc(APIPrefix+endpoints[akmsInterface], qs.handleHttpAkmsInterface)
mux.HandleFunc(APIPrefix+endpoints[setKeyStore], qs.handleSetKeyStore)
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
}
func (qs *QkdnManagerServer) startServer() {
if err := qs.Server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) {
logrus.Error(err)
}
logrus.Info("Stopped serving new connections.")
}
func (qs *QkdnManagerServer) handleHttpOperationalState(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Handler for OperationalState got request from: %s", r.RemoteAddr)
data := &KmsData{
KMS_ID: qs.kms.GetID(),
Running: true,
}
jsonData, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
logrus.Error(err)
}
}
func (qs *QkdnManagerServer) handleHttpQkdInterface(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Handler for QkdInterface got request from: %s", r.RemoteAddr)
data := &KmsData{
KMS_ID: qs.kms.GetID(),
Running: true,
}
jsonData, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
logrus.Error(err)
}
}
func (qs *QkdnManagerServer) handleHttpInterCKMSInterface(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Handler for InterCKMSInterface got request from: %s", r.RemoteAddr)
var peers []Peer
peerMapCopy := qs.kms.PeersDeepCopy()
for _, peer := range peerMapCopy {
peers = append(peers, Peer{
Peer_ID: peer.GetKmsPeerId(),
Running: true,
})
}
data := &KmsData{
KMS_ID: qs.kms.GetID(),
Peers: peers,
}
jsonData, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
logrus.Error(err)
}
}
func (qs *QkdnManagerServer) handleHttpAkmsInterface(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Handler for AkmsInterface got request from: %s", r.RemoteAddr)
data := &KmsData{
KMS_ID: qs.kms.GetID(),
Running: true,
}
jsonData, err := json.Marshal(data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(jsonData)
if err != nil {
logrus.Error(err)
}
}
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
func (qs *QkdnManagerServer) handleSetKeyStore(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Handler for SetKeyStore got request from: %s", r.RemoteAddr)
err := r.ParseForm()
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
keyFillLevel := r.Form.Get("KeyFillLevel")
peerIDs, ok := r.Form["PeerIds"]
if !ok {
http.Error(w, fmt.Sprintf("No peer IDs provided."), http.StatusBadRequest)
return
}
fetch := r.Form.Get("Fetch")
logrus.Debugf("KeyFillLevel: %s, PeerIDs: %v, Fetch: %s", keyFillLevel, peerIDs, fetch)
for _, peerID := range peerIDs {
peerUUID, err := uuid.Parse(peerID)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
peer, err := qs.kms.FindPeerById(peerUUID.String())
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
eqm, ok := peer.QuantumModule().(*peers.ETSI014HTTPQuantumModule)
if !ok {
http.Error(w, fmt.Sprintf("QuantumModule is not of Type ETSI014"), http.StatusBadRequest)
return
}
if fetch == "true" {
if err := eqm.Initialize(); err != nil {
http.Error(w, fmt.Sprintf("Failed to restart fetching for quantum module of peer: %s", peerID), http.StatusBadRequest)
return
}
} else if fetch == "false" {
eqm.SetActive(false)
if err := peer.ResetKeyStore(qs.kms.GetID().String()); err != nil {
eqm.SetActive(true)
http.Error(w, fmt.Sprintf("Failed to reset keystore for quantum module of peer: %s", peerID), http.StatusBadRequest)
return
}