1
1
package fingerprint
2
2
3
3
import (
4
+ "fmt"
4
5
"io/ioutil"
5
6
"net/http"
6
7
"net/url"
@@ -17,6 +18,7 @@ import (
17
18
18
19
const (
19
20
// DigitalOceanMetadataURL is where the DigitalOcean metadata api normally resides.
21
+ // https://docs.digitalocean.com/products/droplets/how-to/retrieve-droplet-metadata/#how-to-retrieve-droplet-metadata
20
22
DigitalOceanMetadataURL = "http://169.254.169.254/metadata/v1/"
21
23
22
24
// DigitalOceanMetadataTimeout is the timeout used when contacting the DigitalOcean metadata
@@ -67,7 +69,7 @@ func (f *EnvDigitalOceanFingerprint) Get(attribute string, format string) (strin
67
69
}
68
70
69
71
req := & http.Request {
70
- Method : "GET" ,
72
+ Method : http . MethodGet ,
71
73
URL : parsedURL ,
72
74
Header : http.Header {
73
75
"User-Agent" : []string {useragent .String ()},
@@ -76,36 +78,23 @@ func (f *EnvDigitalOceanFingerprint) Get(attribute string, format string) (strin
76
78
77
79
res , err := f .client .Do (req )
78
80
if err != nil {
79
- f .logger .Debug ("could not read value for attribute" , "attribute" , attribute , "error" , err )
80
- return "" , err
81
- } else if res .StatusCode != http .StatusOK {
82
- f .logger .Debug ("could not read value for attribute" , "attribute" , attribute , "resp_code" , res .StatusCode )
81
+ f .logger .Debug ("failed to request metadata" , "attribute" , attribute , "error" , err )
83
82
return "" , err
84
83
}
85
84
86
- resp , err := ioutil .ReadAll (res .Body )
85
+ body , err := ioutil .ReadAll (res .Body )
87
86
res .Body .Close ()
88
87
if err != nil {
89
- f .logger .Error ("error reading response body for DigitalOcean attribute " , "attribute" , attribute , "error" , err )
88
+ f .logger .Error ("failed to read metadata " , "attribute" , attribute , "error" , err , "resp_code" , res . StatusCode )
90
89
return "" , err
91
90
}
92
91
93
- if res .StatusCode >= 400 {
94
- return "" , ReqError {res .StatusCode }
92
+ if res .StatusCode != http .StatusOK {
93
+ f .logger .Debug ("could not read value for attribute" , "attribute" , attribute , "resp_code" , res .StatusCode )
94
+ return "" , fmt .Errorf ("error reading attribute %s. digitalocean metadata api returned an error: resp_code: %d, resp_body: %s" , attribute , res .StatusCode , body )
95
95
}
96
96
97
- return string (resp ), nil
98
- }
99
-
100
- func checkDigitalOceanError (err error , logger log.Logger , desc string ) error {
101
- // If it's a URL error, assume we're not actually in an DigitalOcean environment.
102
- // To the outer layers, this isn't an error so return nil.
103
- if _ , ok := err .(* url.Error ); ok {
104
- logger .Debug ("error querying DigitalOcean attribute; skipping" , "attribute" , desc )
105
- return nil
106
- }
107
- // Otherwise pass the error through.
108
- return err
97
+ return string (body ), nil
109
98
}
110
99
111
100
func (f * EnvDigitalOceanFingerprint ) Fingerprint (request * FingerprintRequest , response * FingerprintResponse ) error {
@@ -138,7 +127,8 @@ func (f *EnvDigitalOceanFingerprint) Fingerprint(request *FingerprintRequest, re
138
127
resp , err := f .Get (attr .path , "text" )
139
128
v := strings .TrimSpace (resp )
140
129
if err != nil {
141
- return checkDigitalOceanError (err , f .logger , k )
130
+ f .logger .Warn ("failed to read attribute" , "attribute" , k , "err" , err )
131
+ continue
142
132
} else if v == "" {
143
133
f .logger .Debug ("read an empty value" , "attribute" , k )
144
134
continue
0 commit comments