Skip to content

Commit

Permalink
add support for keygen to write to string (#4)
Browse files Browse the repository at this point in the history
* add support for keygen to write to string

and basic tests!
  • Loading branch information
vsoch committed Apr 27, 2023
1 parent 35eebc2 commit 56f5098
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 8 deletions.
1 change: 1 addition & 0 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ RUN apt-get update \
&& apt-get -qq install -y --no-install-recommends \
fd-find \
less \
bats \
ripgrep

# Add the group and user that match our ids
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
run: |
cp ./.devcontainer/Dockerfile ./Dockerfile
echo "USER root" >> ./Dockerfile
cat ./Dockerfile
docker build -t ${{ env.container }}-base:latest .
echo "WORKDIR /code" >> ./Dockerfile
echo "COPY . /code" >> ./Dockerfile
Expand Down
32 changes: 32 additions & 0 deletions .github/workflows/main.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test Flux Go

on:

# Test Nightly
schedule:
- cron: 0 0 * * 0

# Test on pull request
pull_request: []

jobs:
build:
permissions:
packages: write
runs-on: ubuntu-latest
container:
image: ghcr.io/converged-computing/flux-go
options: --user root
name: Build
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Install Bats
run: apt-get update && apt-get install -y bats

- name: Test Build
run: make

- name: Run Tests
run: make test
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ fluxgo:
mkdir -p ./bin
$(COMMONENVVAR) $(BUILDENVVAR) go build -ldflags '-w' -o ./bin/fluxgo-submit cmd/submit/main.go
$(COMMONENVVAR) $(BUILDENVVAR) go build -ldflags '-w' -o ./bin/fluxgo-keygen cmd/keygen/main.go


.PHONY: test
test:
bats -t test/bats/cli.bats
7 changes: 6 additions & 1 deletion cmd/keygen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ func main() {
fmt.Println("⭐️ Testing flux keygen in Go! ⭐️")

// A name for the certificate - this is often the hostname
fmt.Println("Generating to path...")
hostname := getHostname()
name := "curve-cert"

// Path to the certificate to save
path := "./curve.cert"
flux.KeyGen(name, hostname, path)
fmt.Println("Generated certificate!")

fmt.Println("Generating to string...")
curveCert := flux.KeyGen(name, hostname, "")
fmt.Println(curveCert)

}
41 changes: 34 additions & 7 deletions pkg/flux/keygen.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@ import (
// so we put it under flux implying that, since "cmd" has special meaning
// in Go!

var (
template = `# **** Generated on 2023-04-26 22:54:42 by CZMQ ****
# ZeroMQ CURVE **Secret** Certificate
# DO NOT PROVIDE THIS FILE TO OTHER USERS nor change its permissions.
metadata
name = "%s"
keygen.hostname = "%s"
curve
public-key = "%s"
secret-key = "%s"`
)

// KeyGen generates a curve certificate
func KeyGen(name string, hostname string, path string) {
func KeyGen(name string, hostname string, path string) string {

// Create the new certificate (likely want to check for error here)
cert := C.zcert_new()
Expand All @@ -45,11 +58,25 @@ func KeyGen(name string, hostname string, path string) {
defer C.free(unsafe.Pointer(hostnameField))
C.flux_zcert_set_meta(cert, hostnameField, hostnameValue)

// Note that we can also generate keygen.time, keygen.userid,
// And other version metadata. See
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
fmt.Printf("Saving to %s\n", path)
C.zcert_save_secret (cert, cpath)
// If we don't have a path, save to string
var curveCert string
if path == "" {

publicKey := C.zcert_public_txt(cert)
secretKey := C.zcert_secret_txt(cert)
public := C.GoString((*C.char)(unsafe.Pointer(publicKey)))
secret := C.GoString((*C.char)(unsafe.Pointer(secretKey)))
curveCert = fmt.Sprintf(template, name, hostname, public, secret)

} else {
// Note that we can also generate keygen.time, keygen.userid,
// And other version metadata. See
cpath := C.CString(path)
defer C.free(unsafe.Pointer(cpath))
fmt.Printf("Saving to %s\n", path)
C.zcert_save_secret (cert, cpath)
}

C.zcert_destroy (&cert)
return curveCert
}
21 changes: 21 additions & 0 deletions test/bats/cli.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bats

# This is run relative to the root
# We also assume the binaries are built
BIN_DIR=./bin
TEST_DIR=./test/bats
SLEEP_TIME=10

@test "keygen test" {

${BIN_DIR}/fluxgo-keygen | grep public-key
cat ./curve.cert | grep ZeroMQ
cat ./curve.cert | grep curve-cert
cat ./curve.cert | grep keygen.hostname
cat ./curve.cert | grep public-key
cat ./curve.cert | grep secret-key
}

@test "submit test" {
flux start ${TEST_DIR}/test_submit.sh
}
9 changes: 9 additions & 0 deletions test/bats/test_submit.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

HERE=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
cd $HERE
../../bin/fluxgo-submit
flux jobs -a | grep sleep

# Give time to finish
sleep 10

0 comments on commit 56f5098

Please sign in to comment.