From db64ead189479ac2a5f811c35fd1c8f23b16ee3f Mon Sep 17 00:00:00 2001
From: Martin Stiemerling <martin.stiemerling@h-da.de>
Date: Wed, 21 Jun 2023 09:17:09 +0000
Subject: [PATCH] Add docker-compose

---
 Dockerfile                            | 14 +++++++++++++
 Makefile                              | 17 +++++++++++++++
 docker-compose.yml                    | 27 ++++++++++++++++++++++++
 main.go                               | 30 ++++++++++++++++++++++++---
 quantumlayer/quantumlayer-emu-prng.go |  9 ++++----
 5 files changed, 89 insertions(+), 8 deletions(-)
 create mode 100644 Dockerfile
 create mode 100644 Makefile
 create mode 100644 docker-compose.yml

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..4e7b6ac
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,14 @@
+ARG GOLANG_VERSION=1.20.5
+
+FROM golang:$GOLANG_VERSION-alpine as builder
+WORKDIR /proto-kms/
+RUN apk add build-base
+COPY . .
+RUN --mount=type=cache,target=/root/go/pkg/mod \
+    --mount=type=cache,target=/root/.cache/go-build \
+    make build-proto-kms
+
+FROM ${GITLAB_PROXY}golang:$GOLANG_VERSION-buster
+WORKDIR /app/
+COPY --from=builder /proto-kms/artifacts/proto-kms ./proto-kms
+ENTRYPOINT ["./proto-kms"]
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..14dcc56
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,17 @@
+GOCMD=go
+GOBUILD=$(GOCMD) build
+GOCLEAN=$(GOCMD) clean
+BUILD_ARTIFACTS_PATH=artifacts
+
+pre:
+	mkdir -p $(BUILD_ARTIFACTS_PATH)
+
+build-proto-kms: pre
+	CGO_ENABLED=0 $(GOBUILD) -o $(BUILD_ARTIFACTS_PATH)/proto-kms .
+
+containerize-proto-kms:
+	docker buildx build --rm -t proto-kms --load -f Dockerfile .
+
+clean:
+	$(GOCLEAN)
+	rm -rf $(BUILD_ARTIFACTS_PATH)
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..3ea7bcb
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,27 @@
+version: '3'
+
+services:
+  proto-kms1:
+    build:
+        dockerfile: Dockerfile
+    command: ["-address", "172.100.20.11:55059", "-name", "proto-kms1"]
+    networks:
+        proto-kms-net:
+            ipv4_address: 172.100.20.10
+
+  proto-kms2:
+    build:
+        dockerfile: Dockerfile
+    command: ["-address", "172.100.20.10:55059","-name", "proto-kms2"]
+    networks:
+        proto-kms-net:
+            ipv4_address: 172.100.20.11
+
+networks:
+    proto-kms-net:
+        ipam:
+            driver: default
+            config:
+                - subnet: "172.100.20.0/24"
+        name: proto-kms-net
+
diff --git a/main.go b/main.go
index 8f6ea93..4b14c64 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"flag"
 	"log"
 	"net"
 
@@ -9,11 +10,34 @@ import (
 )
 
 func main() {
+	// my own Name 
+	var qlName1, udpQL1AddrString string
+	// a name for potential self-testing
+	var qlName2, udpQL2AddrString string
+	// Setup flags that allows to initially provide a address to a second
+	// quantumlayer so generated keys can be fetched. This is mainly to simplify
+	// the initial setup process.
+	flag.StringVar(&udpQL2AddrString, "address", "[::1]:50900",
+		"The UDP address for my quantumlayer so keys can be fetched")
 
-	log.Println("Welcome to the proto-kms")
+	flag.StringVar(&ql1Name, "name", "ekms-ql1",
+		"The name of the quantumlayer")
 
-	go emulatedKMS("kms-ql1", "[::1]:50900", "[::1]:50901")
-	emulatedKMS("kms-ql2", "[::1]:50901", "[::1]:50900")
+	flag.StringVar(&udpQL2AddrString, "address", "[::1]:50901",
+		"The UDP address to a second quantumlayer so keys can be fetched")
+
+	flag.StringVar(&ql2Name, "name", "ekms-ql2",
+		"The name of the quantumlayer")
+
+
+	// Parse the provided flags
+	flag.Parse()
+
+	log.Println("Welcome to the proto-kms called: ", qlName)
+
+	go emulatedKMS(ql2Name, udpQL2AddrString, udpQL1AddrString)
+	emulatedKMS(ql1Name, udpQL1AddrString, udpQL2AddrString)
+	
 
 	return
 }
diff --git a/quantumlayer/quantumlayer-emu-prng.go b/quantumlayer/quantumlayer-emu-prng.go
index 0f8e7e7..a074502 100644
--- a/quantumlayer/quantumlayer-emu-prng.go
+++ b/quantumlayer/quantumlayer-emu-prng.go
@@ -23,9 +23,9 @@ import (
 )
 
 type QuantumPayloadElement struct {
-	BulkKeyId     int64   // the unique ID of this bulk of keys
-	BulkKeyLength int     // the length, counted in bytes, of bulkKey
-	BulkKey       *[]byte // the bulk key
+	BulkKeyId     int64   `json:"bulk-key-id"`     // the unique ID of this bulk of keys
+	BulkKeyLength int     `json:"bulk-key-length"` // the length, counted in bytes, of bulkKey
+	BulkKey       *[]byte `json:"bulk-key"`        // the bulk key
 }
 
 type QuantumlayerEmuPRNG struct {
@@ -74,6 +74,7 @@ func (qlemuprng *QuantumlayerEmuPRNG) PowerOn(localQLAddress ...string) {
 			udpAddrString = localQLAddress[0]
 		}
 		log.Printf("localQLAddress is %s", localQLAddress[0])
+
 		// This reads random numbers from other Quantum end
 		udpSrvPort, err := net.ResolveUDPAddr("udp", udpAddrString)
 		if err != nil {
@@ -178,7 +179,6 @@ func (qlemuprng *QuantumlayerEmuPRNG) AddPeer(addr net.UDPAddr) {
 			}
 		}
 	}(ctx)
-
 }
 
 func (qlemuprng *QuantumlayerEmuPRNG) RemovePeer() {
@@ -297,5 +297,4 @@ func (store *NumberStore) receiveNumbers(incoming chan QuantumPayloadElement) {
 
 		store.mu.Unlock()
 	}
-
 }
-- 
GitLab