Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/added-19163-customers-hub-import.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Customers can import the real name, organization and country a confirmed customer entered on the hub and consented to share with this hive, shown "from the hub" and never over a value set by hand; a withdrawal or three days without confirmation clears it. Off by default: `customers.hub_import.enabled`, `HIVE_CUSTOMERS_HUB_IMPORT_ENABLED`, or Settings, Features ([#19163](https://github.com/hivecommons/hive/issues/19163)).
152 changes: 152 additions & 0 deletions src/cmd/hive/customers_hub_import.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package main

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"strings"
"time"

"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/customers"
spoke "github.com/hivecommons/hive/pkg/hub/spoke"
)

// The Customers hub import (#19163, phase 3 of
// src/docs/design/customers-hub-consent.md). pkg/customers decides what is
// asked and what an answer may change; this file is the network call and the
// wiring. Off unless customers.hub_import.enabled AND a hub registration.

const (
// customersHubMatchPath is the hub's match route (pkg/hub
// customerMatchPath; cmd/hive does not import pkg/hub).
customersHubMatchPath = "/api/customers/match"
// customersHubImportInterval is how often the bounded pass runs.
customersHubImportInterval = customers.HubImportInterval
// customersHubImportTimeout bounds one pass, all its requests together.
customersHubImportTimeout = 60 * time.Second
// customersHubRequestTimeout bounds one request.
customersHubRequestTimeout = 20 * time.Second
// customersHubMaxErrBody caps what is read of a refusal.
customersHubMaxErrBody = 512
)

// customersHubMatcher posts one batch of match hashes to the hub with the
// per-hive heartbeat bearer. It sends no cookie, no identity header and no
// plaintext login.
type customersHubMatcher struct {
hubURL string
hiveID string
client *http.Client
}

func (m customersHubMatcher) Match(ctx context.Context, hashes []string) (customers.HubMatchReply, error) {
bearer := spoke.SpokeHeartbeatKey()
if bearer == "" {
return customers.HubMatchReply{}, errors.New("no hub credential (HIVE_HEARTBEAT_KEY / HIVE_HUB_SECRET) configured")
}
body, err := json.Marshal(struct {
HiveID string `json:"hive_id"`
Hashes []string `json:"hashes"`
}{HiveID: m.hiveID, Hashes: hashes})
if err != nil {
return customers.HubMatchReply{}, err
}
endpoint := strings.TrimRight(strings.TrimSpace(m.hubURL), "/") + customersHubMatchPath
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(body))
if err != nil {
return customers.HubMatchReply{}, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+bearer)
client := m.client
if client == nil {
client = customersHubClient()
}
resp, err := client.Do(req)
if err != nil {
return customers.HubMatchReply{}, err
}
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode != http.StatusOK {
_, _ = io.Copy(io.Discard, io.LimitReader(resp.Body, customersHubMaxErrBody))
return customers.HubMatchReply{}, fmt.Errorf("hub match: HTTP %d", resp.StatusCode)
}
return customers.DecodeHubMatchReply(resp.Body)
}

// customersHubClient never follows a redirect, so the bearer only ever goes
// to the configured hub.
func customersHubClient() *http.Client {
return &http.Client{
Timeout: customersHubRequestTimeout,
CheckRedirect: func(*http.Request, []*http.Request) error {
return http.ErrUseLastResponse
},
}
}

// customersHubLinked reports whether this hive is registered with a hub it
// can ask: hub reporting on, a hub URL and a hive id.
func customersHubLinked(cfg *config.Config) bool {
return cfg != nil && cfg.Hub.Enabled && strings.TrimSpace(cfg.Hub.URL) != "" && strings.TrimSpace(cfg.HiveID) != ""
}

// newCustomersHubImport builds one import over the current config. Enabled
// is the spoke switch and the hub link together: either off, no request.
func newCustomersHubImport(store *customers.Store, cfg *config.Config, logger *slog.Logger) *customers.HubImport {
hiveID := ""
hubURL := ""
if cfg != nil {
hiveID = strings.TrimSpace(cfg.HiveID)
hubURL = cfg.Hub.URL
}
return &customers.HubImport{
Store: store,
Matcher: customersHubMatcher{hubURL: hubURL, hiveID: hiveID},
Enabled: func() bool { return cfg.CustomersHubImportEnabled() && customersHubLinked(cfg) },
Key: func() string { return spoke.SpokeCustomerMatchKey(hiveID) },
Audit: func(person, action string, fields []string) {
// One line per import or clear: person id and field names,
// never values, never a login or a hash.
if logger != nil {
logger.Info("audit: "+action, slog.String("person", person), slog.Any("fields", fields))
}
},
}
}

// runCustomersHubImport is one import pass; only names one person for the
// accept-time pull, "" asks about every confirmed customer. Staleness expiry
// runs on every pass, switch on or off, and makes no request.
func (b *boot) runCustomersHubImport(ctx context.Context, cfg *config.Config, only string) {
if b.customers == nil || cfg == nil || !cfg.CustomersEnabled() {
return
}
ctx, cancel := context.WithTimeout(ctx, customersHubImportTimeout)
defer cancel()
res := newCustomersHubImport(b.customers.svc.Store(), cfg, b.logger).Run(ctx, only)
if b.logger != nil && (res.Requests > 0 || res.Expired > 0) {
b.logger.Info("[customers] hub import", "requests", res.Requests, "asked", res.Asked,
"imported", res.Imported, "cleared", res.Cleared, "expired", res.Expired, "skipped", res.Skipped)
}
}

// hubImportOnAccept pulls the hub's answer for a person just accepted. It
// never delays or fails the accept.
func (c *customersDashboard) hubImportOnAccept(person string) {
if c.hubImport == nil || person == "" {
return
}
run := func() { c.hubImport(context.Background(), person) }
if c.spawn != nil {
c.spawn(run)
return
}
go run()
}
108 changes: 108 additions & 0 deletions src/cmd/hive/customers_hub_import_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package main

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync/atomic"
"testing"

"github.com/hivecommons/hive/pkg/config"
"github.com/hivecommons/hive/pkg/customers"
spoke "github.com/hivecommons/hive/pkg/hub/spoke"
)

func TestCustomersHubMatcherPostsHashesWithBearer(t *testing.T) {
t.Setenv(spoke.EnvHeartbeatKey, "bearer-1")
var got struct {
HiveID string `json:"hive_id"`
Hashes []string `json:"hashes"`
}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != customersHubMatchPath {
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
}
if r.Header.Get("Authorization") != "Bearer bearer-1" {
t.Errorf("bearer = %q", r.Header.Get("Authorization"))
}
if r.Header.Get("Cookie") != "" {
t.Error("no cookie may be sent")
}
if err := json.NewDecoder(r.Body).Decode(&got); err != nil {
t.Errorf("decode: %v", err)
}
_, _ = w.Write([]byte(`{"matches":[{"match_hash":"h1","real_name":"Ada\u0007 L","notes":"never read","cleared":["organization","tags"]}]}`))
}))
defer srv.Close()

reply, err := customersHubMatcher{hubURL: srv.URL + "/", hiveID: "hive-a"}.Match(context.Background(), []string{"h1"})
if err != nil {
t.Fatal(err)
}
if got.HiveID != "hive-a" || len(got.Hashes) != 1 || got.Hashes[0] != "h1" {
t.Fatalf("request body = %+v", got)
}
if len(reply.Matches) != 1 {
t.Fatalf("matches = %+v", reply.Matches)
}
m := reply.Matches[0]
if m.RealName != "Ada L" || len(m.Cleared) != 1 || m.Cleared[0] != customers.FieldOrganization {
t.Fatalf("reply not sanitised: %+v", m)
}
}

func TestCustomersHubMatcherRefusal(t *testing.T) {
t.Setenv(spoke.EnvHeartbeatKey, "bearer-1")
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
http.NotFound(w, nil)
}))
defer srv.Close()
if _, err := (customersHubMatcher{hubURL: srv.URL, hiveID: "hive-a"}).Match(context.Background(), []string{"h1"}); err == nil {
t.Fatal("a 404 (hub sharing off) must be an error")
}
}

func TestCustomersHubImportOffMakesNoRequest(t *testing.T) {
t.Setenv(config.EnvCustomersEnabled, "")
t.Setenv(config.EnvCustomersHubImportEnabled, "")
t.Setenv(spoke.EnvHeartbeatKey, "bearer-1")
t.Setenv(spoke.EnvCustomerMatchKey, "match-key")
var hits atomic.Int32
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
hits.Add(1)
_, _ = w.Write([]byte(`{"matches":[]}`))
}))
defer srv.Close()

store, err := customers.Open(t.TempDir(), nil)
if err != nil {
t.Fatal(err)
}
if _, err := store.Add("Ada", []customers.Identity{{Kind: customers.KindGitHub, Value: "ada"}}, "owner"); err != nil {
t.Fatal(err)
}
on := true
cases := map[string]*config.Config{
"switch off": {HiveID: "hive-a", Hub: config.HubConfig{Enabled: true, URL: srv.URL}},
"no hub": {HiveID: "hive-a", Customers: config.CustomersConfig{HubImport: config.CustomersHubImportConfig{Enabled: &on}}},
}
for name, cfg := range cases {
res := newCustomersHubImport(store, cfg, nil).Run(context.Background(), "")
if res.Requests != 0 || res.Skipped != customers.HubSkipDisabled {
t.Fatalf("%s: %+v", name, res)
}
}
if hits.Load() != 0 {
t.Fatalf("hub was called %d times with the import off", hits.Load())
}

cfg := &config.Config{HiveID: "hive-a", Hub: config.HubConfig{Enabled: true, URL: srv.URL},
Customers: config.CustomersConfig{HubImport: config.CustomersHubImportConfig{Enabled: &on}}}
if res := newCustomersHubImport(store, cfg, nil).Run(context.Background(), ""); res.Requests != 1 {
t.Fatalf("on: %+v", res)
}
if hits.Load() != 1 {
t.Fatalf("hub hits = %d, want 1", hits.Load())
}
}
8 changes: 7 additions & 1 deletion src/cmd/hive/customerswire.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ type customersDashboard struct {
// mentions is the accept-time GitHub search (step E, #18884); nil when
// it is not wired. Only Accept and AcceptInto reach it.
mentions *customers.MentionSearch
logger *slog.Logger
// hubImport is the accept-time hub pull (#19163); nil when not wired.
// It asks only about the person just accepted and is off unless
// customers.hub_import.enabled and a hub registration.
hubImport func(ctx context.Context, person string)
logger *slog.Logger
// spawn runs the search; nil means a goroutine. Tests run it inline.
spawn func(func())
}
Expand Down Expand Up @@ -158,6 +162,7 @@ func (c *customersDashboard) Accept(v dashboard.CustomersViewer, suggestionID st
res, err := customerResult(c.svc.Accept(customersViewer(v), suggestionID))
if err == nil {
c.searchOnAccept(res.ID, ident, found)
c.hubImportOnAccept(res.ID)
}
return res, err
}
Expand Down Expand Up @@ -227,6 +232,7 @@ func (c *customersDashboard) AcceptInto(v dashboard.CustomersViewer, suggestionI
res, err := customerResult(c.svc.AcceptInto(customersViewer(v), suggestionID, into))
if err == nil {
c.searchOnAccept(res.ID, ident, found)
c.hubImportOnAccept(res.ID)
}
return res, err
}
Expand Down
6 changes: 6 additions & 0 deletions src/cmd/hive/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3656,6 +3656,12 @@ func (b *boot) bootDashboardAPIWith(deps bootDashboardAPIDeps) {
// Step E (#18884): the budgeted GitHub search on accept. Off unless
// customers.mention_search.enabled; never reached by the sweep.
svc.mentions = newCustomersMentionSearch(svc.svc, b.customersMentionConfig, b.currentGitHubClient)
// Phase 3 of customers-hub-consent (#19163): pull hub-consented
// fields for a person just accepted. Off unless
// customers.hub_import.enabled and a hub registration.
svc.hubImport = func(ctx context.Context, person string) {
b.runCustomersHubImport(ctx, b.customersMentionConfig(), person)
}
b.dashSrv.SetCustomersService(svc)
if b.sched != nil {
installCustomersAgentContext(svc, b.sched.SetCustomerContextLookup)
Expand Down
7 changes: 7 additions & 0 deletions src/cmd/hive/sweep_workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ func (b *boot) sweepJobs() []sweepJob {
{"customers_inference", fixed(customersInferenceInterval), sweepOptional, func(ctx context.Context, _ *github.Client, cfg *config.Config) {
b.runCustomersInference(ctx, cfg)
}},
// Customers hub import (#19163): off unless
// customers.hub_import.enabled and a hub registration. Posts only
// salted hashes of confirmed customers' GitHub logins to the hub;
// makes no GitHub call. Staleness expiry runs every pass.
{"customers_hub_import", fixed(customersHubImportInterval), sweepOptional, func(ctx context.Context, _ *github.Client, cfg *config.Config) {
b.runCustomersHubImport(ctx, cfg, "")
}},
{"rfc_notify", fixed(rfcnotify.DefaultInterval + time.Second), sweepOptional, func(ctx context.Context, gh *github.Client, cfg *config.Config) {
runRFCNotifySweep(ctx, newRFCNotifySource(gh, notify), len(cfg.Project.Repos), cfg, b.dashSrv, notify, b.logger)
}},
Expand Down
2 changes: 2 additions & 0 deletions src/docs/env-vars.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ new value at the same time.
| `HIVE_CUSTOMERS_ENABLED` | No | config `customers.enabled` (default on) | Turns the Customers section and its people store on or off; `false` opens no store, runs no sweep and hides the section, overriding config. Nothing the store holds leaves the hive. |
| `HIVE_CUSTOMERS_INFERENCE_ENABLED` | No | config `customers.inference.enabled` (default off) | Turns goal and satisfaction suggestions on or off, overriding config. Needs Customers on. When on, a customer's own issues, pull requests and approved mail are sent to the meetings model; every suggestion waits for a person to confirm, edit or reject it. |
| `HIVE_CUSTOMERS_AVATARS_ENABLED` | No | config `customers.avatars.enabled` (default on) | Turns GitHub pictures on the Customers list on or off, overriding config. On, the viewer's browser requests `https://github.com/<login>.png` with no referrer for each person with a confirmed GitHub login; off, the list shows initials and makes no request to github.com. |
| `HIVE_CUSTOMERS_HUB_IMPORT_ENABLED` | No | config `customers.hub_import.enabled` (default off) | Turns the Customers hub import on or off, overriding config. Needs Customers on and a hub registration. When on, the hive posts salted hashes of its confirmed customers' GitHub logins to the hub hourly and on accept; the hub answers only with the real name, organization and country a person entered there and consented to share with this hive. See [customers-hub-consent](design/customers-hub-consent.md). |
| `HIVE_CUSTOMERS_MENTION_SEARCH_ENABLED` | No | config `customers.mention_search.enabled` (default off) | Turns the accept-time GitHub mention search on or off, overriding config. Needs Customers on. When on, accepting a suggested GitHub login makes at most two GitHub search requests for that login, counted against the shared API budget. |
| `HIVE_SESSIONS_ENABLED` | No | config `dashboard.sessions.enabled` (default on) | Turns the dashboard Inception (spek) sessions feature (interactive CLI terminals, `/api/sessions`) on or off; `false` hides it, overriding config. Limits: `HIVE_SESSIONS_MAX`, `HIVE_SESSIONS_MAX_PER_USER`, `HIVE_SESSIONS_IDLE_TTL_HOURS`. Workspace: `HIVE_SESSIONS_WORKSPACE_REPO` (the `org/repo` sessions open in, default `project.org/primary_repo`), `HIVE_SESSIONS_WORKSPACE_DIR` (root of the one shared clone per repo, `<dir>/<org>/<repo>`, and of each session's own git worktree of it, `<dir>/.sessions/<id>/<org>/<repo>` on branch `hive-session/<id>`; sessions share commits and branches but not uncommitted edits or `.spektacular/` state; default `/data/session-sandbox/workspace`), `HIVE_SESSIONS_GID` (the sessions-only group that shares the clone, default 20000). Inbox seeds: `HIVE_SESSIONS_SEED_MAX_BYTES` (cap on one item's enveloped text in `.hive/inbox/`, default 524288); see [Sessions API](api-reference/sessions.md). |
| `HIVE_SESSIONS_AUTOSTART` | No | config `dashboard.sessions.autostart` (default on) | Turns the session startup auto-answer on or off, overriding config: when a session starts the hive answers the vendor CLI's startup screens (first-run notice, trust for the session's folder, onboarding) so its first request runs unattended. It never answers a permission prompt or a login screen. `HIVE_SESSIONS_AUTOSTART_WINDOW_SEC` (default `90`) bounds how long it answers for after a start. See [Startup auto-answer](api-reference/sessions.md#startup-auto-answer). |
Expand Down Expand Up @@ -701,6 +702,7 @@ only when neither is configured.
|---|---:|---|---|
| `HIVE_HEARTBEAT_KEY` | No | derived from `HIVE_HUB_SECRET` | Spoke heartbeat signing sub-key. For a self-service (`byo-`) hive this is the only key the spoke holds: the hub mints it at registration, shows it once, and stores only a hash. There is no derivation fallback for a `byo-` ID; rotate it from the dashboard if it is lost (see [hub-deployment.md](hub-deployment.md#self-service-registration)). |
| `HIVE_SESSION_KEY` | No | derived from `HIVE_HUB_SECRET` | Spoke session-cookie signing sub-key. |
| `HIVE_CUSTOMER_MATCH_KEY` | No | derived from `HIVE_HUB_SECRET` and `HIVE_ID` | Per-hive key the Customers hub import salts GitHub login hashes with (label `hive-customer-match-v1`). Never the heartbeat bearer. Without it and without `HIVE_HUB_SECRET` the import asks nothing. |
| `HIVE_INVITE_KEY` | No | derived from `HIVE_HUB_SECRET` | Per-hive contributor-invite signing key. Symmetric: the spoke both mints and verifies invite tokens with it. |
| `HIVE_TERMINAL_KEY` | No | self-derived per-hive from `HIVE_HUB_SECRET` + `HIVE_ID` | Per-hive terminal-assertion signing key. It never falls back to a fleet-uniform key. |
| `HIVE_SSO_PUBLIC_KEY` | No | none | Ed25519 **public** key a spoke verifies hub-minted SSO handoff tokens with. Holding only the public key, a spoke can verify but cannot mint. |
Expand Down
Loading
Loading