14ecff0730596f8be147b255afc8acb99e9095e2
max
  Fri Sep 18 09:06:06 2026 -0700
hgHubConnect: an api key made on one geo mirror now works on all of them, behind syncHubApiKeys

#Preview2 week - bugs introduced now will need a build patch to fix
#Preview2 week - bugs introduced now will need a build patch to fix
Api keys live in the hgcentral of whichever mirror issued them, so a key made on
genome.ucsc.edu was rejected on genome-euro and genome-asia, and the botDelay
error told the user to go and make a second one. hgHubConnect now tells the other
nodes about a key as soon as it generates or revokes one: cjGenerateApiKey and
cjRevokeApiKey call syncApiKeyToOtherNodes(), which posts a hgHubSyncApiKey
cartJson request to each peer, and cjSyncApiKey() on the far side writes it into
that mirror's own table.

The peers come from hgcentral.gbNode via the new geoMirrorNotifyOtherNodes(), and
the request is signed with login.cookieSalt, which all of a site's mirrors already
share - it is what makes the login cookie verifiable on each of them. So neither a
list of mirror addresses nor a new shared secret needs provisioning. The notify is
best effort: a peer that is down is warn()ed about and skipped, never failing the
local generate or revoke, which has already committed by then.

All of it is off unless hg.conf says syncHubApiKeys=on. With the gate off the
sender returns at once, the receiver refuses the request outright rather than
merely being unreachable, and botDelay keeps printing the old server-specific
wording, which off is still the truth. Registered in hgConfCatalog.py as a gate,
to be flipped once this is released.

refs #38323

diff --git src/hg/lib/hubSpaceKeys.c src/hg/lib/hubSpaceKeys.c
index 9815528dcdd..a00345421a2 100644
--- src/hg/lib/hubSpaceKeys.c
+++ src/hg/lib/hubSpaceKeys.c
@@ -1,27 +1,28 @@
 /* hubSpaceKeys.c was originally generated by the autoSql program, which also 
  * generated hubSpaceKeys.h and hubSpaceKeys.sql.  This module links the database and
  * the RAM representation of objects. */
 
 #include "common.h"
 #include "linefile.h"
 #include "dystring.h"
 #include "jksql.h"
 #include "hubSpaceKeys.h"
 #include "hdb.h"
 #include "hgConfig.h"
 #include "htmshell.h"
+#include "md5.h"
 
 
 char *hubSpaceKeysCommaSepFieldNames = "userName,apiKey";
 
 void hubSpaceKeysStaticLoad(char **row, struct hubSpaceKeys *ret)
 /* Load a row from hubSpaceKeys table into ret.  The contents of ret will
  * be replaced at the next call to this function. */
 {
 
 ret->userName = row[0];
 ret->apiKey = row[1];
 }
 
 struct hubSpaceKeys *hubSpaceKeysLoad(char **row)
 /* Load a hubSpaceKeys from row fetched with select * from hubSpaceKeys
@@ -158,32 +159,62 @@
 
 void hubSpaceRevokeApiKey(char *userName)
 /* Remove any api keys for userName. errAborts if userName is NULL.
  * Run in an errCatch to handle errors. */
 {
 if (!userName)
     errAbort("hubSpaceRevokeApiKey: no userName. You must be logged in to revoke your api key");
 
 char *tableName = cfgOptionDefault("authTableName", AUTH_TABLE_DEFAULT);
 struct sqlConnection *conn = hConnectCentral();
 struct dyString *query = sqlDyStringCreate("delete from %s where userName='%s'", tableName, userName);
 sqlUpdate(conn, dyStringCannibalize(&query));
 hDisconnectCentral(&conn);
 }
 
+static void hubSpaceSaveApiKey(char *userName, char *apiKey)
+/* Insert apiKey for userName into the api key table, replacing any existing key. */
+{
+struct sqlConnection *conn = hConnectCentral();
+char *tableName = cfgOptionDefault("authTableName", AUTH_TABLE_DEFAULT);
+struct dyString *query = sqlDyStringCreate("insert into %s values ('%s', '%s') on duplicate key update apiKey='%s'", tableName, userName, apiKey, apiKey);
+sqlUpdate(conn, dyStringCannibalize(&query));
+hDisconnectCentral(&conn);
+}
+
 char *hubSpaceGenerateApiKey(char *userName)
 /* Make a random (but not crypto-secure) api key for userName, for use of hubtools to upload
  * to hubspace or for bypassing cloudflare. errAborts if userName is NULL.
  * Run in an errCatch to handle errors. */
 {
 if (!userName)
     errAbort("hubSpaceGenerateApiKey: no userName. You must be logged in to generate an api key");
 
 char *apiKey = makeRandomKey(256); // just needs some arbitrary length
-// save this key to the database for this user, the 'on duplicate' part automatically revokes old keys
-struct sqlConnection *conn = hConnectCentral();
-char *tableName = cfgOptionDefault("authTableName", AUTH_TABLE_DEFAULT);
-struct dyString *query = sqlDyStringCreate("insert into %s values ('%s', '%s') on duplicate key update apiKey='%s'", tableName, userName, apiKey, apiKey);
-sqlUpdate(conn, dyStringCannibalize(&query));
-hDisconnectCentral(&conn);
+hubSpaceSaveApiKey(userName, apiKey);
 return apiKey;
 }
+
+void hubSpaceSetApiKey(char *userName, char *apiKey)
+/* Set userName's api key to apiKey, replacing any existing key -- unlike hubSpaceGenerateApiKey,
+ * this does not make up a new key.  Used to adopt a key that a peer geo mirror generated, so
+ * that a key works the same on every UCSC mirror.  errAborts if userName or apiKey is NULL. */
+{
+if (!userName || !apiKey)
+    errAbort("hubSpaceSetApiKey: need both a userName and an apiKey");
+hubSpaceSaveApiKey(userName, apiKey);
+}
+
+char *hubSpaceApiKeySyncSig(char *userName, char *apiKey)
+/* Return a signature over userName and apiKey (empty string for a revoke), made with the
+ * login.cookieSalt shared secret that is already required to be identical across all of a
+ * site's geo mirrors (it is what makes the login cookie itself verifiable on every mirror).
+ * A peer mirror recomputes this to check that a hubSpaceSetApiKey/revoke request genuinely
+ * came from another UCSC mirror acting for this user, not from an outside caller. */
+{
+char *salt = cfgOption("login.cookieSalt");
+if (isEmpty(salt))
+    errAbort("hubSpaceApiKeySyncSig: login.cookieSalt must be set to sync api keys across mirrors");
+char buf[1024];
+safef(buf, sizeof buf, "%s-%s-%s", salt, userName, apiKey ? apiKey : "");
+return md5HexForString(buf);
+}