Skip to content
This repository was archived by the owner on Jun 11, 2024. It is now read-only.

Commit

Permalink
Implement key-generation demo experiment
Browse files Browse the repository at this point in the history
  • Loading branch information
Blake Watters committed May 2, 2020
1 parent 167a781 commit 5853254
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
10 changes: 7 additions & 3 deletions demo/config.yaml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 0 additions & 1 deletion demo/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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.}
43 changes: 42 additions & 1 deletion demo/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package main

import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"encoding/asn1"
"encoding/pem"
"log"
"strconv"
"time"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}

0 comments on commit 5853254

Please sign in to comment.