@@ -73,16 +73,38 @@ func (app *App) versionSubRouter(sr *mux.Router, version string) {
7373 ii .Handle ("" , appHandler (app .trailingSlashRedirect ))
7474 ii .Handle ("/" , appHandler (app .instanceIdentityHandler ))
7575 ii .Handle ("/document" , appHandler (app .instanceIdentityDocumentHandler ))
76- // TODO: implement
77- //ii.Handle("/pkcs7", appHandler(app.instanceIdentityHandler))
78- //ii.Handle("/signature", appHandler(app.instanceIdentityHandler))
76+ ii .Handle ("/document/" , appHandler (app .instanceIdentityDocumentHandler ))
77+ ii .Handle ("/pkcs7" , appHandler (app .instanceIdentityPkcs7Handler ))
78+ ii .Handle ("/pkcs7/" , appHandler (app .instanceIdentityPkcs7Handler ))
79+ ii .Handle ("/signature" , appHandler (app .instanceIdentitySignatureHandler ))
80+ ii .Handle ("/signature/" , appHandler (app .instanceIdentitySignatureHandler ))
7981
8082 m := sr .PathPrefix ("/meta-data" ).Subrouter ()
8183 m .Handle ("" , appHandler (app .trailingSlashRedirect ))
8284 m .Handle ("/" , appHandler (app .metaDataHandler ))
85+ m .Handle ("/ami-id" , appHandler (app .amiIdHandler ))
86+ m .Handle ("/ami-id/" , appHandler (app .amiIdHandler ))
87+ m .Handle ("/ami-launch-index" , appHandler (app .amiLaunchIndexHandler ))
88+ m .Handle ("/ami-launch-index/" , appHandler (app .amiLaunchIndexHandler ))
89+ m .Handle ("/ami-manifest-path" , appHandler (app .amiManifestPathHandler ))
90+ m .Handle ("/ami-manifest-path/" , appHandler (app .amiManifestPathHandler ))
91+
92+ bdm := m .PathPrefix ("/block-device-mapping" ).Subrouter ()
93+ bdm .Handle ("" , appHandler (app .trailingSlashRedirect ))
94+ bdm .Handle ("/" , appHandler (app .blockDeviceMappingHandler ))
95+ bdm .Handle ("/ami" , appHandler (app .blockDeviceMappingAmiHandler ))
96+ bdm .Handle ("/ami/" , appHandler (app .blockDeviceMappingAmiHandler ))
97+ bdm .Handle ("/root" , appHandler (app .blockDeviceMappingRootHandler ))
98+ bdm .Handle ("/root/" , appHandler (app .blockDeviceMappingRootHandler ))
99+
100+ m .Handle ("/hostname" , appHandler (app .hostnameHandler ))
101+ m .Handle ("/hostname/" , appHandler (app .hostnameHandler ))
83102 m .Handle ("/instance-id" , appHandler (app .instanceIDHandler ))
103+ m .Handle ("/instance-id/" , appHandler (app .instanceIDHandler ))
84104 m .Handle ("/local-hostname" , appHandler (app .localHostnameHandler ))
105+ m .Handle ("/local-hostname/" , appHandler (app .localHostnameHandler ))
85106 m .Handle ("/local-ipv4" , appHandler (app .privateIpHandler ))
107+ m .Handle ("/local-ipv4/" , appHandler (app .privateIpHandler ))
86108
87109 p := m .PathPrefix ("/placement" ).Subrouter ()
88110 p .Handle ("/availability-zone" , appHandler (app .availabilityZoneHandler ))
@@ -100,6 +122,7 @@ func (app *App) versionSubRouter(sr *mux.Router, version string) {
100122 d .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
101123 ii .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
102124 m .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
125+ bdm .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
103126 p .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
104127 i .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
105128 n .Handle ("/{path:.*}" , appHandler (app .notFoundHandler ))
@@ -135,6 +158,7 @@ signature
135158` )
136159}
137160
161+ // NOTE: order of keys here differs from real metadata service, in theory most (proper) JSON parsers should be fine with it though...
138162type InstanceIdentityDocument struct {
139163 AvailabilityZone string `json:"availabilityZone"`
140164 Region string `json:"region"`
@@ -157,13 +181,13 @@ func (app *App) instanceIdentityDocumentHandler(w http.ResponseWriter, r *http.R
157181 AvailabilityZone : app .AvailabilityZone ,
158182 Region : app .AvailabilityZone [:len (app .AvailabilityZone )- 1 ],
159183 DevpayProductCodes : nil ,
160- PrivateIp : "127.0.0.1" ,
184+ PrivateIp : app . PrivateIp ,
161185 Version : "2010-08-31" ,
162- InstanceId : "i-wxyz1234" ,
186+ InstanceId : app . InstanceID ,
163187 BillingProducts : nil ,
164188 InstanceType : "t2.micro" ,
165189 AccountId : "1234567890" ,
166- ImageId : "ami-123456" ,
190+ ImageId : app . AmiID ,
167191 PendingTime : "2016-04-15T12:14:15Z" ,
168192 Architecture : "x86_64" ,
169193 KernelId : nil ,
@@ -175,7 +199,20 @@ func (app *App) instanceIdentityDocumentHandler(w http.ResponseWriter, r *http.R
175199 }
176200}
177201
202+ func (app * App ) instanceIdentityPkcs7Handler (w http.ResponseWriter , r * http.Request ) {
203+ // TODO: adjust output to suit
204+ write (w , `
205+ ` )
206+ }
207+
208+ func (app * App ) instanceIdentitySignatureHandler (w http.ResponseWriter , r * http.Request ) {
209+ // TODO: adjust output to suit
210+ write (w , `
211+ ` )
212+ }
213+
178214func (app * App ) metaDataHandler (w http.ResponseWriter , r * http.Request ) {
215+ // TODO: if IAM Role/Instance Profile is disabled, don't add iam/ to the list (same behavior as real metadata service)
179216 write (w , `ami-id
180217ami-launch-index
181218ami-manifest-path
@@ -199,6 +236,36 @@ security-groups
199236services/` )
200237}
201238
239+ func (app * App ) amiIdHandler (w http.ResponseWriter , r * http.Request ) {
240+ write (w , app .AmiID )
241+ }
242+
243+ func (app * App ) amiLaunchIndexHandler (w http.ResponseWriter , r * http.Request ) {
244+ write (w , "0" )
245+ }
246+
247+ func (app * App ) amiManifestPathHandler (w http.ResponseWriter , r * http.Request ) {
248+ write (w , "(unknown)" )
249+ }
250+
251+ func (app * App ) blockDeviceMappingHandler (w http.ResponseWriter , r * http.Request ) {
252+ // Not exposing any extra volumes for now, this is pretty standard for an EBS backed EC2 instance.
253+ write (w , `ami
254+ root` )
255+ }
256+
257+ func (app * App ) blockDeviceMappingAmiHandler (w http.ResponseWriter , r * http.Request ) {
258+ write (w , "/dev/xvda" )
259+ }
260+
261+ func (app * App ) blockDeviceMappingRootHandler (w http.ResponseWriter , r * http.Request ) {
262+ write (w , "/dev/xvda" )
263+ }
264+
265+ func (app * App ) hostnameHandler (w http.ResponseWriter , r * http.Request ) {
266+ write (w , app .Hostname )
267+ }
268+
202269func (app * App ) instanceIDHandler (w http.ResponseWriter , r * http.Request ) {
203270 write (w , app .InstanceID )
204271}
0 commit comments