go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/_motor/providers/awsec2ebs/types.go (about) 1 // Copyright (c) Mondoo, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package awsec2ebs 5 6 import ( 7 "path" 8 "regexp" 9 "strings" 10 11 "github.com/aws/aws-sdk-go-v2/service/ec2/types" 12 "github.com/aws/aws-sdk-go/aws" 13 "github.com/cockroachdb/errors" 14 ) 15 16 type InstanceId struct { 17 Id string 18 Region string 19 Name string 20 Account string 21 Zone string 22 MarketplaceImg bool 23 } 24 25 func NewInstanceId(account string, region string, id string) (*InstanceId, error) { 26 if region == "" || id == "" || account == "" { 27 return nil, errors.New("invalid instance id. account, region and instance id required.") 28 } 29 return &InstanceId{Account: account, Region: region, Id: id}, nil 30 } 31 32 func (s *InstanceId) String() string { 33 // e.g. account/999000999000/region/us-east-2/instance/i-0989478343232 34 return path.Join("account", s.Account, "region", s.Region, "instance", s.Id) 35 } 36 37 func ParseInstanceId(path string) (*InstanceId, error) { 38 if !IsValidInstanceId(path) { 39 return nil, errors.New("invalid instance id. expected account/<id>/region/<region-val>/instance/<instance-id>") 40 } 41 keyValues := strings.Split(path, "/") 42 if len(keyValues) != 6 { 43 return nil, errors.New("invalid instance id. expected account/<id>/region/<region-val>/instance/<instance-id>") 44 } 45 return NewInstanceId(keyValues[1], keyValues[3], keyValues[5]) 46 } 47 48 var VALID_INSTANCE_ID = regexp.MustCompile(`^account/\d{12}/region\/(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d\/instance\/.+$`) 49 50 func IsValidInstanceId(path string) bool { 51 return VALID_INSTANCE_ID.MatchString(path) 52 } 53 54 type SnapshotId struct { 55 Id string 56 Region string 57 Account string 58 } 59 60 type VolumeInfo struct { 61 Id string 62 Region string 63 Account string 64 IsAvailable bool 65 Tags map[string]string 66 } 67 68 func resourceTags(resourceType types.ResourceType, instanceId string) []types.TagSpecification { 69 return []types.TagSpecification{ 70 { 71 ResourceType: resourceType, 72 Tags: []types.Tag{ 73 {Key: aws.String("createdBy"), Value: aws.String("Mondoo")}, 74 {Key: aws.String("Created By"), Value: aws.String("Mondoo")}, 75 {Key: aws.String("Created From Instance"), Value: aws.String(instanceId)}, 76 }, 77 }, 78 } 79 } 80 81 const ( 82 EBSTargetInstance = "instance" 83 EBSTargetVolume = "volume" 84 EBSTargetSnapshot = "snapshot" 85 ) 86 87 type EbsTransportTarget struct { 88 Account string 89 Region string 90 Id string 91 Type string 92 } 93 94 func ParseEbsTransportUrl(path string) (*EbsTransportTarget, error) { 95 keyValues := strings.Split(path, "/") 96 if len(keyValues) != 6 { 97 return nil, errors.New("invalid id. expected account/<id>/region/<region-val>/{instance, volume, or snapshot}/<id>") 98 } 99 100 var itemType string 101 switch keyValues[4] { 102 case "volume": 103 itemType = EBSTargetVolume 104 case "snapshot": 105 itemType = EBSTargetSnapshot 106 default: 107 itemType = EBSTargetInstance 108 } 109 110 return &EbsTransportTarget{Account: keyValues[1], Region: keyValues[3], Id: keyValues[5], Type: itemType}, nil 111 }