diff options
author | Matthias Beyer <matthias.beyer@ifm.com> | 2022-04-22 08:51:26 +0200 |
---|---|---|
committer | Matthias Beyer <matthias.beyer@ifm.com> | 2022-04-22 08:51:26 +0200 |
commit | 0425111d4a9b6957461946a7e7cdf0a9e5616fd5 (patch) | |
tree | 2cc137504b9d21e9c8c49f76f15c825314289657 | |
parent | 57612ba0cd8dbc5aa24c6820089166afcc7990f4 (diff) | |
parent | e0082ad9bf5598a7487ed6e20b3950146ec6ecf0 (diff) |
Merge branch 'feature/add_azure_bridge_plugin' into showcase
-rw-r--r-- | Cargo.lock | 332 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | plugins/plugin_azure_bridge/Cargo.toml | 20 | ||||
-rw-r--r-- | plugins/plugin_azure_bridge/src/error.rs | 16 | ||||
-rw-r--r-- | plugins/plugin_azure_bridge/src/lib.rs | 224 | ||||
-rw-r--r-- | tedge/Cargo.toml | 4 | ||||
-rw-r--r-- | tedge/src/main.rs | 6 |
7 files changed, 542 insertions, 61 deletions
@@ -74,7 +74,7 @@ dependencies = [ "heck 0.3.3", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -164,18 +164,18 @@ checksum = "648ed8c8d2ce5409ccd57453d9d1b214b342a0d69376a6feda1fd6cae3299308" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] name = "async-trait" -version = "0.1.52" +version = "0.1.53" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "061a7acccaa286c011ddc30970520b98fa40e00c9d644633fb26b5fc63a265e3" +checksum = "ed6aa3524a2dfcf9fe180c51eae2b58738348d819517ceadf95789c51fff7600" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -196,6 +196,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" [[package]] +name = "azure_iot_sdk" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eff5168b8ec54ca9f904dea5ad52fd892f8f76ccd8635eda37f0be250e79e5db" +dependencies = [ + "async-trait", + "base64", + "chrono", + "form_urlencoded", + "hmac", + "log", + "mqtt-protocol", + "native-tls", + "serde", + "serde_json", + "serde_urlencoded", + "sha2", + "tokio", + "tokio-native-tls", +] + +[[package]] name = "backoff" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -491,7 +513,7 @@ dependencies = [ "proc-macro-error", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -582,6 +604,16 @@ dependencies = [ ] [[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] name = "core-foundation-sys" version = "0.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -686,6 +718,16 @@ dependencies = [ ] [[package]] +name = "crypto-mac" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1d1a86f49236c215f271d40892d5fc950490551400b02ef360692c29815c714" +dependencies = [ + "generic-array", + "subtle", +] + +[[package]] name = "csv" version = "1.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -693,7 +735,7 @@ checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" dependencies = [ "bstr", "csv-core", - "itoa", + "itoa 0.4.8", "ryu", "serde", ] @@ -714,7 +756,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccc0a48a9b826acdf4028595adc9db92caea352f7af011a3034acd172a52a0aa" dependencies = [ "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -738,7 +780,7 @@ dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", "strsim 0.10.0", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -749,7 +791,7 @@ checksum = "9c972679f83bdf9c42bd905396b6c3588a843a17f0f16dfcfa3e2c5d57441835" dependencies = [ "darling_core", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -766,7 +808,7 @@ checksum = "c73af209b6a5dc8ca7cbaba720732304792cddc933cfea3d74509c2b1ef2f436" dependencies = [ "num-bigint 0.4.3", "num-traits", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -980,6 +1022,21 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" [[package]] +name = "foreign-types" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6f339eb8adc052cd2ca78910fda869aefa38d22d5cb648e6485e4d3fc06f3b1" +dependencies = [ + "foreign-types-shared", +] + +[[package]] +name = "foreign-types-shared" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" + +[[package]] name = "form_urlencoded" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1057,7 +1114,7 @@ checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -1239,6 +1296,16 @@ dependencies = [ ] [[package]] +name = "hmac" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a2a2320eb7ec0ebe8da8f744d7812d9fc4cb4d09344ac01898dbcb6a20ae69b" +dependencies = [ + "crypto-mac", + "digest", +] + +[[package]] name = "http" version = "0.2.5" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1246,7 +1313,7 @@ checksum = "1323096b05d41827dadeaee54c9981958c0f94e670bc94ed80037d1a7b8b186b" dependencies = [ "bytes", "fnv", - "itoa", + "itoa 0.4.8", ] [[package]] @@ -1312,7 +1379,7 @@ dependencies = [ "http-body", "httparse", "httpdate", - "itoa", + "itoa 0.4.8", "pin-project-lite", "socket2", "tokio", @@ -1432,6 +1499,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b71991ff56294aa922b450139ee08b3bfc70982c6b2c7562771375cf73542dd4" [[package]] +name = "itoa" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" + +[[package]] name = "jackiechan" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1590,7 +1663,7 @@ checksum = "45a95a48d0bc28f9af628286e8a4da09f96f34a97744a2e9a5a4db9814ad527d" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -1672,7 +1745,7 @@ dependencies = [ "cfg-if 1.0.0", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -1708,6 +1781,20 @@ dependencies = [ ] [[package]] +name = "mqtt-protocol" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca0b17380dc69fbcf5f967828cfd10e55028ba83a57da1f580c5b0792ab807ac" +dependencies = [ + "byteorder", + "lazy_static", + "log", + "regex", + "thiserror", + "tokio", +] + +[[package]] name = "mqtt_channel" version = "0.5.2" dependencies = [ @@ -1779,6 +1866,24 @@ dependencies = [ ] [[package]] +name = "native-tls" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd7e2f3618557f980e0b17e8856252eee3c97fa12c54dff0ca290fb6266ca4a9" +dependencies = [ + "lazy_static", + "libc", + "log", + "openssl", + "openssl-probe", + "openssl-sys", + "schannel", + "security-framework", + "security-framework-sys", + "tempfile", +] + +[[package]] name = "nix" version = "0.23.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1952,6 +2057,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" [[package]] +name = "openssl" +version = "0.10.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0c7ae222234c30df141154f159066c5093ff73b63204dcda7121eb082fc56a95" +dependencies = [ + "bitflags", + "cfg-if 1.0.0", + "foreign-types", + "libc", + "once_cell", + "openssl-sys", +] + +[[package]] +name = "openssl-probe" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf" + +[[package]] name = "openssl-sys" version = "0.9.72" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2106,7 +2231,7 @@ checksum = "6e8fe8163d14ce7f0cdac2e040116f22eac817edabff0be91e8aff7e9accf389" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -2168,7 +2293,25 @@ dependencies = [ "tedge_api", "tedge_lib", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", +] + +[[package]] +name = "plugin_azure_bridge" +version = "0.1.0" +dependencies = [ + "async-trait", + "azure_iot_sdk", + "miette", + "serde", + "serde_json", + "tedge_api", + "tedge_lib", + "thiserror", + "tokio", + "tokio-util 0.7.1", + "toml", + "tracing", ] [[package]] @@ -2182,7 +2325,7 @@ dependencies = [ "serde", "tedge_api", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "tracing", ] @@ -2201,7 +2344,7 @@ dependencies = [ "tedge_api", "tedge_lib", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "tracing", ] @@ -2214,7 +2357,7 @@ dependencies = [ "miette", "serde", "tedge_api", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "tracing", ] @@ -2229,7 +2372,7 @@ dependencies = [ "tedge_api", "tedge_lib", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "toml", "tracing", ] @@ -2270,7 +2413,7 @@ dependencies = [ "tedge_lib", "thiserror", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "tracing", ] @@ -2289,7 +2432,7 @@ dependencies = [ "tedge_api", "tedge_lib", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "tracing", ] @@ -2329,7 +2472,7 @@ dependencies = [ "tedge_api", "tedge_lib", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "toml", ] @@ -2419,7 +2562,7 @@ dependencies = [ "proc-macro-error-attr", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", "version_check", ] @@ -2512,7 +2655,7 @@ dependencies = [ "itertools", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -2937,6 +3080,16 @@ dependencies = [ ] [[package]] +name = "schannel" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f05ba609c234e60bee0d547fe94a4c7e9da733d1c962cf6e59efa4cd9c8bc75" +dependencies = [ + "lazy_static", + "winapi", +] + +[[package]] name = "scoped-tls" version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2969,6 +3122,29 @@ dependencies = [ ] [[package]] +name = "security-framework" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2dc14f172faf8a0194a3aded622712b0de276821addc574fa54fc0a1167e10dc" +dependencies = [ + "bitflags", + "core-foundation", + "core-foundation-sys", + "libc", + "security-framework-sys", +] + +[[package]] +name = "security-framework-sys" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0160a13a177a45bfb43ce71c01580998474f556ad854dcbca936dd2841a5c556" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] name = "segments" version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3013,16 +3189,16 @@ checksum = "08597e7152fcd306f41838ed3e37be9eaeed2b61c42e2117266a554fab4662f9" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] name = "serde_json" -version = "1.0.72" +version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0ffa0837f2dfa6fb90868c2b5468cad482e175f7dad97e7421951e663f2b527" +checksum = "8e8d9fa5c3b304765ce1fd9c4c8a3de2c8db365a5b91be52f186efc675681d95" dependencies = [ - "itoa", + "itoa 1.0.1", "ryu", "serde", ] @@ -3034,7 +3210,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" dependencies = [ "form_urlencoded", - "itoa", + "itoa 0.4.8", "ryu", "serde", ] @@ -3060,7 +3236,7 @@ dependencies = [ "darling", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -3082,7 +3258,7 @@ checksum = "b2acd6defeddb41eb60bb468f8825d0cfd0c2a76bc03bfd235b6a1dc4f6a1ad5" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -3099,6 +3275,19 @@ dependencies = [ ] [[package]] +name = "sha2" +version = "0.9.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" +dependencies = [ + "block-buffer", + "cfg-if 1.0.0", + "cpufeatures", + "digest", + "opaque-debug", +] + +[[package]] name = "sharded-slab" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3195,10 +3384,16 @@ dependencies = [ "proc-macro-error", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] +name = "subtle" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" + +[[package]] name = "supports-color" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -3239,9 +3434,9 @@ dependencies = [ [[package]] name = "syn" -version = "1.0.82" +version = "1.0.91" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8daf5dd0bb60cbd4137b1b587d2fc0ae729bc07cf01cd70b36a1ed5ade3b9d59" +checksum = "b683b2b825c8eef438b77c36a06dc262294da3d5a5813fac20da149241dcd44d" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", @@ -3308,6 +3503,7 @@ dependencies = [ "env_logger 0.9.0", "miette", "plugin_avg", + "plugin_azure_bridge", "plugin_httpstop", "plugin_inotify", "plugin_log", @@ -3380,7 +3576,7 @@ dependencies = [ "static_assertions", "thiserror", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "toml", ] @@ -3429,7 +3625,7 @@ dependencies = [ "test-log", "thiserror", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "toml", "tracing", "tracing-subscriber 0.3.6", @@ -3569,7 +3765,7 @@ dependencies = [ "cfg-if 1.0.0", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", "version_check", ] @@ -3581,7 +3777,7 @@ checksum = "4235dbf7ea878b3ef12dea20a59c134b405a66aafc4fc2c7b9935916e289e735" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -3642,7 +3838,7 @@ checksum = "aa32fd3f627f367fe16f893e2597ae3c05020f8bba2666a4e6ea73d377e5714b" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -3670,7 +3866,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "41effe7cfa8af36f439fac33861b66b049edc6f9a32331e2312660529c1c24ad" dependencies = [ - "itoa", + "itoa 0.4.8", "libc", "serde", "time-macros", @@ -3746,7 +3942,17 @@ checksum = "b557f72f448c511a979e2564e55d74e6c4432fc96ff4f6241bc6bded342643b7" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", +] + +[[package]] +name = "tokio-native-tls" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7d995660bd2b7f8c1568414c1126076c13fbb725c40112dc0120b78eb9b717b" +dependencies = [ + "native-tls", + "tokio", ] [[package]] @@ -3824,23 +4030,22 @@ dependencies = [ [[package]] name = "tokio-util" -version = "0.7.0" +version = "0.7.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "64910e1b9c1901aaf5375561e35b9c057d95ff41a44ede043a03e09279eabaf1" +checksum = "0edfdeb067411dba2044da6d1cb2df793dd35add7888d73c16e3381ded401764" dependencies = [ "bytes", "futures-core", "futures-sink", - "log", "pin-project-lite", "tokio", ] [[package]] name = "toml" -version = "0.5.8" +version = "0.5.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa" +checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7" dependencies = [ "serde", ] @@ -3885,7 +4090,7 @@ dependencies = [ "proc-macro2 1.0.32", "prost-build", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] @@ -3902,7 +4107,7 @@ dependencies = [ "rand", "slab", "tokio", - "tokio-util 0.7.0", + "tokio-util 0.7.1", "tower-layer", "tower-service", "tracing", @@ -3922,9 +4127,9 @@ checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" [[package]] name = "tracing" -version = "0.1.29" +version = "0.1.33" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "375a639232caf30edfc78e8d89b2d4c375515393e7af7e16f01cd96917fb2105" +checksum = "80b9fa4360528139bc96100c160b7ae879f5567f49f1782b0b02035b0358ebf3" dependencies = [ "cfg-if 1.0.0", "log", @@ -3935,22 +4140,23 @@ dependencies = [ [[package]] name = "tracing-attributes" -version = "0.1.18" +version = "0.1.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f4f480b8f81512e825f337ad51e94c1eb5d3bbdf2b363dcd01e2b19a9ffe3f8e" +checksum = "2e65ce065b4b5c53e73bb28912318cb8c9e9ad3921f1d669eb0e68b4c8143a2b" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", ] [[package]] name = "tracing-core" -version = "0.1.21" +version = "0.1.24" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f4ed65637b8390770814083d20756f87bfa2c21bf2f110babdc5438351746e4" +checksum = "90442985ee2f57c9e1b548ee72ae842f4a9a20e3f417cc38dbc5dc684d9bb4ee" dependencies = [ "lazy_static", + "valuable", ] [[package]] @@ -4166,6 +4372,12 @@ dependencies = [ ] [[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + +[[package]] name = "value-bag" version = "1.0.0-alpha.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -4286,7 +4498,7 @@ dependencies = [ "log", "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", "wasm-bindgen-shared", ] @@ -4320,7 +4532,7 @@ checksum = "bfa8a30d46208db204854cadbb5d4baf5fcf8071ba5bf48190c3e59937962ebc" dependencies = [ "proc-macro2 1.0.32", "quote 1.0.10", - "syn 1.0.82", + "syn 1.0.91", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -5,6 +5,7 @@ members = [ "crates/core/*", "crates/tests/*", "plugins/plugin_avg", + "plugins/plugin_azure_bridge", "plugins/plugin_httpstop", "plugins/plugin_inotify", "plugins/plugin_log", diff --git a/plugins/plugin_azure_bridge/Cargo.toml b/plugins/plugin_azure_bridge/Cargo.toml new file mode 100644 index 00000000..65cd555f --- /dev/null +++ b/plugins/plugin_azure_bridge/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "plugin_azure_bridge" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +async-trait = "0.1.53" +azure_iot_sdk = "0.8.0" +miette = "4.4.0" +serde = { version = "1.0.136", features = ["derive"] } +serde_json = "1.0.79" +tedge_api = { version = "0.1.0", path = "../../crates/core/tedge_api" } +tedge_lib = { version = "0.1.0", path = "../../crates/core/tedge_lib" } +thiserror = "1.0.30" +tokio = "1.17.0" +tokio-util = "0.7.1" +toml = "0.5.9" +tracing = "0.1.33" diff --git a/plugins/plugin_azure_bridge/src/error.rs b/plugins/plugin_azure_bridge/src/error.rs new file mode 100644 index 00000000..7ecd0637 --- /dev/null +++ b/plugins/plugin_azure_bridge/src/error.rs @@ -0,0 +1,16 @@ +use miette::Diagnostic; +use thiserror::Error; + +#[derive(Debug, Diagnostic, Error)] +pub enum AzureError { + #[error("Could not use configuration: {}", .0)] + Configuration(#[from] toml::de::Error), + #[error("Could not serialize: {}", .0)] + JsonSerialization(#[from] serde_json::Error), + #[error("Could not construct a valid device token: {:?}", 0)] + MisconfiguredDeviceToken(azure_iot_sdk::TokenError), + #[error("Could not construct a valid IoTHubClient: {}", 0)] + IotHubClient(String), + #[error("Could not send a message: {}", 0)] + MessageSend(String), +} diff --git a/plugins/plugin_azure_bridge/src/lib.rs b/plugins/plugin_azure_bridge/src/lib.rs new file mode 100644 index 00000000..d9ab6a5f --- /dev/null +++ b/plugins/plugin_azure_bridge/src/lib.rs @@ -0,0 +1,224 @@ +mod error; + +use std::sync::Arc; + +use async_trait::async_trait; + +use azure_iot_sdk::DeviceKeyTokenSource; +use azure_iot_sdk::IoTHubClient; +use azure_iot_sdk::Message as IoTMessage; +use error::AzureError; +use tedge_api::address::ReplySender; +use tedge_api::make_receiver_bundle; +use tedge_api::message::NoReply; +use tedge_api::plugin::BuiltPlugin; +use tedge_api::plugin::Handle; +use tedge_api::plugin::HandleTypes; +use tedge_api::plugin::Message; +use tedge_api::plugin::PluginDeclaration; +use tedge_api::Address; +use tedge_api::Plugin; +use tedge_api::PluginBuilder; +use tedge_api::PluginConfiguration; +use tedge_api::PluginDirectory; +use tedge_api::PluginError; +use tedge_api::PluginExt; +use tedge_lib::measurement::Measurement; +use tokio::sync::Mutex; +use tokio_util::sync::CancellationToken; + +#[derive(Default)] +pub struct AzureBridgeBuilder; + +#[derive(serde::Deserialize, Debug)] +struct AzureConfig { + access_key: String, + hubname: String, + device_id: String, + #[serde(default)] + c2d_target: Option<String>, +} + +#[async_trait] +impl<PD: PluginDirectory> PluginBuilder<PD> for AzureBridgeBuilder { + fn kind_name() -> &'static str + where + Self: Sized, + { + "azure_bridge" + } + + fn kind_message_types() -> HandleTypes + where + Self: Sized, + { + AzureBridge::get_handled_types() + } + + async fn verify_configuration(&self, _config: &PluginConfiguration) -> Result<(), PluginError> { + Ok(()) + } + + async fn instantiate( + &self, + config: PluginConfiguration, + cancellation_token: CancellationToken, + core_comms: &PD, + ) -> Result<BuiltPlugin, PluginError> + where + PD: 'async_trait, + { + let AzureConfig { + access_key, + hubname, + device_id, + c2d_target: c2d_receiver, + } = config.try_into().map_err(AzureError::from)?; + + let token_source = DeviceKeyTokenSource::new(&hubname, &device_id, &access_key) + .map_err(AzureError::MisconfiguredDeviceToken)?; + + let az_client = IoTHubClient::new(&hubname, device_id, token_source) + .await + .map_err(|e| AzureError::IotHubClient(e.to_string()))?; + + let c2d_receiver = c2d_receiver + .map(|name| core_comms.get_address_for(&name)) + .transpose()?; + + Ok(AzureBridge { + state: Arc::new(State { + az_client: Mutex::new(az_client), + cancellation_token, + c2d_receiver, + }), + } + .finish()) + } +} + +struct AzureBridge { + state: Arc<State>, +} + +struct State { + az_client: Mutex<IoTHubClient>, + cancellation_token: CancellationToken, + c2d_receiver: Option<Address<Cloud2DeviceMessageReceiver>>, +} + +impl PluginDeclaration for AzureBridge { + type HandledMessages = (Measurement,); +} + +#[async_trait] +impl Plugin for AzureBridge { + async fn start(&mut self) -> Result<(), PluginError> { + let mut client = self.state.az_client.lock().await; + let mut receiver = client.get_receiver().await; + + let cancel = self.state.cancellation_token.child_token(); + let state = self.state.clone(); + tokio::spawn(async move { + loop { + tokio::select! { + message = receiver.recv() => { + match message { + None => break, + Some(message) => { + tokio::spawn(handle_message(message, state.clone())); + } + } + } + _ = cancel.cancelled() => { + break; + } + } + } + }); + + Ok(()) + } + + async fn shutdown(&mut self) -> Result<(), PluginError> { + Ok(()) + } +} + +#[derive(Debug)] +pub struct Cloud2DeviceMessage { + pub data: Vec<u8>, +} + +impl Message for Cloud2DeviceMessage { + type Reply = NoReply; +} + +make_receiver_bundle!(struct Cloud2DeviceMessageReceiver(Cloud2DeviceMessage)); + +impl Cloud2DeviceMessage { + /// Get a reference to the cloud2 device message's data. + #[must_use] + pub fn data(&self) -> &[u8] { + self.data.as_ref() + } +} + +async fn handle_message(message: azure_iot_sdk::MessageType, client: Arc<State>) { + match message { + azure_iot_sdk::MessageType::C2DMessage(cloud_message) => { + tracing::event!( + tracing::Level::TRACE, + "Received Cloud2Device message of {} bytes", + cloud_message.body.len() + ); + + if let Some(addr) = client.c2d_receiver.as_ref() { + tracing::trace!("Sending cloud message to target"); + if let Err(_) = addr + .send(Cloud2DeviceMessage { + data: cloud_message.body, + }) + .await + { + tracing::error!( + "Could not forward Cloud2Device message as the recipient has disappeared!" + ); + } + } else { + tracing::warn!("Could not forward Cloud2Device message as no recipient has been set. Discarding the message."); + } + } + azure_iot_sdk::MessageType::DesiredPropertyUpdate(_) => (), + azure_iot_sdk::MessageType::DirectMethod(_) => (), + azure_iot_sdk::MessageType::ErrorReceive(err) => { + tracing::warn!("Received an error: {:?}", err) + } + } +} + +#[async_trait] +impl Handle<Measurement> for AzureBridge { + async fn handle_message( + &self, + message: Measurement, + _sender: ReplySender<<Measurement as Message>::Reply>, + ) -> Result<(), PluginError> { + let mut client = self.state.az_client.lock().await; + + let message = IoTMessage::builder() + .set_body(serde_json::to_vec(&message).map_err(AzureError::from)?) + .set_content_type("application/json".to_string()) + .set_content_encoding("UTF-8".to_string()) + .build(); + + client + .send_message(message) + .await + .map_err(|e| AzureError::MessageSend(e.to_string()))?; + + tracing::info!("Sent message to Azure!"); + + Ok(()) + } +} diff --git a/tedge/Cargo.toml b/tedge/Cargo.toml index bc6f0064..a61c3774 100644 --- a/tedge/Cargo.toml +++ b/ |