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"
"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: "/set_key_store",
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
}
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)
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
}
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)
}
}
func (qs *QkdnManagerServer) handleSetKeyStore(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("Handler for SetKeyStore got request from: %s", r.RemoteAddr)
var parsedRequest struct {
KeyFillLevel int `json:"key_fill_level"`
PeerIDs []string `json:"peer_ids"`
Fetch bool `json:"fetch"`
err := json.NewDecoder(r.Body).Decode(&parsedRequest)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
logrus.Debugf("KeyFillLevel: %d, PeerIDs: %v, Fetch: %t", parsedRequest.KeyFillLevel, parsedRequest.PeerIDs, parsedRequest.Fetch)
for _, peerID := range parsedRequest.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 := peer.QuantumModule()
if parsedRequest.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 parsedRequest.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
}