go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/id/awsec2/id.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package awsec2
     5  
     6  import (
     7  	"regexp"
     8  	"strings"
     9  
    10  	"github.com/cockroachdb/errors"
    11  )
    12  
    13  // aws://ec2/v1/accounts/{account}/regions/{region}/instances/{instanceid}
    14  func MondooInstanceID(account string, region string, instanceid string) string {
    15  	return "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/" + account + "/regions/" + region + "/instances/" + instanceid
    16  }
    17  
    18  type MondooInstanceId struct {
    19  	Account string
    20  	Region  string
    21  	Id      string
    22  }
    23  
    24  var VALID_MONDOO_INSTANCE_ID = regexp.MustCompile(`^//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/\d{12}/regions\/(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d\/instances\/.+$`)
    25  
    26  func ParseMondooInstanceID(path string) (*MondooInstanceId, error) {
    27  	if !IsValidMondooInstanceId(path) {
    28  		return nil, errors.New("invalid aws ec2 instance id")
    29  	}
    30  	keyValues := strings.Split(path, "/")
    31  	if len(keyValues) != 13 {
    32  		return nil, errors.New("invalid instance id")
    33  	}
    34  	return &MondooInstanceId{Account: keyValues[8], Region: keyValues[10], Id: keyValues[12]}, nil
    35  }
    36  
    37  func IsValidMondooInstanceId(path string) bool {
    38  	return VALID_MONDOO_INSTANCE_ID.MatchString(path)
    39  }
    40  
    41  // aws://ec2/v1/accounts/{account}/regions/{region}/volumes/{volumeid}
    42  func MondooVolumeID(account string, region string, volumeid string) string {
    43  	return "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/" + account + "/regions/" + region + "/volumes/" + volumeid
    44  }
    45  
    46  type MondooVolumeId struct {
    47  	Account string
    48  	Region  string
    49  	Id      string
    50  }
    51  
    52  var VALID_MONDOO_VOLUME_ID = regexp.MustCompile(`^//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/\d{12}/regions\/(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d\/volumes\/.+$`)
    53  
    54  func ParseMondooVolumeID(path string) (*MondooVolumeId, error) {
    55  	if !IsValidMondooVolumeId(path) {
    56  		return nil, errors.New("invalid aws ec2 volume id")
    57  	}
    58  	keyValues := strings.Split(path, "/")
    59  	if len(keyValues) != 13 {
    60  		return nil, errors.New("invalid volume id")
    61  	}
    62  	return &MondooVolumeId{Account: keyValues[8], Region: keyValues[10], Id: keyValues[12]}, nil
    63  }
    64  
    65  func IsValidMondooVolumeId(path string) bool {
    66  	return VALID_MONDOO_VOLUME_ID.MatchString(path)
    67  }
    68  
    69  // aws://ec2/v1/accounts/{account}/regions/{region}/snapshots/{snapshotid}
    70  func MondooSnapshotID(account string, region string, snapshotid string) string {
    71  	return "//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/" + account + "/regions/" + region + "/snapshots/" + snapshotid
    72  }
    73  
    74  type MondooSnapshotId struct {
    75  	Account string
    76  	Region  string
    77  	Id      string
    78  }
    79  
    80  var VALID_MONDOO_SNAPSHOT_ID = regexp.MustCompile(`^//platformid.api.mondoo.app/runtime/aws/ec2/v1/accounts/\d{12}/regions\/(us(-gov)?|ap|ca|cn|eu|sa)-(central|(north|south)?(east|west)?)-\d\/snapshots\/.+$`)
    81  
    82  func ParseMondooSnapshotID(path string) (*MondooSnapshotId, error) {
    83  	if !IsValidMondooSnapshotId(path) {
    84  		return nil, errors.New("invalid aws ec2 snapshot id")
    85  	}
    86  	keyValues := strings.Split(path, "/")
    87  	if len(keyValues) != 13 {
    88  		return nil, errors.New("invalid snapshot id")
    89  	}
    90  	return &MondooSnapshotId{Account: keyValues[8], Region: keyValues[10], Id: keyValues[12]}, nil
    91  }
    92  
    93  func IsValidMondooSnapshotId(path string) bool {
    94  	return VALID_MONDOO_INSTANCE_ID.MatchString(path)
    95  }
    96  
    97  var VALID_MONDOO_ACCOUNT_ID = regexp.MustCompile(`^//platformid.api.mondoo.app/runtime/aws/accounts/\d{12}$`)
    98  
    99  func ParseMondooAccountID(path string) (string, error) {
   100  	if !IsValidMondooAccountId(path) {
   101  		return "", errors.New("invalid aws account id")
   102  	}
   103  	keyValues := strings.Split(path, "/")
   104  	if len(keyValues) != 7 {
   105  		return "", errors.New("invalid aws account id")
   106  	}
   107  	return keyValues[6], nil
   108  }
   109  
   110  func IsValidMondooAccountId(path string) bool {
   111  	return VALID_MONDOO_ACCOUNT_ID.MatchString(path)
   112  }
   113  
   114  func ParseEc2PlatformID(uri string) *MondooInstanceId {
   115  	// aws://ec2/v1/accounts/{account}/regions/{region}/instances/{instanceid}
   116  	awsec2 := regexp.MustCompile(`^\/\/platformid.api.mondoo.app\/runtime\/aws\/ec2\/v1\/accounts\/(.*)\/regions\/(.*)\/instances\/(.*)$`)
   117  	m := awsec2.FindStringSubmatch(uri)
   118  	if len(m) == 0 {
   119  		return nil
   120  	}
   121  
   122  	return &MondooInstanceId{
   123  		Account: m[1],
   124  		Region:  m[2],
   125  		Id:      m[3],
   126  	}
   127  }