metadataService: Check both ec2 and openstack urls more explicitly

Remove the root url parameter for -from-metadata-service since this
is a guarenteed value. Additionally, check for both ec2 and openstack
urls for the metadata and userdata. Fix a bug with the -from-url
option and a panic on an empty response.
This commit is contained in:
Alex Crawford 2014-06-24 17:56:05 -07:00
parent a5dec7d7bd
commit fae81c78f3
3 changed files with 50 additions and 35 deletions

View File

@ -16,11 +16,11 @@ var (
printVersion bool printVersion bool
ignoreFailure bool ignoreFailure bool
sources struct { sources struct {
file string file string
configDrive string configDrive string
metadataService string metadataService bool
url string url string
procCmdLine bool procCmdLine bool
} }
convertNetconf string convertNetconf string
workspace string workspace string
@ -32,7 +32,7 @@ func init() {
flag.BoolVar(&ignoreFailure, "ignore-failure", false, "Exits with 0 status in the event of malformed input from user-data") flag.BoolVar(&ignoreFailure, "ignore-failure", false, "Exits with 0 status in the event of malformed input from user-data")
flag.StringVar(&sources.file, "from-file", "", "Read user-data from provided file") flag.StringVar(&sources.file, "from-file", "", "Read user-data from provided file")
flag.StringVar(&sources.configDrive, "from-configdrive", "", "Read data from provided cloud-drive directory") flag.StringVar(&sources.configDrive, "from-configdrive", "", "Read data from provided cloud-drive directory")
flag.StringVar(&sources.metadataService, "from-metadata-service", "", "Download data from provided url") flag.BoolVar(&sources.metadataService, "from-metadata-service", false, "Download data from metadata service")
flag.StringVar(&sources.url, "from-url", "", "Download user-data from provided url") flag.StringVar(&sources.url, "from-url", "", "Download user-data from provided url")
flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=<url>', using the cloud-config served by an HTTP GET to <url>", datasource.ProcCmdlineLocation, datasource.ProcCmdlineCloudConfigFlag)) flag.BoolVar(&sources.procCmdLine, "from-proc-cmdline", false, fmt.Sprintf("Parse %s for '%s=<url>', using the cloud-config served by an HTTP GET to <url>", datasource.ProcCmdlineLocation, datasource.ProcCmdlineCloudConfigFlag))
flag.StringVar(&convertNetconf, "convert-netconf", "", "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files (requires the -from-configdrive flag)") flag.StringVar(&convertNetconf, "convert-netconf", "", "Read the network config provided in cloud-drive and translate it from the specified format into networkd unit files (requires the -from-configdrive flag)")
@ -55,7 +55,7 @@ func main() {
os.Exit(0) os.Exit(0)
} }
if convertNetconf != "" && sources.configDrive == "" && sources.metadataService == "" { if convertNetconf != "" && sources.configDrive == "" && !sources.metadataService {
fmt.Println("-convert-netconf flag requires -from-configdrive or -from-metadata-service") fmt.Println("-convert-netconf flag requires -from-configdrive or -from-metadata-service")
os.Exit(1) os.Exit(1)
} }
@ -127,15 +127,15 @@ func getDatasource() datasource.Datasource {
n++ n++
} }
if sources.url != "" { if sources.url != "" {
ds = datasource.NewMetadataService(sources.url) ds = datasource.NewRemoteFile(sources.url)
n++ n++
} }
if sources.configDrive != "" { if sources.configDrive != "" {
ds = datasource.NewConfigDrive(sources.configDrive) ds = datasource.NewConfigDrive(sources.configDrive)
n++ n++
} }
if sources.metadataService != "" { if sources.metadataService {
ds = datasource.NewMetadataService(sources.metadataService) ds = datasource.NewMetadataService()
n++ n++
} }
if sources.procCmdLine { if sources.procCmdLine {

View File

@ -9,23 +9,34 @@ import (
"github.com/coreos/coreos-cloudinit/pkg" "github.com/coreos/coreos-cloudinit/pkg"
) )
// metadataService retrieves metadata from either an OpenStack[1] or EC2[2] compatible endpoint. // metadataService retrieves metadata from either an OpenStack[1] (2012-08-10)
// It will first attempt to directly retrieve a JSON blob from the OpenStack endpoint. If that // or EC2[2] (2009-04-04) compatible endpoint. It will first attempt to
// fails with a 404, it then attempts to retrieve metadata bit-by-bit from the EC2 endpoint, // directly retrieve a JSON blob from the OpenStack endpoint. If that fails
// and populates that into an equivalent JSON blob. // with a 404, it then attempts to retrieve metadata bit-by-bit from the EC2
// endpoint, and populates that into an equivalent JSON blob. metadataService
// also checks for userdata from EC2 and, if that fails with a 404, OpenStack.
// //
// [1] http://docs.openstack.org/grizzly/openstack-compute/admin/content/metadata-service.html // [1] http://docs.openstack.org/grizzly/openstack-compute/admin/content/metadata-service.html
// [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories // [2] http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AESDG-chapter-instancedata.html#instancedata-data-categories
type metadataService struct {
url string const (
} BaseUrl = "http://169.254.169.254/"
Ec2ApiVersion = "2009-04-04"
Ec2UserdataUrl = BaseUrl + Ec2ApiVersion + "/user-data"
Ec2MetadataUrl = BaseUrl + Ec2ApiVersion + "/meta-data/"
OpenstackApiVersion = "openstack/2012-08-10"
OpenstackUserdataUrl = BaseUrl + OpenstackApiVersion + "/user_data"
OpenstackMetadataUrl = BaseUrl + OpenstackApiVersion + "/meta_data.json"
)
type metadataService struct{}
type getter interface { type getter interface {
Get(string) ([]byte, error) Get(string) ([]byte, error)
} }
func NewMetadataService(url string) *metadataService { func NewMetadataService() *metadataService {
return &metadataService{strings.TrimSuffix(url, "/")} return &metadataService{}
} }
func (ms *metadataService) ConfigRoot() string { func (ms *metadataService) ConfigRoot() string {
@ -33,27 +44,31 @@ func (ms *metadataService) ConfigRoot() string {
} }
func (ms *metadataService) FetchMetadata() ([]byte, error) { func (ms *metadataService) FetchMetadata() ([]byte, error) {
client := pkg.NewHttpClient() return fetchMetadata(pkg.NewHttpClient())
return fetchMetadata(client, ms.url)
} }
func (ms *metadataService) FetchUserdata() ([]byte, error) { func (ms *metadataService) FetchUserdata() ([]byte, error) {
client := pkg.NewHttpClient() client := pkg.NewHttpClient()
return client.Get(ms.url + "/latest/user-data") if data, err := client.Get(Ec2UserdataUrl); err == nil {
return data, err
} else if _, ok := err.(pkg.ErrTimeout); ok {
return data, err
}
return client.Get(OpenstackUserdataUrl)
} }
func (ms *metadataService) Type() string { func (ms *metadataService) Type() string {
return "metadata-service" return "metadata-service"
} }
func fetchMetadata(client getter, url string) ([]byte, error) { func fetchMetadata(client getter) ([]byte, error) {
if metadata, err := client.Get(url + "/latest/meta-data.json"); err == nil { if metadata, err := client.Get(OpenstackMetadataUrl); err == nil {
return metadata, nil return metadata, nil
} else if _, ok := err.(pkg.ErrTimeout); ok { } else if _, ok := err.(pkg.ErrTimeout); ok {
return nil, err return nil, err
} }
attrs, err := fetchChildAttributes(client, url+"/latest/meta-data/") attrs, err := fetchChildAttributes(client, Ec2MetadataUrl)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -74,7 +89,7 @@ func fetchAttributes(client getter, url string) ([]string, error) {
} }
func fetchAttribute(client getter, url string) (interface{}, error) { func fetchAttribute(client getter, url string) (interface{}, error) {
if attrs, err := fetchAttributes(client, url); err == nil { if attrs, err := fetchAttributes(client, url); err == nil && len(attrs) > 0 {
return attrs[0], nil return attrs[0], nil
} else { } else {
return "", err return "", err

View File

@ -137,19 +137,19 @@ func TestFetchMetadata(t *testing.T) {
}{ }{
{ {
metadata: map[string]string{ metadata: map[string]string{
"/latest/meta-data/": "a\nb\nc/", "http://169.254.169.254/2009-04-04/meta-data/": "a\nb\nc/",
"/latest/meta-data/c/": "d\ne/", "http://169.254.169.254/2009-04-04/meta-data/c/": "d\ne/",
"/latest/meta-data/c/e/": "f", "http://169.254.169.254/2009-04-04/meta-data/c/e/": "f",
"/latest/meta-data/a": "1", "http://169.254.169.254/2009-04-04/meta-data/a": "1",
"/latest/meta-data/b": "2", "http://169.254.169.254/2009-04-04/meta-data/b": "2",
"/latest/meta-data/c/d": "3", "http://169.254.169.254/2009-04-04/meta-data/c/d": "3",
"/latest/meta-data/c/e/f": "4", "http://169.254.169.254/2009-04-04/meta-data/c/e/f": "4",
}, },
expect: []byte(`{"a":"1","b":"2","c":{"d":"3","e":{"f":"4"}}}`), expect: []byte(`{"a":"1","b":"2","c":{"d":"3","e":{"f":"4"}}}`),
}, },
{ {
metadata: map[string]string{ metadata: map[string]string{
"/latest/meta-data.json": "test", "http://169.254.169.254/openstack/2012-08-10/meta_data.json": "test",
}, },
expect: []byte("test"), expect: []byte("test"),
}, },
@ -157,7 +157,7 @@ func TestFetchMetadata(t *testing.T) {
{err: pkg.ErrNotFound{fmt.Errorf("test error")}}, {err: pkg.ErrNotFound{fmt.Errorf("test error")}},
} { } {
client := &TestHttpClient{tt.metadata, tt.err} client := &TestHttpClient{tt.metadata, tt.err}
metadata, err := fetchMetadata(client, "") metadata, err := fetchMetadata(client)
if err != tt.err { if err != tt.err {
t.Fatalf("bad error (%q): want %q, got %q", tt.metadata, tt.err, err) t.Fatalf("bad error (%q): want %q, got %q", tt.metadata, tt.err, err)
} }