package console import ( "context" "fmt" "log" "net/url" "strings" "time" "github.com/carlosframework/platform/internal/objstore" ) // notifyStoreEntitlement sends the best-effort nudge attached to a durable // entitlement write. Callers launch it after PutEntitlement succeeds, off the // request goroutine. A notification failure never changes the stored result. func (s *Server) notifyStoreEntitlement(account string, e *objstore.Entitlement) { if s.sendApp == nil || e == nil || e.Applied == nil { return } if e.Decision == nil { s.notifyStoreApplied(account) return } s.notifyStoreDecided(account, e) } func (s *Server) notifyStoreApplied(account string) { run, err := s.storeNotificationRecipients() if err != nil { log.Printf("console: store application %s: recipients: %v", account, err) return } if len(run.missing) != 0 { log.Printf("console: store application %s: no keymail grant for %s — not notified (they must sign in to the console once)", account, strings.Join(run.missing, ", ")) } subject := fmt.Sprintf("[carlos] Object storage application for %s", account) body := fmt.Sprintf("Account %s applied for object storage.\n\nReview storage applications:\n%s/fleet/store\n", account, s.cfg.Origin) for _, op := range run.to { s.sendStoreNotification(op, subject, body, "application "+account) } } // storeNotificationRecipients is the intersection of the configured storage // operators and the fleet-operator role. The configured list narrows the role; // it never grants authority by itself. func (s *Server) storeNotificationRecipients() (*alertRun, error) { operators, err := s.store.Operators() if err != nil { return nil, fmt.Errorf("operators: %w", err) } fleet := make(map[string]bool, len(operators)) for _, address := range operators { fleet[strings.ToLower(strings.TrimSpace(address))] = true } configured := make([]string, 0, len(s.cfg.StoreOperators)) for _, address := range s.cfg.StoreOperators { address = strings.ToLower(strings.TrimSpace(address)) if fleet[address] { configured = append(configured, address) } } return s.grantRecipients(configured) } func (s *Server) notifyStoreDecided(account string, e *objstore.Entitlement) { address := strings.ToLower(strings.TrimSpace(e.Applied.By)) run, err := s.grantRecipients([]string{address}) if err != nil { log.Printf("console: store decision %s: recipient: %v", account, err) return } state := "declined" if e.Decision.Approved { state = "approved" } subject := fmt.Sprintf("[carlos] Object storage application %s", state) body := fmt.Sprintf("Object storage for account %s was %s.\n\nAccount settings:\n%s/a/%s/settings\n", account, state, s.cfg.Origin, url.PathEscape(account)) for _, recipient := range run.to { s.sendStoreNotification(recipient, subject, body, "decision "+account) } // Someone who signed in by email has no grant: the notice goes by // Keymail federation or the console's own email instead. for _, missing := range run.missing { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) if _, err := s.deliver(ctx, missing, subject, body, 12*time.Second); err != nil { log.Printf("console: store decision %s: delivery to %s: %v", account, missing, err) } cancel() } } func (s *Server) sendStoreNotification(recipient operatorGrant, subject, body, event string) { ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) defer cancel() if _, err := s.sendApp(ctx, recipient.token, recipient.address, subject, body); err != nil { log.Printf("console: store %s: keymail to %s: %v", event, recipient.address, err) } }