adutra commented on code in PR #18754:
URL: https://github.com/apache/iceberg/pull/18754#discussion_r4511417903


##########
core/src/main/java/org/apache/iceberg/rest/auth/FileTokenSource.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.rest.auth;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.time.Duration;
+import java.util.concurrent.atomic.AtomicReference;
+import java.util.function.LongSupplier;
+import org.apache.iceberg.relocated.com.google.common.base.MoreObjects;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * A {@link BearerTokenSource} that reads the token from a local file, and 
re-reads it once the
+ * token is older than the configured reload interval.
+ *
+ * <p>No background thread is used: reloads happen lazily, on the calling 
thread, when the token is
+ * requested. Only one thread reloads at a time; concurrent callers get the 
current token without
+ * waiting.
+ *
+ * <p>If a reload fails, the last valid token is kept, and the next reload is 
attempted after a full
+ * reload interval. As a consequence, deleting the token file does not revoke 
the token.
+ */
+final class FileTokenSource implements BearerTokenSource {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(FileTokenSource.class);
+
+  /**
+   * Maximum token file size. Large enough for JWTs signed with the largest 
post-quantum signature
+   * schemes.
+   */
+  static final int MAX_FILE_SIZE = 256 * 1024;
+
+  private final Path path;
+  private final Duration reloadInterval;
+  private final long reloadIntervalNanos;
+  private final LongSupplier nanoTime;
+  private final AtomicReference<State> state;
+
+  /**
+   * An immutable snapshot of the source's mutable state, swapped atomically.
+   *
+   * @param token the last valid token
+   * @param readAtNanos when the token was last read, or when the last reload 
attempt failed
+   * @param reloading whether a thread has claimed the current reload
+   * @param failing whether the last reload attempt failed, used to log on 
transitions only
+   */
+  private record State(String token, long readAtNanos, boolean reloading, 
boolean failing) {}
+
+  FileTokenSource(Path path, Duration reloadInterval) {
+    this(path, reloadInterval, System::nanoTime);
+  }
+
+  FileTokenSource(Path path, Duration reloadInterval, LongSupplier nanoTime) {
+    Preconditions.checkArgument(path != null, "Invalid token file: null");
+    Preconditions.checkArgument(reloadInterval != null, "Invalid reload 
interval: null");
+    this.path = path;
+    this.reloadInterval = reloadInterval;
+    this.reloadIntervalNanos = reloadInterval.toNanos();
+    this.nanoTime = nanoTime;
+    // the initial read must succeed, otherwise the session fails to initialize
+    this.state = new AtomicReference<>(new State(readToken(), 
nanoTime.getAsLong(), false, false));
+  }
+
+  @Override
+  public String token() {
+    State current = state.get();
+    if (current.reloading || isFresh(current)) {
+      return current.token;
+    }

Review Comment:
   This is intentional, following `client-go` behavior: callers never wait for 
a reload. "Fresh" here only means "read less than one reload interval ago", not 
"valid". The held token was valid when read, and producers rotate tokens well 
before they expire (Kubernetes at 80% of the TTL). With the default 1-minute 
interval, it's still valid while one thread re-reads the file.
   
   Returning only fresh tokens would mean making concurrent callers wait for 
the reload, which puts file I/O on every request's path, for no gain.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to