From fe160663b9ba38d17f6f84e7b23b775cecd36945 Mon Sep 17 00:00:00 2001
From: Malte Bauch <malte.bauch@stud.h-da.de>
Date: Tue, 23 Jan 2024 16:41:50 +0100
Subject: [PATCH] Linter pleasing

---
 .golangci.yml                           |  1 +
 ekms/handlers/system/hostnameHandler.go |  1 -
 ekms/internal/kms/kms.go                | 14 ++++++--------
 ekms/internal/kms/kmsetsi.go            |  2 --
 ekms/internal/kms/kmspeers.go           | 22 ++++++++++++----------
 ekms/internal/kms/module.go             |  1 -
 ekms/internal/kms/util.go               | 13 ++++++++++---
 go.mod                                  |  2 ++
 go.sum                                  |  4 ++++
 quantumlayer/quantumlayer-emu-prng.go   | 19 ++++++++++++-------
 10 files changed, 47 insertions(+), 32 deletions(-)

diff --git a/.golangci.yml b/.golangci.yml
index 23fdaf7e..6e56a1c1 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 3cd079b0..e03e5776 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 ae199a75..93cdb536 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 6fe86583..205e9d3a 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 cba5db5d..2b7ed0a5 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 9477e663..a2fc337c 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 23e75506..ebd74c40 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 61ac342c..eb9752a0 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 c2832d8c..18878622 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 ebe7f4ac..b252dbf6 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()
 
-- 
GitLab