Skip to content
Snippets Groups Projects
Select Git revision
  • c95035b1c6e34d6a33a98a78fe143d12086d1c71
  • master default protected
  • enhance-ping-utility
  • renovate/rabbitmq-4.x
  • renovate/mongo-8.x
  • renovate/golangci-golangci-lint-2.x
  • renovate/google.golang.org-protobuf-1.x
  • renovate/google.golang.org-grpc-1.x
  • renovate/golang.org-x-sys-0.x
  • renovate/github.com-openconfig-goyang-1.x
  • renovate/github.com-oapi-codegen-runtime-1.x
  • renovate/google.golang.org-genproto-googleapis-api-digest
  • renovate/code.fbi.h-da.de-danet-gnmi-target-digest
  • 54-ecoc-infrastructure-setup
  • kai_masterthesis
  • martin-quipsec
  • request-health-checks-for-peers
  • 44-block-incoming-keys-if-exceeding-max-key-fill-level
  • add-inventory-manager
  • extend-intercom-with-aes-auth-tag
  • debug-ci
  • tud-testing-1
22 results

server.go

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    server.go 5.93 KiB
    package qkdnmanager
    
    import (
    	"encoding/json"
    	"errors"
    	"fmt"
    	"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",
    }
    
    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)
    }
    
    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
    	for _, peer := range qs.kms.KmsPeers {
    		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)
    
    	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 := qs.kms.FindPeerUuid(peerUUID)
    		if peer == nil {
    			http.Error(w, fmt.Sprintf("No peer for ID: %s found", peerID), 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" {
    			eqm.Initialize()
    
    			w.WriteHeader(http.StatusOK)
    			_, err = w.Write([]byte("OK\n"))
    			if err != nil {
    				logrus.Error(err)
    			}
    			return
    		} else if fetch == "false" {
    			eqm.StopKeyFetching()
    			eqm.KeyStore().Reset()
    		}
    	}
    
    	w.WriteHeader(http.StatusOK)
    	_, err = w.Write([]byte("OK\n"))
    	if err != nil {
    		logrus.Error(err)
    	}
    }