metadata.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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 "cloud.google.com/go/compute/metadata"
  20. import (
  21. "encoding/json"
  22. "fmt"
  23. "io/ioutil"
  24. "net"
  25. "net/http"
  26. "net/url"
  27. "os"
  28. "runtime"
  29. "strings"
  30. "sync"
  31. "time"
  32. "golang.org/x/net/context"
  33. "golang.org/x/net/context/ctxhttp"
  34. )
  35. const (
  36. // metadataIP is the documented metadata server IP address.
  37. metadataIP = "169.254.169.254"
  38. // metadataHostEnv is the environment variable specifying the
  39. // GCE metadata hostname. If empty, the default value of
  40. // metadataIP ("169.254.169.254") is used instead.
  41. // This is variable name is not defined by any spec, as far as
  42. // I know; it was made up for the Go package.
  43. metadataHostEnv = "GCE_METADATA_HOST"
  44. userAgent = "gcloud-golang/0.1"
  45. )
  46. type cachedValue struct {
  47. k string
  48. trim bool
  49. mu sync.Mutex
  50. v string
  51. }
  52. var (
  53. projID = &cachedValue{k: "project/project-id", trim: true}
  54. projNum = &cachedValue{k: "project/numeric-project-id", trim: true}
  55. instID = &cachedValue{k: "instance/id", trim: true}
  56. )
  57. var (
  58. metaClient = &http.Client{
  59. Transport: &http.Transport{
  60. Dial: (&net.Dialer{
  61. Timeout: 2 * time.Second,
  62. KeepAlive: 30 * time.Second,
  63. }).Dial,
  64. ResponseHeaderTimeout: 2 * time.Second,
  65. },
  66. }
  67. subscribeClient = &http.Client{
  68. Transport: &http.Transport{
  69. Dial: (&net.Dialer{
  70. Timeout: 2 * time.Second,
  71. KeepAlive: 30 * time.Second,
  72. }).Dial,
  73. },
  74. }
  75. )
  76. // NotDefinedError is returned when requested metadata is not defined.
  77. //
  78. // The underlying string is the suffix after "/computeMetadata/v1/".
  79. //
  80. // This error is not returned if the value is defined to be the empty
  81. // string.
  82. type NotDefinedError string
  83. func (suffix NotDefinedError) Error() string {
  84. return fmt.Sprintf("metadata: GCE metadata %q not defined", string(suffix))
  85. }
  86. // Get returns a value from the metadata service.
  87. // The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
  88. //
  89. // If the GCE_METADATA_HOST environment variable is not defined, a default of
  90. // 169.254.169.254 will be used instead.
  91. //
  92. // If the requested metadata is not defined, the returned error will
  93. // be of type NotDefinedError.
  94. func Get(suffix string) (string, error) {
  95. val, _, err := getETag(metaClient, suffix)
  96. return val, err
  97. }
  98. // getETag returns a value from the metadata service as well as the associated
  99. // ETag using the provided client. This func is otherwise equivalent to Get.
  100. func getETag(client *http.Client, suffix string) (value, etag string, err error) {
  101. // Using a fixed IP makes it very difficult to spoof the metadata service in
  102. // a container, which is an important use-case for local testing of cloud
  103. // deployments. To enable spoofing of the metadata service, the environment
  104. // variable GCE_METADATA_HOST is first inspected to decide where metadata
  105. // requests shall go.
  106. host := os.Getenv(metadataHostEnv)
  107. if host == "" {
  108. // Using 169.254.169.254 instead of "metadata" here because Go
  109. // binaries built with the "netgo" tag and without cgo won't
  110. // know the search suffix for "metadata" is
  111. // ".google.internal", and this IP address is documented as
  112. // being stable anyway.
  113. host = metadataIP
  114. }
  115. url := "http://" + host + "/computeMetadata/v1/" + suffix
  116. req, _ := http.NewRequest("GET", url, nil)
  117. req.Header.Set("Metadata-Flavor", "Google")
  118. req.Header.Set("User-Agent", userAgent)
  119. res, err := client.Do(req)
  120. if err != nil {
  121. return "", "", err
  122. }
  123. defer res.Body.Close()
  124. if res.StatusCode == http.StatusNotFound {
  125. return "", "", NotDefinedError(suffix)
  126. }
  127. if res.StatusCode != 200 {
  128. return "", "", fmt.Errorf("status code %d trying to fetch %s", res.StatusCode, url)
  129. }
  130. all, err := ioutil.ReadAll(res.Body)
  131. if err != nil {
  132. return "", "", err
  133. }
  134. return string(all), res.Header.Get("Etag"), nil
  135. }
  136. func getTrimmed(suffix string) (s string, err error) {
  137. s, err = Get(suffix)
  138. s = strings.TrimSpace(s)
  139. return
  140. }
  141. func (c *cachedValue) get() (v string, err error) {
  142. defer c.mu.Unlock()
  143. c.mu.Lock()
  144. if c.v != "" {
  145. return c.v, nil
  146. }
  147. if c.trim {
  148. v, err = getTrimmed(c.k)
  149. } else {
  150. v, err = Get(c.k)
  151. }
  152. if err == nil {
  153. c.v = v
  154. }
  155. return
  156. }
  157. var (
  158. onGCEOnce sync.Once
  159. onGCE bool
  160. )
  161. // OnGCE reports whether this process is running on Google Compute Engine.
  162. func OnGCE() bool {
  163. onGCEOnce.Do(initOnGCE)
  164. return onGCE
  165. }
  166. func initOnGCE() {
  167. onGCE = testOnGCE()
  168. }
  169. func testOnGCE() bool {
  170. // The user explicitly said they're on GCE, so trust them.
  171. if os.Getenv(metadataHostEnv) != "" {
  172. return true
  173. }
  174. ctx, cancel := context.WithCancel(context.Background())
  175. defer cancel()
  176. resc := make(chan bool, 2)
  177. // Try two strategies in parallel.
  178. // See https://github.com/GoogleCloudPlatform/google-cloud-go/issues/194
  179. go func() {
  180. req, _ := http.NewRequest("GET", "http://"+metadataIP, nil)
  181. req.Header.Set("User-Agent", userAgent)
  182. res, err := ctxhttp.Do(ctx, metaClient, req)
  183. if err != nil {
  184. resc <- false
  185. return
  186. }
  187. defer res.Body.Close()
  188. resc <- res.Header.Get("Metadata-Flavor") == "Google"
  189. }()
  190. go func() {
  191. addrs, err := net.LookupHost("metadata.google.internal")
  192. if err != nil || len(addrs) == 0 {
  193. resc <- false
  194. return
  195. }
  196. resc <- strsContains(addrs, metadataIP)
  197. }()
  198. tryHarder := systemInfoSuggestsGCE()
  199. if tryHarder {
  200. res := <-resc
  201. if res {
  202. // The first strategy succeeded, so let's use it.
  203. return true
  204. }
  205. // Wait for either the DNS or metadata server probe to
  206. // contradict the other one and say we are running on
  207. // GCE. Give it a lot of time to do so, since the system
  208. // info already suggests we're running on a GCE BIOS.
  209. timer := time.NewTimer(5 * time.Second)
  210. defer timer.Stop()
  211. select {
  212. case res = <-resc:
  213. return res
  214. case <-timer.C:
  215. // Too slow. Who knows what this system is.
  216. return false
  217. }
  218. }
  219. // There's no hint from the system info that we're running on
  220. // GCE, so use the first probe's result as truth, whether it's
  221. // true or false. The goal here is to optimize for speed for
  222. // users who are NOT running on GCE. We can't assume that
  223. // either a DNS lookup or an HTTP request to a blackholed IP
  224. // address is fast. Worst case this should return when the
  225. // metaClient's Transport.ResponseHeaderTimeout or
  226. // Transport.Dial.Timeout fires (in two seconds).
  227. return <-resc
  228. }
  229. // systemInfoSuggestsGCE reports whether the local system (without
  230. // doing network requests) suggests that we're running on GCE. If this
  231. // returns true, testOnGCE tries a bit harder to reach its metadata
  232. // server.
  233. func systemInfoSuggestsGCE() bool {
  234. if runtime.GOOS != "linux" {
  235. // We don't have any non-Linux clues available, at least yet.
  236. return false
  237. }
  238. slurp, _ := ioutil.ReadFile("/sys/class/dmi/id/product_name")
  239. name := strings.TrimSpace(string(slurp))
  240. return name == "Google" || name == "Google Compute Engine"
  241. }
  242. // Subscribe subscribes to a value from the metadata service.
  243. // The suffix is appended to "http://${GCE_METADATA_HOST}/computeMetadata/v1/".
  244. // The suffix may contain query parameters.
  245. //
  246. // Subscribe calls fn with the latest metadata value indicated by the provided
  247. // suffix. If the metadata value is deleted, fn is called with the empty string
  248. // and ok false. Subscribe blocks until fn returns a non-nil error or the value
  249. // is deleted. Subscribe returns the error value returned from the last call to
  250. // fn, which may be nil when ok == false.
  251. func Subscribe(suffix string, fn func(v string, ok bool) error) error {
  252. const failedSubscribeSleep = time.Second * 5
  253. // First check to see if the metadata value exists at all.
  254. val, lastETag, err := getETag(subscribeClient, suffix)
  255. if err != nil {
  256. return err
  257. }
  258. if err := fn(val, true); err != nil {
  259. return err
  260. }
  261. ok := true
  262. if strings.ContainsRune(suffix, '?') {
  263. suffix += "&wait_for_change=true&last_etag="
  264. } else {
  265. suffix += "?wait_for_change=true&last_etag="
  266. }
  267. for {
  268. val, etag, err := getETag(subscribeClient, suffix+url.QueryEscape(lastETag))
  269. if err != nil {
  270. if _, deleted := err.(NotDefinedError); !deleted {
  271. time.Sleep(failedSubscribeSleep)
  272. continue // Retry on other errors.
  273. }
  274. ok = false
  275. }
  276. lastETag = etag
  277. if err := fn(val, ok); err != nil || !ok {
  278. return err
  279. }
  280. }
  281. }
  282. // ProjectID returns the current instance's project ID string.
  283. func ProjectID() (string, error) { return projID.get() }
  284. // NumericProjectID returns the current instance's numeric project ID.
  285. func NumericProjectID() (string, error) { return projNum.get() }
  286. // InternalIP returns the instance's primary internal IP address.
  287. func InternalIP() (string, error) {
  288. return getTrimmed("instance/network-interfaces/0/ip")
  289. }
  290. // ExternalIP returns the instance's primary external (public) IP address.
  291. func ExternalIP() (string, error) {
  292. return getTrimmed("instance/network-interfaces/0/access-configs/0/external-ip")
  293. }
  294. // Hostname returns the instance's hostname. This will be of the form
  295. // "<instanceID>.c.<projID>.internal".
  296. func Hostname() (string, error) {
  297. return getTrimmed("instance/hostname")
  298. }
  299. // InstanceTags returns the list of user-defined instance tags,
  300. // assigned when initially creating a GCE instance.
  301. func InstanceTags() ([]string, error) {
  302. var s []string
  303. j, err := Get("instance/tags")
  304. if err != nil {
  305. return nil, err
  306. }
  307. if err := json.NewDecoder(strings.NewReader(j)).Decode(&s); err != nil {
  308. return nil, err
  309. }
  310. return s, nil
  311. }
  312. // InstanceID returns the current VM's numeric instance ID.
  313. func InstanceID() (string, error) {
  314. return instID.get()
  315. }
  316. // InstanceName returns the current VM's instance ID string.
  317. func InstanceName() (string, error) {
  318. host, err := Hostname()
  319. if err != nil {
  320. return "", err
  321. }
  322. return strings.Split(host, ".")[0], nil
  323. }
  324. // Zone returns the current VM's zone, such as "us-central1-b".
  325. func Zone() (string, error) {
  326. zone, err := getTrimmed("instance/zone")
  327. // zone is of the form "projects/<projNum>/zones/<zoneName>".
  328. if err != nil {
  329. return "", err
  330. }
  331. return zone[strings.LastIndex(zone, "/")+1:], nil
  332. }
  333. // InstanceAttributes returns the list of user-defined attributes,
  334. // assigned when initially creating a GCE VM instance. The value of an
  335. // attribute can be obtained with InstanceAttributeValue.
  336. func InstanceAttributes() ([]string, error) { return lines("instance/attributes/") }
  337. // ProjectAttributes returns the list of user-defined attributes
  338. // applying to the project as a whole, not just this VM. The value of
  339. // an attribute can be obtained with ProjectAttributeValue.
  340. func ProjectAttributes() ([]string, error) { return lines("project/attributes/") }
  341. func lines(suffix string) ([]string, error) {
  342. j, err := Get(suffix)
  343. if err != nil {
  344. return nil, err
  345. }
  346. s := strings.Split(strings.TrimSpace(j), "\n")
  347. for i := range s {
  348. s[i] = strings.TrimSpace(s[i])
  349. }
  350. return s, nil
  351. }
  352. // InstanceAttributeValue returns the value of the provided VM
  353. // instance attribute.
  354. //
  355. // If the requested attribute is not defined, the returned error will
  356. // be of type NotDefinedError.
  357. //
  358. // InstanceAttributeValue may return ("", nil) if the attribute was
  359. // defined to be the empty string.
  360. func InstanceAttributeValue(attr string) (string, error) {
  361. return Get("instance/attributes/" + attr)
  362. }
  363. // ProjectAttributeValue returns the value of the provided
  364. // project attribute.
  365. //
  366. // If the requested attribute is not defined, the returned error will
  367. // be of type NotDefinedError.
  368. //
  369. // ProjectAttributeValue may return ("", nil) if the attribute was
  370. // defined to be the empty string.
  371. func ProjectAttributeValue(attr string) (string, error) {
  372. return Get("project/attributes/" + attr)
  373. }
  374. // Scopes returns the service account scopes for the given account.
  375. // The account may be empty or the string "default" to use the instance's
  376. // main account.
  377. func Scopes(serviceAccount string) ([]string, error) {
  378. if serviceAccount == "" {
  379. serviceAccount = "default"
  380. }
  381. return lines("instance/service-accounts/" + serviceAccount + "/scopes")
  382. }
  383. func strsContains(ss []string, s string) bool {
  384. for _, v := range ss {
  385. if v == s {
  386. return true
  387. }
  388. }
  389. return false
  390. }