Skip to content

Commit

Permalink
initializer: add slog
Browse files Browse the repository at this point in the history
  • Loading branch information
malt3 committed Dec 20, 2023
1 parent fd1ba30 commit 5d4dd8f
Showing 1 changed file with 31 additions and 16 deletions.
47 changes: 31 additions & 16 deletions initializer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import (
"crypto/x509"
"encoding/hex"
"encoding/pem"
"errors"
"fmt"
"log"
"log/slog"
"net"
"os"
"time"
Expand All @@ -22,33 +23,46 @@ import (
)

func main() {
log.Println("Initializer started")
if err := run(); err != nil {
os.Exit(1)
}
}

func run() (err error) {
logger := slog.Default()
defer func() {
if err != nil {
logger.Error(err.Error())
}
}()

logger.Info("Initializer started")

coordinatorHostname := os.Getenv("COORDINATOR_HOST")
if coordinatorHostname == "" {
log.Fatalf("COORDINATOR_HOST not set")
return errors.New("COORDINATOR_HOST not set")
}

ctx := context.Background()

privKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log.Fatalf("generating key: %v", err)
return fmt.Errorf("generating key: %w", err)
}

pubKey, err := x509.MarshalPKIXPublicKey(&privKey.PublicKey)
if err != nil {
log.Fatalf("marshaling public key: %v", err)
return fmt.Errorf("marshaling public key: %w", err)
}
pubKeyHash := sha256.Sum256(pubKey)
pubKeyHashStr := hex.EncodeToString(pubKeyHash[:])
log.Printf("pubKeyHash: %v", pubKeyHashStr)
logger.Info("Deriving public key", "pubKeyHash", pubKeyHashStr)

requestCert := func() (*intercom.NewMeshCertResponse, error) {
dial := dialer.NewWithKey(snp.NewIssuer(), atls.NoValidator, &net.Dialer{}, privKey)
conn, err := dial.Dial(ctx, net.JoinHostPort(coordinatorHostname, intercom.Port))
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
return nil, fmt.Errorf("dialing: %w", err)
}
defer conn.Close()

Expand All @@ -59,7 +73,7 @@ func main() {
}
resp, err := client.NewMeshCert(ctx, req)
if err != nil {
return nil, fmt.Errorf("Error: calling NewMeshCert: %v", err)
return nil, fmt.Errorf("calling NewMeshCert: %w", err)
}
return resp, nil
}
Expand All @@ -68,18 +82,18 @@ func main() {
for {
resp, err = requestCert()
if err == nil {
log.Printf("Response: %v", resp)
logger.Info("Requesting cert", "response", resp)
break
}
log.Printf("Error: %v", err)
log.Println("retrying in 10s")
logger.Warn("Requesting cert", "err", err)
logger.Info("Retrying in 10s")
time.Sleep(10 * time.Second)
}

// convert privKey to PEM
privKeyBytes, err := x509.MarshalPKCS8PrivateKey(privKey)
if err != nil {
log.Fatalf("marshaling private key: %v", err)
return fmt.Errorf("marshaling private key: %v", err)
}
pemEncodedPrivKey := pem.EncodeToMemory(&pem.Block{
Type: "PRIVATE KEY",
Expand All @@ -89,16 +103,17 @@ func main() {
// write files to disk
err = os.WriteFile("/tls-config/CACert.pem", resp.CaCert, 0o644)
if err != nil {
log.Fatalf("writing cert.pem: %v", err)
return fmt.Errorf("writing cert.pem: %v", err)
}
err = os.WriteFile("/tls-config/certChain.pem", resp.CertChain, 0o644)
if err != nil {
log.Fatalf("writing cert.pem: %v", err)
return fmt.Errorf("writing cert.pem: %v", err)
}
err = os.WriteFile("/tls-config/key.pem", pemEncodedPrivKey, 0o600)
if err != nil {
log.Fatalf("writing key.pem: %v", err)
return fmt.Errorf("writing key.pem: %v", err)
}

log.Println("Initializer done")
logger.Info("Initializer done")
return nil
}

0 comments on commit 5d4dd8f

Please sign in to comment.