Skip to content

Commit 6550366

Browse files
committed
Add new switch GenerateSshKey to generate SSH keys if necessary.
1 parent 563a23c commit 6550366

File tree

8 files changed

+120
-9
lines changed

8 files changed

+120
-9
lines changed

src/Aks/Aks/Commands/CreateOrUpdateKubeBase.cs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using System;
1717
using System.Collections;
1818
using System.Collections.Generic;
19+
using System.Diagnostics;
1920
using System.IO;
2021
using System.Linq;
2122
using System.Management.Automation;
@@ -116,6 +117,11 @@ public abstract class CreateOrUpdateKubeBase : KubeCmdletBase
116117
[Alias("SshKeyPath")]
117118
public string SshKeyValue { get; set; }
118119

120+
[Parameter(
121+
Mandatory = false,
122+
HelpMessage = "Generate ssh key file to {HOME}/.ssh/id_rsa.")]
123+
public SwitchParameter GenerateSshKey { get; set; }
124+
119125
[Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")]
120126
public SwitchParameter AsJob { get; set; }
121127

@@ -451,5 +457,47 @@ protected string DefaultDnsPrefix()
451457
var subPart = string.Join("", DefaultContext.Subscription.Id.Take(4));
452458
return $"{namePart}{subPart}";
453459
}
460+
461+
protected string GenerateSshKeyValue()
462+
{
463+
String generateSshKeyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", "id_rsa"); ;
464+
if (File.Exists(generateSshKeyPath))
465+
{
466+
throw new ArgumentException(string.Format(Resources.DefaultSshKeyAlreadyExist));
467+
}
468+
using (Process process = new Process())
469+
{
470+
process.StartInfo.FileName = "ssh-keygen.exe";
471+
process.StartInfo.Arguments = "-f " + generateSshKeyPath;
472+
process.StartInfo.UseShellExecute = false;
473+
process.StartInfo.RedirectStandardInput = true;
474+
process.StartInfo.RedirectStandardOutput = true;
475+
process.Start();
476+
477+
Console.WriteLine(process.StandardOutput.ReadToEnd());
478+
479+
process.WaitForExit();
480+
}
481+
return GetSshKey(generateSshKeyPath);
482+
}
483+
484+
protected virtual void PreValidate()
485+
{
486+
if (this.IsParameterBound(c => c.GenerateSshKey))
487+
{
488+
if (this.IsParameterBound(c => c.SshKeyValue))
489+
{
490+
throw new ArgumentException(string.Format(Resources.DonotUseGenerateSshKeyWithSshKeyValue));
491+
}
492+
}
493+
}
494+
495+
protected void PrepareParameter()
496+
{
497+
if (this.IsParameterBound(c => c.GenerateSshKey))
498+
{
499+
SshKeyValue = GenerateSshKeyValue();
500+
}
501+
}
454502
}
455503
}

src/Aks/Aks/Commands/NewAzureRmAks.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public override void ExecuteCmdlet()
3535
{
3636
base.ExecuteCmdlet();
3737
PreValidate();
38+
PrepareParameter();
3839

3940
Action action = () =>
4041
{
@@ -67,11 +68,12 @@ public override void ExecuteCmdlet()
6768
}
6869
}
6970

70-
private void PreValidate()
71+
protected override void PreValidate()
7172
{
7273
if ((this.IsParameterBound(c => c.NodeMinCount) || this.IsParameterBound(c => c.NodeMaxCount) || this.EnableNodeAutoScaling.IsPresent) &&
7374
!(this.IsParameterBound(c => c.NodeMinCount) && this.IsParameterBound(c => c.NodeMaxCount) && this.EnableNodeAutoScaling.IsPresent))
7475
throw new PSInvalidCastException(Resources.AksNodePoolAutoScalingParametersMustAppearTogether);
76+
base.PreValidate();
7577
}
7678
}
7779
}

src/Aks/Aks/Commands/SetAzureRmAks.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public class SetAzureRmAks : CreateOrUpdateKubeBase
5757
public override void ExecuteCmdlet()
5858
{
5959
base.ExecuteCmdlet();
60+
PreValidate();
61+
PrepareParameter();
6062

6163
ManagedCluster cluster = null;
6264
switch (ParameterSetName)
@@ -103,7 +105,11 @@ public override void ExecuteCmdlet()
103105
cluster.DnsPrefix = DnsNamePrefix;
104106
}
105107

106-
if (this.IsParameterBound(c => c.SshKeyValue))
108+
if (this.IsParameterBound(c => c.GenerateSshKey))
109+
{
110+
SshKeyValue = GenerateSshKeyValue();
111+
}
112+
if (string.IsNullOrEmpty(SshKeyValue))
107113
{
108114
WriteVerbose(Resources.UpdatingSshKeyValue);
109115
cluster.LinuxProfile.Ssh.PublicKeys = new List<ContainerServiceSshPublicKey>

src/Aks/Aks/Properties/Resources.Designer.cs

Lines changed: 19 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Aks/Aks/Properties/Resources.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,10 @@
369369
<data name="WindowsNodePoolNameLengthLimitation" xml:space="preserve">
370370
<value>Windows agent pool name can not be longer than 6 characters.</value>
371371
</data>
372+
<data name="DonotUseGenerateSshKeyWithSshKeyValue" xml:space="preserve">
373+
<value>Don't use -GenerateSshKey and -SshKeyVaule that the same time.</value>
374+
</data>
375+
<data name="DefaultSshKeyAlreadyExist" xml:space="preserve">
376+
<value>Default ssh key is already exist. Please use -SshKeyVaule.</value>
377+
</data>
372378
</root>

src/Aks/Aks/help/Az.Aks.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,4 @@ Stop the Kubectl SSH tunnel created in Start-AzKubernetesDashboard.
4949

5050
### [Update-AzAksNodePool](Update-AzAksNodePool.md)
5151
Update node pool in a managed cluster.
52+

src/Aks/Aks/help/New-AzAksCluster.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ New-AzAksCluster [-Force] [-NodeVmSetType <String>] [-NodeVnetSubnetID <String>]
2121
[-Location <String>] [-LinuxProfileAdminUserName <String>] [-DnsNamePrefix <String>]
2222
[-KubernetesVersion <String>] [-NodeName <String>] [-NodeMinCount <Int32>] [-NodeMaxCount <Int32>]
2323
[-EnableNodeAutoScaling] [-NodeCount <Int32>] [-NodeOsDiskSize <Int32>] [-NodeVmSize <String>]
24-
[-SshKeyValue <String>] [-AsJob] [-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
25-
[-Confirm] [<CommonParameters>]
24+
[-SshKeyValue <String>] [-GenerateSshKey] [-AsJob] [-Tag <Hashtable>]
25+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2626
```
2727

2828
## DESCRIPTION
@@ -154,6 +154,21 @@ Accept pipeline input: False
154154
Accept wildcard characters: False
155155
```
156156
157+
### -GenerateSshKey
158+
Generate ssh key file to {HOME}/.ssh/id_rsa.
159+
160+
```yaml
161+
Type: System.Management.Automation.SwitchParameter
162+
Parameter Sets: (All)
163+
Aliases:
164+
165+
Required: False
166+
Position: Named
167+
Default value: None
168+
Accept pipeline input: False
169+
Accept wildcard characters: False
170+
```
171+
157172
### -KubernetesVersion
158173
The version of Kubernetes to use for creating the cluster.
159174

src/Aks/Aks/help/Set-AzAksCluster.md

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ Set-AzAksCluster [-ResourceGroupName] <String> [-Name] <String> [[-ServicePrinci
1818
[-Location <String>] [-LinuxProfileAdminUserName <String>] [-DnsNamePrefix <String>]
1919
[-KubernetesVersion <String>] [-NodeName <String>] [-NodeMinCount <Int32>] [-NodeMaxCount <Int32>]
2020
[-EnableNodeAutoScaling] [-NodeCount <Int32>] [-NodeOsDiskSize <Int32>] [-NodeVmSize <String>]
21-
[-SshKeyValue <String>] [-AsJob] [-Tag <Hashtable>] [-DefaultProfile <IAzureContextContainer>] [-WhatIf]
22-
[-Confirm] [<CommonParameters>]
21+
[-SshKeyValue <String>] [-GenerateSshKey] [-AsJob] [-Tag <Hashtable>]
22+
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
2323
```
2424

2525
### InputObjectParameterSet
2626
```powershell
2727
Set-AzAksCluster -InputObject <PSKubernetesCluster> [-Location <String>] [-LinuxProfileAdminUserName <String>]
2828
[-DnsNamePrefix <String>] [-KubernetesVersion <String>] [-NodeName <String>] [-NodeMinCount <Int32>]
2929
[-NodeMaxCount <Int32>] [-EnableNodeAutoScaling] [-NodeCount <Int32>] [-NodeOsDiskSize <Int32>]
30-
[-NodeVmSize <String>] [-SshKeyValue <String>] [-AsJob] [-Tag <Hashtable>]
30+
[-NodeVmSize <String>] [-SshKeyValue <String>] [-GenerateSshKey] [-AsJob] [-Tag <Hashtable>]
3131
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
3232
```
3333

@@ -36,7 +36,7 @@ Set-AzAksCluster -InputObject <PSKubernetesCluster> [-Location <String>] [-Linux
3636
Set-AzAksCluster [-Id] <String> [-Location <String>] [-LinuxProfileAdminUserName <String>]
3737
[-DnsNamePrefix <String>] [-KubernetesVersion <String>] [-NodeName <String>] [-NodeMinCount <Int32>]
3838
[-NodeMaxCount <Int32>] [-EnableNodeAutoScaling] [-NodeCount <Int32>] [-NodeOsDiskSize <Int32>]
39-
[-NodeVmSize <String>] [-SshKeyValue <String>] [-AsJob] [-Tag <Hashtable>]
39+
[-NodeVmSize <String>] [-SshKeyValue <String>] [-GenerateSshKey] [-AsJob] [-Tag <Hashtable>]
4040
[-DefaultProfile <IAzureContextContainer>] [-WhatIf] [-Confirm] [<CommonParameters>]
4141
```
4242

@@ -114,6 +114,21 @@ Accept pipeline input: False
114114
Accept wildcard characters: False
115115
```
116116
117+
### -GenerateSshKey
118+
Generate ssh key file. Defaults to {HOME}/.ssh/.
119+
120+
```yaml
121+
Type: System.Management.Automation.SwitchParameter
122+
Parameter Sets: (All)
123+
Aliases:
124+
125+
Required: False
126+
Position: Named
127+
Default value: None
128+
Accept pipeline input: False
129+
Accept wildcard characters: False
130+
```
131+
117132
### -Id
118133
Id of a managed Kubernetes cluster
119134

0 commit comments

Comments
 (0)