github.com/gravitational/teleport/api@v0.0.0-20240507183017-3110591cbafc/utils/aws/ec2.go (about)

     1  /*
     2  Copyright 2023 Gravitational, Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package aws
    18  
    19  import (
    20  	"regexp"
    21  )
    22  
    23  // EC2 Node IDs are {AWS account ID}-{EC2 resource ID} eg:
    24  //
    25  //	123456789012-i-1234567890abcdef0
    26  //
    27  // AWS account ID is always a 12 digit number, see
    28  //
    29  //	https://docs.aws.amazon.com/general/latest/gr/acct-identifiers.html
    30  //
    31  // EC2 resource ID is i-{8 or 17 hex digits}, see
    32  //
    33  //	https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/resource-ids.html
    34  var ec2NodeIDRE = regexp.MustCompile("^[0-9]{12}-i-[0-9a-f]{8,}$")
    35  
    36  // IsEC2NodeID returns true if the given ID looks like an EC2 node ID. Uses a
    37  // simple regex to check. Node IDs are almost always UUIDs when set
    38  // automatically, but can be manually overridden by admins. If someone manually
    39  // sets a host ID that looks like one of our generated EC2 node IDs, they may be
    40  // able to trick this function, so don't use it for any critical purpose.
    41  func IsEC2NodeID(id string) bool {
    42  	return ec2NodeIDRE.MatchString(id)
    43  }