singhpk234 commented on code in PR #18754: URL: https://github.com/apache/iceberg/pull/18754#discussion_r4499502938
########## core/src/main/java/org/apache/iceberg/rest/auth/BearerAuthConfig.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.time.format.DateTimeParseException; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +/** Configuration for the {@link BearerAuthManager}. */ +record BearerAuthConfig( + @Nullable Path tokenFile, @Nullable Duration reloadInterval, @Nullable String token) { + + static final String BEARER_TOKEN = "rest.auth.bearer.token"; + static final String BEARER_TOKEN_FILE = "rest.auth.bearer.token-file"; + static final String BEARER_TOKEN_FILE_RELOAD_INTERVAL = + "rest.auth.bearer.token-file.reload-interval"; + + static final Duration DEFAULT_RELOAD_INTERVAL = Duration.ofMinutes(1); + static final Duration MIN_RELOAD_INTERVAL = Duration.ofSeconds(1); + + BearerAuthConfig { + Preconditions.checkArgument( + tokenFile == null || token == null, + "Invalid bearer configuration: token file and static token are mutually exclusive"); + Preconditions.checkArgument( + tokenFile != null || token != null, + "Invalid bearer configuration: one of token file or static token is required"); + Preconditions.checkArgument( + tokenFile == null || reloadInterval != null, + "Invalid bearer configuration: reload interval is required when using a token file"); Review Comment: its a bit harsh check here, why not have a default for like 1 hour, and give this option to folks who want to override ? ########## 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; + } + + State claimed = new State(current.token, current.readAtNanos, true, current.failing); + if (!state.compareAndSet(current, claimed)) { + // another thread claimed the reload + return current.token; + } + + // only the thread holding the claim gets here, so a plain set() is enough to both publish the + // new state and release the claim; on unexpected errors, the unclaimed state is restored so + // that the next call retries again. + State next = current; + try { + next = reload(current); + } finally { + state.set(next); + } + + return next.token; + } + + private State reload(State current) { + String token; + try { + token = readToken(); + } catch (RuntimeException e) { + if (!current.failing) { + LOG.warn( + "Failed to reload bearer token from {}, will keep using the last valid token", path, e); + } + + // keep the last valid token and wait a full interval before the next attempt + return new State(current.token, nanoTime.getAsLong(), false, true); + } + + if (current.failing) { + LOG.info("Successfully reloaded bearer token from {}", path); + } Review Comment: if the current is failing how can we have the reload success ? ########## core/src/main/java/org/apache/iceberg/rest/auth/BearerAuthConfig.java: ########## @@ -0,0 +1,108 @@ +/* + * 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.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.time.format.DateTimeParseException; +import java.util.Map; +import javax.annotation.Nullable; +import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; + +/** Configuration for the {@link BearerAuthManager}. */ +record BearerAuthConfig( Review Comment: why not a class ? ########## 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: why to return a token when its reloading, we might be prefetching it before expiry but i feel better is to only return the isFresh() true ? ########## docs/docs/rest-catalog.md: ########## @@ -110,17 +110,42 @@ The following properties configure the table cache used for freshness-aware tabl Most deployments require authentication, which is specific to the catalog service. The following catalog properties configure the potential -authentication mechanisms: Basic, OAuth2, SigV4, and Google. +authentication mechanisms: Basic, Bearer, OAuth2, SigV4, and Google. ### REST auth properties | Property | Default | Description | |--------------------------------------|------------------|-------------------------------------------------------------------------------------------------------------------| -| `rest.auth.type` | `none` | Authentication mechanism for REST catalog access. Supported values: `none`, `basic`, `oauth2`, `sigv4`, `google`. | +| `rest.auth.type` | `none` | Authentication mechanism for REST catalog access. Supported values: `none`, `basic`, `bearer`, `oauth2`, `sigv4`, `google`. | | `rest.auth.basic.username` | null | Username for Basic authentication. Required if `rest.auth.type` = `basic`. | | `rest.auth.basic.password` | null | Password for Basic authentication. Required if `rest.auth.type` = `basic`. | | `rest.auth.sigv4.delegate-auth-type` | `oauth2` | Auth type to delegate to after `sigv4` signing. | +### Bearer auth properties +Required and optional properties to include while using `bearer` authentication + +The `bearer` auth manager sets an `Authorization: Bearer <token>` header on every request, as defined in +[RFC 6750](https://www.rfc-editor.org/rfc/rfc6750). It never contacts an authorization server and never refreshes +the token: obtaining and renewing the token is the responsibility of an external process, such as a Kubernetes +[projected service account token](https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/#launch-a-pod-using-service-account-token-projection), +a sidecar like Vault Agent, or a script. + +| Property | Default | Description | +|-----------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| +| `rest.auth.bearer.token-file` | null | Path to a local file containing the bearer token. The file is re-read periodically. Exactly one of `rest.auth.bearer.token-file` or `rest.auth.bearer.token` is required. | +| `rest.auth.bearer.token` | null | A static bearer token. Exactly one of `rest.auth.bearer.token-file` or `rest.auth.bearer.token` is required. | Review Comment: head up, the catalog configs are logged into the spark UI : https://github.com/apache/iceberg/pull/17920/changes please consider adding it to the allowlist -- 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]
