2015-10-05 05:37:45 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2016-01-23 02:12:16 +00:00
|
|
|
"fmt"
|
2015-10-05 05:37:45 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2015-10-07 18:35:00 +00:00
|
|
|
"strconv"
|
2015-10-05 05:37:45 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2015-10-22 18:21:07 +00:00
|
|
|
"github.com/hashicorp/go-cleanhttp"
|
2015-10-05 05:37:45 +00:00
|
|
|
"github.com/hashicorp/nomad/client/config"
|
|
|
|
"github.com/hashicorp/nomad/nomad/structs"
|
|
|
|
)
|
|
|
|
|
2015-10-05 17:59:02 +00:00
|
|
|
// This is where the GCE metadata server normally resides. We hardcode the
|
|
|
|
// "instance" path as well since it's the only one we access here.
|
2015-10-05 17:57:45 +00:00
|
|
|
const DEFAULT_GCE_URL = "http://169.254.169.254/computeMetadata/v1/instance/"
|
|
|
|
|
2015-10-07 18:35:00 +00:00
|
|
|
type GCEMetadataNetworkInterface struct {
|
|
|
|
AccessConfigs []struct {
|
|
|
|
ExternalIp string
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
ForwardedIps []string
|
|
|
|
Ip string
|
|
|
|
Network string
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReqError struct {
|
|
|
|
StatusCode int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e ReqError) Error() string {
|
|
|
|
return http.StatusText(e.StatusCode)
|
|
|
|
}
|
|
|
|
|
2015-10-07 18:35:00 +00:00
|
|
|
func lastToken(s string) string {
|
|
|
|
index := strings.LastIndex(s, "/")
|
|
|
|
return s[index+1:]
|
|
|
|
}
|
|
|
|
|
2015-10-12 21:56:33 +00:00
|
|
|
// EnvGCEFingerprint is used to fingerprint GCE metadata
|
2015-10-05 18:13:25 +00:00
|
|
|
type EnvGCEFingerprint struct {
|
2015-11-05 21:46:02 +00:00
|
|
|
StaticFingerprinter
|
2015-10-05 18:13:25 +00:00
|
|
|
client *http.Client
|
|
|
|
logger *log.Logger
|
|
|
|
metadataURL string
|
|
|
|
}
|
|
|
|
|
2015-10-12 21:56:33 +00:00
|
|
|
// NewEnvGCEFingerprint is used to create a fingerprint from GCE metadata
|
2015-10-05 18:13:25 +00:00
|
|
|
func NewEnvGCEFingerprint(logger *log.Logger) Fingerprint {
|
2015-10-05 05:37:45 +00:00
|
|
|
// Read the internal metadata URL from the environment, allowing test files to
|
|
|
|
// provide their own
|
|
|
|
metadataURL := os.Getenv("GCE_ENV_URL")
|
|
|
|
if metadataURL == "" {
|
2015-10-05 17:57:45 +00:00
|
|
|
metadataURL = DEFAULT_GCE_URL
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// assume 2 seconds is enough time for inside GCE network
|
|
|
|
client := &http.Client{
|
2015-10-22 14:58:23 +00:00
|
|
|
Timeout: 2 * time.Second,
|
|
|
|
Transport: cleanhttp.DefaultTransport(),
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-05 18:13:25 +00:00
|
|
|
return &EnvGCEFingerprint{
|
|
|
|
client: client,
|
2015-10-05 05:37:45 +00:00
|
|
|
logger: logger,
|
|
|
|
metadataURL: metadataURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:13:25 +00:00
|
|
|
func (f *EnvGCEFingerprint) Get(attribute string, recursive bool) (string, error) {
|
|
|
|
reqUrl := f.metadataURL + attribute
|
2015-10-05 05:37:45 +00:00
|
|
|
if recursive {
|
|
|
|
reqUrl = reqUrl + "?recursive=true"
|
|
|
|
}
|
|
|
|
|
|
|
|
parsedUrl, err := url.Parse(reqUrl)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &http.Request{
|
|
|
|
Method: "GET",
|
|
|
|
URL: parsedUrl,
|
|
|
|
Header: http.Header{
|
|
|
|
"Metadata-Flavor": []string{"Google"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2015-10-05 18:13:25 +00:00
|
|
|
res, err := f.client.Do(req)
|
2015-12-21 16:55:33 +00:00
|
|
|
if err != nil || res.StatusCode != http.StatusOK {
|
2016-05-09 06:34:32 +00:00
|
|
|
f.logger.Printf("[DEBUG] fingerprint.env_gce: Could not read value for attribute %q", attribute)
|
2015-10-05 05:37:45 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := ioutil.ReadAll(res.Body)
|
|
|
|
res.Body.Close()
|
|
|
|
if err != nil {
|
2016-04-15 20:42:57 +00:00
|
|
|
f.logger.Printf("[ERR] fingerprint.env_gce: Error reading response body for GCE %s", attribute)
|
2015-10-05 05:37:45 +00:00
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if res.StatusCode >= 400 {
|
|
|
|
return "", ReqError{res.StatusCode}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(resp), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkError(err error, logger *log.Logger, desc string) error {
|
|
|
|
// If it's a URL error, assume we're not actually in an GCE environment.
|
|
|
|
// To the outer layers, this isn't an error so return nil.
|
|
|
|
if _, ok := err.(*url.Error); ok {
|
2015-11-24 19:06:51 +00:00
|
|
|
logger.Printf("[DEBUG] fingerprint.env_gce: Error querying GCE " + desc + ", skipping")
|
2015-10-05 05:37:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Otherwise pass the error through.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EnvGCEFingerprint) Fingerprint(cfg *config.Config, node *structs.Node) (bool, error) {
|
|
|
|
if !f.isGCE() {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if node.Links == nil {
|
|
|
|
node.Links = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:55:38 +00:00
|
|
|
// Keys and whether they should be namespaced as unique. Any key whose value
|
|
|
|
// uniquely identifies a node, such as ip, should be marked as unique. When
|
|
|
|
// marked as unique, the key isn't included in the computed node class.
|
|
|
|
keys := map[string]bool{
|
|
|
|
"hostname": true,
|
|
|
|
"id": true,
|
|
|
|
"cpu-platform": false,
|
|
|
|
"scheduling/automatic-restart": false,
|
|
|
|
"scheduling/on-host-maintenance": false,
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, unique := range keys {
|
2015-10-05 18:13:25 +00:00
|
|
|
value, err := f.Get(k, false)
|
2015-10-05 05:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, checkError(err, f.logger, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
// assume we want blank entries
|
2016-01-23 02:12:16 +00:00
|
|
|
key := "platform.gce." + strings.Replace(k, "/", ".", -1)
|
2016-01-26 22:55:38 +00:00
|
|
|
if unique {
|
2016-01-23 02:12:16 +00:00
|
|
|
key = structs.UniqueNamespace(key)
|
|
|
|
}
|
|
|
|
node.Attributes[key] = strings.Trim(string(value), "\n")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// These keys need everything before the final slash removed to be usable.
|
2016-01-26 22:55:38 +00:00
|
|
|
keys = map[string]bool{
|
|
|
|
"machine-type": false,
|
|
|
|
"zone": false,
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
2016-01-26 22:55:38 +00:00
|
|
|
for k, unique := range keys {
|
2015-10-05 18:13:25 +00:00
|
|
|
value, err := f.Get(k, false)
|
2015-10-05 05:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, checkError(err, f.logger, k)
|
|
|
|
}
|
|
|
|
|
2016-01-26 22:55:38 +00:00
|
|
|
key := "platform.gce." + k
|
|
|
|
if unique {
|
|
|
|
key = structs.UniqueNamespace(key)
|
|
|
|
}
|
|
|
|
node.Attributes[key] = strings.Trim(lastToken(value), "\n")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
2015-10-07 18:35:00 +00:00
|
|
|
// Get internal and external IPs (if they exist)
|
|
|
|
value, err := f.Get("network-interfaces/", true)
|
|
|
|
var interfaces []GCEMetadataNetworkInterface
|
|
|
|
if err := json.Unmarshal([]byte(value), &interfaces); err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.env_gce: Error decoding network interface information: %s", err.Error())
|
2015-10-12 22:57:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, intf := range interfaces {
|
|
|
|
prefix := "platform.gce.network." + lastToken(intf.Network)
|
2016-01-23 02:12:16 +00:00
|
|
|
uniquePrefix := "unique." + prefix
|
2015-10-12 22:57:45 +00:00
|
|
|
node.Attributes[prefix] = "true"
|
2016-01-23 02:12:16 +00:00
|
|
|
node.Attributes[uniquePrefix+".ip"] = strings.Trim(intf.Ip, "\n")
|
2015-10-12 22:57:45 +00:00
|
|
|
for index, accessConfig := range intf.AccessConfigs {
|
2016-01-23 02:12:16 +00:00
|
|
|
node.Attributes[uniquePrefix+".external-ip."+strconv.Itoa(index)] = accessConfig.ExternalIp
|
2015-10-07 18:35:00 +00:00
|
|
|
}
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var tagList []string
|
2015-10-05 18:13:25 +00:00
|
|
|
value, err = f.Get("tags", false)
|
2015-10-05 05:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, checkError(err, f.logger, "tags")
|
|
|
|
}
|
2015-10-07 17:39:23 +00:00
|
|
|
if err := json.Unmarshal([]byte(value), &tagList); err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.env_gce: Error decoding instance tags: %s", err.Error())
|
2015-10-12 21:56:33 +00:00
|
|
|
}
|
|
|
|
for _, tag := range tagList {
|
2016-01-23 02:12:16 +00:00
|
|
|
attr := "platform.gce.tag."
|
|
|
|
var key string
|
|
|
|
|
|
|
|
// If the tag is namespaced as unique, we strip it from the tag and
|
|
|
|
// prepend to the whole attribute.
|
|
|
|
if structs.IsUniqueNamespace(tag) {
|
|
|
|
tag = strings.TrimPrefix(tag, structs.NodeUniqueNamespace)
|
|
|
|
key = fmt.Sprintf("%s%s%s", structs.NodeUniqueNamespace, attr, tag)
|
|
|
|
} else {
|
|
|
|
key = fmt.Sprintf("%s%s", attr, tag)
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Attributes[key] = "true"
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var attrDict map[string]string
|
2015-10-05 18:13:25 +00:00
|
|
|
value, err = f.Get("attributes/", true)
|
2015-10-05 05:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, checkError(err, f.logger, "attributes/")
|
|
|
|
}
|
2015-10-07 17:39:23 +00:00
|
|
|
if err := json.Unmarshal([]byte(value), &attrDict); err != nil {
|
|
|
|
f.logger.Printf("[WARN] fingerprint.env_gce: Error decoding instance attributes: %s", err.Error())
|
2015-10-12 21:56:33 +00:00
|
|
|
}
|
|
|
|
for k, v := range attrDict {
|
2016-01-23 02:12:16 +00:00
|
|
|
attr := "platform.gce.attr."
|
|
|
|
var key string
|
|
|
|
|
|
|
|
// If the key is namespaced as unique, we strip it from the
|
|
|
|
// key and prepend to the whole attribute.
|
|
|
|
if structs.IsUniqueNamespace(k) {
|
|
|
|
k = strings.TrimPrefix(k, structs.NodeUniqueNamespace)
|
|
|
|
key = fmt.Sprintf("%s%s%s", structs.NodeUniqueNamespace, attr, k)
|
|
|
|
} else {
|
|
|
|
key = fmt.Sprintf("%s%s", attr, k)
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Attributes[key] = strings.Trim(v, "\n")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// populate Links
|
2016-01-23 02:12:16 +00:00
|
|
|
node.Links["gce"] = node.Attributes["unique.platform.gce.id"]
|
2015-10-05 05:37:45 +00:00
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *EnvGCEFingerprint) isGCE() bool {
|
|
|
|
// TODO: better way to detect GCE?
|
|
|
|
|
|
|
|
// Query the metadata url for the machine type, to verify we're on GCE
|
2015-10-05 18:13:25 +00:00
|
|
|
machineType, err := f.Get("machine-type", false)
|
2015-10-05 05:37:45 +00:00
|
|
|
if err != nil {
|
|
|
|
if re, ok := err.(ReqError); !ok || re.StatusCode != 404 {
|
|
|
|
// If it wasn't a 404 error, print an error message.
|
2015-11-24 19:06:51 +00:00
|
|
|
f.logger.Printf("[DEBUG] fingerprint.env_gce: Error querying GCE Metadata URL, skipping")
|
2015-10-05 05:37:45 +00:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
match, err := regexp.MatchString("projects/.+/machineTypes/.+", machineType)
|
2016-06-16 21:32:07 +00:00
|
|
|
if err != nil || !match {
|
2015-10-05 05:37:45 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|