Feat v0.7.7 api tokens ext permissions (#61)
All checks were successful
CI/CD / detect-changes (push) Successful in 4s
CI/CD / e2e-smoke (push) Has been skipped
CI/CD / test-frontend (push) Successful in 6s
CI/CD / test-runners (push) Has been skipped
CI/CD / test-go-pg (push) Successful in 2m48s
CI/CD / test-sqlite (push) Successful in 2m49s
CI/CD / build-and-deploy (push) Successful in 28s

Co-authored-by: Jeffrey Smith <jasafpro@gmail.com>
Co-committed-by: Jeffrey Smith <jasafpro@gmail.com>
This commit was merged in pull request #61.
This commit is contained in:
2026-04-02 19:11:47 +00:00
committed by xcaliber
parent e02b13dc12
commit e4f0bdbd36
34 changed files with 1769 additions and 58 deletions

View File

@@ -2,13 +2,12 @@ package handlers
import (
"context"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
@@ -384,8 +383,7 @@ func (h *AuthHandler) generateTokens(user *models.User, keepLogin bool) (gin.H,
}
func hashToken(token string) string {
h := sha256.Sum256([]byte(token))
return hex.EncodeToString(h[:])
return auth.HashToken(token)
}
// ── Vault Lifecycle ─────────────────────────
@@ -582,6 +580,60 @@ func BootstrapAdmin(cfg *config.Config, s store.Stores, uekCache ...*crypto.UEKC
log.Printf(" ✅ Admin user '%s' created", cfg.AdminUsername)
}
// BootstrapPAT creates a personal access token for the admin user when
// ARMATURE_BOOTSTRAP_PAT=true. Writes the token to /tmp/armature-admin-pat.txt
// for use by CI scripts. The token has all permissions and no expiry.
func BootstrapPAT(cfg *config.Config, s store.Stores) {
if os.Getenv("ARMATURE_BOOTSTRAP_PAT") != "true" {
return
}
if cfg.AdminUsername == "" || s.APITokens == nil {
return
}
ctx := context.Background()
admin, err := s.Users.GetByUsername(ctx, cfg.AdminUsername)
if err != nil || admin == nil {
log.Printf("⚠ BootstrapPAT: admin user not found")
return
}
// Check if a bootstrap token already exists
existing, _ := s.APITokens.ListForUser(ctx, admin.ID)
for _, t := range existing {
if t.Name == "bootstrap-ci" {
log.Printf(" Bootstrap PAT already exists (prefix: %s)", t.Prefix)
return
}
}
// Generate and store
raw, tokenHash, prefix := generateToken()
allPerms := auth.AllPermissions
permsCopy := make([]string, len(allPerms))
copy(permsCopy, allPerms)
token := &models.APIToken{
UserID: admin.ID,
Name: "bootstrap-ci",
TokenHash: tokenHash,
Prefix: prefix,
Permissions: permsCopy,
CreatedBy: &admin.ID,
}
if err := s.APITokens.Create(ctx, token); err != nil {
log.Printf("⚠ BootstrapPAT: failed to create token: %v", err)
return
}
// Write token to file for CI consumption
if err := os.WriteFile("/tmp/armature-admin-pat.txt", []byte(raw), 0600); err != nil {
log.Printf("⚠ BootstrapPAT: failed to write token file: %v", err)
// Still log it for docker-compose stdout capture
}
log.Printf(" ✅ Bootstrap PAT created (prefix: %s), written to /tmp/armature-admin-pat.txt", prefix)
}
// SeedUsers creates or updates users from the SEED_USERS env var.
// Format: "user:pass[:admin],user2:pass2" — third field "admin" adds to Admins group.
// Upsert: existing users get their password refreshed on every restart.