java—是否可以使用azure托管id操作vm?

o7jaxewo  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(343)

是否可以使用azure托管id操作vm?
我使用一个服务主体来编写代码,从我的pc操作一个虚拟机。

/**
     * Main function which runs the actual sample.
     *
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
    public static boolean runSample(Azure azure) {
        final String rgName1 = "rgName";
        final String linuxVMName = "vmName";

        try {

            VirtualMachine virtualMachine = azure.virtualMachines().getByResourceGroup(rgName1, linuxVMName);

            System.out.println("Running Command");
            List<String> commands = new ArrayList<>();
            commands.add("whoami");
            commands.add("touch /tmp/tmp.txt");

            RunCommandInput runParams = new RunCommandInput()
                    .withCommandId("RunShellScript")
                    .withScript(commands);

            RunCommandResult runResult = azure.virtualMachines().runCommand(virtualMachine.resourceGroupName(), virtualMachine.name(), runParams);

            for (InstanceViewStatus resopnse : runResult.value()) {
                 System.out.println("code : " + resopnse.code());
                 System.out.println("status : " + resopnse.displayStatus());
                 System.out.println("message : " + resopnse.message());
            }

            return true;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        } finally {
            System.out.println("final");
        }
        return false;
    }

    /**
     * Main entry point.
     *
     * @param args the parameters
     */
    public static void main(String[] args) {
        try {

            // Authenticate
            String clientId = "XXXXXXXXX";
            String domain = "XXXXXXXXXX";
            String secret = "XXXXXXXXXX";
            //MSICredentials credentials = new MSICredentials();

            AzureTokenCredentials credentials = new ApplicationTokenCredentials(clientId, domain, secret, AzureEnvironment.AZURE);

            Azure azure = Azure
                    .configure()
                    .withLogLevel(LogLevel.NONE)
                    .authenticate(credentials)
                    .withDefaultSubscription();

            // Print selected subscription
            System.out.println("Selected subscription: " + azure.subscriptionId());

            runSample(azure);

        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }

我想修改一些代码以在azure函数上运行。
有没有可能在不使用服务主体的情况下使用azure功能管理id来操作vm?

baubqpgj

baubqpgj1#

根据我的理解,你想用msi来使用azure vm运行命令功能中的azure函数。如果是,请参考以下步骤
在azure函数中启用azure msi

将azure r角色分配给msi。
运行命令需要 Microsoft.Compute/virtualMachines/runCommand/action 许可。虚拟机参与者角色和更高级别具有此权限。

代码。我用这个包裹 com.microsoft.azure:azure:1.38.0 ```
String subscriptionId="";
AppServiceMSICredentials appServiceMsiCredentials = new AppServiceMSICredentials(AzureEnvironment.AZURE);
Azure azure = Azure
.configure()
.withLogLevel(LogLevel.NONE)
.authenticate(appServiceMsiCredentials)
.withSubscription(subscriptionId);
final String rgName1 = "testlinux_group";
final String linuxVMName = "testlinux";
try {

        VirtualMachine virtualMachine = azure.virtualMachines().getByResourceGroup(rgName1, linuxVMName);

        System.out.println("Running Command");
        List<String> commands = new ArrayList<>();

        commands.add("echo 1");

        RunCommandInput runParams = new RunCommandInput()
                .withCommandId("RunShellScript")
                .withScript(commands);

        RunCommandResult runResult = azure.virtualMachines().runCommand(virtualMachine.resourceGroupName(), virtualMachine.name(), runParams);

        for (InstanceViewStatus res : runResult.value()) {
            context.getLogger().info("code : " + res.code());
            context.getLogger().info("status : " + res.displayStatus());
            context.getLogger().info("message : " + res.message());
        }

    } catch (Exception e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    } finally {
        System.out.println("final");
    }
![](https://i.stack.imgur.com/lkGjU.png)

相关问题