metadata.go 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright 2014 Google Inc. All Rights Reserved.
  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 metadata provides access to Google Compute Engine (GCE)
  15. // metadata and API service accounts.
  16. //
  17. // This package is a wrapper around the GCE metadata service,
  18. // as documented at https://developers.google.com/compute/docs/metadata.
  19. package metadata // import "google.golang.org/cloud/compute/metadata"
  20. import (
  21. "encoding/json"
  22. "fmt"
  23. "io/ioutil"
  24. "net"
  25. "net/http"
  26. "net/url"
  27. "os"
  28. "strings"
  29. "sync"
  30. "time"
  31. "google.golang.org/cloud/internal"
  32. )
  33. type cachedValue struct {
  34. k string
  35. trim bool
  36. mu sync.Mutex
  37. v string
  38. }
  39. var (
  40. projID = &cachedValue{k: "project/project-id", trim: true}
  41. projNum = &cachedValue{k: "project/numeric-project-id", trim: true}
  42. instID = &cachedValue{k: "instance/id", trim: true}
  43. )
  44. var metaClient = &http.Client{
  45. Transport: &internal.Transport{
  46. Base: &http.Transport{
  47. Dial: (&net.Dialer{
  48. Timeout: 750 * time.Millisecond,
  49. KeepAlive: 30 * time.Second,
  50. }).Dial,
  51. ResponseHeaderTimeout: 750 * time.Millisecond,
  52. },
  53. },
  54. }
  55. // NotDefinedError is returned when requested metadata is not defined.
  56. //
  57. // The underlying string is the suffix after "/computeMetadata/v1/".
  58. //
  59. // This error is not returned if the value is defined to be the empty
  60. // string.
  61. type NotDefinedError string
  62. func (suffix NotDefinedError) Error() string {
  63. return fmt.Sprintf("metadata: GCE metadata %q not defined", string(suffix))
  64. }
  65. // Get returns a value from the metadata service.
  66. // The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
  67. //
  68. // If the GCE_METADATA_HOST environment variable is not defined, a default of
  69. // 169.254.169.254 will be used instead.
  70. //
  71. // If the requested metadata is not defined, the returned error will
  72. // be of type NotDefinedError.
  73. func Get(suffix string) (string, error) {
  74. val, _, err := getETag(suffix)
  75. return val, err
  76. }
  77. // getETag returns a value from the metadata service as well as the associated
  78. // ETag. This func is otherwise equivalent to Get.
  79. func getETag(suffix string) (value, etag string, err error) {
  80. // Using a fixed IP makes it very difficult to spoof the metadata service in
  81. // a container, which is an important use-case for local testing of cloud
  82. // deployments. To enable spoofing of the metadata service, the environment
  83. // variable GCE_METADATA_HOST is first inspected to decide where metadata
  84. // requests shall go.
  85. host := os.Getenv("GCE_METADATA_HOST")
  86. if host == "" {
  87. // Using 169.254.169.254 instead of "metadata" here because Go
  88. // binaries built with the "netgo" tag and without cgo won't
  89. // know the search suffix for "metadata" is
  90. // ".google.internal", and this IP address is documented as
  91. // being stable anyway.
  92. host = "169.254.169.254"
  93. }
  94. url := "http://" + host + "/computeMetadata/v1/" + suffix
  95. req, _ := http.NewRequest("GET", url, nil)
  96. req.Header.Set("Metadata-Flavor", "Google")
  97. res, err := metaClient.Do(req)
  98. if err != nil {
  99. return "", "", err
  100. }
  101. defer res.Body.Close()
  102. if res.StatusCode == http.StatusNotFound {
  103. return "", "", NotDefinedError(suffix)
  104. }
  105. if res.StatusCode != 200 {
  106. return "", "", fmt.Errorf("status code %d trying to fetch %s", res.StatusCode, url)
  107. }
  108. all, err := ioutil.ReadAll(res.Body)
  109. if err != nil {
  110. return "", "", err
  111. }
  112. return string(all), res.Header.Get("Etag"), nil
  113. }
  114. func getTrimmed(suffix string) (s string, err error) {
  115. s, err = Get(suffix)
  116. s = strings.TrimSpace(s)
  117. return
  118. }
  119. func (c *cachedValue) get() (v string, err error) {
  120. defer c.mu.Unlock()
  121. c.mu.Lock()
  122. if c.v != "" {
  123. return c.v, nil
  124. }
  125. if c.trim {
  126. v, err = getTrimmed(c.k)
  127. } else {
  128. v, err = Get(c.k)
  129. }
  130. if err == nil {
  131. c.v = v
  132. }
  133. return
  134. }
  135. var onGCE struct {
  136. sync.Mutex
  137. set bool
  138. v bool
  139. }
  140. // OnGCE reports whether this process is running on Google Compute Engine.
  141. func OnGCE() bool {
  142. defer onGCE.Unlock()
  143. onGCE.Lock()
  144. if onGCE.set {
  145. return onGCE.v
  146. }
  147. onGCE.set = true
  148. // We use the DNS name of the metadata service here instead of the IP address
  149. // because we expect that to fail faster in the not-on-GCE case.
  150. res, err := metaClient.Get("http://metadata.google.internal")
  151. if err != nil {
  152. return false
  153. }
  154. onGCE.v = res.Header.Get("Metadata-Flavor") == "Google"
  155. return onGCE.v
  156. }
  157. // Subscribe subscribes to a value from the metadata service.
  158. // The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
  159. // The suffix may contain query parameters.
  160. //
  161. // Subscribe calls fn with the latest metadata value indicated by the provided
  162. // suffix. If the metadata value is deleted, fn is called with the empty string
  163. // and ok false. Subscribe blocks until fn returns a non-nil error or the value
  164. // is deleted. Subscribe returns the error value returned from the last call to
  165. // fn, which may be nil when ok == false.
  166. func Subscribe(suffix string, fn func(v string, ok bool) error) error {
  167. const failedSubscribeSleep = time.Second * 5
  168. // First check to see if the metadata value exists at all.
  169. val, lastETag, err := getETag(suffix)
  170. if err != nil {
  171. return err
  172. }
  173. if err := fn(val, true); err != nil {
  174. return err
  175. }
  176. ok := true
  177. if strings.ContainsRune(suffix, '?') {
  178. suffix += "&wait_for_change=true&last_etag="
  179. } else {
  180. suffix += "?wait_for_change=true&last_etag="
  181. }
  182. for {
  183. val, etag, err := getETag(suffix + url.QueryEscape(lastETag))
  184. if err != nil {
  185. if _, deleted := err.(NotDefinedError); !deleted {
  186. time.Sleep(failedSubscribeSleep)
  187. continue // Retry on other errors.
  188. }
  189. ok = false
  190. }
  191. lastETag = etag
  192. if err := fn(val, ok); err != nil || !ok {
  193. return err
  194. }
  195. }
  196. }
  197. // ProjectID returns the current instance's project ID string.
  198. func ProjectID() (string, error) { return projID.get() }
  199. // NumericProjectID returns the current instance's numeric project ID.
  200. func NumericProjectID() (string, error) { return projNum.get() }
  201. // InternalIP returns the instance's primary internal IP address.
  202. func InternalIP() (string, error) {
  203. return getTrimmed("instance/network-interfaces/0/ip")
  204. }
  205. // ExternalIP returns the instance's primary external (public) IP address.
  206. func ExternalIP() (string, error) {
  207. return getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")
  208. }
  209. // Hostname returns the instance's hostname. This will be of the form
  210. // "<instanceID>.c.<projID>.internal".
  211. func Hostname() (string, error) {
  212. return getTrimmed("instance/hostname")
  213. }
  214. // InstanceTags returns the list of user-defined instance tags,
  215. // assigned when initially creating a GCE instance.
  216. func InstanceTags() ([]string, error) {
  217. var s []string
  218. j, err := Get("instance/tags")
  219. if err != nil {
  220. return nil, err
  221. }
  222. if err := json.NewDecoder(strings.NewReader(j)).Decode(&s); err != nil {
  223. return nil, err
  224. }
  225. return s, nil
  226. }
  227. // InstanceID returns the current VM's numeric instance ID.
  228. func InstanceID() (string, error) {
  229. return instID.get()
  230. }
  231. // InstanceName returns the current VM's instance ID string.
  232. func InstanceName() (string, error) {
  233. host, err := Hostname()
  234. if err != nil {
  235. return "", err
  236. }
  237. return strings.Split(host, ".")[0], nil
  238. }
  239. // Zone returns the current VM's zone, such as "us-central1-b".
  240. func Zone() (string, error) {
  241. zone, err := getTrimmed("instance/zone")
  242. // zone is of the form "projects/<projNum>/zones/<zoneName>".
  243. if err != nil {
  244. return "", err
  245. }
  246. return zone[strings.LastIndex(zone, "/")+1:], nil
  247. }
  248. // InstanceAttributes returns the list of user-defined attributes,
  249. // assigned when initially creating a GCE VM instance. The value of an
  250. // attribute can be obtained with InstanceAttributeValue.
  251. func InstanceAttributes() ([]string, error) { return lines("instance/attributes/") }
  252. // ProjectAttributes returns the list of user-defined attributes
  253. // applying to the project as a whole, not just this VM. The value of
  254. // an attribute can be obtained with ProjectAttributeValue.
  255. func ProjectAttributes() ([]string, error) { return lines("project/attributes/") }
  256. func lines(suffix string) ([]string, error) {
  257. j, err := Get(suffix)
  258. if err != nil {
  259. return nil, err
  260. }
  261. s := strings.Split(strings.TrimSpace(j), "\n")
  262. for i := range s {
  263. s[i] = strings.TrimSpace(s[i])
  264. }
  265. return s, nil
  266. }
  267. // InstanceAttributeValue returns the value of the provided VM
  268. // instance attribute.
  269. //
  270. // If the requested attribute is not defined, the returned error will
  271. // be of type NotDefinedError.
  272. //
  273. // InstanceAttributeValue may return ("", nil) if the attribute was
  274. // defined to be the empty string.
  275. func InstanceAttributeValue(attr string) (string, error) {
  276. return Get("instance/attributes/" + attr)
  277. }
  278. // ProjectAttributeValue returns the value of the provided
  279. // project attribute.
  280. //
  281. // If the requested attribute is not defined, the returned error will
  282. // be of type NotDefinedError.
  283. //
  284. // ProjectAttributeValue may return ("", nil) if the attribute was
  285. // defined to be the empty string.
  286. func ProjectAttributeValue(attr string) (string, error) {
  287. return Get("project/attributes/" + attr)
  288. }
  289. // Scopes returns the service account scopes for the given account.
  290. // The account may be empty or the string "default" to use the instance's
  291. // main account.
  292. func Scopes(serviceAccount string) ([]string, error) {
  293. if serviceAccount == "" {
  294. serviceAccount = "default"
  295. }
  296. return lines("instance/service-accounts/" + serviceAccount + "/scopes")
  297. }