Skip to content

Commit

Permalink
Merge pull request #538 from hazelops/IZE-695-ize-tunnel-up-instance-…
Browse files Browse the repository at this point in the history
…connect-check-for-ami-version

IZE-695 added checking OS version of instance
  • Loading branch information
psihachina authored Nov 21, 2022
2 parents d13c222 + cf54c6c commit ea31043
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions internal/commands/tunnel_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"strings"
"text/template"

"github.com/Masterminds/semver"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2instanceconnect"
Expand Down Expand Up @@ -212,6 +213,11 @@ func (o *TunnelUpOptions) Run() error {
logrus.Debugf("public key path: %s", o.PublicKeyFile)
logrus.Debugf("private key path: %s", o.PrivateKeyFile)

err := o.checkOsVersion()
if err != nil {
return err
}

pk, err := getPublicKey(o.PublicKeyFile)
if err != nil {
return fmt.Errorf("can't get public key: %s", err)
Expand Down Expand Up @@ -242,6 +248,40 @@ func (o *TunnelUpOptions) Run() error {
return nil
}

func (o *TunnelUpOptions) checkOsVersion() error {
diio, err := o.Config.AWSClient.SSMClient.DescribeInstanceInformation(&ssm.DescribeInstanceInformationInput{
Filters: []*ssm.InstanceInformationStringFilter{
{
Key: aws.String("InstanceIds"),
Values: aws.StringSlice([]string{o.BastionHostID}),
},
},
})
if err != nil {
return fmt.Errorf("can't get instance '%s' information: %s", o.BastionHostID, err)
}

if len(diio.InstanceInformationList) == 0 {
return fmt.Errorf("can't get instance '%s' information", o.BastionHostID)
}

osName := *diio.InstanceInformationList[0].PlatformName
osVersion := *diio.InstanceInformationList[0].PlatformVersion

switch osName {
case "Ubuntu":
if semver.MustParse(osVersion).LessThan(semver.MustParse("20.04")) {
pterm.Warning.Printfln("Your bastion host AMI is Ubuntu %s, Instance Connect is not installed by default on that version of OS. Please upgrade to at least v20.04", osVersion)
}
case "Amazon Linux AMI":
if semver.MustParse(osVersion).LessThan(semver.MustParse("2.0.20190618")) {
pterm.Warning.Printfln("Your bastion host AMI is Amazon Linux AMI %s, Instance Connect is not installed by default on that version of OS. Please upgrade your AMI to at least 2.0.20190618", osVersion)
}
}

return nil
}

func (o *TunnelUpOptions) upTunnel() (string, error) {
sshConfigPath := fmt.Sprintf("%s/ssh.config", o.Config.EnvDir)
logrus.Debugf("ssh config path: %s", sshConfigPath)
Expand Down

0 comments on commit ea31043

Please sign in to comment.