@@ -47,13 +47,33 @@ pub struct RedisConfig {
4747#[ derive( Clone , Debug , Deserialize ) ]
4848pub struct ElasticConfig {
4949 pub url : UrlPort ,
50- pub api_key : String ,
50+ pub api_key : Option < String > ,
51+ pub username : Option < String > ,
52+ pub password : Option < String > ,
5153 #[ serde( default = "default_chunk_size" ) ]
5254 pub chunk_size : u16 ,
5355 #[ serde( default = "default_index" ) ]
5456 pub index : String ,
5557}
5658
59+ impl ElasticConfig {
60+ pub fn credentials ( & self ) -> Result < elasticsearch:: auth:: Credentials , & str > {
61+ if let Some ( api_key) = & self . api_key {
62+ Ok ( elasticsearch:: auth:: Credentials :: EncodedApiKey (
63+ api_key. into ( ) ,
64+ ) )
65+ } else if let Some ( username) = & self . username
66+ && let Some ( password) = & self . password
67+ {
68+ Ok ( elasticsearch:: auth:: Credentials :: Basic (
69+ username. to_owned ( ) ,
70+ password. to_owned ( ) ,
71+ ) )
72+ } else {
73+ Err ( "No credentials in config!" )
74+ }
75+ }
76+ }
5777#[ derive( Clone , Debug , Deserialize ) ]
5878pub struct IngestorConfig {
5979 pub redis : RedisConfig ,
@@ -113,7 +133,7 @@ port = 9876
113133 let config: IngestorConfig = toml:: from_str ( & test_str) . unwrap ( ) ;
114134 assert_eq ! ( config. redis. url. full_url( ) , "http://127.0.0.1:12345" ) ;
115135 assert_eq ! ( config. elastic. url. full_url( ) , "http://127.0.0.1:9876" ) ;
116- assert_eq ! ( config. elastic. api_key, "abcdefgh==" ) ;
136+ assert_eq ! ( config. elastic. api_key, Some ( "abcdefgh==" . into ( ) ) ) ;
117137 }
118138
119139 #[ test]
@@ -138,7 +158,7 @@ api_key = \"testkey\"
138158" ;
139159 let elastic: ElasticConfig = toml:: from_str ( & test_str) . unwrap ( ) ;
140160 assert_eq ! ( elastic. chunk_size, 100 ) ;
141- assert_eq ! ( elastic. api_key, "testkey" ) ;
161+ assert_eq ! ( elastic. api_key, Some ( "testkey" . into ( ) ) ) ;
142162 assert_eq ! ( elastic. url. full_url( ) , "http://localhost:9200" ) ;
143163 }
144164
0 commit comments