2015-01-24 19:32:33 -08:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-17 15:36:22 -07:00
|
|
|
|
2014-07-30 14:23:10 -07:00
|
|
|
package ec2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2015-01-22 17:35:39 -08:00
|
|
|
"net"
|
2014-07-30 14:23:10 -07:00
|
|
|
"strings"
|
|
|
|
|
2015-01-22 17:35:39 -08:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource"
|
2014-08-15 18:13:37 -07:00
|
|
|
"github.com/coreos/coreos-cloudinit/datasource/metadata"
|
2014-07-30 14:23:10 -07:00
|
|
|
"github.com/coreos/coreos-cloudinit/pkg"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DefaultAddress = "http://169.254.169.254/"
|
2014-09-24 20:33:29 +03:00
|
|
|
apiVersion = "2009-04-04/"
|
|
|
|
userdataPath = apiVersion + "user-data"
|
|
|
|
metadataPath = apiVersion + "meta-data"
|
2014-07-30 14:23:10 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type metadataService struct {
|
2014-08-15 18:13:37 -07:00
|
|
|
metadata.MetadataService
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatasource(root string) *metadataService {
|
2014-08-15 18:13:37 -07:00
|
|
|
return &metadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath)}
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
2015-01-22 17:35:39 -08:00
|
|
|
func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
|
|
|
|
metadata := datasource.Metadata{}
|
|
|
|
|
2014-08-15 18:13:37 -07:00
|
|
|
if keynames, err := ms.fetchAttributes(fmt.Sprintf("%s/public-keys", ms.MetadataUrl())); err == nil {
|
2014-07-30 14:23:10 -07:00
|
|
|
keyIDs := make(map[string]string)
|
|
|
|
for _, keyname := range keynames {
|
|
|
|
tokens := strings.SplitN(keyname, "=", 2)
|
|
|
|
if len(tokens) != 2 {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, fmt.Errorf("malformed public key: %q", keyname)
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
keyIDs[tokens[1]] = tokens[0]
|
|
|
|
}
|
|
|
|
|
2015-01-22 17:35:39 -08:00
|
|
|
metadata.SSHPublicKeys = map[string]string{}
|
2014-07-30 14:23:10 -07:00
|
|
|
for name, id := range keyIDs {
|
2014-08-15 18:13:37 -07:00
|
|
|
sshkey, err := ms.fetchAttribute(fmt.Sprintf("%s/public-keys/%s/openssh-key", ms.MetadataUrl(), id))
|
2014-07-30 14:23:10 -07:00
|
|
|
if err != nil {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, err
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
2015-01-22 17:35:39 -08:00
|
|
|
metadata.SSHPublicKeys[name] = sshkey
|
2014-07-30 14:23:10 -07:00
|
|
|
fmt.Printf("Found SSH key for %q\n", name)
|
|
|
|
}
|
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, err
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
2014-08-15 18:13:37 -07:00
|
|
|
if hostname, err := ms.fetchAttribute(fmt.Sprintf("%s/hostname", ms.MetadataUrl())); err == nil {
|
2015-01-22 17:35:39 -08:00
|
|
|
metadata.Hostname = strings.Split(hostname, " ")[0]
|
2014-07-30 14:23:10 -07:00
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, err
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
2014-08-15 18:13:37 -07:00
|
|
|
if localAddr, err := ms.fetchAttribute(fmt.Sprintf("%s/local-ipv4", ms.MetadataUrl())); err == nil {
|
2015-01-22 17:35:39 -08:00
|
|
|
metadata.PrivateIPv4 = net.ParseIP(localAddr)
|
2014-07-30 14:23:10 -07:00
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, err
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
2014-08-15 18:13:37 -07:00
|
|
|
if publicAddr, err := ms.fetchAttribute(fmt.Sprintf("%s/public-ipv4", ms.MetadataUrl())); err == nil {
|
2015-01-22 17:35:39 -08:00
|
|
|
metadata.PublicIPv4 = net.ParseIP(publicAddr)
|
2014-07-30 14:23:10 -07:00
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, err
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
2015-01-22 17:35:39 -08:00
|
|
|
if contentPath, err := ms.fetchAttribute(fmt.Sprintf("%s/network_config/content_path", ms.MetadataUrl())); err == nil {
|
|
|
|
metadata.NetworkConfigPath = contentPath
|
2014-07-30 14:23:10 -07:00
|
|
|
} else if _, ok := err.(pkg.ErrNotFound); !ok {
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, err
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
2015-01-22 17:35:39 -08:00
|
|
|
return metadata, nil
|
2014-07-30 14:23:10 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ms metadataService) Type() string {
|
|
|
|
return "ec2-metadata-service"
|
|
|
|
}
|
|
|
|
|
2014-08-15 18:13:37 -07:00
|
|
|
func (ms metadataService) fetchAttributes(url string) ([]string, error) {
|
|
|
|
resp, err := ms.FetchData(url)
|
2014-07-30 14:23:10 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(bytes.NewBuffer(resp))
|
|
|
|
data := make([]string, 0)
|
|
|
|
for scanner.Scan() {
|
|
|
|
data = append(data, scanner.Text())
|
|
|
|
}
|
|
|
|
return data, scanner.Err()
|
|
|
|
}
|
|
|
|
|
2014-08-15 18:13:37 -07:00
|
|
|
func (ms metadataService) fetchAttribute(url string) (string, error) {
|
|
|
|
if attrs, err := ms.fetchAttributes(url); err == nil && len(attrs) > 0 {
|
2014-07-30 14:23:10 -07:00
|
|
|
return attrs[0], nil
|
|
|
|
} else {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|