metadata.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2016 CoreOS, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package gce
  15. import (
  16. "fmt"
  17. "net"
  18. "net/http"
  19. "strconv"
  20. "strings"
  21. "github.com/coreos/coreos-cloudinit/datasource"
  22. "github.com/coreos/coreos-cloudinit/datasource/metadata"
  23. )
  24. const (
  25. apiVersion = "computeMetadata/v1/"
  26. metadataPath = apiVersion
  27. userdataPath = apiVersion + "instance/attributes/user-data"
  28. )
  29. type metadataService struct {
  30. metadata.MetadataService
  31. }
  32. func NewDatasource(root string) *metadataService {
  33. return &metadataService{metadata.NewDatasource(root, apiVersion, userdataPath, metadataPath, http.Header{"Metadata-Flavor": {"Google"}})}
  34. }
  35. func (ms metadataService) FetchMetadata() (datasource.Metadata, error) {
  36. public, err := ms.fetchIP("instance/network-interfaces/0/access-configs/0/external-ip")
  37. if err != nil {
  38. return datasource.Metadata{}, err
  39. }
  40. local, err := ms.fetchIP("instance/network-interfaces/0/ip")
  41. if err != nil {
  42. return datasource.Metadata{}, err
  43. }
  44. hostname, err := ms.fetchString("instance/hostname")
  45. if err != nil {
  46. return datasource.Metadata{}, err
  47. }
  48. projectSshKeys, err := ms.fetchString("project/attributes/sshKeys")
  49. if err != nil {
  50. return datasource.Metadata{}, err
  51. }
  52. instanceSshKeys, err := ms.fetchString("instance/attributes/sshKeys")
  53. if err != nil {
  54. return datasource.Metadata{}, err
  55. }
  56. keyStrings := strings.Split(projectSshKeys+"\n"+instanceSshKeys, "\n")
  57. sshPublicKeys := map[string]string{}
  58. i := 0
  59. for _, keyString := range keyStrings {
  60. keySlice := strings.SplitN(keyString, ":", 2)
  61. if len(keySlice) == 2 {
  62. key := strings.TrimSpace(keySlice[1])
  63. if key != "" {
  64. sshPublicKeys[strconv.Itoa(i)] = strings.TrimSpace(keySlice[1])
  65. i++
  66. }
  67. }
  68. }
  69. return datasource.Metadata{
  70. PublicIPv4: public,
  71. PrivateIPv4: local,
  72. Hostname: hostname,
  73. SSHPublicKeys: sshPublicKeys,
  74. }, nil
  75. }
  76. func (ms metadataService) Type() string {
  77. return "gce-metadata-service"
  78. }
  79. func (ms metadataService) fetchString(key string) (string, error) {
  80. data, err := ms.FetchData(ms.MetadataUrl() + key)
  81. if err != nil {
  82. return "", err
  83. }
  84. return string(data), nil
  85. }
  86. func (ms metadataService) fetchIP(key string) (net.IP, error) {
  87. str, err := ms.fetchString(key)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if str == "" {
  92. return nil, nil
  93. }
  94. if ip := net.ParseIP(str); ip != nil {
  95. return ip, nil
  96. } else {
  97. return nil, fmt.Errorf("couldn't parse %q as IP address", str)
  98. }
  99. }
  100. func (ms metadataService) FetchUserdata() ([]byte, error) {
  101. data, err := ms.FetchData(ms.UserdataUrl())
  102. if err != nil {
  103. return nil, err
  104. }
  105. if len(data) == 0 {
  106. data, err = ms.FetchData(ms.MetadataUrl() + "instance/attributes/startup-script")
  107. if err != nil {
  108. return nil, err
  109. }
  110. }
  111. return data, nil
  112. }