From 585325405fb2dd5127867a578dec578116c1d5bf Mon Sep 17 00:00:00 2001 From: Blake Watters Date: Sat, 2 May 2020 09:19:29 -0700 Subject: [PATCH] Implement key-generation demo experiment --- demo/config.yaml | 10 +++++++--- demo/docker-compose.yaml | 1 - demo/main.go | 43 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/demo/config.yaml b/demo/config.yaml index 06a5b89..e9c74c5 100644 --- a/demo/config.yaml +++ b/demo/config.yaml @@ -1,11 +1,15 @@ -application: +statestore: components: app: + cost_formula: "1" settings: - key_size: + mem: type: enum - unit: bits + unit: mb value: 1024 + key_size: + type: enum + unit: bits values: - 1024 - 2048 diff --git a/demo/docker-compose.yaml b/demo/docker-compose.yaml index 4007369..90a3256 100644 --- a/demo/docker-compose.yaml +++ b/demo/docker-compose.yaml @@ -33,6 +33,5 @@ services: restart: always volumes: - ./config.yaml:/servo/config.yaml - - ./config.yaml:/servo/measure.d/config.yaml - ./opsani.token:/run/secrets/opsani.token command: --auth-token /run/secrets/opsani.token --account ${OPTUNE_ACCOUNT:?The Opsani Account Name must be configured.} ${OPTUNE_APP_ID:?The Opsani App ID must be configured.} diff --git a/demo/main.go b/demo/main.go index beeb429..05954be 100644 --- a/demo/main.go +++ b/demo/main.go @@ -1,6 +1,12 @@ package main import ( + "bytes" + "crypto/rand" + "crypto/rsa" + "crypto/x509" + "encoding/asn1" + "encoding/pem" "log" "strconv" "time" @@ -15,6 +21,8 @@ import ( const metricsPath string = "/metrics" const subsystemName string = "demo" +var keySizeInBits int = 1024 + // Metrics maintains the values to be emitted to Prometheus type Metrics struct { requestCount *prometheus.CounterVec @@ -89,10 +97,43 @@ func main() { }) app.Get("/", func(c *fiber.Ctx) { + // Generate RSA keys to make this interesting + reader := rand.Reader + key, _ := rsa.GenerateKey(reader, keySizeInBits) + + var privateKey = &pem.Block{ + Type: "PRIVATE KEY", + Bytes: x509.MarshalPKCS1PrivateKey(key), + } + privateKeyPem := new(bytes.Buffer) + _ = pem.Encode(privateKeyPem, privateKey) + + asn1Bytes, _ := asn1.Marshal(key.PublicKey) + var pemkey = &pem.Block{ + Type: "PUBLIC KEY", + Bytes: asn1Bytes, + } + publicKeyPem := new(bytes.Buffer) + _ = pem.Encode(publicKeyPem, pemkey) + c.JSON(fiber.Map{ - "hello": "world", + "private_key": privateKeyPem.String(), + "public_key": publicKeyPem.String(), }) }) + app.Put("/set/:size", func(c *fiber.Ctx) { + size := c.Params("size") + i, err := strconv.Atoi(size) + if err == nil { + keySizeInBits = i + } + c.SendStatus(200) + c.JSON(fiber.Map{ + "size": keySizeInBits, + }) + log.Println(string(c.Path())) + }) + app.Listen(8080) }