cloud-metadata 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,379 @@
1
+ Metadata-Version: 2.4
2
+ Name: cloud-metadata
3
+ Version: 1.1.0
4
+ Summary: One metadata command and Python API for every public cloud
5
+ License: GPL-3.0-or-later
6
+ License-File: LICENSE
7
+ Author: Sean Marlow
8
+ Author-email: sean.marlow@suse.com
9
+ Maintainer: Sean Marlow
10
+ Maintainer-email: sean.marlow@suse.com
11
+ Requires-Python: >=3.11
12
+ Classifier: License :: OSI Approved :: GNU General Public License v3 or later (GPLv3+)
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Classifier: Programming Language :: Python :: 3.14
18
+ Classifier: Programming Language :: Python :: 3.15
19
+ Requires-Dist: jmespath (>=1.0.1)
20
+ Description-Content-Type: text/markdown
21
+
22
+ # cloud-metadata
23
+
24
+ [![CI](https://github.com/smarlowucf/cloud-metadata/actions/workflows/ci.yml/badge.svg)](https://github.com/smarlowucf/cloud-metadata/actions/workflows/ci.yml)
25
+ [![License: GPL v3+](https://img.shields.io/badge/license-GPL--3.0--or--later-blue.svg)](LICENSE)
26
+
27
+ **One metadata tool for every cloud. Same command, same keys, same exit
28
+ codes.**
29
+
30
+ If you run workloads on more than one cloud, you know the problem: AWS, Azure,
31
+ Google Cloud, Oracle Cloud and Alibaba Cloud each expose instance metadata
32
+ through a different URL, different headers, a different token scheme and a
33
+ different JSON layout. Every script that needs the region or the private IP
34
+ address ends up with a branch per cloud.
35
+
36
+ `cloud-metadata` removes those branches. It detects the cloud it is running
37
+ on and returns the same standard keys with the same syntax everywhere:
38
+
39
+ ```
40
+ $ cloud-metadata provider
41
+ amazon
42
+ $ cloud-metadata get region
43
+ eu-central-1
44
+ $ cloud-metadata get private-ipv4
45
+ 10.0.1.25
46
+ $ cloud-metadata keys
47
+ instance-id
48
+ instance-type
49
+ region
50
+ zone
51
+ ...
52
+ ```
53
+
54
+ The identical script gives you the identical answer on an EC2 instance, an
55
+ Azure VM, a Compute Engine VM, an OCI instance or an Alibaba ECS instance:
56
+
57
+ ```
58
+ region=$(cloud-metadata get region) || exit 1
59
+ ```
60
+
61
+ *The output above is example output, not a recording.*
62
+
63
+ ## Why use it instead of curl?
64
+
65
+ - **Built for multi-cloud.** One command and one Python API cover five clouds.
66
+ Your provisioning scripts, health checks, log shippers and agents need no
67
+ per-cloud code.
68
+ - **Auto-detection.** It reads the DMI data, confirms it against the
69
+ metadata service and falls back to probing. You can also set
70
+ `CLOUD_METADATA_PROVIDER` when you know the cloud.
71
+ - **The protocol details are handled.** That means the session tokens that AWS
72
+ (IMDSv2) and Alibaba use and the request headers that Azure, Google and
73
+ Oracle require. Metadata requests
74
+ never go through a proxy from the environment.
75
+ - **Standard keys.** `instance-id`, `region`, `zone`, `private-ipv4`,
76
+ `public-ipv6` and so on mean the same on every cloud.
77
+ - **Escape hatch.** When a standard key is not enough, `query` runs a
78
+ [JMESPath](https://jmespath.org/) expression against the cloud's own
79
+ metadata document, and `raw` prints it unchanged.
80
+ - **Scripting friendly.** The value is printed alone on stdout, messages go to
81
+ stderr, and exit codes tell "not on a cloud" from "key missing" from
82
+ "metadata service down".
83
+ - **Extensible.** A cloud is one JSON file. You can also register your own
84
+ provider at runtime.
85
+
86
+ ## Supported clouds
87
+
88
+ | Provider name | Cloud | Authentication handled |
89
+ |---|---|---|
90
+ | `amazon` | AWS EC2 | IMDSv2 session token |
91
+ | `microsoft` | Azure | Request header |
92
+ | `google` | Google Compute Engine | Request header |
93
+ | `oracle` | Oracle Cloud Infrastructure | Request header |
94
+ | `alibaba` | Alibaba Cloud ECS | Session token |
95
+
96
+ ## Python or Go: which one to install
97
+
98
+ The repository has two implementations of the same command line tool. They
99
+ read the same provider tables and use the same commands, flags and exit codes.
100
+
101
+ | | Python package | Go binary |
102
+ |---|---|---|
103
+ | Command line tool | Yes | Yes |
104
+ | Python API | Yes | No |
105
+ | Needs a Python interpreter | Yes (3.11 or later) | No, it is a static binary |
106
+ | Best for | Python tooling, scripts on hosts that already have Python | Minimal hosts and container images |
107
+ | Where | `src/cloud_metadata/` | `go/` |
108
+
109
+ Pick the Python package if you want to call it from Python or the host already
110
+ has Python. Pick the Go binary if you want a single file with no runtime. The
111
+ two are tested against each other, but a few results differ, mostly the order
112
+ of keys in objects that a JMESPath expression builds. `DESIGN.md` section 16
113
+ lists the differences.
114
+
115
+ ## Install
116
+
117
+ ### Python package
118
+
119
+ Python 3.11 or later is required. `jmespath` is the only dependency.
120
+
121
+ ```
122
+ pip install cloud-metadata
123
+ ```
124
+
125
+ ### Go binary
126
+
127
+ Build a static binary into `dist/cloud-metadata`. This needs a Go toolchain:
128
+
129
+ ```
130
+ make go-build
131
+ ```
132
+
133
+ ### Container image
134
+
135
+ `go/Containerfile` packages the Go binary on SUSE's `bci-micro` image:
136
+
137
+ ```
138
+ podman build -t cloud-metadata -f go/Containerfile go
139
+ podman run --rm --network host cloud-metadata get region
140
+ ```
141
+
142
+ On AWS, a container needs an IMDS hop limit of 2 or host networking.
143
+
144
+ ### SUSE distributions
145
+
146
+ RPM packages are built on OBS in the
147
+ [Cloud:Tools:CI](https://build.opensuse.org/project/show/Cloud:Tools:CI)
148
+ project. Add the repository for your distribution, then install either the Go
149
+ binary (`cloud-metadata`) or the Python package (`python3*-cloud-metadata`).
150
+ The two packages install the same `/usr/bin/cloud-metadata`, so install only
151
+ one of them. See [Packaging](#packaging).
152
+
153
+ openSUSE Tumbleweed:
154
+
155
+ ```
156
+ zypper addrepo https://download.opensuse.org/repositories/Cloud:Tools:CI/openSUSE_Tumbleweed/Cloud:Tools:CI.repo
157
+ zypper refresh
158
+ zypper install cloud-metadata # Go binary
159
+ zypper install python313-cloud-metadata # or the Python package (also python314)
160
+ ```
161
+
162
+ openSUSE Leap 16.0 and SUSE Linux Enterprise Server 16.0:
163
+
164
+ ```
165
+ zypper addrepo https://download.opensuse.org/repositories/Cloud:Tools:CI/16.0/Cloud:Tools:CI.repo
166
+ zypper refresh
167
+ zypper install cloud-metadata # Go binary
168
+ zypper install python313-cloud-metadata # or the Python package
169
+ ```
170
+
171
+ SUSE Linux Enterprise Server 15 SP7:
172
+
173
+ ```
174
+ zypper addrepo https://download.opensuse.org/repositories/Cloud:Tools:CI/SLE_15_SP7/Cloud:Tools:CI.repo
175
+ zypper refresh
176
+ zypper install cloud-metadata # Go binary
177
+ zypper install python311-cloud-metadata # or the Python package
178
+ ```
179
+
180
+ ## Command line
181
+
182
+ ```
183
+ cloud-metadata get <key> [--provider P] [--timeout N]
184
+ cloud-metadata provider [--timeout N]
185
+ cloud-metadata keys [--provider P] [--timeout N]
186
+ cloud-metadata query '<jmespath>' [--document NAME] [--provider P] [--timeout N]
187
+ cloud-metadata raw [--document NAME] [--provider P] [--timeout N]
188
+ cloud-metadata --version
189
+ ```
190
+
191
+ - `get` prints one standard key.
192
+ - `provider` prints the detected provider name.
193
+ - `keys` prints the standard keys the provider supports.
194
+ - `query` evaluates a JMESPath expression against a metadata document. `raw`
195
+ prints the document unchanged.
196
+ - `--provider` skips detection. `--timeout` is in seconds (default 2).
197
+ - `--version` prints the version, for example `cloud-metadata 1.0.0`. The
198
+ Python and Go tools report the same version for the same release.
199
+
200
+ Strings are printed as they are. Other values from `query` and `raw` are
201
+ printed as JSON. Messages go to stderr.
202
+
203
+ | Exit code | Meaning |
204
+ |---|---|
205
+ | 0 | Success |
206
+ | 2 | Provider not detected or unknown name, or a usage error |
207
+ | 3 | Key not available |
208
+ | 4 | The metadata service failed, timed out or cannot be reached |
209
+
210
+ ## Python API
211
+
212
+ ```python
213
+ import cloud_metadata as cm
214
+
215
+ cm.detect_provider() # "amazon", "microsoft", ...
216
+ cm.get("private-ipv4") # str
217
+ cm.get("region", provider="google", timeout=5)
218
+ cm.query("compute.tags") # JMESPath against the primary document
219
+ cm.raw() # the primary document, unevaluated
220
+ cm.keys() # standard keys the provider supports
221
+ cm.providers() # registered provider names
222
+ cm.clear_cache() # forget cached documents and the detected provider
223
+ ```
224
+
225
+ Errors derive from `cloud_metadata.CloudMetadataError`:
226
+
227
+ - `ProviderNotDetected`
228
+ - `KeyNotAvailable`
229
+ - `MetadataUnavailable`
230
+ - `DocumentNotFound`, also a `KeyError`: `query` or `raw` got an unknown document
231
+ - `InvalidExpression`, also a `ValueError`: `query` got an invalid expression
232
+
233
+ ## Standard keys
234
+
235
+ `instance-id`, `instance-type`, `region`, `zone`, `private-ipv4`,
236
+ `private-ipv6`, `public-ipv4`, `public-ipv6`, `ipv6`, `hostname`, `image-id`
237
+
238
+ IPv4 and IPv6 addresses are separate keys, so you always know which address
239
+ family you get. On an IPv6-only instance the IPv4 keys are unavailable, and
240
+ the other way round.
241
+
242
+ `ipv6` returns the first IPv6 address without saying whether it is public or
243
+ private. It works on every cloud. Use `private-ipv6` or `public-ipv6` when the
244
+ scope matters.
245
+
246
+ A provider may not support every key. A key whose value is empty on the
247
+ instance is also unavailable. Both cases raise `KeyNotAvailable` and exit
248
+ with code 3.
249
+
250
+ ### Known gaps
251
+
252
+ - **Oracle:** IMDS does not expose public IP addresses, so `public-ipv4` and
253
+ `public-ipv6` are unavailable. No external service is called to work around
254
+ this.
255
+ - **AWS:** EC2 has no private IPv6 address, so `private-ipv6` is unavailable.
256
+ - **Alibaba:** the metadata does not say whether an IPv6 address is public or
257
+ private, so `private-ipv6` and `public-ipv6` are unavailable. Use `ipv6`.
258
+ - **Google:** internal and external IPv6 addresses share one field, so they
259
+ are told apart by range (`fd..` is private, `2...` is public).
260
+ - **AWS:** `architecture` (`x86_64`, `arm64` and so on, as EC2 spells it) is
261
+ mapped on AWS only, so it is not a standard key. The other clouds have no
262
+ such field in their metadata. `cloud-metadata keys` lists it after the
263
+ standard keys on AWS.
264
+ - **Azure:** `image-id` is the resource ID for images that are addressed by
265
+ one (custom and gallery images). For Marketplace images it is the URN
266
+ `publisher:offer:sku:version`, with the real version from `exactVersion`
267
+ (or `version`, which may be `latest`, if that is empty). It is unavailable
268
+ if the image has neither. `latest` can name different images over time.
269
+ - **Azure and Google:** `zone` is empty on instances that are not in a zone.
270
+ - **Alibaba:** `public-ipv4` is the public address of the primary network
271
+ interface. An attached Elastic IP (`eipv4`) is not mapped.
272
+ - **AWS in containers:** with IMDSv2 and a hop limit of 1, the token request
273
+ from inside a container times out. The error message then names the hop
274
+ limit. Raise `HttpPutResponseHopLimit` to 2 or allow IMDSv1.
275
+
276
+ ## Detection
277
+
278
+ 1. If `CLOUD_METADATA_PROVIDER` is set, that provider is used.
279
+ 2. The DMI files (`sys_vendor`, `product_name`, `chassis_asset_tag`) are
280
+ compared with each provider. A match is confirmed with the provider's
281
+ metadata service. If that fails, DMI is trusted and a warning is logged.
282
+ 3. If DMI is inconclusive, every provider's metadata service is probed in
283
+ registry order.
284
+ 4. As a last step, `dmidecode -s system-manufacturer` is tried.
285
+ 5. If nothing matches, `ProviderNotDetected` is raised.
286
+
287
+ `DMI_DIR_PATH` overrides the DMI directory (default `/sys/class/dmi/id`), for
288
+ testing. The result is cached for the life of the process. Each failed
289
+ metadata probe can wait for the timeout, so use `--provider` or
290
+ `CLOUD_METADATA_PROVIDER` where the cloud is known.
291
+
292
+ The detection code is ported from
293
+ [cloud-registration-engine](https://github.com/SUSE-Enceladus/cloud-registration-engine).
294
+
295
+ ## Status
296
+
297
+ - **Verified on real instances:** the metadata mappings for Amazon, Azure and
298
+ Google.
299
+ - **Not yet verified on real instances:** the Oracle and Alibaba mappings come
300
+ from each cloud's documentation. The test fixtures are also derived from
301
+ documentation, not captured from instances.
302
+ - **Go container image:** it has not been built or run yet, because the
303
+ machine it was written on has no container runtime.
304
+ - **RPM specs:** `python-cloud-metadata.spec` and `cloud-metadata.spec` are
305
+ being built on OBS in
306
+ [Cloud:Tools:CI](https://build.opensuse.org/package/show/Cloud:Tools:CI/python-cloud-metadata)
307
+ ([Go version](https://build.opensuse.org/package/show/Cloud:Tools:CI/cloud-metadata)).
308
+ They have not been through a release yet. Check the build results there and
309
+ run `rpmlint` on the packages before relying on them.
310
+ - **Releases:** there are no tagged releases yet, so install from a checkout
311
+ until there are.
312
+
313
+ Reports from Oracle and Alibaba instances are especially welcome.
314
+
315
+ ## Contributing
316
+
317
+ ### Adding a provider
318
+
319
+ In the code base, a cloud is one JSON file in
320
+ `src/cloud_metadata/providers/data/` and one line in `index.json` there. The
321
+ file layout is described in `providers/_loader.py`. The Go version embeds a
322
+ copy of the same files, which `go/scripts/sync-data.sh` updates. At runtime,
323
+ register a `ProviderSpec` from Python:
324
+
325
+ ```python
326
+ import cloud_metadata as cm
327
+
328
+ cm.register(
329
+ cm.ProviderSpec(
330
+ name="examplecloud",
331
+ dmi_match=("examplecloud",),
332
+ probes=(cm.Probe("/v1/"),),
333
+ base_urls=("http://169.254.169.254",),
334
+ documents={"instance": cm.Document("/v1/instance", "json")},
335
+ keys={"instance-id": ("instance", "id")},
336
+ )
337
+ )
338
+ cm.get("instance-id", provider="examplecloud")
339
+ ```
340
+
341
+ `register()` validates the spec and raises `ValueError` for a duplicate name
342
+ unless `replace=True` is given. The command line only knows the built-in
343
+ providers. `DESIGN.md` describes the spec in detail.
344
+
345
+ ### Development
346
+
347
+ ```
348
+ poetry install
349
+ make check # ruff, ruff format --check, pytest with coverage, poetry build
350
+ make fmt # fix lint findings and format
351
+ make go-check # gofmt, go vet and go test for the Go version in go/
352
+ make check-all # both
353
+ ```
354
+
355
+ The test suite uses no network access. See `DESIGN.md` for the design and
356
+ `PLAN.md` for the implementation plan.
357
+
358
+ To release, run `poetry run bump-my-version bump patch` (or `minor` or `major`).
359
+ It updates the version in `pyproject.toml`, `src/cloud_metadata/__init__.py`
360
+ and both spec files. It does not commit or tag, and it does not write the
361
+ `.changes` entries, so add those by hand.
362
+
363
+ ### Packaging
364
+
365
+ `python-cloud-metadata.spec` is a SUSE multibuild Python spec. It builds the
366
+ package for each Python flavour that is 3.11 or later and installs
367
+ `/usr/bin/cloud-metadata` through `update-alternatives`.
368
+ `python-cloud-metadata.changes` is the changelog for OBS. The details are in
369
+ `DESIGN.md` section 12.
370
+
371
+ The Go version has its own spec, `cloud-metadata.spec`. Make its sources with
372
+ `go/scripts/make-rpm-sources.sh`. The two packages install the same
373
+ `/usr/bin/cloud-metadata`, so they conflict, and a system has one or the
374
+ other.
375
+
376
+ ## License
377
+
378
+ GPL-3.0-or-later. See `LICENSE`.
379
+
@@ -0,0 +1,23 @@
1
+ cloud_metadata/__init__.py,sha256=daPSHBTCKF9E-oCcC4-EfHimq7vSpbwdN_o35KHreNI,5620
2
+ cloud_metadata/_http.py,sha256=ni1y_5owgtmEd9BmPgbkDg6LhEkmRjx9-Zm_6O3EfMY,3608
3
+ cloud_metadata/cli.py,sha256=97lBosl0b2E47_hmDCqogGIzHzVfj5KQQsWol4ovSaQ,6042
4
+ cloud_metadata/detect.py,sha256=C-hwourd8gzaRNnCwFnBqOD1a8v01uFDEs8tzxX78tM,6647
5
+ cloud_metadata/engine.py,sha256=2ZEoK8W06xLFpn5O731gaKvD9NxC2aB6OhAZ4YQP4fY,11554
6
+ cloud_metadata/errors.py,sha256=IdBEU2Kx38MfT2pKq8zAC9rZTKv0ZuiU2f18Gq_H8dk,2303
7
+ cloud_metadata/functions.py,sha256=wMbXMpI2Xz3AkniCLJrrRLiTb8QqPAb9VLIzJQizbfU,7595
8
+ cloud_metadata/providers/__init__.py,sha256=1wUuhBkowccaREeUVBzYHHYAq5-lBxKJd5bhQNRnoHo,925
9
+ cloud_metadata/providers/_loader.py,sha256=fH33s9rx3kLVqGnW4GW57i0UGJeimnlEaQ5l2fRZ88Q,9444
10
+ cloud_metadata/providers/_spec.py,sha256=o-bDZU3zgpM_cKVd1tMBaUUrmaMbmIJ7vs3Kqhj6ORs,7015
11
+ cloud_metadata/providers/data/alibaba.json,sha256=-k9RNZNNQW2L34eKNK67al9K9mWB9U5xfGjIegLeRk8,3012
12
+ cloud_metadata/providers/data/amazon.json,sha256=_aJ58t7rwUJVHBAVamQynTaet2tmNCLRFt98deMnWWI,2532
13
+ cloud_metadata/providers/data/google.json,sha256=OXVOFOb6hlNFp2aPEAKk9MI_KlxYvN-7BR5EGfveSn0,2446
14
+ cloud_metadata/providers/data/index.json,sha256=Vm5yxAXdJphqtpgOZVvVg9962dQXS-emQECHZYqJop8,98
15
+ cloud_metadata/providers/data/microsoft.json,sha256=34ran0_q8uVcJUZae742cf2ZvbbCbjdSZi0OhLmunlM,2568
16
+ cloud_metadata/providers/data/oracle.json,sha256=cGQqGaKTJv5oDt6oFwPZEhPe2NbzI0exTPBQ6AbzYLs,1966
17
+ cloud_metadata/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
18
+ cloud_metadata/registry.py,sha256=IiCKqH3j1KCnyhsC8KOQiL73tUAtv56RLsf0nkA3-SY,3769
19
+ cloud_metadata-1.1.0.dist-info/METADATA,sha256=20moOvYOWCZQKFr6UJnLPRWctZrv3DahpZI_0VOKg1Y,14742
20
+ cloud_metadata-1.1.0.dist-info/WHEEL,sha256=L-WvLdSBvJWHXv5zrQxDvNRnIgoBzLUpcbuiw19KCuQ,88
21
+ cloud_metadata-1.1.0.dist-info/entry_points.txt,sha256=MhN5GL9vLCt52UlFt2Q1iwQFvzPRtXSWCkxgq3n83NI,58
22
+ cloud_metadata-1.1.0.dist-info/licenses/LICENSE,sha256=D-htZH7g_APjyKfccX4ecS3hGJZ82Oll1ahY_6LmzL4,32314
23
+ cloud_metadata-1.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: poetry-core 2.5.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,3 @@
1
+ [console_scripts]
2
+ cloud-metadata=cloud_metadata.cli:main
3
+