Skip to content

Commit 148a592

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

File tree

5 files changed

+89
-4
lines changed

5 files changed

+89
-4
lines changed

src/Aks/Aks/Commands/NewAzureRmAks.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
// ----------------------------------------------------------------------------------
1414

1515
using System;
16+
using System.Diagnostics;
17+
using System.IO;
1618
using System.Management.Automation;
1719
using Microsoft.Azure.Commands.Aks.Models;
1820
using Microsoft.Azure.Commands.Aks.Properties;
@@ -31,10 +33,16 @@ public class NewAzureRmAks : NewKubeBase
3133
[Parameter(Mandatory = false, HelpMessage = "Create cluster even if it already exists")]
3234
public SwitchParameter Force { get; set; }
3335

36+
[Parameter(
37+
Mandatory = false,
38+
HelpMessage = "Generate ssh key file to {HOME}/.ssh/id_rsa.")]
39+
public SwitchParameter GenerateSshKey { get; set; }
40+
3441
public override void ExecuteCmdlet()
3542
{
3643
base.ExecuteCmdlet();
3744
PreValidate();
45+
PrepareParameter();
3846

3947
Action action = () =>
4048
{
@@ -72,6 +80,43 @@ private void PreValidate()
7280
if ((this.IsParameterBound(c => c.NodeMinCount) || this.IsParameterBound(c => c.NodeMaxCount) || this.EnableNodeAutoScaling.IsPresent) &&
7381
!(this.IsParameterBound(c => c.NodeMinCount) && this.IsParameterBound(c => c.NodeMaxCount) && this.EnableNodeAutoScaling.IsPresent))
7482
throw new PSInvalidCastException(Resources.AksNodePoolAutoScalingParametersMustAppearTogether);
83+
84+
if (this.IsParameterBound(c => c.GenerateSshKey) && this.IsParameterBound(c => c.SshKeyValue))
85+
{
86+
throw new ArgumentException(string.Format(Resources.DonotUseGenerateSshKeyWithSshKeyValue));
87+
}
88+
}
89+
90+
private string GenerateSshKeyValue()
91+
{
92+
String generateSshKeyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", "id_rsa"); ;
93+
if (File.Exists(generateSshKeyPath))
94+
{
95+
throw new ArgumentException(string.Format(Resources.DefaultSshKeyAlreadyExist));
96+
}
97+
using (Process process = new Process())
98+
{
99+
process.StartInfo.FileName = "ssh-keygen";
100+
process.StartInfo.Arguments = "-f " + generateSshKeyPath;
101+
process.StartInfo.UseShellExecute = false;
102+
process.StartInfo.RedirectStandardInput = true;
103+
process.StartInfo.RedirectStandardError = true;
104+
process.StartInfo.RedirectStandardOutput = true;
105+
process.Start();
106+
107+
Console.WriteLine(process.StandardOutput.ReadToEnd());
108+
109+
process.WaitForExit();
110+
}
111+
return GetSshKey(generateSshKeyPath);
112+
}
113+
114+
protected void PrepareParameter()
115+
{
116+
if (this.IsParameterBound(c => c.GenerateSshKey))
117+
{
118+
SshKeyValue = GenerateSshKeyValue();
119+
}
75120
}
76121
}
77122
}

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: 7 additions & 1 deletion
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-
</root>
372+
<data name="DonotUseGenerateSshKeyWithSshKeyValue" xml:space="preserve">
373+
<value>Don't use -GenerateSshKey and -SshKeyVaule at the same time.</value>
374+
</data>
375+
<data name="DefaultSshKeyAlreadyExist" xml:space="preserve">
376+
<value>Default ssh key already exists. Please use -SshKeyVaule.</value>
377+
</data>
378+
</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

0 commit comments

Comments
 (0)