| new file mode 100644 |
| index 000000000..e8e143859 |
| --- /dev/null |
| +++ b/store_credential.go |
| @@ -0,0 +1,191 @@ |
| +// store_credential.go is `carlos store credential`: mint a write credential |
| +// for an app's store, or revoke every one it issued (store write credential |
| +// spec, 2026-11-25). API mode only, like the rest of `store`. A write |
| +// credential can only upload under one prefix of the store, for a lifetime |
| +// from 15 minutes to 24 hours, and is meant for a third party such as a |
| +// LiveKit recorder. The console mints it; this file sends the request and |
| +// prints the answer. |
| +// |
| +// The secret and session token go to stdout and nowhere else. |
| +package main |
| + |
| +import ( |
| + "context" |
| + "encoding/json" |
| + "errors" |
| + "flag" |
| + "fmt" |
| + "io" |
| + "net/http" |
| + "strings" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/objstore" |
| +) |
| + |
| +// storeCredentialCopy is this verb's text by key. New copy, pending the Luna |
| +// pass (docs/superpowers/notes/copy-luna-brief-store-write-credential.md). |
| +var storeCredentialCopy = map[string]string{ |
| + "usage": "usage: carlos store credential --write --prefix <folder/> --ttl <duration> --app <a> [--environment <e>] [--json]\n" + |
| + " carlos store credential --revoke --app <a> [--environment <e>] [--json]", |
| + "store_usage": " credential --write --prefix <folder/> --ttl <duration> --app <a> [--json]\n" + |
| + " Prints a credential that can only upload files\n" + |
| + " under one folder of the app's store, for 15 minutes\n" + |
| + " to 24 hours. Give it to a service that uploads for\n" + |
| + " you, such as a LiveKit recorder. --revoke stops\n" + |
| + " every one issued so far.", |
| + "flag_write": "mint a credential that can only upload under --prefix", |
| + "flag_revoke": "revoke every write credential this store has issued", |
| + "flag_prefix": "the folder the credential may upload into, ending in /, such as recordings/", |
| + "flag_ttl": "how long the credential works, from 15m to 24h, such as 4h", |
| + "flag_json": "print the answer as JSON, with the field names LiveKit's S3Upload takes", |
| + "flag_app": "app whose store the credential is for", |
| + "need_one": "say --write or --revoke", |
| + "need_prefix": "--write needs --prefix, such as --prefix recordings/", |
| + "need_ttl": "--write needs --ttl, such as --ttl 4h", |
| + "revoke_flags": "--revoke takes no --prefix or --ttl", |
| + "line_expires": "write credential expires %s (in %s)", |
| + "line_scope": "can do upload under %s, nothing else", |
| + "line_bucket": "bucket %s", |
| + "line_region": "region %s", |
| + "line_access_key": "access_key %s", |
| + "line_secret": "secret %s", |
| + "line_token": "session_token %s", |
| + "line_revoked": "write credentials revoked", |
| + "line_revoked_at": "issued before %s", |
| + "line_revoke_note": "note this can take a few minutes to reach every upload", |
| + "unreadable": "store credential: unreadable response: %w", |
| +} |
| + |
| +func storeCredentialText(key string) string { return storeCredentialCopy[key] } |
| + |
| +// apiWriteCredential mirrors the console's mint answer. |
| +type apiWriteCredential struct { |
| + AccessKey string `json:"access_key"` |
| + Secret string `json:"secret"` |
| + SessionToken string `json:"session_token"` |
| + Region string `json:"region"` |
| + Bucket string `json:"bucket"` |
| + Prefix string `json:"prefix"` |
| + ExpiresAt time.Time `json:"expires_at"` |
| +} |
| + |
| +// apiWriteRevoke mirrors the console's revoke answer. |
| +type apiWriteRevoke struct { |
| + RevokedBefore time.Time `json:"revoked_before"` |
| +} |
| + |
| +func (s *apiSession) storeCredentialCall(ctx context.Context, a apiAcct, app, env, tail string, in any, out any) error { |
| + status, body, err := s.postJSON(ctx, apiPath(a, app, storeEnvTail(tail, env)), in) |
| + if err != nil { |
| + return err |
| + } |
| + if status != http.StatusOK { |
| + return apiErrorFromBody(status, body) |
| + } |
| + if err := json.Unmarshal(body, out); err != nil { |
| + return fmt.Errorf(storeCredentialText("unreadable"), err) |
| + } |
| + return nil |
| +} |
| + |
| +func runStoreCredential(args []string) error { |
| + fs := flag.NewFlagSet("store credential", flag.ExitOnError) |
| + app := appFlag(fs, storeCredentialText("flag_app")) |
| + write := fs.Bool("write", false, storeCredentialText("flag_write")) |
| + revoke := fs.Bool("revoke", false, storeCredentialText("flag_revoke")) |
| + prefix := fs.String("prefix", "", storeCredentialText("flag_prefix")) |
| + ttl := fs.Duration("ttl", 0, storeCredentialText("flag_ttl")) |
| + asJSON := fs.Bool("json", false, storeCredentialText("flag_json")) |
| + environment := storeEnvironmentFlag(fs, "a credential for this config environment's store instead of the app's own") |
| + acctRaw, acctIDRaw, _ := accountFlags(fs) |
| + consoleFlag := consoleOption(fs, "console origin to act through (API mode only; default: the only logged-in one)") |
| + fs.Usage = flagUsage(fs, "") |
| + fs.Parse(args) |
| + |
| + if err := storeCredentialFlags(*write, *revoke, *prefix, *ttl); err != nil { |
| + return fmt.Errorf("store credential: %w\n%s", err, storeCredentialText("usage")) |
| + } |
| + name := strings.TrimSpace(*app) |
| + if name != "" { |
| + if err := validAppName(name); err != nil { |
| + return fmt.Errorf("store credential: %w", err) |
| + } |
| + } |
| + sess := requireStoreAPISession(*consoleFlag) |
| + if !*asJSON { |
| + printUsing(stdout, sess) |
| + } |
| + acct := resolvedAccount(sess, acctRaw, acctIDRaw) |
| + name = resolvedApp(sess, acct, name, storeCredentialText("usage")) |
| + env := strings.TrimSpace(*environment) |
| + ctx := context.Background() |
| + if *revoke { |
| + var res apiWriteRevoke |
| + if err := sess.storeCredentialCall(ctx, acct, name, env, "/store/credentials/revoke", struct{}{}, &res); err != nil { |
| + return fmt.Errorf("store credential --revoke: %w", err) |
| + } |
| + return printStoreRevoke(stdout, res, *asJSON) |
| + } |
| + in := struct { |
| + Access string `json:"access"` |
| + Prefix string `json:"prefix"` |
| + TTLSeconds int64 `json:"ttl_seconds"` |
| + }{"write", *prefix, int64(*ttl / time.Second)} |
| + var res apiWriteCredential |
| + if err := sess.storeCredentialCall(ctx, acct, name, env, "/store/credentials", in, &res); err != nil { |
| + return fmt.Errorf("store credential --write: %w", err) |
| + } |
| + return printStoreCredential(stdout, res, *asJSON, time.Now()) |
| +} |
| + |
| +// storeCredentialFlags checks the flags before any request: exactly one of |
| +// --write and --revoke, and for --write a prefix and lifetime the console |
| +// would take. The console checks them again. |
| +func storeCredentialFlags(write, revoke bool, prefix string, ttl time.Duration) error { |
| + switch { |
| + case write == revoke: |
| + return errors.New(storeCredentialText("need_one")) |
| + case revoke && (prefix != "" || ttl != 0): |
| + return errors.New(storeCredentialText("revoke_flags")) |
| + case revoke: |
| + return nil |
| + case prefix == "": |
| + return errors.New(storeCredentialText("need_prefix")) |
| + case ttl == 0: |
| + return errors.New(storeCredentialText("need_ttl")) |
| + } |
| + if err := objstore.ValidWritePrefix(prefix); err != nil { |
| + return err |
| + } |
| + return objstore.ValidWriteTTL(ttl) |
| +} |
| + |
| +// printStoreCredential leads with when the credential stops working and |
| +// what it may do, then the values a third party needs. |
| +func printStoreCredential(w io.Writer, c apiWriteCredential, asJSON bool, now time.Time) error { |
| + if asJSON { |
| + enc := json.NewEncoder(w) |
| + enc.SetIndent("", " ") |
| + return enc.Encode(c) |
| + } |
| + left := c.ExpiresAt.Sub(now).Round(time.Minute) |
| + fmt.Fprintf(w, storeCredentialText("line_expires")+"\n", c.ExpiresAt.UTC().Format("2006-01-02 15:04 UTC"), left) |
| + fmt.Fprintf(w, storeCredentialText("line_scope")+"\n", c.Prefix) |
| + fmt.Fprintf(w, storeCredentialText("line_bucket")+"\n", c.Bucket) |
| + fmt.Fprintf(w, storeCredentialText("line_region")+"\n", c.Region) |
| + fmt.Fprintf(w, storeCredentialText("line_access_key")+"\n", c.AccessKey) |
| + fmt.Fprintf(w, storeCredentialText("line_secret")+"\n", c.Secret) |
| + fmt.Fprintf(w, storeCredentialText("line_token")+"\n", c.SessionToken) |
| + return nil |
| +} |
| + |
| +func printStoreRevoke(w io.Writer, r apiWriteRevoke, asJSON bool) error { |
| + if asJSON { |
| + return json.NewEncoder(w).Encode(r) |
| + } |
| + fmt.Fprintln(w, storeCredentialText("line_revoked")) |
| + fmt.Fprintf(w, storeCredentialText("line_revoked_at")+"\n", r.RevokedBefore.UTC().Format("2006-01-02 15:04:05 UTC")) |
| + fmt.Fprintln(w, storeCredentialText("line_revoke_note")) |
| + return nil |
| +} |