Keep an admin token and an HMAC signing key out of a Go Fiber v3 repository, with Encore secrets set per environment or with .env files and AWS Secrets Manager on ECS.

A Fiber API collects secrets quickly: a token for internal admin routes, an HMAC key for signed links, keys for the email and payment providers. They usually start in a .env file next to main.go, which stays readable to anyone who opens the repository, coding agents included, and production then needs the same values in AWS Secrets Manager, an IAM policy to read them and a redeploy every time one rotates.
This guide adds two secrets to a Fiber v3 API that issues expiring download links: an admin token checked by Fiber's keyauth middleware and a signing key for the links. It starts with Encore, where secrets are declared in Go and set per environment from the CLI, then shows the .env and Secrets Manager setup on ECS.
Encore works alongside Fiber here, so keyauth and the handlers stay as they are and read their keys from a Go struct that Encore fills in at startup. The struct is the whole declaration in the secrets API, and the Fiber app is mounted with adaptor.FiberApp in one fallback route.
Run in your terminal to get started locally.
The service checks the signing key length in initService, so a deploy with a short or missing key fails at startup. /admin/links sits behind keyauth with a constant-time comparison against secrets.AdminToken, and the links it returns carry an expiry and an HMAC over the file name and expiry, which /files/:name verifies before serving anything:
// files/files.go
package files
import (
"crypto/hmac"
"crypto/sha256"
"crypto/subtle"
"encoding/hex"
"errors"
"net/http"
"net/url"
"regexp"
"strconv"
"time"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/middleware/adaptor"
"github.com/gofiber/fiber/v3/middleware/keyauth"
)
// Values are set per environment with `encore secret set` and never live in the repo.
var secrets struct {
AdminToken string // bearer token for the /admin routes
LinkSigningKey string // HMAC key for expiring download links
}
var name = regexp.MustCompile(`^[a-z0-9-]+\.pdf$`)
//encore:service
type Service struct {
handler http.HandlerFunc
}
func initService() (*Service, error) {
if len(secrets.LinkSigningKey) < 32 {
return nil, errors.New("LinkSigningKey must be at least 32 bytes")
}
app := fiber.New()
admin := app.Group("/admin", keyauth.New(keyauth.Config{
Validator: func(c fiber.Ctx, key string) (bool, error) {
if subtle.ConstantTimeCompare([]byte(key), []byte(secrets.AdminToken)) == 1 {
return true, nil
}
return false, keyauth.ErrMissingOrMalformedAPIKey
},
}))
admin.Post("/links", createLink)
app.Get("/files/:name", download)
return &Service{handler: adaptor.FiberApp(app)}, nil
}
//encore:api public raw path=/!fallback
func (s *Service) Fallback(w http.ResponseWriter, req *http.Request) {
s.handler(w, req)
}
func sign(file string, exp int64) string {
mac := hmac.New(sha256.New, []byte(secrets.LinkSigningKey))
mac.Write([]byte(file + "\n" + strconv.FormatInt(exp, 10)))
return hex.EncodeToString(mac.Sum(nil))
}
// createLink returns a download link that stops working after 15 minutes.
func createLink(c fiber.Ctx) error {
var in struct {
File string `json:"file"`
}
if err := c.Bind().Body(&in); err != nil || !name.MatchString(in.File) {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "file must look like invoice-42.pdf"})
}
exp := time.Now().Add(15 * time.Minute).Unix()
q := url.Values{"exp": {strconv.FormatInt(exp, 10)}, "sig": {sign(in.File, exp)}}
return c.Status(fiber.StatusCreated).JSON(fiber.Map{"url": "/files/" + in.File + "?" + q.Encode()})
}
func download(c fiber.Ctx) error {
file := c.Params("name")
exp, err := strconv.ParseInt(c.Query("exp"), 10, 64)
if err != nil || !name.MatchString(file) {
return fiber.ErrNotFound
}
if !hmac.Equal([]byte(c.Query("sig")), []byte(sign(file, exp))) {
return c.Status(fiber.StatusForbidden).JSON(fiber.Map{"error": "bad signature"})
}
if time.Now().Unix() > exp {
return c.Status(fiber.StatusGone).JSON(fiber.Map{"error": "link expired"})
}
// Stream the file from your bucket here.
return c.JSON(fiber.Map{"file": file, "ok": true})
}
Running this with encore run and local values set, POST /admin/links without a token or with a wrong one returns 401 from keyauth, a valid token returns 201 with a link, the link returns 200, the same link with another file name returns 403, a file name like ../etc is rejected with 400, and a correctly signed link whose expiry has passed returns 410. With a signing key shorter than 32 bytes the service fails to initialize and logs the reason.
Secret values stay out of the repository, and a coding agent that edits the link handler works with secrets.LinkSigningKey as a name, never the value it holds. On deploy, Encore replicates each value to the secrets manager in your own cloud account, Secrets Manager on AWS or Secret Manager on GCP, and the service receives it at runtime. Rotating the signing key is encore secret set --type prod LinkSigningKey and a redeploy, and links signed with the old key stop working at that point, which is usually what you want for download links.
Best fit: Fiber APIs that want admin tokens and signing keys declared in Go, set per environment from the CLI and stored in their own AWS or GCP account.
The common hand-built setup reads os.Getenv("ADMIN_TOKEN") and os.Getenv("LINK_SIGNING_KEY") at startup, loads them from a .env file locally with godotenv, and in production references Secrets Manager from the ECS task definition so the values arrive as environment variables:
{
"name": "api",
"image": "123456789012.dkr.ecr.eu-west-1.amazonaws.com/files-api:latest",
"secrets": [
{ "name": "ADMIN_TOKEN", "valueFrom": "arn:aws:secretsmanager:eu-west-1:123456789012:secret:files-api/admin-token" },
{ "name": "LINK_SIGNING_KEY", "valueFrom": "arn:aws:secretsmanager:eu-west-1:123456789012:secret:files-api/link-signing-key" }
]
}
The Fiber code is the same as the Encore version with the struct fields swapped for the environment variables. The task execution role needs secretsmanager:GetSecretValue on both secrets (plus kms:Decrypt if they use a customer managed key), and the ECS documentation notes that values are injected when the container starts, so a rotated key only reaches the service after a new deployment. Staging needs its own secrets and policy, and the .env file stays on every developer machine, readable by any tool that can read the working tree.
Best fit: teams that already run ECS from Terraform and manage Secrets Manager entries by hand.
Setting ADMIN_TOKEN directly as a plain environment variable in the task definition or the platform settings is the shortest path and keeps nothing in the repository. The value is stored in plain text in the deploy configuration, visible to anyone who can describe the task definition, and it lands in every revision of that definition, which makes it hard to say who had access to an old key.
Best fit: low-risk values in a prototype, not keys that sign links or grant admin access.
Pick Encore to keep Fiber's keyauth and handlers as they are, declare the keys in Go, set them per environment from the CLI and have them stored in Secrets Manager or Secret Manager in your own account, with nothing in the repository for people or agents to read. Pick .env with Secrets Manager on ECS when the secrets already live in an AWS account you manage by hand, and add .env to .gitignore before the first commit. Plain environment variables are fine for values that would not matter if they leaked.
Read them from the environment at startup, keep any local .env file out of git, and in production inject the values from a secrets manager, such as AWS Secrets Manager referenced in the ECS task definition. Validate required secrets at startup so a missing or too-short key stops the service instead of failing on the first request.
Declare a package-level var secrets struct with one string field per secret, set the values with encore secret set --type for each environment type, and read secrets.AdminToken or secrets.LinkSigningKey in Fiber handlers and middleware. The values are not stored in the repository, and on deploy Encore replicates them to Secrets Manager on AWS or Secret Manager on GCP in your own account.
Use the keyauth middleware on a route group and compare the key in Validator with subtle.ConstantTimeCompare against a secret. By default keyauth reads a Bearer token from the Authorization header and returns 401 with missing or invalid API Key when the validator rejects it.
If the values sit in a .env file in the working tree, any agent that can read the repository can read them. Keep .env out of git, inject production values from a secrets manager, and avoid committing example files that contain real keys, so the working tree has no secret values for an agent to copy.
Deploy to your cloud on AWS and Google Cloud
without managing infrastructure
Encore lets developers and agents define application infrastructure directly in code, then automatically provisions it from local development to production in your AWS or GCP account.