Skip to content

Commit 7b8118e

Browse files
chore(ci): add ci
1 parent fa2a794 commit 7b8118e

File tree

4 files changed

+73
-9
lines changed

4 files changed

+73
-9
lines changed

.github/workflows/ci.yaml

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
name: CI
22
on: [push, pull_request]
33
jobs:
4-
5-
build:
4+
hsm:
5+
env:
6+
HSM_SO_PIN: 1234
7+
HSM_PIN: 1234
8+
TOKEN_LABEL: 'test'
9+
KEY_LABEL: 'test-key'
610
name: Build and Test
711
runs-on: ubuntu-latest
812
strategy:
@@ -33,7 +37,7 @@ jobs:
3337
- name: Generate Server Certificate and Sign with Private Key
3438
working-directory: keys-and-certs
3539
run: |
36-
openssl req -new -x509 -days 365 -config openssl.cnf -key private/key.pem -out certs/cert.pem -extensions v3_ca -addext "subjectAltName = DNS:localhost" -subj "/C=DE/ST=BW/L=Walldorf/O=OCM/CN=localhost" -sha256
40+
openssl req -new -x509 -days 5 -config openssl.cnf -key private/key.pem -out certs/cert.pem -extensions v3_ca -addext "subjectAltName = DNS:localhost" -subj "/C=DE/ST=BW/L=Walldorf/O=OCM/CN=localhost" -sha256
3741
3842
- name: Client Certificate Generation
3943
working-directory: keys-and-certs
@@ -44,12 +48,28 @@ jobs:
4448
mkdir client/csr
4549
openssl genpkey -algorithm RSA -out client/private/key.pem
4650
openssl req -new -sha256 -config openssl.cnf -key client/private/key.pem -out client/csr/csr.pem -subj "/C=DE/ST=BW/L=Walldorf/O=OCM/CN=localhost"
47-
openssl x509 -req -in client/csr/csr.pem -CA certs/cert.pem -CAkey private/key.pem -out client/certs/cert.pem -CAcreateserial -days 365 -sha256
51+
openssl x509 -req -in client/csr/csr.pem -CA certs/cert.pem -CAkey private/key.pem -out client/certs/cert.pem -CAcreateserial -days 5 -sha256
4852
4953
- name: Setup SoftHSM
54+
env:
55+
SOFTHSM2_CONF: ${{ github.workspace }}/softhsm2.conf
56+
id: softhsm
5057
run: |
58+
mkdir test_data
5159
sudo apt-get update
52-
sudo apt-get -y install libsofthsm2
53-
60+
sudo apt-get -y install libsofthsm2 gnutls-bin
61+
62+
# set output of lib to environment variable
63+
64+
softhsm2-util --init-token --slot 0 --free --label $TOKEN_LABEL --so-pin $HSM_SO_PIN --pin $HSM_PIN
65+
p11tool --generate-privkey=rsa --login --set-pin=$HSM_PIN --label="$KEY_LABEL" "pkcs11:token=$TOKEN_LABEL"
5466
- name: Build
55-
run: make
67+
run: make
68+
- name: Run Server Tests
69+
working-directory: ${{ github.workspace }}
70+
env:
71+
SOFTHSM2_CONF: ${{ github.workspace }}/softhsm2.conf
72+
HSM_MODULE: ${{ steps.softhsm.outputs.SOFTHSM2_LIB }}
73+
SIGNING_SERVER_BIN: ${{ github.workspace }}/signing-server
74+
run: |
75+
go test -v ./cmd/signing-server

cmd/signing-server/main.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ func run(cfg *Config) error {
495495
return err
496496
}
497497
}
498-
return RunServer(cfg, responseBuilders)
498+
return RunServer(context.Background(), cfg, responseBuilders)
499499
} else {
500500
return RunSigner(cfg, pflag.CommandLine.Args(), responseBuilders)
501501
}
@@ -564,7 +564,7 @@ func RunSigner(cfg *Config, args []string, responseBuilders map[string]encoding.
564564
return err
565565
}
566566

567-
func RunServer(cfg *Config, responseBuilders map[string]encoding.ResponseBuilder) error {
567+
func RunServer(ctx context.Context, cfg *Config, responseBuilders map[string]encoding.ResponseBuilder) error {
568568
var err error
569569

570570
addr := ":" + cfg.Port
@@ -575,6 +575,11 @@ func RunServer(cfg *Config, responseBuilders map[string]encoding.ResponseBuilder
575575
cfg.Logger.Info("register route", zap.String("route", route))
576576
r.Methods(http.MethodPost).Path(route).Handler(h.HTTPHandler(responseBuilders, cfg.MaxBodySizeBytes))
577577
}
578+
r.Methods(http.MethodGet).Path("/healthz").HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
579+
w.Header().Set("Content-Type", "text/plain")
580+
w.WriteHeader(http.StatusOK)
581+
_, _ = w.Write([]byte("ok"))
582+
})
578583
lm := logutil.LoggingMiddleware{
579584
Logger: cfg.Logger,
580585
}
@@ -628,6 +633,7 @@ func RunServer(cfg *Config, responseBuilders map[string]encoding.ResponseBuilder
628633
select {
629634
case <-c:
630635
case <-stop:
636+
case <-ctx.Done():
631637
}
632638

633639
// Create a deadline to wait for.

concurrent-test.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import asyncio
2+
import httpx
3+
import logging
4+
5+
# Setup logging
6+
logging.basicConfig(
7+
level=logging.INFO,
8+
format="%(asctime)s [%(levelname)s] %(message)s"
9+
)
10+
11+
URL = "http://localhost:8080/sign/rsassa-pss?hashAlgorithm=sha256"
12+
HEADERS = {
13+
"Content-Type": "text/plain",
14+
"Content-Encoding": "hex",
15+
"Accept": "application/x-pem-file",
16+
}
17+
BODY = "aec070645fe53ee3b3763059376134f058cc337247c978add178b6ccdfb0019f"
18+
19+
async def send_request(client, index):
20+
try:
21+
resp = await client.post(URL, content=BODY, headers=HEADERS)
22+
logging.info(f"Response {index}: {resp.status_code} - {resp.text[:100]!r}")
23+
except httpx.RequestError as e:
24+
logging.error(f"Request {index} failed: {e}")
25+
except Exception as e:
26+
logging.exception(f"Unexpected error on request {index}")
27+
28+
async def main():
29+
async with httpx.AsyncClient() as client:
30+
tasks = [send_request(client, i) for i in range(10)] # 10 concurrent requests
31+
await asyncio.gather(*tasks)
32+
33+
if __name__ == "__main__":
34+
asyncio.run(main())

softhsm2.conf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
log.level = INFO
2+
objectstore.backend = file
3+
directories.tokendir = test_data
4+
slots.removable = false

0 commit comments

Comments
 (0)