DaanHoogland commented on code in PR #12124: URL: https://github.com/apache/cloudstack/pull/12124#discussion_r2664086469
########## plugins/storage/object/ECS/src/main/java/org/apache/cloudstack/storage/datastore/driver/EcsMgmtTokenManager.java: ########## @@ -0,0 +1,161 @@ +/* + * 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.cloudstack.storage.datastore.driver; + +import java.nio.charset.StandardCharsets; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +import org.apache.http.auth.UsernamePasswordCredentials; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpGet; +import org.apache.http.impl.auth.BasicScheme; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.util.EntityUtils; + +import com.cloud.utils.exception.CloudRuntimeException; + +public class EcsMgmtTokenManager { + private static final long DEFAULT_TOKEN_MAX_AGE_SEC = 300; + private static final long EXPIRY_SKEW_SEC = 30; + + private static final ConcurrentHashMap<TokenKey, TokenEntry> TOKEN_CACHE = new ConcurrentHashMap<>(); + private static final ConcurrentHashMap<TokenKey, Object> TOKEN_LOCKS = new ConcurrentHashMap<>(); + + static final class EcsUnauthorizedException extends RuntimeException { + EcsUnauthorizedException(final String msg) { super(msg); } + } + + @FunctionalInterface + public interface WithToken<T> { T run(String token) throws Exception; } + + private static final class TokenKey { + final String mgmtUrl; + final String user; + TokenKey(final String mgmtUrl, final String user) { + this.mgmtUrl = mgmtUrl; + this.user = user; + } + @Override public boolean equals(final Object o) { + if (this == o) return true; + if (!(o instanceof TokenKey)) return false; + final TokenKey k = (TokenKey) o; + return Objects.equals(mgmtUrl, k.mgmtUrl) && Objects.equals(user, k.user); + } + @Override public int hashCode() { return Objects.hash(mgmtUrl, user); } + } + + private static final class TokenEntry { + final String token; + final long expiresAtMs; + TokenEntry(final String token, final long expiresAtMs) { + this.token = token; + this.expiresAtMs = expiresAtMs; + } + boolean validNow() { + return token != null && !token.isBlank() && System.currentTimeMillis() < expiresAtMs; + } + } + + public <T> T callWithRetry401(final EcsObjectStoreDriverImpl.EcsCfg cfg, + final WithToken<T> op, + final HttpClientFactory httpFactory) throws Exception { + try { + return op.run(getAuthToken(cfg, httpFactory)); + } catch (EcsUnauthorizedException u) { + invalidate(cfg); + return op.run(getAuthToken(cfg, httpFactory)); + } + } + + public void invalidate(final EcsObjectStoreDriverImpl.EcsCfg cfg) { + TOKEN_CACHE.remove(new TokenKey(trimTail(cfg.mgmtUrl), cfg.saUser)); + } + + public String getAuthToken(final EcsObjectStoreDriverImpl.EcsCfg cfg, + final HttpClientFactory httpFactory) { + final String mu = trimTail(cfg.mgmtUrl); + final TokenKey key = new TokenKey(mu, cfg.saUser); + + final TokenEntry cached = TOKEN_CACHE.get(key); + if (cached != null && cached.validNow()) return cached.token; + + final Object lock = TOKEN_LOCKS.computeIfAbsent(key, k -> new Object()); + synchronized (lock) { + final TokenEntry cached2 = TOKEN_CACHE.get(key); + if (cached2 != null && cached2.validNow()) return cached2.token; + + final TokenEntry fresh = loginAndGetTokenFresh(mu, cfg.saUser, cfg.saPass, cfg.insecure, httpFactory); + TOKEN_CACHE.put(key, fresh); + return fresh.token; + } + } + + private TokenEntry loginAndGetTokenFresh(final String mgmtUrl, Review Comment: this method contains three levels of try blocks. the second level has no catch or final blocks. @mhkadhum , are you sure about this? Iād try to dissect it in smaller methods (no š !) ########## plugins/storage/object/ECS/src/main/java/org/apache/cloudstack/storage/datastore/driver/EcsObjectStoreDriverImpl.java: ########## Review Comment: He @mhkadhum , this file still contains a few very large methods and also methods that contain nested try blocks. Will you address those? As discussed earlier, this will remain your responsibility so I am not demanding any change but I highly recommend you address these conditions of the code. -- 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]
