Skip to content

Commit 9790689

Browse files
committed
Add new switch GenerateSshKey to generate SSH keys if necessary.
1 parent 00bc26d commit 9790689

File tree

5 files changed

+133
-3
lines changed

5 files changed

+133
-3
lines changed

src/Aks/Aks/Commands/NewAzureRmAks.cs

Lines changed: 78 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,76 @@ 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 void VerifySshKeyGenBinaryExist()
91+
{
92+
using (Process process = new Process())
93+
{
94+
if (Environment.OSVersion.Platform.ToString().Contains("Win"))
95+
{
96+
process.StartInfo.FileName = "where.exe";
97+
}
98+
else
99+
{
100+
process.StartInfo.FileName = "whereis";
101+
}
102+
process.StartInfo.Arguments = "ssh-keygen";
103+
process.StartInfo.UseShellExecute = false;
104+
process.StartInfo.RedirectStandardOutput = true;
105+
106+
process.Start();
107+
process.WaitForExit();
108+
109+
string result = process.StandardOutput.ReadLine();
110+
if (result.Contains("not found") || result.Contains("Could not find") || result.Trim().Equals("ssh-keygen:"))
111+
{
112+
throw new ArgumentException(string.Format(Resources.EnableSsh));
113+
}
114+
115+
if (process.ExitCode != 0)
116+
{
117+
throw new ArgumentException(string.Format(Resources.EnableSsh));
118+
}
119+
}
120+
}
121+
122+
private string GenerateSshKeyValue()
123+
{
124+
VerifySshKeyGenBinaryExist();
125+
String generateSshKeyPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".ssh", "id_rsa"); ;
126+
if (File.Exists(generateSshKeyPath))
127+
{
128+
throw new ArgumentException(string.Format(Resources.DefaultSshKeyAlreadyExist));
129+
}
130+
using (Process process = new Process())
131+
{
132+
process.StartInfo.FileName = "ssh-keygen";
133+
process.StartInfo.Arguments = "-f " + generateSshKeyPath;
134+
process.StartInfo.UseShellExecute = false;
135+
process.StartInfo.RedirectStandardInput = true;
136+
process.StartInfo.RedirectStandardError = true;
137+
process.StartInfo.RedirectStandardOutput = true;
138+
process.Start();
139+
140+
Console.WriteLine(process.StandardOutput.ReadToEnd());
141+
142+
process.WaitForExit();
143+
}
144+
return GetSshKey(generateSshKeyPath);
145+
}
146+
147+
protected void PrepareParameter()
148+
{
149+
if (this.IsParameterBound(c => c.GenerateSshKey))
150+
{
151+
SshKeyValue = GenerateSshKeyValue();
152+
}
75153
}
76154
}
77155
}

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

Lines changed: 28 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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,4 +369,13 @@
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 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+
<data name="EnableSsh" xml:space="preserve">
379+
<value>Cannot find ssh-keygen. Please enable OpenSSH on your local machine.</value>
380+
</data>
372381
</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)