nfsantos commented on code in PR #1545:
URL: https://github.com/apache/jackrabbit-oak/pull/1545#discussion_r1687488286


##########
oak-segment-azure/src/main/java/org/apache/jackrabbit/oak/segment/azure/AzureStorageCredentialManager.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.jackrabbit.oak.segment.azure;
+
+import com.azure.core.credential.AccessToken;
+import com.azure.core.credential.TokenRequestContext;
+import com.azure.identity.ClientSecretCredential;
+import com.azure.identity.ClientSecretCredentialBuilder;
+import com.microsoft.azure.storage.StorageCredentials;
+import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
+import com.microsoft.azure.storage.StorageCredentialsToken;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
+import org.apache.jackrabbit.oak.segment.azure.util.Environment;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Objects;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_CLIENT_ID;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_CLIENT_SECRET;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_SECRET_KEY;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_TENANT_ID;
+
+public class AzureStorageCredentialManager implements Closeable {
+    private static final Logger log = 
LoggerFactory.getLogger(AzureStorageCredentialManager.class);
+    private static final String AZURE_DEFAULT_SCOPE = 
"https://storage.azure.com/.default";;
+    private static final long TOKEN_REFRESHER_INITIAL_DELAY = 45L;
+    private static final long TOKEN_REFRESHER_DELAY = 1L;
+    private ClientSecretCredential clientSecretCredential;
+    private AccessToken accessToken;
+    private StorageCredentialsToken storageCredentialsToken;
+    private final ScheduledExecutorService executorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public StorageCredentials getStorageCredentialsFromEnvironment(@NotNull 
String accountName, @NotNull Environment environment) {
+        final String clientId = environment.getVariable(AZURE_CLIENT_ID);
+        final String clientSecret = 
environment.getVariable(AZURE_CLIENT_SECRET);
+        final String tenantId = environment.getVariable(AZURE_TENANT_ID);
+
+        if (StringUtils.isNoneBlank(clientId, clientSecret, tenantId)) {
+            try {
+                return 
getStorageCredentialAccessTokenFromServicePrincipals(accountName, clientId, 
clientSecret, tenantId);
+            } catch (IllegalArgumentException | 
StringIndexOutOfBoundsException e) {
+                throw new IllegalArgumentException(
+                        "Could not connect to the Azure Storage. Please verify 
if AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID environment 
variables are correctly set!");

Review Comment:
   You should log the original exception or else propagate it, so that the 
cause of the problem is displayed on the logs.



##########
oak-segment-azure/src/main/java/org/apache/jackrabbit/oak/segment/azure/AzureStorageCredentialManager.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.jackrabbit.oak.segment.azure;
+
+import com.azure.core.credential.AccessToken;
+import com.azure.core.credential.TokenRequestContext;
+import com.azure.identity.ClientSecretCredential;
+import com.azure.identity.ClientSecretCredentialBuilder;
+import com.microsoft.azure.storage.StorageCredentials;
+import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
+import com.microsoft.azure.storage.StorageCredentialsToken;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
+import org.apache.jackrabbit.oak.segment.azure.util.Environment;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Objects;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_CLIENT_ID;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_CLIENT_SECRET;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_SECRET_KEY;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_TENANT_ID;
+
+public class AzureStorageCredentialManager implements Closeable {
+    private static final Logger log = 
LoggerFactory.getLogger(AzureStorageCredentialManager.class);
+    private static final String AZURE_DEFAULT_SCOPE = 
"https://storage.azure.com/.default";;
+    private static final long TOKEN_REFRESHER_INITIAL_DELAY = 45L;
+    private static final long TOKEN_REFRESHER_DELAY = 1L;
+    private ClientSecretCredential clientSecretCredential;
+    private AccessToken accessToken;
+    private StorageCredentialsToken storageCredentialsToken;
+    private final ScheduledExecutorService executorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public StorageCredentials getStorageCredentialsFromEnvironment(@NotNull 
String accountName, @NotNull Environment environment) {
+        final String clientId = environment.getVariable(AZURE_CLIENT_ID);
+        final String clientSecret = 
environment.getVariable(AZURE_CLIENT_SECRET);
+        final String tenantId = environment.getVariable(AZURE_TENANT_ID);
+
+        if (StringUtils.isNoneBlank(clientId, clientSecret, tenantId)) {
+            try {
+                return 
getStorageCredentialAccessTokenFromServicePrincipals(accountName, clientId, 
clientSecret, tenantId);
+            } catch (IllegalArgumentException | 
StringIndexOutOfBoundsException e) {
+                throw new IllegalArgumentException(
+                        "Could not connect to the Azure Storage. Please verify 
if AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID environment 
variables are correctly set!");
+            }
+        }
+
+        log.warn("AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID 
environment variables empty or missing. Switching to authentication with 
AZURE_SECRET_KEY.");
+
+        String key = environment.getVariable(AZURE_SECRET_KEY);
+        try {
+            return new StorageCredentialsAccountAndKey(accountName, key);
+        } catch (IllegalArgumentException | StringIndexOutOfBoundsException e) 
{
+            throw new IllegalArgumentException(
+                    "Could not connect to the Azure Storage. Please verify if 
AZURE_SECRET_KEY environment variable is correctly set!");

Review Comment:
   Same thing here.



##########
oak-segment-azure/src/main/java/org/apache/jackrabbit/oak/segment/azure/AzureStorageCredentialManager.java:
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.jackrabbit.oak.segment.azure;
+
+import com.azure.core.credential.AccessToken;
+import com.azure.core.credential.TokenRequestContext;
+import com.azure.identity.ClientSecretCredential;
+import com.azure.identity.ClientSecretCredentialBuilder;
+import com.microsoft.azure.storage.StorageCredentials;
+import com.microsoft.azure.storage.StorageCredentialsAccountAndKey;
+import com.microsoft.azure.storage.StorageCredentialsToken;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
+import org.apache.jackrabbit.oak.segment.azure.util.Environment;
+import org.jetbrains.annotations.NotNull;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.Closeable;
+import java.time.LocalDateTime;
+import java.time.OffsetDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Objects;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_CLIENT_ID;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_CLIENT_SECRET;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_SECRET_KEY;
+import static 
org.apache.jackrabbit.oak.segment.azure.AzureUtilities.AZURE_TENANT_ID;
+
+public class AzureStorageCredentialManager implements Closeable {
+    private static final Logger log = 
LoggerFactory.getLogger(AzureStorageCredentialManager.class);
+    private static final String AZURE_DEFAULT_SCOPE = 
"https://storage.azure.com/.default";;
+    private static final long TOKEN_REFRESHER_INITIAL_DELAY = 45L;
+    private static final long TOKEN_REFRESHER_DELAY = 1L;
+    private ClientSecretCredential clientSecretCredential;
+    private AccessToken accessToken;
+    private StorageCredentialsToken storageCredentialsToken;
+    private final ScheduledExecutorService executorService = 
Executors.newSingleThreadScheduledExecutor();
+
+    public StorageCredentials getStorageCredentialsFromEnvironment(@NotNull 
String accountName, @NotNull Environment environment) {
+        final String clientId = environment.getVariable(AZURE_CLIENT_ID);
+        final String clientSecret = 
environment.getVariable(AZURE_CLIENT_SECRET);
+        final String tenantId = environment.getVariable(AZURE_TENANT_ID);
+
+        if (StringUtils.isNoneBlank(clientId, clientSecret, tenantId)) {
+            try {
+                return 
getStorageCredentialAccessTokenFromServicePrincipals(accountName, clientId, 
clientSecret, tenantId);
+            } catch (IllegalArgumentException | 
StringIndexOutOfBoundsException e) {
+                throw new IllegalArgumentException(
+                        "Could not connect to the Azure Storage. Please verify 
if AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID environment 
variables are correctly set!");
+            }
+        }
+
+        log.warn("AZURE_CLIENT_ID, AZURE_CLIENT_SECRET and AZURE_TENANT_ID 
environment variables empty or missing. Switching to authentication with 
AZURE_SECRET_KEY.");
+
+        String key = environment.getVariable(AZURE_SECRET_KEY);
+        try {
+            return new StorageCredentialsAccountAndKey(accountName, key);
+        } catch (IllegalArgumentException | StringIndexOutOfBoundsException e) 
{
+            throw new IllegalArgumentException(
+                    "Could not connect to the Azure Storage. Please verify if 
AZURE_SECRET_KEY environment variable is correctly set!");
+        }
+    }
+
+    public StorageCredentials 
getStorageCredentialAccessTokenFromServicePrincipals(String accountName, String 
clientId, String clientSecret, String tenantId) {
+        boolean isAccessTokenGenerated = false;
+        if (accessToken == null) {
+            clientSecretCredential = new ClientSecretCredentialBuilder()
+                    .clientId(clientId)
+                    .clientSecret(clientSecret)
+                    .tenantId(tenantId)
+                    .build();
+            accessToken = clientSecretCredential.getTokenSync(new 
TokenRequestContext().addScopes(AZURE_DEFAULT_SCOPE));
+            if (accessToken == null || 
StringUtils.isBlank(accessToken.getToken())) {
+                log.error("Access token is null or empty");
+                throw new IllegalArgumentException("Could not connect to azure 
storage, access token is null or empty");

Review Comment:
   I suggest removing the log.error from here, because the exception contains 
the same information. 



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

Reply via email to