Support managed identity for storage resources

This commit is contained in:
Flanker32 2024-07-15 17:06:10 +08:00
Родитель 61be7bcc39
Коммит 0414cd8221
2 изменённых файлов: 42 добавлений и 3 удалений

Просмотреть файл

@ -9,7 +9,7 @@ import com.intellij.openapi.project.Project;
import com.microsoft.azure.toolkit.intellij.common.AzureFormJPanel;
import com.microsoft.azure.toolkit.intellij.connector.Connection;
import com.microsoft.azure.toolkit.intellij.connector.Resource;
import com.microsoft.azure.toolkit.intellij.connector.spring.SpringSupported;
import com.microsoft.azure.toolkit.intellij.connector.spring.SpringManagedIdentitySupported;
import com.microsoft.azure.toolkit.lib.Azure;
import com.microsoft.azure.toolkit.lib.auth.AzureCloud;
import com.microsoft.azure.toolkit.lib.storage.AzureStorageAccount;
@ -28,7 +28,7 @@ import java.util.*;
@Getter
public class StorageAccountResourceDefinition extends BaseStorageAccountResourceDefinition
implements SpringSupported<IStorageAccount> {
implements SpringManagedIdentitySupported<IStorageAccount> {
public static final StorageAccountResourceDefinition INSTANCE = new StorageAccountResourceDefinition();
@ -86,6 +86,25 @@ public class StorageAccountResourceDefinition extends BaseStorageAccountResource
return fields;
}
@Override
public List<Pair<String, String>> getSpringPropertiesForManagedIdentity(String key) {
final List<Pair<String, String>> properties = new ArrayList<>();
final String suffix = Azure.az(AzureCloud.class).get().getStorageEndpointSuffix();
if (StringUtils.containsIgnoreCase(key, "blob")) {
properties.add(Pair.of("spring.cloud.azure.storage.blob.account-name", String.format("${%s_ACCOUNT_NAME}", Connection.ENV_PREFIX)));
properties.add(Pair.of("spring.cloud.azure.storage.blob.endpoint", String.format("https://${%s_ACCOUNT_NAME}.blob%s", Connection.ENV_PREFIX, suffix)));
} else if (StringUtils.containsIgnoreCase(key, "share")) {
properties.add(Pair.of("spring.cloud.azure.storage.fileshare.account-name", String.format("${%s_ACCOUNT_NAME}", Connection.ENV_PREFIX)));
properties.add(Pair.of("spring.cloud.azure.storage.fileshare.endpoint", String.format("https://${%s_ACCOUNT_NAME}.file%s", Connection.ENV_PREFIX, suffix)));
} else {
properties.add(Pair.of("spring.cloud.azure.storage.fileshare.account-name", String.format("${%s_ACCOUNT_NAME}", Connection.ENV_PREFIX)));
properties.add(Pair.of("spring.cloud.azure.storage.fileshare.endpoint", String.format("https://${%s_ACCOUNT_NAME}.file%s", Connection.ENV_PREFIX, suffix)));
properties.add(Pair.of("spring.cloud.azure.storage.blob.account-name", String.format("${%s_ACCOUNT_NAME}", Connection.ENV_PREFIX)));
properties.add(Pair.of("spring.cloud.azure.storage.blob.endpoint", String.format("https://${%s_ACCOUNT_NAME}.blob%s", Connection.ENV_PREFIX, suffix)));
}
return properties;
}
@Data
@AllArgsConstructor
public static class TempData {

Просмотреть файл

@ -5,12 +5,14 @@
package com.microsoft.azure.toolkit.intellij.storage.connection;
import com.azure.resourcemanager.authorization.models.BuiltInRole;
import com.azure.resourcemanager.resources.fluentcore.arm.ResourceId;
import com.intellij.openapi.project.Project;
import com.microsoft.azure.toolkit.ide.common.icon.AzureIcons;
import com.microsoft.azure.toolkit.intellij.common.auth.IntelliJSecureStore;
import com.microsoft.azure.toolkit.intellij.connector.AzureServiceResource;
import com.microsoft.azure.toolkit.intellij.connector.Connection;
import com.microsoft.azure.toolkit.intellij.connector.IManagedIdentitySupported;
import com.microsoft.azure.toolkit.intellij.connector.Resource;
import com.microsoft.azure.toolkit.intellij.connector.function.FunctionSupported;
import com.microsoft.azure.toolkit.lib.Azure;
@ -29,7 +31,7 @@ import static com.microsoft.azure.toolkit.lib.common.model.AbstractConnectionStr
@Getter
public abstract class BaseStorageAccountResourceDefinition extends AzureServiceResource.Definition<IStorageAccount>
implements FunctionSupported<IStorageAccount> {
implements FunctionSupported<IStorageAccount>, IManagedIdentitySupported<IStorageAccount> {
public static final int METHOD_AZURE = 0;
public static final int METHOD_AZURITE = 1;
public static final int METHOD_STRING = 2;
@ -94,4 +96,22 @@ public abstract class BaseStorageAccountResourceDefinition extends AzureServiceR
public String getResourceConnectionString(@Nonnull IStorageAccount resource) {
return resource instanceof AzuriteStorageAccount ? LOCAL_STORAGE_CONNECTION_STRING : resource.getConnectionString();
}
@Override
public Map<String, String> initIdentityEnv(AzureServiceResource<IStorageAccount> accountDef, Project project) {
final HashMap<String, String> env = new HashMap<>();
final IStorageAccount account = accountDef.getData();
if (Objects.nonNull(account)) {
env.put(ACCOUNT_NAME_KEY, account.getName());
}
return env;
}
@Nullable
@Override
public Map<String, BuiltInRole> getBuiltInRoles() {
return Map.of("ba92f5b4-2d11-453d-a403-e96b0029c9fe", BuiltInRole.STORAGE_BLOB_DATA_CONTRIBUTOR,
"974c5e8b-45b9-4653-ba55-5f855dd0fb88", BuiltInRole.STORAGE_QUEUE_DATA_CONTRIBUTOR,
"0c867c2a-1d8c-454a-a3db-ab2ea1bdc8bb", BuiltInRole.STORAGE_FILE_DATA_SMB_SHARE_CONTRIBUTOR);
}
}