How-To: Add EZSSH Access to Endpoints With Pulumi
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:
- Adding EZSSH access to new VMs with cloud-init
- Adding EZSSH access to existing endpoints with a bash script
Prerequisites for Deploying EZSSH Endpoints With Pulumi
- You have created at least one EZSSH policy.
- You have installed Pulumi for the C# runtime.
- 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.
-
Go to the EZSSH portal.
-
From the left-hand menu, select Hybrid Policies or Azure Policies, depending on the type of policy you want to set up.
-
Expand the policy you want to set up. In this example, we expand “AWS EastUS Policy”.
-
Click Get Script at the top of the policy.
Only Need One Access Policy?The Get Script button at the top of the policy sets up the Linux principals of every access policy in it. To set up the Linux principals of a single access policy, click the Get Script button of that access policy instead.
Once the Setup Script dialog is open, download the cloud-init file:
-
Stay on the Cloud Init tab.
-
Under Select your Linux Distro, select the distribution your endpoints run (CentOS, Debian, RedHat, or Ubuntu).
-
Click Download.
-
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
-
Open the folder of the EZSSH Pulumi Azure Sample in Visual Studio Code.
-
Edit
Pulumi.dev.yamlto select the location where you want to deploy your VMs. -
Open
MyStack.cs. This file has all your infrastructure as code. -
Change the
forloop to the number of VMs you want to create. -
Change the
CustomDataline to match the path and name of the cloud-init file you downloaded in step 1.
-
Open a terminal inside the folder and run:
pulumi up -
The first time, Pulumi asks you to sign in. If you do not have a Pulumi account, you can create one for free.
-
When Pulumi asks you to create a stack, create one called
dev. -
Once the project builds, Pulumi shows the changes it will make to your subscription.
-
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
-
In the EZSSH portal, navigate to Azure Policies and edit your policy.
-
Click Refresh Endpoints.
-
Click Save Changes.
How to Add the New Endpoints to a Hybrid Policy
-
In the EZSSH portal, navigate to Hybrid Policies and edit your policy.
-
Under Policy Endpoints, enter the IP address or FQDN of each new VM and click Add Endpoint.
-
Click Save Changes.
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.
-
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"); -
It creates a network security group named
exampleNetworkSecurityGroupin 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, }); -
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, });Restrict Who Can Reach Port 22This sample rule is very open. In your own deployment, only allow a select number of IP addresses into your administration ports.
-
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, }, } }); -
It starts a
forloop 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. -
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" }); -
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 }, }, }); -
It creates the VM with the following properties:
- Name:
TestVMplus 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") } }); - Name:
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