| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "log" |
| 7 | "net/url" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/objstore" |
| 12 | ) |
| 13 | |
| 14 | // notifyStoreEntitlement sends the best-effort nudge attached to a durable |
| 15 | // entitlement write. Callers launch it after PutEntitlement succeeds, off the |
| 16 | // request goroutine. A notification failure never changes the stored result. |
| 17 | func (s *Server) notifyStoreEntitlement(account string, e *objstore.Entitlement) { |
| 18 | if s.sendApp == nil || e == nil || e.Applied == nil { |
| 19 | return |
| 20 | } |
| 21 | if e.Decision == nil { |
| 22 | s.notifyStoreApplied(account) |
| 23 | return |
| 24 | } |
| 25 | s.notifyStoreDecided(account, e) |
| 26 | } |
| 27 | |
| 28 | func (s *Server) notifyStoreApplied(account string) { |
| 29 | run, err := s.storeNotificationRecipients() |
| 30 | if err != nil { |
| 31 | log.Printf("console: store application %s: recipients: %v", account, err) |
| 32 | return |
| 33 | } |
| 34 | if len(run.missing) != 0 { |
| 35 | log.Printf("console: store application %s: no keymail grant for %s — not notified (they must sign in to the console once)", |
| 36 | account, strings.Join(run.missing, ", ")) |
| 37 | } |
| 38 | subject := fmt.Sprintf("[carlos] Object storage application for %s", account) |
| 39 | body := fmt.Sprintf("Account %s applied for object storage.\n\nReview storage applications:\n%s/fleet/store\n", account, s.cfg.Origin) |
| 40 | for _, op := range run.to { |
| 41 | s.sendStoreNotification(op, subject, body, "application "+account) |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | // storeNotificationRecipients is the intersection of the configured storage |
| 46 | // operators and the fleet-operator role. The configured list narrows the role; |
| 47 | // it never grants authority by itself. |
| 48 | func (s *Server) storeNotificationRecipients() (*alertRun, error) { |
| 49 | operators, err := s.store.Operators() |
| 50 | if err != nil { |
| 51 | return nil, fmt.Errorf("operators: %w", err) |
| 52 | } |
| 53 | fleet := make(map[string]bool, len(operators)) |
| 54 | for _, address := range operators { |
| 55 | fleet[strings.ToLower(strings.TrimSpace(address))] = true |
| 56 | } |
| 57 | configured := make([]string, 0, len(s.cfg.StoreOperators)) |
| 58 | for _, address := range s.cfg.StoreOperators { |
| 59 | address = strings.ToLower(strings.TrimSpace(address)) |
| 60 | if fleet[address] { |
| 61 | configured = append(configured, address) |
| 62 | } |
| 63 | } |
| 64 | return s.grantRecipients(configured) |
| 65 | } |
| 66 | |
| 67 | func (s *Server) notifyStoreDecided(account string, e *objstore.Entitlement) { |
| 68 | address := strings.ToLower(strings.TrimSpace(e.Applied.By)) |
| 69 | run, err := s.grantRecipients([]string{address}) |
| 70 | if err != nil { |
| 71 | log.Printf("console: store decision %s: recipient: %v", account, err) |
| 72 | return |
| 73 | } |
| 74 | state := "declined" |
| 75 | if e.Decision.Approved { |
| 76 | state = "approved" |
| 77 | } |
| 78 | subject := fmt.Sprintf("[carlos] Object storage application %s", state) |
| 79 | body := fmt.Sprintf("Object storage for account %s was %s.\n\nAccount settings:\n%s/a/%s/settings\n", |
| 80 | account, state, s.cfg.Origin, url.PathEscape(account)) |
| 81 | for _, recipient := range run.to { |
| 82 | s.sendStoreNotification(recipient, subject, body, "decision "+account) |
| 83 | } |
| 84 | // Someone who signed in by email has no grant: the notice goes by |
| 85 | // Keymail federation or the console's own email instead. |
| 86 | for _, missing := range run.missing { |
| 87 | ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 88 | if _, err := s.deliver(ctx, missing, subject, body, 12*time.Second); err != nil { |
| 89 | log.Printf("console: store decision %s: delivery to %s: %v", account, missing, err) |
| 90 | } |
| 91 | cancel() |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | func (s *Server) sendStoreNotification(recipient operatorGrant, subject, body, event string) { |
| 96 | ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 97 | defer cancel() |
| 98 | if _, err := s.sendApp(ctx, recipient.token, recipient.address, subject, body); err != nil { |
| 99 | log.Printf("console: store %s: keymail to %s: %v", event, recipient.address, err) |
| 100 | } |
| 101 | } |
| 102 | |