From f2e05986c4d8cdf4abfde92e51e36f456e276ccd Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 24 Apr 2020 17:46:52 +0000 Subject: Starting to be S3 compatible --- Cargo.lock | 111 ++++++++++++++++++ TODO | 3 +- src/api/Cargo.toml | 6 + src/api/api_server.rs | 140 +++++++++++++++++------ src/api/lib.rs | 1 + src/api/signature.rs | 307 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/util/config.rs | 9 +- src/util/error.rs | 4 + 8 files changed, 543 insertions(+), 38 deletions(-) create mode 100644 src/api/signature.rs diff --git a/Cargo.lock b/Cargo.lock index 46533dec..aa3fb4cf 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,6 +106,16 @@ name = "cfg-if" version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "chrono" +version = "0.4.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", + "time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "clap" version = "2.33.0" @@ -156,6 +166,15 @@ dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "crypto-mac" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)", + "subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "digest" version = "0.8.1" @@ -342,16 +361,22 @@ name = "garage_api" version = "0.1.0" dependencies = [ "bytes 0.4.12 (registry+https://github.com/rust-lang/crates.io-index)", + "chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)", + "crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "futures-util 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "garage_core 0.1.0", "garage_table 0.1.0", "garage_util 0.1.0", "hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", "http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", + "httpdate 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", "hyper 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", + "sha2 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)", + "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -512,6 +537,15 @@ name = "hex" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "hmac" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "http" version = "0.2.1" @@ -536,6 +570,11 @@ name = "httparse" version = "1.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "humantime" version = "1.3.0" @@ -582,6 +621,16 @@ dependencies = [ "webpki 0.21.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "idna" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "indexmap" version = "1.3.2" @@ -646,6 +695,11 @@ dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "maybe-uninit" version = "2.0.0" @@ -713,6 +767,15 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "num-integer" +version = "0.1.42" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "num-traits" version = "0.2.11" @@ -757,6 +820,11 @@ dependencies = [ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "pin-project" version = "0.4.9" @@ -1094,6 +1162,11 @@ dependencies = [ "syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "subtle" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "syn" version = "1.0.17" @@ -1237,6 +1310,22 @@ name = "typenum" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "smallvec 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-segmentation" version = "1.6.0" @@ -1257,6 +1346,16 @@ name = "untrusted" version = "0.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "url" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", + "percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "version_check" version = "0.9.1" @@ -1406,11 +1505,13 @@ dependencies = [ "checksum bytes 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" "checksum cc 1.0.51 (registry+https://github.com/rust-lang/crates.io-index)" = "9c9384ca4b90c0ea47e19a5c996d6643a3e73dedf9b89c65efb67587e34da1bb" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" +"checksum chrono 0.4.11 (registry+https://github.com/rust-lang/crates.io-index)" = "80094f509cf8b5ae86a4966a39b3ff66cd7e2a3e594accec3743ff3fabeab5b2" "checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" "checksum cloudabi 0.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ddfc5b9aa5d4507acaf872de71051dfd0e309860e88966e1051e462a077aac4f" "checksum crc32fast 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ba125de2af0df55319f41944744ad91c71113bf74a4646efff39afe1f6842db1" "checksum crossbeam-epoch 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" "checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +"checksum crypto-mac 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4434400df11d95d556bac068ddfedd482915eb18fe8bea89bc80b6e4b1c179e5" "checksum digest 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f3d0c8c8752312f9713efd397ff63acb9f85585afbf179282e720e7704954dd5" "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36" "checksum err-derive 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "22deed3a8124cff5fa835713fa105621e43bbdc46690c3a6b68328a012d350d4" @@ -1436,12 +1537,15 @@ dependencies = [ "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hermit-abi 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)" = "8a0d737e0f947a1864e93d33fdef4af8445a00d1ed8dc0c8ddb73139ea6abf15" "checksum hex 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "805026a5d0141ffc30abb3be3173848ad46a1b1664fe632428479619a3644d77" +"checksum hmac 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5dcb5e64cda4c23119ab41ba960d1e170a774c8e4b9d9e6a9bc18aabf5e59695" "checksum http 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "28d569972648b2c512421b5f2a405ad6ac9666547189d0c5477a3f200f3e02f9" "checksum http-body 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "13d5ff830006f7646652e057693569bfe0d51760c0085a071769d142a205111b" "checksum httparse 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9" +"checksum httpdate 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" "checksum humantime 1.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f" "checksum hyper 0.13.5 (registry+https://github.com/rust-lang/crates.io-index)" = "96816e1d921eca64d208a85aab4f7798455a8e34229ee5a88c935bdee1b78b14" "checksum hyper-rustls 0.20.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac965ea399ec3a25ac7d13b8affd4b8f39325cca00858ddf5eb29b79e6b14b08" +"checksum idna 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "02e2673c30ee86b5b96a9cb52ad15718aa1f966f5ab9ad54a8b95d5ca33120a9" "checksum indexmap 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "076f042c5b7b98f31d205f1249267e12a6518c1481e9dae9764af19b707d2292" "checksum iovec 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b2b3ea6ff95e175473f8ffe6a7eb7c00d054240321b84c57051175fe3c1e075e" "checksum itoa 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)" = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" @@ -1451,6 +1555,7 @@ dependencies = [ "checksum libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)" = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" "checksum lock_api 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "c4da24a77a3d8a6d4862d95f72e6fdb9c09a643ecdb402d754004a557f2bec75" "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7" +"checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" "checksum maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" "checksum memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" "checksum memoffset 0.5.4 (registry+https://github.com/rust-lang/crates.io-index)" = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" @@ -1458,11 +1563,13 @@ dependencies = [ "checksum mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)" = "966257a94e196b11bb43aca423754d87429960a768de9414f3691d6957abf125" "checksum miow 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f2f3b1cf331de6896aabf6e9d55dca90356cc9960cca7eaaf408a355ae919" "checksum net2 0.2.33 (registry+https://github.com/rust-lang/crates.io-index)" = "42550d9fb7b6684a6d404d9fa7250c2eb2646df731d1c06afc06dcee9e1bcf88" +"checksum num-integer 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "3f6ea62e9d81a77cd3ee9a2a5b9b609447857f3d358704331e4ef39eb247fcba" "checksum num-traits 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)" = "c62be47e61d1842b9170f0fdeec8eba98e60e90e5446449a0545e5152acd7096" "checksum num_cpus 1.13.0 (registry+https://github.com/rust-lang/crates.io-index)" = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" "checksum opaque-debug 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2839e79665f131bdb5782e51f2c6c9599c133c6098982a54c794358bf432529c" "checksum parking_lot 0.10.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a704eb390aafdc107b0e392f56a82b668e3a71366993b5340f5833fd62505e" "checksum parking_lot_core 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d58c7c768d4ba344e3e8d72518ac13e259d7c7ade24167003b8488e10b6740a3" +"checksum percent-encoding 2.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" "checksum pin-project 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "6f6a7f5eee6292c559c793430c55c00aea9d3b3d1905e855806ca4d7253426a2" "checksum pin-project-internal 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)" = "8988430ce790d8682672117bc06dda364c0be32d3abd738234f19f3240bad99a" "checksum pin-project-lite 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)" = "237844750cfbb86f67afe27eee600dfbbcb6188d734139b534cbfbf4f96792ae" @@ -1503,6 +1610,7 @@ dependencies = [ "checksum spin 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d" "checksum structopt 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef" "checksum structopt-derive 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a" +"checksum subtle 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2d67a5a62ba6e01cb2192ff309324cb4875d0c451d55fe2319433abe7a05a8ee" "checksum syn 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "0df0eb663f387145cab623dea85b09c2c5b4b0aef44e945d928e682fce71bb03" "checksum syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" "checksum synstructure 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "67656ea1dc1b41b1451851562ea232ec2e5a80242139f7e679ceccfb5d61f545" @@ -1518,10 +1626,13 @@ dependencies = [ "checksum tower-service 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" "checksum typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" +"checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +"checksum unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum untrusted 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60369ef7a31de49bcb3f6ca728d4ba7300d9a1658f94c727d4cab8c8d9f4aece" +"checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum want 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" diff --git a/TODO b/TODO index e47be9ab..1644f6af 100644 --- a/TODO +++ b/TODO @@ -8,11 +8,10 @@ We will have to introduce lots of dummy data and then add/remove nodes many time Attaining S3 compatibility -------------------------- -- table for access keys -- S3 request signature verification - api_server following the S3 semantics for get/put/delete - implement object listing - possibly other necessary endpoints ? +- PUT requests: verify content-md5 if provided Lower priority diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml index 3606c1df..0da02338 100644 --- a/src/api/Cargo.toml +++ b/src/api/Cargo.toml @@ -17,6 +17,10 @@ garage_core = { path = "../core" } bytes = "0.4" hex = "0.3" log = "0.4" +chrono = "0.4" +sha2 = "0.8" +hmac = "0.7" +crypto-mac = "0.7" futures = "0.3" futures-util = "0.3" @@ -24,5 +28,7 @@ tokio = { version = "0.2", default-features = false, features = ["rt-core", "rt- http = "0.2" hyper = "0.13" +url = "2.1" +httpdate = "0.3" diff --git a/src/api/api_server.rs b/src/api/api_server.rs index de79ffc2..97cc6b93 100644 --- a/src/api/api_server.rs +++ b/src/api/api_server.rs @@ -1,6 +1,7 @@ use std::collections::VecDeque; use std::net::SocketAddr; use std::sync::Arc; +use std::time::{Duration, UNIX_EPOCH}; use futures::future::Future; use futures::stream::*; @@ -21,6 +22,7 @@ use garage_core::object_table::*; use garage_core::version_table::*; use crate::http_util::*; +use crate::signature::check_signature; type BodyType = Box + Send + Unpin>; @@ -28,7 +30,7 @@ pub async fn run_api_server( garage: Arc, shutdown_signal: impl Future, ) -> Result<(), Error> { - let addr = &garage.config.api_bind_addr; + let addr = &garage.config.s3_api.api_bind_addr; let service = make_service_fn(|conn: &AddrStream| { let garage = garage.clone(); @@ -55,12 +57,18 @@ async fn handler( req: Request, addr: SocketAddr, ) -> Result, Error> { - match handler_inner(garage, req, addr).await { - Ok(x) => Ok(x), + info!("{} {} {}", addr, req.method(), req.uri()); + debug!("{:?}", req); + match handler_inner(garage, req).await { + Ok(x) => { + debug!("{} {:?}", x.status(), x.headers()); + Ok(x) + } Err(e) => { let body: BodyType = Box::new(BytesBody::from(format!("{}\n", e))); let mut http_error = Response::new(body); *http_error.status_mut() = e.http_status_code(); + warn!("Response: error {}, {}", e.http_status_code(), e); Ok(http_error) } } @@ -69,38 +77,59 @@ async fn handler( async fn handler_inner( garage: Arc, req: Request, - addr: SocketAddr, ) -> Result, Error> { - info!("{} {} {}", addr, req.method(), req.uri()); - - let bucket = req - .headers() - .get(hyper::header::HOST) - .map(|x| x.to_str().map_err(Error::from)) - .unwrap_or(Err(Error::BadRequest(format!("Host: header missing"))))? - .to_lowercase(); - let key = req.uri().path().to_string(); - - match req.method() { - &Method::GET => Ok(handle_get(garage, &bucket, &key).await?), - &Method::PUT => { - let mime_type = req - .headers() - .get(hyper::header::CONTENT_TYPE) - .map(|x| x.to_str()) - .unwrap_or(Ok("blob"))? - .to_string(); - let version_uuid = - handle_put(garage, &mime_type, &bucket, &key, req.into_body()).await?; - let response = format!("{}\n", hex::encode(version_uuid,)); - Ok(Response::new(Box::new(BytesBody::from(response)))) + let path = req.uri().path().to_string(); + let path = path.trim_start_matches('/'); + let (bucket, key) = match path.find('/') { + Some(i) => { + let (bucket, key) = path.split_at(i); + (bucket, Some(key)) } - &Method::DELETE => { - let version_uuid = handle_delete(garage, &bucket, &key).await?; - let response = format!("{}\n", hex::encode(version_uuid,)); - Ok(Response::new(Box::new(BytesBody::from(response)))) + None => (path, None), + }; + if bucket.len() == 0 { + return Err(Error::Forbidden(format!( + "Operations on buckets not allowed" + ))); + } + + let api_key = check_signature(&garage, &req).await?; + let allowed = match req.method() { + &Method::HEAD | &Method::GET => api_key.allow_read(&bucket), + _ => api_key.allow_write(&bucket), + }; + if !allowed { + return Err(Error::Forbidden(format!( + "Operation is not allowed for this key." + ))); + } + + if let Some(key) = key { + match req.method() { + &Method::HEAD => Ok(handle_head(garage, &bucket, &key).await?), + &Method::GET => Ok(handle_get(garage, &bucket, &key).await?), + &Method::PUT => { + let mime_type = req + .headers() + .get(hyper::header::CONTENT_TYPE) + .map(|x| x.to_str()) + .unwrap_or(Ok("blob"))? + .to_string(); + let version_uuid = + handle_put(garage, &mime_type, &bucket, &key, req.into_body()).await?; + let response = format!("{}\n", hex::encode(version_uuid,)); + Ok(Response::new(Box::new(BytesBody::from(response)))) + } + &Method::DELETE => { + let version_uuid = handle_delete(garage, &bucket, &key).await?; + let response = format!("{}\n", hex::encode(version_uuid,)); + Ok(Response::new(Box::new(BytesBody::from(response)))) + } + _ => Err(Error::BadRequest(format!("Invalid method"))), } - _ => Err(Error::BadRequest(format!("Invalid method"))), + } else { + // TODO: listing buckets + Err(Error::Forbidden("Unimplemented".into())) } } @@ -280,6 +309,49 @@ async fn handle_delete(garage: Arc, bucket: &str, key: &str) -> Result http::response::Builder { + let date = UNIX_EPOCH + Duration::from_millis(version.timestamp); + let date_str = httpdate::fmt_http_date(date); + + Response::builder() + .header("Content-Type", version.mime_type.to_string()) + .header("Content-Length", format!("{}", version.size)) + .header("Last-Modified", date_str) +} + +async fn handle_head( + garage: Arc, + bucket: &str, + key: &str, +) -> Result, Error> { + let object = match garage + .object_table + .get(&bucket.to_string(), &key.to_string()) + .await? + { + None => return Err(Error::NotFound), + Some(o) => o, + }; + + let version = match object + .versions() + .iter() + .rev() + .filter(|v| v.is_complete && v.data != ObjectVersionData::DeleteMarker) + .next() + { + Some(v) => v, + None => return Err(Error::NotFound), + }; + + let body: BodyType = Box::new(BytesBody::from(vec![])); + let response = object_headers(&version) + .status(StatusCode::OK) + .body(body) + .unwrap(); + Ok(response) +} + async fn handle_get( garage: Arc, bucket: &str, @@ -305,9 +377,7 @@ async fn handle_get( None => return Err(Error::NotFound), }; - let resp_builder = Response::builder() - .header("Content-Type", last_v.mime_type.to_string()) - .status(StatusCode::OK); + let resp_builder = object_headers(&last_v).status(StatusCode::OK); match &last_v.data { ObjectVersionData::DeleteMarker => Err(Error::NotFound), diff --git a/src/api/lib.rs b/src/api/lib.rs index b313d45d..3bcd02f1 100644 --- a/src/api/lib.rs +++ b/src/api/lib.rs @@ -3,3 +3,4 @@ extern crate log; pub mod api_server; pub mod http_util; +pub mod signature; diff --git a/src/api/signature.rs b/src/api/signature.rs new file mode 100644 index 00000000..2e82269c --- /dev/null +++ b/src/api/signature.rs @@ -0,0 +1,307 @@ +use std::collections::HashMap; + +use chrono::{DateTime, Duration, NaiveDateTime, Utc}; +use hmac::{Hmac, Mac}; +use hyper::{Body, Method, Request}; +use sha2::{Digest, Sha256}; + +use garage_table::*; +use garage_util::error::Error; + +use garage_core::garage::Garage; +use garage_core::key_table::*; + +const SHORT_DATE: &str = "%Y%m%d"; +const LONG_DATETIME: &str = "%Y%m%dT%H%M%SZ"; + +type HmacSha256 = Hmac; + +pub async fn check_signature(garage: &Garage, request: &Request) -> Result { + let mut headers = HashMap::new(); + for (key, val) in request.headers() { + headers.insert(key.to_string(), val.to_str()?.to_string()); + } + if let Some(query) = request.uri().query() { + let query_pairs = url::form_urlencoded::parse(query.as_bytes()); + for (key, val) in query_pairs { + headers.insert(key.to_lowercase(), val.to_string()); + } + } + + let authorization = if let Some(authorization) = headers.get("authorization") { + parse_authorization(authorization, &headers)? + } else { + parse_query_authorization(&headers)? + }; + + let date = headers + .get("x-amz-date") + .ok_or(Error::BadRequest("Missing X-Amz-Date field".into()))?; + let date: NaiveDateTime = NaiveDateTime::parse_from_str(date, LONG_DATETIME) + .map_err(|e| Error::BadRequest(format!("Invalid date: {}", e)))? + .into(); + let date: DateTime = DateTime::from_utc(date, Utc); + + if Utc::now() - date > Duration::hours(24) { + return Err(Error::BadRequest(format!("Date is too old"))); + } + + let scope = format!( + "{}/{}/s3/aws4_request", + date.format(SHORT_DATE), + garage.config.s3_api.s3_region + ); + if authorization.scope != scope { + return Err(Error::BadRequest(format!( + "Invalid scope in authorization field, expected: {}", + scope + ))); + } + + let key = garage + .key_table + .get(&EmptyKey, &authorization.key_id) + .await? + .filter(|k| !k.deleted) + .ok_or(Error::Forbidden(format!( + "No such key: {}", + authorization.key_id + )))?; + + let canonical_request = canonical_request( + request.method(), + &request.uri().path().to_string(), + &canonical_query_string(&request.uri()), + &headers, + &authorization.signed_headers, + &authorization.content_sha256, + ); + let string_to_sign = string_to_sign(&date, &scope, &canonical_request); + + let mut hmac = signing_hmac( + &date, + &key.secret_key, + &garage.config.s3_api.s3_region, + "s3", + ) + .map_err(|e| Error::Message(format!("Unable to build signing HMAC: {}", e)))?; + hmac.input(string_to_sign.as_bytes()); + let signature = hex::encode(hmac.result().code()); + + if authorization.signature != signature { + return Err(Error::Forbidden(format!("Invalid signature"))); + } + + Ok(key) +} + +struct Authorization { + key_id: String, + scope: String, + signed_headers: String, + signature: String, + content_sha256: String, +} + +fn parse_authorization( + authorization: &str, + headers: &HashMap, +) -> Result { + let first_space = authorization + .find(' ') + .ok_or(Error::BadRequest("Authorization field too short".into()))?; + let (auth_kind, rest) = authorization.split_at(first_space); + + if auth_kind != "AWS4-HMAC-SHA256" { + return Err(Error::BadRequest("Unsupported authorization method".into())); + } + + let mut auth_params = HashMap::new(); + for auth_part in rest.split(',') { + let auth_part = auth_part.trim(); + let eq = auth_part.find('=').ok_or(Error::BadRequest(format!( + "Missing =value in authorization field {}", + auth_part + )))?; + let (key, value) = auth_part.split_at(eq); + auth_params.insert(key.to_string(), value.trim_start_matches('=').to_string()); + } + + let cred = auth_params + .get("Credential") + .ok_or(Error::BadRequest(format!( + "Could not find Credential in Authorization field" + )))?; + let (key_id, scope) = parse_credential(cred)?; + + let content_sha256 = headers + .get("x-amz-content-sha256") + .ok_or(Error::BadRequest( + "Missing X-Amz-Content-Sha256 field".into(), + ))?; + + let auth = Authorization { + key_id, + scope, + signed_headers: auth_params + .get("SignedHeaders") + .ok_or(Error::BadRequest(format!( + "Could not find SignedHeaders in Authorization field" + )))? + .to_string(), + signature: auth_params + .get("Signature") + .ok_or(Error::BadRequest(format!( + "Could not find Signature in Authorization field" + )))? + .to_string(), + content_sha256: content_sha256.to_string(), + }; + Ok(auth) +} + +fn parse_query_authorization(headers: &HashMap) -> Result { + let algo = headers + .get("x-amz-algorithm") + .ok_or(Error::BadRequest(format!( + "X-Amz-Algorithm not found in query parameters" + )))?; + if algo != "AWS4-HMAC-SHA256" { + return Err(Error::BadRequest(format!( + "Unsupported authorization method" + ))); + } + + let cred = headers + .get("x-amz-credential") + .ok_or(Error::BadRequest(format!( + "X-Amz-Credential not found in query parameters" + )))?; + let (key_id, scope) = parse_credential(cred)?; + let signed_headers = headers + .get("x-amz-signedheaders") + .ok_or(Error::BadRequest(format!( + "X-Amz-SignedHeaders not found in query parameters" + )))?; + let signature = headers + .get("x-amz-signature") + .ok_or(Error::BadRequest(format!( + "X-Amz-Signature not found in query parameters" + )))?; + + Ok(Authorization { + key_id, + scope, + signed_headers: signed_headers.to_string(), + signature: signature.to_string(), + content_sha256: "UNSIGNED-PAYLOAD".to_string(), + }) +} + +fn parse_credential(cred: &str) -> Result<(String, String), Error> { + let first_slash = cred.find('/').ok_or(Error::BadRequest(format!( + "Credentials does not contain / in authorization field" + )))?; + let (key_id, scope) = cred.split_at(first_slash); + Ok(( + key_id.to_string(), + scope.trim_start_matches('/').to_string(), + )) +} + +fn string_to_sign(datetime: &DateTime, scope_string: &str, canonical_req: &str) -> String { + let mut hasher = Sha256::default(); + hasher.input(canonical_req.as_bytes()); + [ + "AWS4-HMAC-SHA256", + &datetime.format(LONG_DATETIME).to_string(), + scope_string, + &hex::encode(hasher.result().as_slice()), + ] + .join("\n") +} + +fn signing_hmac( + datetime: &DateTime, + secret_key: &str, + region: &str, + service: &str, +) -> Result { + let secret = String::from("AWS4") + secret_key; + let mut date_hmac = HmacSha256::new_varkey(secret.as_bytes())?; + date_hmac.input(datetime.format(SHORT_DATE).to_string().as_bytes()); + let mut region_hmac = HmacSha256::new_varkey(&date_hmac.result().code())?; + region_hmac.input(region.as_bytes()); + let mut service_hmac = HmacSha256::new_varkey(®ion_hmac.result().code())?; + service_hmac.input(service.as_bytes()); + let mut signing_hmac = HmacSha256::new_varkey(&service_hmac.result().code())?; + signing_hmac.input(b"aws4_request"); + let hmac = HmacSha256::new_varkey(&signing_hmac.result().code())?; + Ok(hmac) +} + +fn canonical_request( + method: &Method, + url_path: &str, + canonical_query_string: &str, + headers: &HashMap, + signed_headers: &str, + content_sha256: &str, +) -> String { + [ + method.as_str(), + url_path, + canonical_query_string, + &canonical_header_string(&headers, signed_headers), + "", + signed_headers, + content_sha256, + ] + .join("\n") +} + +fn canonical_header_string(headers: &HashMap, signed_headers: &str) -> String { + let signed_headers_vec = signed_headers.split(';').collect::>(); + let mut items = headers + .iter() + .filter(|(key, _)| signed_headers_vec.contains(&key.as_str())) + .map(|(key, value)| key.to_lowercase() + ":" + value.trim()) + .collect::>(); + items.sort(); + items.join("\n") +} + +fn canonical_query_string(uri: &hyper::Uri) -> String { + if let Some(query) = uri.query() { + let query_pairs = url::form_urlencoded::parse(query.as_bytes()); + let mut items = query_pairs + .filter(|(key, _)| key != "X-Amz-Signature") + .map(|(key, value)| uri_encode(&key, true) + "=" + &uri_encode(&value, true)) + .collect::>(); + items.sort(); + items.join("&") + } else { + "".to_string() + } +} + +fn uri_encode(string: &str, encode_slash: bool) -> String { + let mut result = String::with_capacity(string.len() * 2); + for c in string.chars() { + match c { + 'a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '~' | '.' => result.push(c), + '/' if encode_slash => result.push_str("%2F"), + '/' if !encode_slash => result.push('/'), + _ => { + result.push('%'); + result.push_str( + &format!("{}", c) + .bytes() + .map(|b| format!("{:02X}", b)) + .collect::(), + ); + } + } + } + result +} diff --git a/src/util/config.rs b/src/util/config.rs index cb871562..28349530 100644 --- a/src/util/config.rs +++ b/src/util/config.rs @@ -11,7 +11,6 @@ pub struct Config { pub metadata_dir: PathBuf, pub data_dir: PathBuf, - pub api_bind_addr: SocketAddr, pub rpc_bind_addr: SocketAddr, pub bootstrap_peers: Vec, @@ -32,6 +31,8 @@ pub struct Config { pub data_replication_factor: usize, pub rpc_tls: Option, + + pub s3_api: ApiConfig, } #[derive(Deserialize, Debug, Clone)] @@ -41,6 +42,12 @@ pub struct TlsConfig { pub node_key: String, } +#[derive(Deserialize, Debug, Clone)] +pub struct ApiConfig { + pub api_bind_addr: SocketAddr, + pub s3_region: String, +} + fn default_max_concurrent_rpc_requests() -> usize { 12 } diff --git a/src/util/error.rs b/src/util/error.rs index f73d6915..cb99cbbc 100644 --- a/src/util/error.rs +++ b/src/util/error.rs @@ -69,6 +69,9 @@ pub enum Error { #[error(display = "Bad request: {}", _0)] BadRequest(String), + #[error(display = "Forbidden: {}", _0)] + Forbidden(String), + #[error(display = "Not found")] NotFound, @@ -84,6 +87,7 @@ impl Error { match self { Error::BadRequest(_) => StatusCode::BAD_REQUEST, Error::NotFound => StatusCode::NOT_FOUND, + Error::Forbidden(_) => StatusCode::FORBIDDEN, Error::RPC(_) => StatusCode::SERVICE_UNAVAILABLE, _ => StatusCode::INTERNAL_SERVER_ERROR, } -- cgit v1.2.3