about summary refs log tree commit diff
path: root/ops
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2023-06-13T19·47+0300
committertazjin <tazjin@tvl.su>2023-06-14T10·06+0000
commit2e893dca1dc8365dd2a244dda3f92f52c884a660 (patch)
tree2aa4663e6b4b7b726fa9c5e8c6b34d5072658224 /ops
parented388f019aa84eca634dbfbd1cfed2e153780aa7 (diff)
refactor(ops/yandex-cloud-rs): allow TokenProvider impls to fail r/6289
It's actually quite common that a token provider might fail, for
example when fetching a token from instance metadata.

Change-Id: Ie0126fb92c6c613ad36b5583fd68505fdd97f2c1
Reviewed-on: https://cl.tvl.fyi/c/depot/+/8764
Tested-by: BuildkiteCI
Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'ops')
-rw-r--r--ops/yandex-cloud-rs/src/lib.rs12
1 files changed, 6 insertions, 6 deletions
diff --git a/ops/yandex-cloud-rs/src/lib.rs b/ops/yandex-cloud-rs/src/lib.rs
index 5e92f05417..87a9164e07 100644
--- a/ops/yandex-cloud-rs/src/lib.rs
+++ b/ops/yandex-cloud-rs/src/lib.rs
@@ -52,18 +52,18 @@ pub mod tonic_exports {
 /// tokens for Yandex Cloud.
 pub trait TokenProvider {
     /// Fetch a currently valid authentication token for Yandex Cloud.
-    fn get_token<'a>(&'a mut self) -> &'a str;
+    fn get_token<'a>(&'a mut self) -> Result<&'a str, tonic::Status>;
 }
 
 impl TokenProvider for String {
-    fn get_token<'a>(&'a mut self) -> &'a str {
-        self.as_str()
+    fn get_token<'a>(&'a mut self) -> Result<&'a str, tonic::Status> {
+        Ok(self.as_str())
     }
 }
 
 impl TokenProvider for &'static str {
-    fn get_token(&mut self) -> &'static str {
-        *self
+    fn get_token(&mut self) -> Result<&'static str, tonic::Status> {
+        Ok(*self)
     }
 }
 
@@ -89,7 +89,7 @@ impl<T: TokenProvider> Interceptor for AuthInterceptor<T> {
         &mut self,
         mut request: tonic::Request<()>,
     ) -> Result<tonic::Request<()>, tonic::Status> {
-        let token: MetadataValue<Ascii> = format!("Bearer {}", self.token_provider.get_token())
+        let token: MetadataValue<Ascii> = format!("Bearer {}", self.token_provider.get_token()?)
             .try_into()
             .map_err(|_| {
                 tonic::Status::invalid_argument("authorization token contained invalid characters")