Skip to content

Commit

Permalink
Merge pull request #143 from pradipd/user/pradipd/sshkeys
Browse files Browse the repository at this point in the history
Adding support for multiple ssh keys
  • Loading branch information
pradipd authored Jul 22, 2021
2 parents 4b11680 + e4c6130 commit b2783a3
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 16 deletions.
2 changes: 2 additions & 0 deletions api/v1alpha3/azurestackhcimachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type AzureStackHCIMachineSpec struct {
// AllocatePublicIP allows the ability to create dynamic public ips for machines where this value is true.
// +optional
AllocatePublicIP bool `json:"allocatePublicIP,omitempty"`

AdditionalSSHKeys []string `json:"additionalSSHKeys,omitempty"`
}

// AzureStackHCIMachineStatus defines the observed state of AzureStackHCIMachine
Expand Down
2 changes: 2 additions & 0 deletions api/v1alpha3/azurestackhcivirtualmachine_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type AzureStackHCIVirtualMachineSpec struct {
ClusterName string `json:"clusterName"`
SubnetName string `json:"subnetName"`
BackendPoolNames []string `json:"backendPoolNames,omitempty"`

AdditionalSSHKeys []string `json:"additionalSSHKeys,omitempty"`
}

// AzureStackHCIVirtualMachineStatus defines the observed state of AzureStackHCIVirtualMachine
Expand Down
17 changes: 17 additions & 0 deletions api/v1alpha3/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 14 additions & 15 deletions cloud/services/virtualmachines/virtualmachines.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const (
type Spec struct {
Name string
NICName string
SSHKeyData string
SSHKeyData []string
Size string
Zone string
Image infrav1.Image
Expand Down Expand Up @@ -99,7 +99,7 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
klog.V(2).Infof("creating vm %s : %v", vmSpec.Name, vmSpec)

sshKeyData := vmSpec.SSHKeyData
if sshKeyData == "" {
if len(sshKeyData) == 0 {
privateKey, perr := rsa.GenerateKey(rand.Reader, 2048)
if perr != nil {
return errors.Wrap(perr, "Failed to generate private key")
Expand All @@ -109,7 +109,16 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
if perr != nil {
return errors.Wrap(perr, "Failed to generate public key")
}
sshKeyData = string(ssh.MarshalAuthorizedKey(publicRsaKey))
sshKeyData = []string{string(ssh.MarshalAuthorizedKey(publicRsaKey))}
}

sshPublicKeys := []compute.SSHPublicKey{}
sshKeyPath := fmt.Sprintf("/home/%s/.ssh/authorized_keys", azurestackhci.DefaultUserName)
for i := 0; i < len(sshKeyData); i++ {
sshPublicKeys = append(sshPublicKeys, compute.SSHPublicKey{
Path: &sshKeyPath,
KeyData: &sshKeyData[i],
})
}

randomPassword, err := GenerateRandomString(32)
Expand All @@ -131,12 +140,7 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {
OsType: compute.OperatingSystemTypes(vmSpec.OSDisk.OSType),
LinuxConfiguration: &compute.LinuxConfiguration{
SSH: &compute.SSHConfiguration{
PublicKeys: &[]compute.SSHPublicKey{
{
Path: to.StringPtr(fmt.Sprintf("/home/%s/.ssh/authorized_keys", azurestackhci.DefaultUserName)),
KeyData: to.StringPtr(sshKeyData),
},
},
PublicKeys: &sshPublicKeys,
},
DisablePasswordAuthentication: to.BoolPtr(false),
},
Expand Down Expand Up @@ -164,12 +168,7 @@ func (s *Service) Reconcile(ctx context.Context, spec interface{}) error {

virtualMachine.OsProfile.WindowsConfiguration = &compute.WindowsConfiguration{
SSH: &compute.SSHConfiguration{
PublicKeys: &[]compute.SSHPublicKey{
{
Path: to.StringPtr(fmt.Sprintf("/users/%s/.ssh/authorized_keys", azurestackhci.DefaultUserName)),
KeyData: to.StringPtr(sshKeyData),
},
},
PublicKeys: &sshPublicKeys,
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ spec:
spec:
description: AzureStackHCIMachineSpec defines the desired state of AzureStackHCIMachine
properties:
additionalSSHKeys:
items:
type: string
type: array
allocatePublicIP:
description: AllocatePublicIP allows the ability to create dynamic
public ips for machines where this value is true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ spec:
description: Spec is the specification of the desired behavior
of the machine.
properties:
additionalSSHKeys:
items:
type: string
type: array
allocatePublicIP:
description: AllocatePublicIP allows the ability to create
dynamic public ips for machines where this value is true.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ spec:
description: AzureStackHCIVirtualMachineSpec defines the desired state
of AzureStackHCIVirtualMachine
properties:
additionalSSHKeys:
items:
type: string
type: array
availabilityZone:
properties:
enabled:
Expand Down
1 change: 1 addition & 0 deletions controllers/azurestackhcimachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ func (r *AzureStackHCIMachineReconciler) reconcileVirtualMachineNormal(machineSc
vm.Spec.Location = machineScope.AzureStackHCIMachine.Spec.Location
vm.Spec.SSHPublicKey = machineScope.AzureStackHCIMachine.Spec.SSHPublicKey
vm.Spec.BootstrapData = &bootstrapData
vm.Spec.AdditionalSSHKeys = machineScope.AzureStackHCIMachine.Spec.AdditionalSSHKeys

return nil
}
Expand Down
12 changes: 11 additions & 1 deletion controllers/azurestackhcivirtualmachine_reconciler.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,20 @@ func (s *azureStackHCIVirtualMachineService) reconcileNetworkInterface(nicName s

func (s *azureStackHCIVirtualMachineService) createVirtualMachine(nicName string) (*infrav1.VM, error) {
var vm *infrav1.VM
decodedKeys := []string{}
decoded, err := base64.StdEncoding.DecodeString(s.vmScope.AzureStackHCIVirtualMachine.Spec.SSHPublicKey)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode ssh public key")
}
decodedKeys = append(decodedKeys, string(decoded))

for _, key := range s.vmScope.AzureStackHCIVirtualMachine.Spec.AdditionalSSHKeys {
decoded, err = base64.StdEncoding.DecodeString(key)
if err != nil {
return nil, errors.Wrapf(err, "failed to decode an additional ssh public key")
}
decodedKeys = append(decodedKeys, string(decoded))
}

vmSpec := &virtualmachines.Spec{
Name: s.vmScope.Name(),
Expand Down Expand Up @@ -205,7 +215,7 @@ func (s *azureStackHCIVirtualMachineService) createVirtualMachine(nicName string
vmSpec = &virtualmachines.Spec{
Name: s.vmScope.Name(),
NICName: nicName,
SSHKeyData: string(decoded),
SSHKeyData: decodedKeys,
Size: s.vmScope.AzureStackHCIVirtualMachine.Spec.VMSize,
OSDisk: s.vmScope.AzureStackHCIVirtualMachine.Spec.OSDisk,
Image: s.vmScope.AzureStackHCIVirtualMachine.Spec.Image,
Expand Down

0 comments on commit b2783a3

Please sign in to comment.