feat(build): make summer new apps admin-ready
- NewOptions.AdminSecret: summer new writes SUMMER_ADMIN__JWT__SECRET to .env (still 0600), generated from 32 crypto/rand bytes when empty and validated as at least 32 .env-safe characters - .env.example gets an empty SUMMER_ADMIN__JWT__SECRET= placeholder with a comment; the secret never reaches the printed next steps - The next steps and the generated CLAUDE.md explain the admin at /backend: make:admin-controller, build, migrate and summer admin:create --email you@example.com --superuser - Goldens use a fixed, non-secret admin secret fixture - Scaffolding, installation and users-and-permissions docs updated
This commit is contained in:
@@ -3,6 +3,7 @@ package build
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
@@ -38,6 +39,10 @@ type NewOptions struct {
|
||||
// AppKey is written to .env as SUMMER_APP__KEY. It must be the base64
|
||||
// (standard encoding) of 32 bytes; the CLI passes lagoon.GenerateAppKey.
|
||||
AppKey string
|
||||
// AdminSecret is written to .env as SUMMER_ADMIN__JWT__SECRET, the HMAC
|
||||
// secret of admin tokens (admin.jwt.secret). Empty means NewApp
|
||||
// generates the base64 of 32 crypto/rand bytes.
|
||||
AdminSecret string
|
||||
// NoGit skips git init and the first commit.
|
||||
NoGit bool
|
||||
}
|
||||
@@ -77,15 +82,16 @@ type genFile struct {
|
||||
|
||||
// newData feeds the new/* templates.
|
||||
type newData struct {
|
||||
Name string
|
||||
Module string
|
||||
Framework string
|
||||
GoVersion string
|
||||
Toolchain string
|
||||
Replace string
|
||||
DSN string
|
||||
AppKey string
|
||||
NoPlugin bool
|
||||
Name string
|
||||
Module string
|
||||
Framework string
|
||||
GoVersion string
|
||||
Toolchain string
|
||||
Replace string
|
||||
DSN string
|
||||
AppKey string
|
||||
AdminSecret string
|
||||
NoPlugin bool
|
||||
}
|
||||
|
||||
// ValidateAppName reports whether name is usable as `summer new <name>`.
|
||||
@@ -127,6 +133,16 @@ func NewApp(ctx context.Context, opts NewOptions, out io.Writer) (string, error)
|
||||
if err := validateAppKey(opts.AppKey); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if opts.AdminSecret == "" {
|
||||
secret, err := generateAdminSecret()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("build: generate admin secret: %w", err)
|
||||
}
|
||||
opts.AdminSecret = secret
|
||||
}
|
||||
if err := validateAdminSecret(opts.AdminSecret); err != nil {
|
||||
return "", err
|
||||
}
|
||||
parent := opts.Parent
|
||||
if parent == "" {
|
||||
parent = "."
|
||||
@@ -191,6 +207,29 @@ func validateAppKey(key string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// adminSecretRe is the character set an admin secret may use: it is written
|
||||
// unquoted into .env, whose reader trims whitespace and strips quotes.
|
||||
var adminSecretRe = regexp.MustCompile(`^[A-Za-z0-9+/=_-]{32,}$`)
|
||||
|
||||
// generateAdminSecret returns the base64 (standard encoding) of 32
|
||||
// crypto/rand bytes.
|
||||
func generateAdminSecret() (string, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.StdEncoding.EncodeToString(raw), nil
|
||||
}
|
||||
|
||||
// validateAdminSecret requires at least 32 characters that are safe to
|
||||
// write unquoted into .env.
|
||||
func validateAdminSecret(s string) error {
|
||||
if !adminSecretRe.MatchString(s) {
|
||||
return fmt.Errorf("build: admin secret must be at least 32 characters of letters, digits, +, /, =, _ or -")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// devDSN is the .env database DSN matching compose.yaml.
|
||||
func devDSN(name string) string {
|
||||
return "postgres://" + devDBUser + ":" + devDBPassword + "@" + devDBHostPort + "/" + name + "?sslmode=disable"
|
||||
@@ -318,6 +357,9 @@ func planNewApp(opts NewOptions, target, framework string) ([]genFile, error) {
|
||||
if err := validateAppKey(opts.AppKey); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := validateAdminSecret(opts.AdminSecret); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := checkFramework(framework); err != nil {
|
||||
return nil, fmt.Errorf("build: %w", err)
|
||||
}
|
||||
@@ -326,15 +368,16 @@ func planNewApp(opts NewOptions, target, framework string) ([]genFile, error) {
|
||||
return nil, fmt.Errorf("build: framework replace: %w", err)
|
||||
}
|
||||
data := newData{
|
||||
Name: opts.Name,
|
||||
Module: appModulePath(opts.Name),
|
||||
Framework: frameworkModule,
|
||||
GoVersion: goVersionLine,
|
||||
Toolchain: toolchainLine,
|
||||
Replace: replace,
|
||||
DSN: devDSN(opts.Name),
|
||||
AppKey: opts.AppKey,
|
||||
NoPlugin: opts.NoPlugin,
|
||||
Name: opts.Name,
|
||||
Module: appModulePath(opts.Name),
|
||||
Framework: frameworkModule,
|
||||
GoVersion: goVersionLine,
|
||||
Toolchain: toolchainLine,
|
||||
Replace: replace,
|
||||
DSN: devDSN(opts.Name),
|
||||
AppKey: opts.AppKey,
|
||||
AdminSecret: opts.AdminSecret,
|
||||
NoPlugin: opts.NoPlugin,
|
||||
}
|
||||
plan := []struct {
|
||||
rel string
|
||||
|
||||
Reference in New Issue
Block a user