How-To: Add EZSSH Access to Endpoints With Pulumi

Learn how to deploy Azure VMs that trust your EZSSH SSH Certificate Authority with Pulumi and cloud-init, so zero trust SSH access is part of your infrastructure as code.

Overview - How to Add SSH Certificate Authentication to Your Infrastructure as Code

The best way to deploy cloud resources is with infrastructure as code. EZSSH uses SSH certificates, a native OpenSSH feature, so no custom code has to run on your endpoints. Once an endpoint has your EZSSH policy’s CA in its TrustedUserCAKeys, it works with EZSSH.

This guide shows you how to do this by deploying VMs to Azure with Pulumi and the cloud-init file of your EZSSH policy. You might also be interested in:

Prerequisites for Deploying EZSSH Endpoints With Pulumi

  1. You have created at least one EZSSH policy.
  2. You have installed Pulumi for the C# runtime.
  3. You have cloned or downloaded the EZSSH Pulumi sample.

Step-by-Step Guide - How to Deploy EZSSH Endpoints With Pulumi

Step 1: How to Download the cloud-init File From EZSSH

Every EZSSH policy has its own SSH Certificate Authority (CA). The setup script for a policy adds that CA, and the policy’s Linux principals, to your endpoint.

  1. Go to the EZSSH portal.

  2. From the left-hand menu, select Hybrid Policies or Azure Policies, depending on the type of policy you want to set up.

  3. Expand the policy you want to set up. In this example, we expand “AWS EastUS Policy”.

    EZSSH Agentless JIT Access Hybrid Policies page with expand button highlighted on a policy
  4. Click Get Script at the top of the policy.

    EZSSH Agentless JIT Access expanded hybrid policy with Get Script button highlighted

Once the Setup Script dialog is open, download the cloud-init file:

  1. Stay on the Cloud Init tab.

  2. Under Select your Linux Distro, select the distribution your endpoints run (CentOS, Debian, RedHat, or Ubuntu).

    EZSSH Agentless JIT Access Setup Script dialog with Linux distro selection dropdown highlighted
  3. Click Download.

    EZSSH Agentless JIT Access Setup Script dialog with Download button highlighted
  4. Your browser saves the cloud-init file to your downloads folder as <policy name>.yaml.

Step 2: How to Deploy the Pulumi Sample to Azure

  1. Open the folder of the EZSSH Pulumi Azure Sample in Visual Studio Code.

    VS Code editor with EZSSH Pulumi Azure Sample project open showing MyStack.cs
  2. Edit Pulumi.dev.yaml to select the location where you want to deploy your VMs.

  3. Open MyStack.cs. This file has all your infrastructure as code.

  4. Change the for loop to the number of VMs you want to create.

  5. Change the CustomData line to match the path and name of the cloud-init file you downloaded in step 1.

    VS Code showing MyStack.cs with CustomData cloud init file path line highlighted
  6. Open a terminal inside the folder and run:

    pulumi up
    
  7. The first time, Pulumi asks you to sign in. If you do not have a Pulumi account, you can create one for free.

  8. When Pulumi asks you to create a stack, create one called dev.

  9. Once the project builds, Pulumi shows the changes it will make to your subscription.

    VS Code terminal showing pulumi up preview of Azure resources to create with confirmation prompt
  10. Verify the changes, move the arrow to yes, and press Enter. Pulumi deploys the resources to Azure.

Step 3: How to Add the New VMs to Your EZSSH Policy

The new VMs already trust your policy’s CA. The last step is to make sure they are listed as endpoints of the policy.

How to Refresh the Endpoints of an Azure Policy

  1. In the EZSSH portal, navigate to Azure Policies and edit your policy.

  2. Click Refresh Endpoints.

    EZSSH Agentless JIT Access Azure Policy page with Refresh Endpoints button highlighted
  3. Click Save Changes.

How to Add the New Endpoints to a Hybrid Policy

  1. In the EZSSH portal, navigate to Hybrid Policies and edit your policy.

  2. Under Policy Endpoints, enter the IP address or FQDN of each new VM and click Add Endpoint.

    EZSSH Agentless JIT Access Hybrid Policy showing Policy Endpoints section with Add Endpoint button highlighted
  3. Click Save Changes.

    EZSSH Agentless JIT Access hybrid policy edit page showing Save Changes button after adding new endpoints

Done! Your new VMs are ready to be used with EZSSH.

How Does the EZSSH Pulumi Sample Work?

This section goes through the Pulumi deployment to help you create your own.

  1. It creates a resource group named SampleVMs. Pulumi adds some random characters at the end of the name to make it unique.

    var resourceGroup = new Azure.Core.ResourceGroup("SampleVMs");
    
  2. It creates a network security group named exampleNetworkSecurityGroup in the new resource group, in the same location.

    var exampleNetworkSecurityGroup = new Azure.Network.NetworkSecurityGroup("exampleNetworkSecurityGroup", new Azure.Network.NetworkSecurityGroupArgs
    {
        Location = resourceGroup.Location,
        ResourceGroupName = resourceGroup.Name,
    });
    
  3. It creates an inbound rule that accepts any connection to port 22.

    var exampleNetworkSecurityRuleInbound = new Azure.Network.NetworkSecurityRule("exampleNetworkSecurityRule", new Azure.Network.NetworkSecurityRuleArgs
    {
        Priority = 100,
        Direction = "Inbound",
        Access = "Allow",
        Protocol = "*",
        SourcePortRange = "*",
        DestinationPortRange = "22",
        SourceAddressPrefix = "*",
        DestinationAddressPrefix = "*",
        ResourceGroupName = resourceGroup.Name,
        NetworkSecurityGroupName = exampleNetworkSecurityGroup.Name,
    });
    
  4. It creates a virtual network for the VMs.

    var exampleVirtualNetwork = new Azure.Network.VirtualNetwork("testVMSVirtualNetwork", new Azure.Network.VirtualNetworkArgs
    {
        AddressSpaces =
        {
            "10.0.0.0/16",
        },
        Location = resourceGroup.Location,
        ResourceGroupName = resourceGroup.Name,
        Subnets =
        {
            new Azure.Network.Inputs.VirtualNetworkSubnetArgs
            {
                Name = "subnet3",
                AddressPrefix = "10.0.3.0/24",
                SecurityGroup = exampleNetworkSecurityGroup.Id,
            },
        }
    });
    
  5. It starts a for loop that creates the VMs, so you can create multiple VMs at once. The rest of the code is inside this loop and creates one of each resource per VM.

  6. Inside the loop, it creates a public IP address for the VM. In this sample the IP address is dynamic, so it changes each time the VM is allocated.

    var publicIP = new Azure.Network.PublicIp("publicIP-"+ i, new Azure.Network.PublicIpArgs
    {
        ResourceGroupName = resourceGroup.Name,
        AllocationMethod = "Dynamic"
    });
    
  7. It creates a network interface and attaches the public IP address to it.

    var exampleNetworkInterface = new Azure.Network.NetworkInterface("exampleNetworkInterface"+ i, new Azure.Network.NetworkInterfaceArgs
    {
        Location = resourceGroup.Location,
        ResourceGroupName = resourceGroup.Name,
        IpConfigurations =
        {
            new Azure.Network.Inputs.NetworkInterfaceIpConfigurationArgs
            {
                Name = "internal",
                SubnetId = exampleVirtualNetwork.Subnets.First().Apply(x => x.Id),
                PrivateIpAddressAllocation = "Dynamic",
                PublicIpAddressId = publicIP.Id
            },
        },
    });
    
  8. It creates the VM with the following properties:

    • Name: TestVM plus the number of the VM in the loop.
    • Location: The same location as the resource group.
    • Network: The network interface created in the previous step.
    • Size: Standard_B1s, the cheapest size.
    • Image: Ubuntu Server 18.04 LTS. We recommend changing this to a currently supported Ubuntu LTS release in your own deployment.
    • Disk: A new disk for the operating system.
    • Admin user: Azure requires an admin user, so the sample sets one up with a randomly generated password that is not kept.
    • CustomData: The cloud-init file you downloaded from EZSSH. This is the line that makes the VM trust your EZSSH policy.
    var mainVirtualMachine = new Azure.Compute.VirtualMachine("mainVirtualMachine" + i, new Azure.Compute.VirtualMachineArgs
    {
        Name = "TestVM" + i,
        Location = resourceGroup.Location,
        ResourceGroupName = resourceGroup.Name,
        NetworkInterfaceIds =
        {
            exampleNetworkInterface.Id,
        },
        VmSize = "Standard_B1s",
        StorageImageReference = new Azure.Compute.Inputs.VirtualMachineStorageImageReferenceArgs
        {
            Publisher = "Canonical",
            Offer = "UbuntuServer",
            Sku = "18.04-LTS",
            Version = "latest",
        },
        StorageOsDisk = new Azure.Compute.Inputs.VirtualMachineStorageOsDiskArgs
        {
            Name = "myosdisk" + i,
            Caching = "ReadWrite",
            CreateOption = "FromImage",
            ManagedDiskType = "Standard_LRS",
        },
    
        OsProfileLinuxConfig = new Azure.Compute.Inputs.VirtualMachineOsProfileLinuxConfigArgs
        {
            DisablePasswordAuthentication = false,
        },
        OsProfile = new Azure.Compute.Inputs.VirtualMachineOsProfileArgs
        {
            AdminUsername = "randomAdminUser",
            AdminPassword = Password.Generate(32, 12),
            ComputerName = "testpc" + i,
            CustomData = File.ReadAllText(Environment.GetFolderPath(
            Environment.SpecialFolder.UserProfile) + "\\Downloads\\AWS EastUS Policy.yaml")
        }
    });
    

Next Steps - How to Connect to Your New Endpoints

Now that your VMs trust your EZSSH policy, you can connect to them with the EZSSH client.

🚀 Set Up the EZSSH Client