Skip to content

Agent Mesh

hub libp2p key identity

Antonio Ojea55af84eb7bba
diff --git a/.github/k8s/sam-hub-template.yaml b/.github/k8s/sam-hub-template.yaml
index 150beffe..1a3bfb02 100644
--- a/.github/k8s/sam-hub-template.yaml
+++ b/.github/k8s/sam-hub-template.yaml
@@ -64,6 +64,12 @@ spec:
name: sam-hub-secret
key: sam-hub-key
optional: true # Make it optional so it doesn't block local kind testing if not created
+ - name: SAM_LIBP2P_KEY
+ valueFrom:
+ secretKeyRef:
+ name: sam-hub-secret
+ key: sam-libp2p-key
+ optional: true
ports:
- containerPort: 4001
protocol: TCP
@@ -98,6 +104,7 @@ spec:
- "--tls-cert-file=/etc/sam/certs/tls.crt"
- "--tls-key-file=/etc/sam/certs/tls.key"
- "--policy-file=/etc/sam/policies/policies.yaml"
+ - "--libp2p-key=$(SAM_LIBP2P_KEY)"
resources:
requests:
cpu: 100m
diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml
index f9fea58d..0a5cc9e9 100644
--- a/.github/workflows/deploy.yaml
+++ b/.github/workflows/deploy.yaml
@@ -163,13 +163,22 @@ jobs:
export NAMESPACE="sam-${ENV_NAME}"
# Idempotently provision the sam-hub-secret if it doesn't already exist
if ! kubectl get secret sam-hub-secret -n ${NAMESPACE} >/dev/null 2>&1; then
- echo "Generating a new random 32-byte hex key for SAM_HUB_KEY..."
+ echo "Generating a new random 32-byte hex key for SAM_HUB_KEY and SAM_LIBP2P_KEY..."
HUB_KEY=$(openssl rand -hex 32)
+ LIBP2P_KEY=$(openssl rand -hex 32)
kubectl create secret generic sam-hub-secret \
--namespace=${NAMESPACE} \
- --from-literal=sam-hub-key="${HUB_KEY}"
+ --from-literal=sam-hub-key="${HUB_KEY}" \
+ --from-literal=sam-libp2p-key="${LIBP2P_KEY}"
else
- echo "sam-hub-secret already exists in namespace ${NAMESPACE}. Skipping generation."
+ echo "sam-hub-secret already exists in namespace ${NAMESPACE}."
+ # Check if sam-libp2p-key is missing, and patch it if needed
+ if ! kubectl get secret sam-hub-secret -n ${NAMESPACE} -o json | grep -q '"sam-libp2p-key"'; then
+ echo "sam-libp2p-key is missing from existing secret. Generating and patching..."
+ LIBP2P_KEY=$(openssl rand -hex 32)
+ LIBP2P_KEY_BASE64=$(echo -n "${LIBP2P_KEY}" | base64 | tr -d '\n')
+ kubectl patch secret sam-hub-secret -n ${NAMESPACE} --type='json' -p="[{\"op\": \"add\", \"path\": \"/data/sam-libp2p-key\", \"value\": \"${LIBP2P_KEY_BASE64}\"}]"
+ fi
fi
- name: Copy Wildcard TLS Secret to sam-hub
diff --git a/cmd/sam-hub/main.go b/cmd/sam-hub/main.go
index 8eac8e4d..99f6f988 100644
--- a/cmd/sam-hub/main.go
+++ b/cmd/sam-hub/main.go
@@ -36,6 +36,7 @@ import (
"github.com/libp2p/go-libp2p"
dht "github.com/libp2p/go-libp2p-kad-dht"
pubsub "github.com/libp2p/go-libp2p-pubsub"
+ "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
@@ -96,6 +97,7 @@ var (
tlsCAFile string
externalMultiaddrs []string
allowedAudiencesFlag string
+ libp2pKeyHex string
)
var logger = golog.Logger("sam-hub")
@@ -127,7 +129,7 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig) (*Hub, error) {
}
// Multi-transport setup for firewall traversal
- h, err := libp2p.New(
+ opts := []libp2p.Option{
libp2p.Transport(libp2pquic.NewTransport),
libp2p.Transport(tcp.NewTCPTransport),
libp2p.ListenAddrStrings(listenAddrs...),
@@ -138,7 +140,28 @@ func NewHub(ctx context.Context, policy *api.PolicyConfig) (*Hub, error) {
libp2p.ConnectionManager(cm),
libp2p.EnableAutoNATv2(),
libp2p.EnableNATService(),
- )
+ }
+
+ if libp2pKeyHex != "" {
+ logger.Infof("Initializing libp2p host with static PeerID...")
+ seed, err := hex.DecodeString(libp2pKeyHex)
+ if err != nil {
+ return nil, fmt.Errorf("failed to decode libp2p key hex: %w", err)
+ }
+ if len(seed) != ed25519.SeedSize {
+ return nil, fmt.Errorf("invalid libp2p key seed size: %d, expected %d", len(seed), ed25519.SeedSize)
+ }
+ priv := ed25519.NewKeyFromSeed(seed)
+ libp2pPrivKey, err := crypto.UnmarshalEd25519PrivateKey(priv)
+ if err != nil {
+ return nil, fmt.Errorf("failed to unmarshal libp2p private key: %w", err)
+ }
+ opts = append(opts, libp2p.Identity(libp2pPrivKey))
+ } else {
+ logger.Warnf("No libp2p key configured, using a dynamic ephemeral identity")
+ }
+
+ h, err := libp2p.New(opts...)
if err != nil {
return nil, err
}
@@ -705,6 +728,7 @@ func main() {
rootCmd.Flags().StringVar(&oidcIssuer, "issuer", defIssuer, "OIDC Issuer URL")
rootCmd.Flags().StringVar(&clientID, "client-id", os.Getenv("SAM_OIDC_ID"), "OIDC Client ID")
rootCmd.Flags().StringVar(&biscuitHex, "key", os.Getenv("SAM_HUB_KEY"), "Hub Private Key (32-byte Hex)")
+ rootCmd.Flags().StringVar(&libp2pKeyHex, "libp2p-key", os.Getenv("SAM_LIBP2P_KEY"), "Hub libp2p Private Key (32-byte Hex)")
rootCmd.Flags().StringSliceVar(&listenAddrs, "listen", []string{"/ip4/0.0.0.0/udp/8080/quic-v1", "/ip4/0.0.0.0/tcp/8080"}, "libp2p Listen Addrs")
rootCmd.Flags().StringSliceVar(&externalMultiaddrs, "external-multiaddr", []string{}, "External multiaddrs to announce")
rootCmd.Flags().StringVar(&meshName, "mesh", DefaultMeshName, "Mesh federation name")