|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +use std::str::FromStr; |
| 5 | + |
| 6 | +use azure_core::{credentials::Secret, fmt::SafeDebug, Error}; |
| 7 | + |
| 8 | +/// Represents a Cosmos DB connection string. |
| 9 | +#[derive(Clone, PartialEq, Eq, SafeDebug)] |
| 10 | +pub struct ConnectionString { |
| 11 | + pub account_endpoint: String, |
| 12 | + pub account_key: Secret, |
| 13 | +} |
| 14 | + |
| 15 | +impl TryFrom<&Secret> for ConnectionString { |
| 16 | + type Error = azure_core::Error; |
| 17 | + fn try_from(secret: &Secret) -> Result<Self, Self::Error> { |
| 18 | + secret.secret().parse() |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +impl FromStr for ConnectionString { |
| 23 | + type Err = azure_core::Error; |
| 24 | + fn from_str(connection_string: &str) -> Result<Self, Self::Err> { |
| 25 | + if connection_string.is_empty() { |
| 26 | + return Err(Error::new( |
| 27 | + azure_core::error::ErrorKind::Other, |
| 28 | + "connection string cannot be empty", |
| 29 | + )); |
| 30 | + } |
| 31 | + |
| 32 | + let splat = connection_string.split(';'); |
| 33 | + let mut account_endpoint = None; |
| 34 | + let mut account_key = None; |
| 35 | + for part in splat { |
| 36 | + let part = part.trim(); |
| 37 | + if part.is_empty() { |
| 38 | + continue; |
| 39 | + } |
| 40 | + |
| 41 | + let (key, value) = part.split_once('=').ok_or(Error::new( |
| 42 | + azure_core::error::ErrorKind::Other, |
| 43 | + "invalid connection string", |
| 44 | + ))?; |
| 45 | + |
| 46 | + if key.eq_ignore_ascii_case("AccountEndpoint") { |
| 47 | + account_endpoint = Some(value.to_string()) |
| 48 | + } |
| 49 | + |
| 50 | + if key.eq_ignore_ascii_case("AccountKey") { |
| 51 | + account_key = Some(Secret::new(value.to_string())) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + let Some(endpoint) = account_endpoint else { |
| 56 | + return Err(Error::new( |
| 57 | + azure_core::error::ErrorKind::Other, |
| 58 | + "invalid connection string, missing 'AccountEndpoint'", |
| 59 | + )); |
| 60 | + }; |
| 61 | + |
| 62 | + let Some(key) = account_key else { |
| 63 | + return Err(Error::new( |
| 64 | + azure_core::error::ErrorKind::Other, |
| 65 | + "invalid connection string, missing 'AccountKey'", |
| 66 | + )); |
| 67 | + }; |
| 68 | + |
| 69 | + Ok(Self { |
| 70 | + account_endpoint: endpoint, |
| 71 | + account_key: key, |
| 72 | + }) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +#[cfg(test)] |
| 77 | +mod tests { |
| 78 | + use super::ConnectionString; |
| 79 | + use azure_core::credentials::Secret; |
| 80 | + |
| 81 | + #[test] |
| 82 | + pub fn test_valid_connection_string() { |
| 83 | + let connection_string = |
| 84 | + "AccountEndpoint=https://accountname.documents.azure.com:443/;AccountKey=key"; |
| 85 | + let secret = Secret::new(connection_string); |
| 86 | + let connection_str = ConnectionString::try_from(&secret).unwrap(); |
| 87 | + |
| 88 | + assert_eq!( |
| 89 | + "https://accountname.documents.azure.com:443/", |
| 90 | + connection_str.account_endpoint |
| 91 | + ); |
| 92 | + |
| 93 | + assert_eq!("key", connection_str.account_key.secret()); |
| 94 | + } |
| 95 | + |
| 96 | + #[test] |
| 97 | + pub fn test_valid_connection_string_mismatched_case() { |
| 98 | + let connection_string = |
| 99 | + "accountendpoint=https://accountname.documents.azure.com:443/;accountkey=key"; |
| 100 | + let secret = Secret::new(connection_string); |
| 101 | + let connection_str = ConnectionString::try_from(&secret).unwrap(); |
| 102 | + |
| 103 | + // should pass, we don't expect connection string keys to be case sensitive |
| 104 | + assert_eq!( |
| 105 | + "https://accountname.documents.azure.com:443/", |
| 106 | + connection_str.account_endpoint |
| 107 | + ); |
| 108 | + |
| 109 | + assert_eq!("key", connection_str.account_key.secret()); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + pub fn test_empty_connection_string() { |
| 114 | + test_bad_connection_string("", "connection string cannot be empty") |
| 115 | + } |
| 116 | + |
| 117 | + #[test] |
| 118 | + pub fn test_malformed_connection_string() { |
| 119 | + test_bad_connection_string( |
| 120 | + "AccountEndpointhttps://accountname.documents.azure.com:443AccountKeyaccountkey", |
| 121 | + "invalid connection string", |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + #[test] |
| 126 | + pub fn test_partially_malformed_connection_string() { |
| 127 | + test_bad_connection_string( |
| 128 | + "AccountEndpointhttps://accountname.documents.azure.com:443/AccountKey=accountkey", |
| 129 | + "invalid connection string, missing 'AccountEndpoint'", |
| 130 | + ); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + pub fn test_connection_string_missing_account_endpoint() { |
| 135 | + test_bad_connection_string( |
| 136 | + "AccountKey=key", |
| 137 | + "invalid connection string, missing 'AccountEndpoint'", |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + #[test] |
| 142 | + pub fn test_connection_string_missing_account_key() { |
| 143 | + test_bad_connection_string( |
| 144 | + "AccountEndpoint=https://accountname.documents.azure.com:443/;", |
| 145 | + "invalid connection string, missing 'AccountKey'", |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + fn test_bad_connection_string(connection_string: &str, expected_error_message: &str) { |
| 150 | + let secret = Secret::new(connection_string.to_owned()); |
| 151 | + let connection_str = ConnectionString::try_from(&secret); |
| 152 | + let err = connection_str.unwrap_err(); |
| 153 | + let actual_error_message = format!("{}", err); |
| 154 | + assert_eq!(expected_error_message, actual_error_message.as_str()) |
| 155 | + } |
| 156 | +} |
0 commit comments