SPB Git

Repository Public

main.go 8.2 KB 275 lines go

1/**2 * =============================================================================3 *                  SP BOUCHER | INFRASTRUCTURE & AUTOMATION4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : MacLustr Command Center (MCC)8 * File             : services/cmd/telemetry-ingest/main.go9 * Module           : services10 * Repository       : https://git.spboucher.ai/maclustr-command-center11 * Project Website  : https://clustr.spboucher.ai12 * Created          : 2026-11-1813 * Last Modified    : 2026-11-1814 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: admin@spboucher.ai23 * Technical Contact: dev@spboucher.ai24 * Security/Admin   : admin@spboucher.ai25 *26 * Copyright (c) 202627 * Simon-Pierre Boucher28 * All rights reserved where applicable. This file is governed by the29 * applicable project license, copyright notices and relevant law.30 * Existing third-party rights and notices remain unaffected.31 *32 * License          : None specified (no license file in this repository)33 *34 * Do not embed credentials, private keys, access tokens,35 * certificates or confidential personal information.36 *37 * =============================================================================38 * END OF SP BOUCHER SOURCE HEADER39 * =============================================================================40 */4142// Command telemetry-ingest is the agent-facing service of MacLustr Command43// Center: enrollment, mTLS telemetry intake, signed-command transport,44// rollups and retention.45//46//	telemetry-ingest                 serve (configuration from MCC_* variables)47//	telemetry-ingest migrate         apply database migrations (dev and test only)48//	telemetry-ingest dev-token …     create an enrollment token (dev and test only)49//	telemetry-ingest dev-proxy …     stand-in public front for the signed transport (dev and test only)50//	telemetry-ingest version51package main5253import (54	"context"55	"errors"56	"flag"57	"fmt"58	"log/slog"59	"net"60	"net/http"61	"net/http/httputil"62	"net/url"63	"os"64	"os/signal"65	"sync"66	"syscall"67	"time"6869	"github.com/jackc/pgx/v5/pgxpool"7071	"clustr.spboucher.ai/mcc/internal/buildinfo"72	"clustr.spboucher.ai/mcc/internal/config"73	"clustr.spboucher.ai/mcc/internal/db"74	"clustr.spboucher.ai/mcc/internal/ingest"75	"clustr.spboucher.ai/mcc/internal/pki"76)7778func main() {79	log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelInfo}))80	slog.SetDefault(log)81	cfg, err := config.Load()82	if err != nil {83		fatal(err)84	}85	cmd := "serve"86	if len(os.Args) > 1 {87		cmd = os.Args[1]88	}89	switch cmd {90	case "serve":91		err = serve(cfg, log)92	case "migrate":93		err = migrate(cfg)94	case "dev-token":95		err = devToken(cfg, os.Args[2:])96	case "dev-proxy":97		err = devProxy(cfg, os.Args[2:])98	case "version":99		fmt.Println("telemetry-ingest", buildinfo.String())100	default:101		err = fmt.Errorf("unknown command %q (serve | migrate | dev-token | dev-proxy | version)", cmd)102	}103	if err != nil {104		fatal(err)105	}106}107108func fatal(err error) {109	fmt.Fprintln(os.Stderr, "telemetry-ingest:", err)110	os.Exit(1)111}112113func devOnly(cfg *config.Config, what string) error {114	if cfg.Env == config.Prod {115		return fmt.Errorf("%s is refused when MCC_ENV=prod", what)116	}117	return nil118}119120func migrate(cfg *config.Config) error {121	if err := devOnly(cfg, "migrate"); err != nil {122		return err123	}124	ctx, cancel := context.WithTimeout(context.Background(), time.Minute)125	defer cancel()126	pool, err := db.Open(ctx, cfg.DatabaseURL)127	if err != nil {128		return err129	}130	defer pool.Close()131	applied, err := db.Migrate(ctx, pool)132	fmt.Println("applied:", applied)133	return err134}135136func devToken(cfg *config.Config, args []string) error {137	if err := devOnly(cfg, "dev-token"); err != nil {138		return err139	}140	fs := flag.NewFlagSet("dev-token", flag.ContinueOnError)141	alias := fs.String("alias", "", "node alias the token enrolls")142	kind := fs.String("kind", "mac", "node kind: mac | linux")143	ttl := fs.Duration("ttl", 15*time.Minute, "token lifetime")144	if err := fs.Parse(args); err != nil {145		return err146	}147	ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)148	defer cancel()149	pool, err := db.Open(ctx, cfg.DatabaseURL)150	if err != nil {151		return err152	}153	defer pool.Close()154	tok, err := ingest.CreateEnrollmentToken(ctx, pool, *alias, *kind, *ttl)155	if err != nil {156		return err157	}158	fmt.Println(tok) // shown once; only its hash is stored159	return nil160}161162// devProxy stands in for control-api in development: it reverse-proxies163// /agent/v1/ to the signed-request listener, exactly as the public front does.164func devProxy(cfg *config.Config, args []string) error {165	if err := devOnly(cfg, "dev-proxy"); err != nil {166		return err167	}168	fs := flag.NewFlagSet("dev-proxy", flag.ContinueOnError)169	listen := fs.String("listen", "127.0.0.1:8735", "loopback address of the stand-in front")170	if err := fs.Parse(args); err != nil {171		return err172	}173	target, err := url.Parse("http://" + cfg.IngestSignedListen)174	if err != nil {175		return err176	}177	rp := httputil.NewSingleHostReverseProxy(target)178	rp.FlushInterval = -1179	mux := http.NewServeMux()180	mux.Handle("/agent/v1/", rp)181	srv := &http.Server{Addr: *listen, Handler: mux, ReadHeaderTimeout: 10 * time.Second}182	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)183	defer stop()184	go func() { <-ctx.Done(); srv.Close() }()185	fmt.Fprintln(os.Stderr, "dev-proxy: http://"+*listen+"/agent/v1/ ->", target)186	if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {187		return err188	}189	return nil190}191192func serve(cfg *config.Config, log *slog.Logger) error {193	ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)194	defer stop()195196	ca, err := pki.LoadOrCreate(cfg.SecretsDir)197	if err != nil {198		return err199	}200	// Bind before waiting for the database: a bad listen address is a201	// configuration error and must fail at once.202	ln, err := net.Listen("tcp", cfg.IngestListen)203	if err != nil {204		return err205	}206	defer ln.Close()207	sln, err := net.Listen("tcp", cfg.IngestSignedListen)208	if err != nil {209		return err210	}211	defer sln.Close()212	if host, _, _ := net.SplitHostPort(cfg.IngestSignedListen); host != "localhost" {213		if ip := net.ParseIP(host); ip == nil || !ip.IsLoopback() {214			log.Warn("signed-request listener is not bound to loopback: it must only be reachable by the public front", "listen", cfg.IngestSignedListen)215		}216	}217218	// The service does not crash on a database outage, including at start.219	var pool *pgxpool.Pool220	for backoff := time.Second; ; {221		pool, err = db.Open(ctx, cfg.DatabaseURL)222		if err == nil {223			break224		}225		log.Warn("database not reachable, retrying", "err", err, "retry_in", backoff.String())226		select {227		case <-ctx.Done():228			return nil229		case <-time.After(backoff):230		}231		if backoff < 15*time.Second {232			backoff *= 2233		}234	}235	defer pool.Close()236237	srv, err := ingest.New(ingest.Options{Cfg: cfg, Pool: pool, CA: ca, Version: buildinfo.Version, Log: log})238	if err != nil {239		return err240	}241	log.Info("telemetry-ingest starting", "version", buildinfo.String(), "env", string(cfg.Env), "listen", cfg.IngestListen,242		"signed_listen", cfg.IngestSignedListen, "server_name", cfg.IngestServerName, "health", cfg.IngestHealthListen, "ca_fingerprint", ca.Fingerprint())243244	var wg sync.WaitGroup245	errc := make(chan error, 3)246	wg.Add(4)247	go func() {248		defer wg.Done()249		if err := srv.ServeSigned(ctx, sln); err != nil && !errors.Is(err, net.ErrClosed) {250			errc <- fmt.Errorf("signed-request listener: %w", err)251		}252	}()253	go func() { defer wg.Done(); srv.Background(ctx) }()254	go func() {255		defer wg.Done()256		if err := srv.ServeHealth(ctx, cfg.IngestHealthListen); err != nil {257			errc <- fmt.Errorf("health endpoint: %w", err)258		}259	}()260	go func() {261		defer wg.Done()262		if err := srv.Serve(ctx, ln); err != nil && !errors.Is(err, net.ErrClosed) {263			errc <- fmt.Errorf("agent listener: %w", err)264		}265	}()266	select {267	case <-ctx.Done():268		log.Info("telemetry-ingest stopping")269	case err = <-errc:270		stop()271	}272	wg.Wait()273	return err274}275
Files main
Menu

Theme