metadata.go 12 KB

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