Skip to content

Commit d9670b0

Browse files
committed
Added support for new command line parameter --disable-imds-v1 to disable IMDSv1 for Elastic BeanStalk environments.
1 parent f6a7330 commit d9670b0

File tree

4 files changed

+38
-2
lines changed

4 files changed

+38
-2
lines changed

src/Amazon.ElasticBeanstalk.Tools/Amazon.ElasticBeanstalk.Tools.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackAsTool>true</PackAsTool>
1111
<ToolCommandName>dotnet-eb</ToolCommandName>
1212
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
13-
<Version>4.3.4</Version>
13+
<Version>4.4.0</Version>
1414
<AssemblyName>dotnet-eb</AssemblyName>
1515
<Authors>Amazon Web Services</Authors>
1616
<Copyright>Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.</Copyright>

src/Amazon.ElasticBeanstalk.Tools/Commands/CommandProperties.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public class DeployEnvironmentProperties
1919
public string IISWebSite { get; set; }
2020
public bool? WaitForUpdate { get; set; }
2121
public bool? EnableXRay { get; set; }
22+
public bool? DisableIMDSv1 { get; set; }
2223
public Dictionary<string,string> Tags { get; set; }
2324
public Dictionary<string, string> AdditionalOptions { get; set; }
2425

@@ -92,6 +93,8 @@ internal void ParseCommandArguments(CommandOptions values)
9293
this.LoadBalancerType = tuple.Item2.StringValue;
9394
if ((tuple = values.FindCommandOption(EBDefinedCommandOptions.ARGUMENT_ENABLE_STICKY_SESSIONS.Switch)) != null)
9495
this.EnableStickySessions = tuple.Item2.BoolValue;
96+
if ((tuple = values.FindCommandOption(EBDefinedCommandOptions.ARGUMENT_DISABLE_IMDS_V1.Switch)) != null)
97+
this.DisableIMDSv1 = tuple.Item2.BoolValue;
9598

9699
if ((tuple = values.FindCommandOption(EBDefinedCommandOptions.ARGUMENT_PROXY_SERVER.Switch)) != null)
97100
this.ProxyServer = tuple.Item2.StringValue;
@@ -119,6 +122,7 @@ internal void PersistSettings(EBBaseCommand command, JsonData data)
119122
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_ENVIRONMENT_TYPE.ConfigFileKey, command.GetStringValueOrDefault(this.EnvironmentType, EBDefinedCommandOptions.ARGUMENT_ENVIRONMENT_TYPE, false));
120123
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_LOADBALANCER_TYPE.ConfigFileKey, command.GetStringValueOrDefault(this.LoadBalancerType, EBDefinedCommandOptions.ARGUMENT_LOADBALANCER_TYPE, false));
121124
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_ENABLE_STICKY_SESSIONS.ConfigFileKey, command.GetBoolValueOrDefault(this.EnableStickySessions, EBDefinedCommandOptions.ARGUMENT_ENABLE_STICKY_SESSIONS, false));
125+
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_DISABLE_IMDS_V1.ConfigFileKey, command.GetBoolValueOrDefault(this.DisableIMDSv1, EBDefinedCommandOptions.ARGUMENT_DISABLE_IMDS_V1, false));
122126
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_CNAME_PREFIX.ConfigFileKey, command.GetStringValueOrDefault(this.CNamePrefix, EBDefinedCommandOptions.ARGUMENT_CNAME_PREFIX, false));
123127
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_INSTANCE_TYPE.ConfigFileKey, command.GetStringValueOrDefault(this.InstanceType, EBDefinedCommandOptions.ARGUMENT_INSTANCE_TYPE, false));
124128
data.SetIfNotNull(EBDefinedCommandOptions.ARGUMENT_EC2_KEYPAIR.ConfigFileKey, command.GetStringValueOrDefault(this.EC2KeyPair, EBDefinedCommandOptions.ARGUMENT_EC2_KEYPAIR, false));

src/Amazon.ElasticBeanstalk.Tools/Commands/DeployEnvironmentCommand.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class DeployEnvironmentCommand : EBBaseCommand
4040
EBDefinedCommandOptions.ARGUMENT_INSTANCE_TYPE,
4141
EBDefinedCommandOptions.ARGUMENT_HEALTH_CHECK_URL,
4242
EBDefinedCommandOptions.ARGUMENT_ENABLE_XRAY,
43+
EBDefinedCommandOptions.ARGUMENT_DISABLE_IMDS_V1,
4344
EBDefinedCommandOptions.ARGUMENT_ENHANCED_HEALTH_TYPE,
4445
EBDefinedCommandOptions.ARGUMENT_INSTANCE_PROFILE,
4546
EBDefinedCommandOptions.ARGUMENT_SERVICE_ROLE,
@@ -59,6 +60,9 @@ public class DeployEnvironmentCommand : EBBaseCommand
5960
const string OPTIONS_NAME_PROXY_SERVER = "ProxyServer";
6061
const string OPTIONS_NAME_APPLICATION_PORT = "PORT";
6162

63+
const string OPTIONS_NAMESPACE_DISABLE_IMDS_V1 = "aws:autoscaling:launchconfiguration";
64+
const string OPTIONS_NAME_DISABLE_IMDS_V1 = "DisableIMDSv1";
65+
6266
public string Package { get; set; }
6367

6468
public DeployEnvironmentProperties DeployEnvironmentOptions { get; } = new DeployEnvironmentProperties();
@@ -415,7 +419,6 @@ private async Task<string> CreateEnvironment(string application, string environm
415419
Value = loadBalancerType
416420
});
417421
}
418-
419422

420423
AddAdditionalOptions(createRequest.OptionSettings, true, isWindowsEnvironment);
421424

@@ -456,6 +459,26 @@ private void AddAdditionalOptions(IList<ConfigurationOptionSetting> settings, bo
456459
}
457460
}
458461

462+
var disableIMDSv1 = this.GetBoolValueOrDefault(this.DeployEnvironmentOptions.DisableIMDSv1, EBDefinedCommandOptions.ARGUMENT_DISABLE_IMDS_V1, false);
463+
if (disableIMDSv1.HasValue)
464+
{
465+
var existingSetting = settings.FirstOrDefault(s => s.Namespace == OPTIONS_NAMESPACE_DISABLE_IMDS_V1 && s.OptionName == OPTIONS_NAME_DISABLE_IMDS_V1);
466+
467+
if (existingSetting != null)
468+
{
469+
existingSetting.Value = disableIMDSv1.Value.ToString(CultureInfo.InvariantCulture).ToLowerInvariant();
470+
}
471+
else
472+
{
473+
settings.Add(new ConfigurationOptionSetting()
474+
{
475+
Namespace = OPTIONS_NAMESPACE_DISABLE_IMDS_V1,
476+
OptionName = OPTIONS_NAME_DISABLE_IMDS_V1,
477+
Value = disableIMDSv1.Value.ToString(CultureInfo.InvariantCulture).ToLowerInvariant()
478+
});
479+
}
480+
}
481+
459482
var enableXRay = this.GetBoolValueOrDefault(this.DeployEnvironmentOptions.EnableXRay, EBDefinedCommandOptions.ARGUMENT_ENABLE_XRAY, false);
460483
if(enableXRay.HasValue)
461484
{

src/Amazon.ElasticBeanstalk.Tools/EBDefinedCommandOptions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,5 +205,14 @@ public class EBDefinedCommandOptions
205205
ValueType = CommandOption.CommandOptionValueType.IntValue,
206206
Description = $"The application port that will be redirect to port 80. The default is port {EBConstants.DEFAULT_APPLICATION_PORT}."
207207
};
208+
209+
public static readonly CommandOption ARGUMENT_DISABLE_IMDS_V1 =
210+
new CommandOption
211+
{
212+
Name = "Disable IMDSv1",
213+
Switch = "--disable-imds-v1",
214+
ValueType = CommandOption.CommandOptionValueType.BoolValue,
215+
Description = "If set to true then the IMDSv1 will be disabled on EC2 instances running the application."
216+
};
208217
}
209218
}

0 commit comments

Comments
 (0)