@@ -91,6 +91,17 @@ private static Schema withRowId(Schema user) {
9191 return new Schema (fields );
9292 }
9393
94+ /** Whether {@code needle} occurs in {@code haystack}: the never-plaintext check whoami reports. */
95+ static boolean contains (byte [] haystack , byte [] needle ) {
96+ if (haystack == null || needle .length == 0 ) return false ;
97+ outer :
98+ for (int i = 0 ; i + needle .length <= haystack .length ; i ++) {
99+ for (int j = 0 ; j < needle .length ; j ++) if (haystack [i + j ] != needle [j ]) continue outer ;
100+ return true ;
101+ }
102+ return false ;
103+ }
104+
94105 static String hex (byte [] b ) {
95106 return b == null ? null : HexFormat .of ().formatHex (b );
96107 }
@@ -99,6 +110,11 @@ static String token(AttachOptions o) {
99110 return o .getString ("token" );
100111 }
101112
113+ /** The state partition of the attach's token (a digest; the token itself is never stored). */
114+ static String partition (AttachOptions o ) {
115+ return TxFixState .partition (token (o ));
116+ }
117+
102118 /** The transaction a call stages under: its id on a transactional attach, else none. */
103119 static String stagingTx (AttachOptions o , byte [] transactionId ) {
104120 return o .getBoolean ("transactional" , false ) ? hex (transactionId ) : null ;
@@ -179,7 +195,7 @@ static final class Hooks implements TransactionHooks {
179195 public byte [] begin (AttachContext attach ) {
180196 byte [] id = new byte [16 ];
181197 RNG .nextBytes (id );
182- TxFixState .withPartition (token (attach .options ()), p -> {
198+ TxFixState .withPartition (partition (attach .options ()), p -> {
183199 p .event ("begin" , hex (id ), true );
184200 return null ;
185201 });
@@ -189,7 +205,7 @@ public byte[] begin(AttachContext attach) {
189205 @ Override
190206 public void commit (AttachContext attach , byte [] transactionId ) {
191207 String tx = hex (transactionId );
192- String refused = TxFixState .withPartition (token (attach .options ()), p -> {
208+ String refused = TxFixState .withPartition (partition (attach .options ()), p -> {
193209 TreeMap <Integer , TxFixState .Row > staged = p .staged .remove (tx );
194210 if (staged != null ) {
195211 for (TxFixState .Row r : staged .values ()) {
@@ -209,7 +225,7 @@ public void commit(AttachContext attach, byte[] transactionId) {
209225 @ Override
210226 public void rollback (AttachContext attach , byte [] transactionId ) {
211227 String tx = hex (transactionId );
212- TxFixState .withPartition (token (attach .options ()), p -> {
228+ TxFixState .withPartition (partition (attach .options ()), p -> {
213229 p .staged .remove (tx );
214230 p .event ("rollback" , tx , true );
215231 return null ;
@@ -271,18 +287,18 @@ static final class Reset extends RowsFunction {
271287 public TableProducerState createProducer (TableInitParams params ) {
272288 ResetState s = new ResetState ();
273289 s .outputSchema = params .outputSchema ();
274- s .token = token (params .attachOptions ());
290+ s .partition = partition (params .attachOptions ());
275291 return s ;
276292 }
277293 }
278294
279295 /** {@code reset()} producer. */
280296 public static final class ResetState extends OnceState {
281- public String token ;
297+ public String partition ;
282298
283299 @ Override
284300 List <Map <String , Object >> rows () {
285- TxFixState .withPartition (token , p -> {
301+ TxFixState .withPartition (partition , p -> {
286302 p .reset ();
287303 return null ;
288304 });
@@ -296,30 +312,34 @@ static final class WhoAmI extends RowsFunction {
296312 @ Override String name0 () { return "whoami" ; }
297313 @ Override Schema schema () {
298314 return new Schema (List .of (Schemas .nullable ("token" , Schemas .UTF8 ),
299- Schemas .nullable ("transactional" , Schemas .BOOL )));
315+ Schemas .nullable ("transactional" , Schemas .BOOL ),
316+ Schemas .nullable ("token_in_attach_data" , Schemas .BOOL )));
300317 }
301318 @ Override String description () { return "The attach's options as the function sees them" ; }
302319
303320 @ Override
304321 public TableProducerState createProducer (TableInitParams params ) {
305322 WhoAmIState s = new WhoAmIState ();
306323 s .outputSchema = params .outputSchema ();
307- s . token = token ( params . attachOptions ());
308- s .transactional = params .attachOptions (). getBoolean ( "transactional" , false );
324+ // Only the attach id is kept: the token is opened again while producing.
325+ s .attachId = params .attachId ( );
309326 return s ;
310327 }
311328 }
312329
313- /** {@code whoami()} producer. */
330+ /** {@code whoami()} producer: re-opens the options from the attach id; no secret in state . */
314331 public static final class WhoAmIState extends OnceState {
315- public String token ;
316- public boolean transactional ;
332+ public byte [] attachId ;
317333
318334 @ Override
319335 List <Map <String , Object >> rows () {
336+ AttachOptions options = AttachOptions .open (attachId );
337+ String token = token (options );
320338 Map <String , Object > row = new LinkedHashMap <>();
321339 row .put ("token" , token );
322- row .put ("transactional" , transactional );
340+ row .put ("transactional" , options .getBoolean ("transactional" , false ));
341+ row .put ("token_in_attach_data" ,
342+ token != null && contains (attachId , token .getBytes (StandardCharsets .UTF_8 )));
323343 return List .of (row );
324344 }
325345 }
@@ -367,18 +387,18 @@ static final class Events extends RowsFunction {
367387 public TableProducerState createProducer (TableInitParams params ) {
368388 EventsState s = new EventsState ();
369389 s .outputSchema = params .outputSchema ();
370- s .token = token (params .attachOptions ());
390+ s .partition = partition (params .attachOptions ());
371391 return s ;
372392 }
373393 }
374394
375395 /** {@code events()} producer. */
376396 public static final class EventsState extends OnceState {
377- public String token ;
397+ public String partition ;
378398
379399 @ Override
380400 List <Map <String , Object >> rows () {
381- return TxFixState .withPartition (token , p -> {
401+ return TxFixState .withPartition (partition , p -> {
382402 List <Map <String , Object >> out = new ArrayList <>();
383403 for (TxFixState .Event e : p .events ) {
384404 Map <String , Object > row = new LinkedHashMap <>();
@@ -404,20 +424,20 @@ static final class ItemsScan extends RowsFunction {
404424 public TableProducerState createProducer (TableInitParams params ) {
405425 ItemsState s = new ItemsState ();
406426 s .outputSchema = params .outputSchema ();
407- s .token = token (params .attachOptions ());
427+ s .partition = partition (params .attachOptions ());
408428 s .txId = stagingTx (params .attachOptions (), params .transactionId ());
409429 return s ;
410430 }
411431 }
412432
413433 /** {@code items_scan()} producer: committed rows overlaid with the transaction's staged ones. */
414434 public static final class ItemsState extends OnceState {
415- public String token ;
435+ public String partition ;
416436 public String txId ;
417437
418438 @ Override
419439 List <Map <String , Object >> rows () {
420- return TxFixState .withPartition (token , p -> {
440+ return TxFixState .withPartition (partition , p -> {
421441 List <Map <String , Object >> out = new ArrayList <>();
422442 for (TxFixState .Row r : p .view (txId ).values ()) {
423443 Map <String , Object > row = r .asMap ();
@@ -486,7 +506,7 @@ public BindResponse onBind(TableInOutBindParams params) {
486506 @ Override
487507 public TableInOutExchangeState createExchange (TableInOutInitParams params ) {
488508 UpdateState s = new UpdateState ();
489- s .token = token (params .attachOptions ());
509+ s .partition = partition (params .attachOptions ());
490510 s .transactional = params .attachOptions ().getBoolean ("transactional" , false );
491511 s .txId = stagingTx (params .attachOptions (), params .transactionId ());
492512 s .mode = WriteOptions .from (params .arguments ()).resultMode ().wireName ();
@@ -496,7 +516,7 @@ public TableInOutExchangeState createExchange(TableInOutInitParams params) {
496516
497517 /** {@code items_update} exchange: validates every row, then applies or stages them. */
498518 public static final class UpdateState extends TableInOutExchangeState {
499- public String token ;
519+ public String partition ;
500520 public boolean transactional ;
501521 public String txId ;
502522 public String mode ;
@@ -508,7 +528,7 @@ public void onInputBatch(AnnotatedBatch input, OutputCollector out, CallContext
508528 if (rowids == null ) throw new IllegalArgumentException ("txfix: UPDATE input carries no rowid" );
509529 List <Map <String , Object >> before = new ArrayList <>();
510530 List <Map <String , Object >> after = new ArrayList <>();
511- TxFixState .withPartition (token , p -> {
531+ TxFixState .withPartition (partition , p -> {
512532 TreeMap <Integer , TxFixState .Row > current = p .view (txId );
513533 List <TxFixState .Row > updated = new ArrayList <>();
514534 for (int i = 0 ; i < batch .getRowCount (); i ++) {
0 commit comments