morrySnow commented on code in PR #63068:
URL: https://github.com/apache/doris/pull/63068#discussion_r3271090302


##########
fe/fe-core/src/main/java/org/apache/doris/datasource/DelegatedCredential.java:
##########
@@ -0,0 +1,86 @@
+// 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.doris.datasource;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+
+import java.util.Objects;
+import java.util.OptionalLong;
+
+public class DelegatedCredential {
+    private final Type type;
+    private final String token;
+    private final Long expiresAtMillis;
+
+    public DelegatedCredential(Type type, String token) {
+        this(type, token, OptionalLong.empty());
+    }
+
+    public DelegatedCredential(Type type, String token, OptionalLong 
expiresAtMillis) {
+        this.type = Objects.requireNonNull(type, "type is required");
+        this.token = Objects.requireNonNull(token, "token is required");
+        Objects.requireNonNull(expiresAtMillis, "expiresAtMillis is required");
+        this.expiresAtMillis = expiresAtMillis.isPresent() ? 
expiresAtMillis.getAsLong() : null;
+    }
+
+    public Type getType() {
+        return type;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public String getIcebergCredentialKey() {
+        return type.getIcebergCredentialKey();
+    }
+
+    public OptionalLong getExpiresAtMillis() {
+        return expiresAtMillis == null ? OptionalLong.empty() : 
OptionalLong.of(expiresAtMillis);
+    }
+
+    public boolean isExpired(long currentTimeMillis) {
+        return expiresAtMillis != null && currentTimeMillis >= expiresAtMillis;
+    }
+
+    @Override
+    public String toString() {
+        return "DelegatedCredential{"
+                + "type=" + type
+                + ", token=<redacted>"
+                + ", expiresAtMillis=" + expiresAtMillis
+                + '}';
+    }
+
+    public enum Type {
+        ACCESS_TOKEN(OAuth2Properties.TOKEN),
+        ID_TOKEN(OAuth2Properties.ID_TOKEN_TYPE),
+        JWT(OAuth2Properties.JWT_TOKEN_TYPE),
+        SAML(OAuth2Properties.SAML2_TOKEN_TYPE);
+
+        private final String icebergCredentialKey;
+
+        Type(String icebergCredentialKey) {
+            this.icebergCredentialKey = icebergCredentialKey;

Review Comment:
   why type has key?



##########
fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java:
##########
@@ -214,11 +217,24 @@ protected void resolveWorkloadGroupName() {
         ctx.setWorkloadGroupName(groupName);
     }
 
+    protected boolean rejectExpiredDelegatedCredential(String originStmt) {
+        if 
(!ctx.getSessionContext().isDelegatedCredentialExpired(System.currentTimeMillis()))
 {
+            return false;
+        }
+        ctx.getState().setError(ErrorCode.ERR_ACCESS_DENIED_ERROR,
+                "Authentication token has expired; reconnect to refresh 
credentials");
+        auditAfterExec(originStmt, null, null, true);
+        return true;
+    }
+
     // only throw an exception when there is a problem interacting with the 
requesting client
     protected void handleQuery(String originStmt) throws ConnectionException {
         // Before executing the query, the queryId should be set to empty.
         // Otherwise, if SQL parsing fails, the audit log will record the 
queryId from the previous query.
         ctx.resetQueryId();
+        if (rejectExpiredDelegatedCredential(originStmt)) {

Review Comment:
   why check it here? if the credential is expired, we could not execute any 
query?



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergSessionCatalogAdapter.java:
##########
@@ -0,0 +1,159 @@
+// 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.doris.datasource.iceberg;
+
+import org.apache.doris.datasource.DelegatedCredential;
+import org.apache.doris.datasource.SessionContext;
+import 
org.apache.doris.datasource.property.metastore.IcebergRestProperties.DelegatedTokenMode;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.catalog.BaseSessionCatalog;
+import org.apache.iceberg.catalog.BaseViewSessionCatalog;
+import org.apache.iceberg.catalog.Catalog;
+import org.apache.iceberg.catalog.SupportsNamespaces;
+import org.apache.iceberg.catalog.ViewCatalog;
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+import java.util.Optional;
+
+class IcebergSessionCatalogAdapter {

Review Comment:
   add javadoc style comment to explain what is this class use for



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/DelegatedCredential.java:
##########
@@ -0,0 +1,86 @@
+// 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.doris.datasource;
+
+import org.apache.iceberg.rest.auth.OAuth2Properties;
+
+import java.util.Objects;
+import java.util.OptionalLong;
+
+public class DelegatedCredential {
+    private final Type type;
+    private final String token;
+    private final Long expiresAtMillis;
+
+    public DelegatedCredential(Type type, String token) {
+        this(type, token, OptionalLong.empty());
+    }
+
+    public DelegatedCredential(Type type, String token, OptionalLong 
expiresAtMillis) {
+        this.type = Objects.requireNonNull(type, "type is required");
+        this.token = Objects.requireNonNull(token, "token is required");
+        Objects.requireNonNull(expiresAtMillis, "expiresAtMillis is required");
+        this.expiresAtMillis = expiresAtMillis.isPresent() ? 
expiresAtMillis.getAsLong() : null;
+    }
+
+    public Type getType() {
+        return type;
+    }
+
+    public String getToken() {
+        return token;
+    }
+
+    public String getIcebergCredentialKey() {
+        return type.getIcebergCredentialKey();
+    }
+
+    public OptionalLong getExpiresAtMillis() {
+        return expiresAtMillis == null ? OptionalLong.empty() : 
OptionalLong.of(expiresAtMillis);
+    }
+
+    public boolean isExpired(long currentTimeMillis) {
+        return expiresAtMillis != null && currentTimeMillis >= expiresAtMillis;
+    }
+
+    @Override
+    public String toString() {
+        return "DelegatedCredential{"
+                + "type=" + type
+                + ", token=<redacted>"
+                + ", expiresAtMillis=" + expiresAtMillis
+                + '}';
+    }
+
+    public enum Type {
+        ACCESS_TOKEN(OAuth2Properties.TOKEN),
+        ID_TOKEN(OAuth2Properties.ID_TOKEN_TYPE),
+        JWT(OAuth2Properties.JWT_TOKEN_TYPE),
+        SAML(OAuth2Properties.SAML2_TOKEN_TYPE);
+
+        private final String icebergCredentialKey;

Review Comment:
   This is a general class for the datasource. However, the name of the key 
contains "iceberg", which is quite strange.



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/ExternalCatalog.java:
##########
@@ -349,6 +349,10 @@ public final List<String> listTableNames(SessionContext 
ctx, String dbName) {
      */
     protected abstract List<String> listTableNamesFromRemote(SessionContext 
ctx, String dbName);
 
+    protected boolean shouldBypassTableNameCache(SessionContext ctx) {

Review Comment:
   add javadoc style comment to explain this interface



##########
fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java:
##########
@@ -558,6 +574,9 @@ protected void handleQueryException(Throwable throwable, 
String origStmt,
     @SuppressWarnings("rawtypes")
     protected void handleFieldList(String tableName) throws 
ConnectionException {
         // Already get command code.
+        if (rejectExpiredDelegatedCredential(tableName)) {

Review Comment:
   same as handleQuery?



##########
fe/fe-core/src/main/java/org/apache/doris/qe/ConnectProcessor.java:
##########
@@ -819,6 +839,22 @@ public TMasterOpResult proxyExecute(TMasterOpRequest 
request) throws TException
         return result;
     }
 
+    static void restoreForwardedSessionContext(ConnectContext context, 
TMasterOpRequest request) {
+        if (!request.isSetDelegatedCredentialToken()) {
+            return;
+        }
+        OptionalLong expiresAtMillis = 
request.isSetDelegatedCredentialExpiresAtMillis()
+                ? 
OptionalLong.of(request.getDelegatedCredentialExpiresAtMillis())
+                : OptionalLong.empty();
+        String sessionId = request.isSetDelegatedCredentialSessionId()
+                ? request.getDelegatedCredentialSessionId()
+                : UUID.randomUUID().toString();

Review Comment:
   If the session id doesn't need to be consistent between FEs, then what is 
the necessity of its existence? Under what circumstances would there be no 
session id in the request?



##########
fe/fe-core/src/main/java/org/apache/doris/qe/MysqlConnectProcessor.java:
##########
@@ -218,6 +218,9 @@ private void handleExecute() {
                     "msg: Not supported such prepared statement");
             return;
         }
+        if 
(rejectExpiredDelegatedCredential(preparedStatementContext.command.getOriginalStmt().originStmt))
 {
+            return;
+        }

Review Comment:
   same as handleQuery



##########
fe/fe-core/src/main/java/org/apache/doris/datasource/iceberg/IcebergMetadataOps.java:
##########
@@ -1169,6 +1219,65 @@ public List<String> listViewNames(String db) {
         }
     }
 
+    private Catalog catalog(SessionContext ctx) {
+        if (useSessionCatalog(ctx)) {
+            return sessionCatalogAdapter.delegatedCatalog(ctx);
+        }
+        return catalog;
+    }
+
+    private SupportsNamespaces namespaces(SessionContext ctx) {
+        if (useSessionCatalog(ctx)) {
+            return sessionCatalogAdapter.delegatedNamespaces(ctx);
+        }
+        return nsCatalog;
+    }
+
+    private Optional<ViewCatalog> viewCatalog(SessionContext ctx) {
+        if (!isViewCatalogEnabled()) {
+            return Optional.empty();
+        }
+        if (useSessionCatalog(ctx)) {
+            return sessionCatalogAdapter.delegatedViewCatalog(ctx);
+        }
+        return Optional.of((ViewCatalog) catalog);
+    }
+
+    private boolean useSessionCatalog(SessionContext ctx) {
+        return ctx != null && ctx.hasDelegatedCredential() && 
isIcebergRestUserSessionEnabled();

Review Comment:
   What is the relationship between delegate credential and session catalog?



-- 
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