diff --git a/.golangci.yml b/.golangci.yml index 23fdaf7ea5e24b760cbf8952694a04085e41205e..6e56a1c19e01e0f958299813accb800e54ef8906 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -11,6 +11,7 @@ run: - ekms/model - ekms/models - artifacts/ + - ekms/api/go skip-dirs-default: true #skip-files: # - http.go diff --git a/ekms/handlers/system/hostnameHandler.go b/ekms/handlers/system/hostnameHandler.go index 3cd079b0db67edb323201a8888fcf6e80a6fcfc7..e03e5776fa92c793b8812583a06a3f953115b815 100644 --- a/ekms/handlers/system/hostnameHandler.go +++ b/ekms/handlers/system/hostnameHandler.go @@ -15,7 +15,6 @@ type HostnameHandler struct { name string paths map[string]struct{} osClient osclient.Osclient - weight int } func NewHostnameHandler() *HostnameHandler { diff --git a/ekms/internal/kms/kms.go b/ekms/internal/kms/kms.go index ae199a75de4787200a6dec665a36d2bb843726be..93cdb536663d9427d10517f55b2b3e535e8255e4 100644 --- a/ekms/internal/kms/kms.go +++ b/ekms/internal/kms/kms.go @@ -46,8 +46,6 @@ type EKMS struct { quantumModulesMutex sync.RWMutex kmsPeersMutex sync.Mutex // TODO(maba): find a better name for this - // TODO: add mutex - keysForPathId map[uuid.UUID]string routingTable map[uuid.UUID]*Route routingTableMutex sync.RWMutex KmsPeers map[string]*kmsPeer @@ -230,25 +228,25 @@ func (kms *EKMS) FindPeerUuid(lookup uuid.UUID) (peer *kmsPeer) { } func (kms *EKMS) RoutingTableDeepCopy() map[uuid.UUID]*Route { - copy := make(map[uuid.UUID]*Route, len(kms.KmsPeers)) + routingTableCopy := make(map[uuid.UUID]*Route, len(kms.KmsPeers)) kms.routingTableMutex.Lock() for k, v := range kms.routingTable { - copy[k] = v + routingTableCopy[k] = v } kms.routingTableMutex.Unlock() - return copy + return routingTableCopy } func (kms *EKMS) PeersDeepCopy() map[string]*kmsPeer { - copy := make(map[string]*kmsPeer, len(kms.KmsPeers)) + peersCopy := make(map[string]*kmsPeer, len(kms.KmsPeers)) kms.kmsPeersMutex.Lock() for k, v := range kms.KmsPeers { - copy[k] = v + peersCopy[k] = v } kms.kmsPeersMutex.Unlock() - return copy + return peersCopy } diff --git a/ekms/internal/kms/kmsetsi.go b/ekms/internal/kms/kmsetsi.go index 6fe86583a507286ed463eecbaa69e616f3111609..205e9d3a70082b8c3856810bf1147a4c72cb863d 100644 --- a/ekms/internal/kms/kmsetsi.go +++ b/ekms/internal/kms/kmsetsi.go @@ -17,8 +17,6 @@ import ( "google.golang.org/grpc/status" ) -var etsiPort = flag.Int("port", 50900, "The server port") - // TODO: remove etsiServer. type etsiServer struct { pb.UnimplementedKmsETSIServer diff --git a/ekms/internal/kms/kmspeers.go b/ekms/internal/kms/kmspeers.go index cba5db5d5720a33171c035e569d528a7cadddcd9..2b7ed0a5ea8f5a00ed2f17d42c4aedf9c4544345 100644 --- a/ekms/internal/kms/kmspeers.go +++ b/ekms/internal/kms/kmspeers.go @@ -24,12 +24,13 @@ const ( KmsPeerUnknown // not known, not initialized ) -type kmsPeerInfo interface { - GetKmsPeerStatus() KmsPeerStatus - GetKmsPeerId() uuid.UUID - GetKmsPeerQkdiId() uint32 - KmsPeerKeyInit() -} +// NOTE: currently not used, could be of usage later on +// type kmsPeerInfo interface { +// GetKmsPeerStatus() KmsPeerStatus +// GetKmsPeerId() uuid.UUID +// GetKmsPeerQkdiId() uint32 +// KmsPeerKeyInit() +// } type kmsPeer struct { peerClient pbIC.KmsTalkerClient @@ -40,10 +41,11 @@ type kmsPeer struct { tcpSocket *net.TCPAddr // the IP address and TCP port (aka socket) of the kms peer tcpSocketStr string // string rep. of tcpSocket et CryptoAlgorithm - name string // the name of the kms peer - id uuid.UUID // uuid of the peer - quit chan bool // cancel the peer goroutine - eventBus *event.EventBus + // NOTE: currently not used, could be of usage later on + // name string // the name of the kms peer + id uuid.UUID // uuid of the peer + quit chan bool // cancel the peer goroutine + eventBus *event.EventBus } func NewKmsPeer(peerKmsId string, servQM QuantumModule, tcpSocketStr string, interComAddr string, eventBus *event.EventBus) (*kmsPeer, error) { diff --git a/ekms/internal/kms/module.go b/ekms/internal/kms/module.go index 9477e663b4c21c9e0f29c842ba16d20411eadec9..a2fc337c7693a1e90e6ed597c1ec319f584d7ed9 100644 --- a/ekms/internal/kms/module.go +++ b/ekms/internal/kms/module.go @@ -291,7 +291,6 @@ func (eqe *ETSI014HTTPQuantumModule) Initialize() error { if err := addETSIKeysToKeystore(eqe.keyStore, container.GetKeys()); err != nil { log.Error(err) - break } } } diff --git a/ekms/internal/kms/util.go b/ekms/internal/kms/util.go index 23e755063151476e176626d3eb54125faa746f59..ebd74c40b62c77bf389cf5da93ed23e2b2cdca0e 100644 --- a/ekms/internal/kms/util.go +++ b/ekms/internal/kms/util.go @@ -8,6 +8,7 @@ import ( etsi14 "code.fbi.h-da.de/danet/quant/ekms/api/go/rest/etsi/client" "github.com/google/uuid" + "github.com/hashicorp/go-multierror" ) func RandomItemFromMap[T comparable, M any](m map[T]M) (M, error) { @@ -59,7 +60,7 @@ func addETSIKeysToKeystore(keyStore *kmsKeyStore, keyContainer []etsi14.KeyConta } // NOTE: For demo purpose only. -func SendKmsInfoMessage(url string, json []byte) error { +func SendKmsInfoMessage(url string, json []byte) (err error) { request, err := http.NewRequest("POST", url, bytes.NewBuffer(json)) if err != nil { return err @@ -68,10 +69,16 @@ func SendKmsInfoMessage(url string, json []byte) error { client := &http.Client{} response, err := client.Do(request) if err != nil { - response.Body.Close() + if closeError := response.Body.Close(); closeError != nil { + err = multierror.Append(err, closeError) + } return err } - defer response.Body.Close() + defer func() { + if closeError := response.Body.Close(); closeError != nil { + err = multierror.Append(err, closeError) + } + }() return nil } diff --git a/go.mod b/go.mod index 61ac342c5f5ab59a0a20ed27278c661a5ca1019a..eb9752a045a7e4dd94aba873b529cdfecbeaf047 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/google/uuid v1.3.1 github.com/gorilla/mux v1.8.0 github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 + github.com/hashicorp/go-multierror v1.1.1 github.com/openconfig/gnmi v0.10.0 github.com/openconfig/goyang v1.4.2 github.com/openconfig/ygot v0.29.12 @@ -32,6 +33,7 @@ require ( github.com/golang/glog v1.1.2 // indirect github.com/golang/protobuf v1.5.3 // indirect github.com/google/go-cmp v0.5.9 // indirect + github.com/hashicorp/errwrap v1.0.0 // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/kylelemons/godebug v1.1.0 // indirect diff --git a/go.sum b/go.sum index c2832d8c09c9c682c0851a7d2b6544eddb162620..188786223d6c21b77a5ad43b267d321107726e66 100644 --- a/go.sum +++ b/go.sum @@ -142,6 +142,10 @@ github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0 h1:RtRsiaGvWxcwd8y3BiRZxsylPT8hLWZ5SPcfI+3IDNk= github.com/grpc-ecosystem/grpc-gateway/v2 v2.18.0/go.mod h1:TzP6duP4Py2pHLVPPQp42aoYI92+PCrVotyR5e8Vqlk= +github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA= +github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4= +github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo= +github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM= github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= diff --git a/quantumlayer/quantumlayer-emu-prng.go b/quantumlayer/quantumlayer-emu-prng.go index ebe7f4ac44e5a44dbaab45aa8c775193e826d506..b252dbf68f224ce7408ed4f7843b3478a905849f 100644 --- a/quantumlayer/quantumlayer-emu-prng.go +++ b/quantumlayer/quantumlayer-emu-prng.go @@ -56,7 +56,7 @@ func NewQuantumlayerEmuPRNG(client pb.KmsQkdmCommunicationServiceClient, logOutp * Setup logging */ - //What level + // What level log.SetLevel(logLevel) // Where to send log out put log.SetOutput(logOutput) @@ -110,7 +110,7 @@ func (qlemuprng *QuantumlayerEmuPRNG) PowerOn() { log.Errorf("QuantumlayerEmuPRNG: Sorry, the quantum layer is not configured for action. You've missed Configure()") return } - //qlemuprng.poweron = false + // qlemuprng.poweron = false log.Infof("QuantumlayerEmuPRNG: is powering on...charging.") if qlemuprng.generateKeys { @@ -137,7 +137,12 @@ func (qlemuprng *QuantumlayerEmuPRNG) PowerOn() { defer qlemuprng.udpSrvConn.Close() // Retrieve local UDP address and store it for further actions. - qlemuprng.qlLocalPort = qlemuprng.udpSrvConn.LocalAddr().(*net.UDPAddr) + tempPort, ok := qlemuprng.udpSrvConn.LocalAddr().(*net.UDPAddr) + if !ok { + return + } + + qlemuprng.qlLocalPort = tempPort // TODO: This does not seem to be necessary if the gle is not generating rands // serve UDP incoming @@ -155,7 +160,7 @@ func (qlemuprng *QuantumlayerEmuPRNG) PowerOn() { // Warning this is not checking the validity of the sender, i.e., spoofing is possible if addr.String() == qlemuprng.qlPeer { log.Debugf("QuantumlayerEmuPRNG: Peer %s listed", addr) - //dumb the received data into the channel and carry on + // dumb the received data into the channel and carry on // TODO/XXX: no vetting for anything // Unmarshall out of JSON var inQBuffer QuantumPayloadElement @@ -193,7 +198,7 @@ func (qlemuprng *QuantumlayerEmuPRNG) AddPeer(addr *net.UDPAddr) { if !qlemuprng.poweron { return } - //TODO/XXX check the incoming addr + // TODO/XXX check the incoming addr // Add peer to the .... qlemuprng.qlPeerMutex.Lock() @@ -216,7 +221,7 @@ func (qlemuprng *QuantumlayerEmuPRNG) AddPeer(addr *net.UDPAddr) { // retrieve a new back of random numbers newNumberBatch := qlemuprng.GenerateRandomNumbers() // TODO: Replace this by some generic encapsulation reader and not just JSON - //Get JSON for transmission ready + // Get JSON for transmission ready qpe := QuantumPayloadElement{time.Now().UnixNano(), len(newNumberBatch), &newNumberBatch} // XXX/TODO: error must be handled @@ -353,7 +358,7 @@ func (store *NumberStore) receiveNumbers(incoming chan QuantumPayloadElement, cl BulkKeyLength: receivedNumbers.BulkKeyLength, BulkKey: receivedNumbers.BulkKey, } - //store.bulkKeyStorage[receivedNumbers.BulkKeyId] = mem + // store.bulkKeyStorage[receivedNumbers.BulkKeyId] = mem store.bulkKeyStorage = append(store.bulkKeyStorage, mem) store.mu.Unlock()