Skip to content

Commit 0e23f5e

Browse files
author
aliasgar16
committed
Added secret and secret-file option in Chef Extension for ASM commands and daemon option for ARM commands.
Signed-off-by: aliasgar16 <[email protected]>
1 parent 54f4be1 commit 0e23f5e

File tree

4 files changed

+66
-4
lines changed

4 files changed

+66
-4
lines changed

src/ResourceManager/Compute/Commands.Compute/Extension/Chef/SetAzureVMChefExtension.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public class SetAzureVMChefExtensionCommand : VirtualMachineExtensionBaseCmdlet
4848
private string JsonAttributeTemplate = "custom_json_attr";
4949
private string ChefServiceIntervalTemplate = "chef_service_interval";
5050
private string RunListTemplate = "runlist";
51+
private string DaemonTemplate = "daemon";
5152

5253
[Parameter(
5354
Mandatory = true,
@@ -114,6 +115,15 @@ public string TypeHandlerVersion
114115
[ValidateNotNullOrEmpty]
115116
public string ChefServiceInterval { get; set; }
116117

118+
[Parameter(
119+
ValueFromPipelineByPropertyName = true,
120+
HelpMessage = "Configures the chef-client service for unattended execution. The node platform should be Windows." +
121+
"Options: 'none' or 'service'." +
122+
"none - Currently prevents the chef-client service from being configured as a service." +
123+
"service - Configures the chef-client to run automatically in the background as a service.")]
124+
[ValidateNotNullOrEmpty]
125+
public string Daemon { get; set; }
126+
117127
[Parameter(
118128
ValueFromPipelineByPropertyName = true,
119129
HelpMessage = "The Chef Server Node Runlist.")]
@@ -228,6 +238,7 @@ private Hashtable PublicConfiguration
228238
bool IsJsonAttributeEmpty = string.IsNullOrEmpty(this.JsonAttribute);
229239
bool IsChefServiceIntervalEmpty = string.IsNullOrEmpty(this.ChefServiceInterval);
230240
string BootstrapVersion = string.IsNullOrEmpty(this.BootstrapVersion) ? "" : this.BootstrapVersion;
241+
bool IsDaemonEmpty = string.IsNullOrEmpty(this.Daemon);
231242

232243
//Cases handled:
233244
// 1. When clientRb given by user and:
@@ -300,6 +311,11 @@ private Hashtable PublicConfiguration
300311
hashTable.Add(ChefServiceIntervalTemplate, ChefServiceInterval);
301312
}
302313

314+
if (this.Windows.IsPresent && !IsDaemonEmpty)
315+
{
316+
hashTable.Add(DaemonTemplate, this.Daemon);
317+
}
318+
303319
this.publicConfiguration = hashTable;
304320
}
305321

@@ -404,6 +420,14 @@ private void ValidateParameters()
404420
throw new ArgumentException(
405421
"Required -ClientRb or -ChefServerUrl and -ValidationClientName options.");
406422
}
423+
424+
bool IsDaemonValueInvalid = Array.IndexOf(new String[2] {"none", "service"}, this.Daemon) == -1;
425+
// Validation against the invalid use of Daemon option.
426+
if (IsDaemonValueInvalid || this.Linux.IsPresent)
427+
{
428+
throw new ArgumentException(
429+
"Invalid use of -Daemon option.");
430+
}
407431
}
408432

409433
public override void ExecuteCmdlet()

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/Chef/SetAzureVMChefExtension.cs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,24 @@ public class SetAzureVMChefExtensionCommand : VirtualMachineChefExtensionCmdletB
107107
[Parameter(
108108
ValueFromPipelineByPropertyName = true,
109109
HelpMessage = "Configures the chef-client service for unattended execution. The node platform should be Windows." +
110-
"Options: 'auto' or 'service'." +
111-
"auto - Currently prevents the chef-client service from being configured as a service." +
110+
"Options: 'none' or 'service'." +
111+
"none - Currently prevents the chef-client service from being configured as a service." +
112112
"service - Configures the chef-client to run automatically in the background as a service.")]
113113
[ValidateNotNullOrEmpty]
114114
public string Daemon { get; set; }
115115

116+
[Parameter(
117+
ValueFromPipelineByPropertyName = true,
118+
HelpMessage = "The encryption key used to encrypt and decrypt the data bag item values.")]
119+
[ValidateNotNullOrEmpty]
120+
public string Secret { get; set; }
121+
122+
[Parameter(
123+
ValueFromPipelineByPropertyName = true,
124+
HelpMessage = "The path to the file that contains the encryption key used to encrypt and decrypt the data bag item values.")]
125+
[ValidateNotNullOrEmpty]
126+
public string SecretFile { get; set; }
127+
116128
[Parameter(
117129
Mandatory = true,
118130
ParameterSetName = LinuxParameterSetName,
@@ -170,8 +182,17 @@ private void SetDefault()
170182

171183
private void SetPrivateConfig()
172184
{
173-
this.PrivateConfiguration = string.Format(PrivateConfigurationTemplate,
185+
var hashTable = new Hashtable();
186+
187+
if (!string.IsNullOrEmpty(this.SecretFile))
188+
hashTable.Add(SecretTemplate, File.ReadAllText(this.SecretFile).TrimEnd('\r', '\n'));
189+
else if (!string.IsNullOrEmpty(this.Secret))
190+
hashTable.Add(SecretTemplate, this.Secret);
191+
192+
hashTable.Add(PrivateConfigurationTemplate,
174193
File.ReadAllText(this.ValidationPem).TrimEnd('\r', '\n'));
194+
195+
this.PrivateConfiguration = JsonConvert.SerializeObject(hashTable);
175196
}
176197

177198
private void SetPublicConfig()
@@ -258,7 +279,7 @@ private void SetPublicConfig()
258279
hashTable.Add(ChefServiceIntervalTemplate, this.ChefServiceInterval);
259280
}
260281

261-
if (!IsDaemonEmpty)
282+
if (this.Windows.IsPresent && !IsDaemonEmpty)
262283
{
263284
hashTable.Add(DaemonTemplate, this.Daemon);
264285
}
@@ -278,6 +299,20 @@ protected override void ValidateParameters()
278299
throw new ArgumentException(
279300
"Required -ClientRb or -ChefServerUrl and -ValidationClientName options.");
280301
}
302+
303+
bool IsDaemonValueInvalid = Array.IndexOf(new String[2] {"none", "service"}, this.Daemon) == -1;
304+
// Validation against the invalid use of Daemon option.
305+
if (IsDaemonValueInvalid || this.Linux.IsPresent)
306+
{
307+
throw new ArgumentException(
308+
"Invalid use of -Daemon option.");
309+
}
310+
311+
if (!string.IsNullOrEmpty(this.SecretFile) && !File.Exists(this.SecretFile))
312+
{
313+
throw new FileNotFoundException(
314+
"File specified in -SecretFile option does not exist.");
315+
}
281316
}
282317

283318
protected override void ProcessRecord()

src/ServiceManagement/Compute/Commands.ServiceManagement/IaaS/Extensions/Chef/VirtualMachineChefExtensionCmdletBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class VirtualMachineChefExtensionCmdletBase : VirtualMachineExtensionCmdl
2929
protected const string ChefServiceIntervalTemplate = "chef_service_interval";
3030
protected const string RunListTemplate = "runlist";
3131
protected const string DaemonTemplate = "daemon";
32+
protected const string SecretTemplate = "encrypted_data_bag_secret";
3233

3334
public VirtualMachineChefExtensionCmdletBase()
3435
{

src/ServiceManagement/Services/Commands.Utilities/ChangeLog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
- LogBackupFrequencyInMinutes : Specifies the frequency of Sql Server Log Backup.
3030
* Updated Set-AzureVMChefExtension cmdlet to add following new options :
3131
- Daemon: Configures the chef-client service for unattended execution. e.g. -Daemon 'auto' or e.g. -Daemon 'service'."
32+
- Secret: The encryption key used to encrypt and decrypt the data bag item values.
33+
- SecretFile: The path to the file that contains the encryption key used to encrypt and decrypt the data bag item values.
3234

3335
## Version 3.3.0
3436
* Updated Set-AzureVMChefExtension cmdlet to add following new options :

0 commit comments

Comments
 (0)