func (c *AWSClient) InjectKeyPair(instanceId string) (string, string, string, error) { instanceInput := &ec2.DescribeInstancesInput{ InstanceIDs: []*string{ aws.String(instanceId), // Required }, } instanceOutput, err := c.EC2Client.DescribeInstances(instanceInput) if err != nil { return "", "", "", err } ip, _ := strconv.Unquote(awsutil.StringValue(instanceOutput.Reservations[0].Instances[0].PublicIPAddress)) pemBytes, err := utils.ReadFile(path.Join(os.Getenv("HOME"), KEYPAIR_DIR_NAME, PIRVATE_KEY_FILE_NAME)) if err != nil { return "", "", "", err } awsSShClient, err := utils.GetSshClient(LINUX_USER, pemBytes, ip) if err != nil { return "", "", "", err } command := `rm -f ./broker_id_rsa ./broker_id_rsa.pub ssh-keygen -q -t rsa -N "" -f ./broker_id_rsa cat ./broker_id_rsa.pub >> .ssh/authorized_keys cat ./broker_id_rsa` privateKey, err := awsSShClient.ExecCommand(command) if err != nil { return "", "", "", err } return ip, LINUX_USER, privateKey, nil }
func LoadConfig(path string) (*Config, error) { bytes, err := utils.ReadFile(path) if err != nil { return ¤tConfiguration, err } err = json.Unmarshal(bytes, ¤tConfiguration) if err != nil { return ¤tConfiguration, err } return ¤tConfiguration, nil }
func (c *AWSClient) RevokeKeyPair(instanceId string, privateKey string) error { instanceInput := &ec2.DescribeInstancesInput{ InstanceIDs: []*string{ aws.String(instanceId), }, } instanceOutput, err := c.EC2Client.DescribeInstances(instanceInput) if err != nil { return err } ip, _ := strconv.Unquote(awsutil.StringValue(instanceOutput.Reservations[0].Instances[0].PublicIPAddress)) pemBytes, err := utils.ReadFile(path.Join(os.Getenv("HOME"), KEYPAIR_DIR_NAME, PIRVATE_KEY_FILE_NAME)) if err != nil { return err } awsSShClient, err := utils.GetSshClient(LINUX_USER, pemBytes, ip) if err != nil { return err } publicKey, err := utils.GeneratePublicKey([]byte(privateKey)) if err != nil { return err } escapedPublicKey := strings.Replace(publicKey, "/", "\\/", -1) command := fmt.Sprintf("sed '/%s/d' -i ~/.ssh/authorized_keys && echo 'revoked the public key: %s'", escapedPublicKey, publicKey) result, err := awsSShClient.ExecCommand(command) if err != nil { return err } fmt.Println(result) return nil }