1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::collections::hash_map::HashMap;
use std::fs::File;
use std::io;
use std::path::Path;
use serde::Deserialize;
use serde_json::Value;
use thiserror::Error;
use crate::handler::Handler;
#[derive(Deserialize, Debug)]
pub struct Config {
#[serde(default)]
pub(crate) post_paths: HashMap<String, Handler>,
secrets: Option<String>,
}
#[derive(Debug, Error)]
pub enum ConfigError {
#[error("deserialization error: {}", source)]
Deserialize {
#[source]
source: serde_json::Error,
},
#[error("failed to read file: {}", source)]
Read {
#[source]
source: io::Error,
},
}
impl Config {
pub fn from_path<P: AsRef<Path>>(path: P) -> Result<Self, ConfigError> {
let fin = File::open(path.as_ref()).map_err(|source| {
ConfigError::Read {
source,
}
})?;
serde_json::from_reader(fin).map_err(|source| {
ConfigError::Deserialize {
source,
}
})
}
pub(crate) fn secrets(&self) -> Result<Value, ConfigError> {
if let Some(ref path) = self.secrets {
let fin = File::open(path).map_err(|source| {
ConfigError::Read {
source,
}
})?;
serde_json::from_reader(fin).map_err(|source| {
ConfigError::Deserialize {
source,
}
})
} else {
Ok(Value::Null)
}
}
#[cfg(test)]
pub(crate) fn secrets_path(&self) -> Option<&String> {
self.secrets.as_ref()
}
}
#[cfg(test)]
mod test {
use std::fs::File;
use std::io::Write;
use serde_json::{json, Value};
use crate::config::{Config, ConfigError};
use crate::test_utils;
#[test]
fn test_config_empty_paths() {
let tempdir = test_utils::create_tempdir();
let path = test_utils::write_config(tempdir.path(), json!({}));
let config = Config::from_path(&path).unwrap();
assert!(config.post_paths.is_empty());
assert_eq!(config.secrets, None);
assert_eq!(config.secrets().unwrap(), Value::Null);
}
#[test]
fn test_config_unreadable() {
let path = {
let tempdir = test_utils::create_tempdir();
test_utils::write_config(tempdir.path(), json!({}))
};
let err = Config::from_path(path).unwrap_err();
if let ConfigError::Read {
..
} = err
{
} else {
panic!("unexpected error: {:?}", err);
}
}
#[test]
fn test_config_unparseable() {
let tempdir = test_utils::create_tempdir();
let path = tempdir.path().join("config.json");
{
let mut fout = File::create(&path).unwrap();
fout.write_all(b"not json\n").unwrap();
}
let err = Config::from_path(path).unwrap_err();
if let ConfigError::Deserialize {
..
} = err
{
} else {
panic!("unexpected error: {:?}", err);
}
}
#[test]
fn test_secrets_unreadable() {
let config = {
let tempdir = test_utils::create_tempdir();
let (path, _) = test_utils::write_config_secrets(tempdir.path(), json!({}), json!({}));
Config::from_path(path).unwrap()
};
let err = config.secrets().unwrap_err();
if let ConfigError::Read {
..
} = err
{
} else {
panic!("unexpected error: {:?}", err);
}
}
#[test]
fn test_secrets_unparseable() {
let tempdir = test_utils::create_tempdir();
let secrets_path = tempdir.path().join("secrets.json");
let path = test_utils::write_config(
tempdir.path(),
json!({
"secrets": secrets_path.to_str().unwrap(),
}),
);
{
let mut fout = File::create(&secrets_path).unwrap();
fout.write_all(b"not json\n").unwrap();
}
let config = Config::from_path(path).unwrap();
let err = config.secrets().unwrap_err();
if let ConfigError::Deserialize {
..
} = err
{
} else {
panic!("unexpected error: {:?}", err);
}
}
#[test]
fn test_config_parse() {
let tempdir = test_utils::create_tempdir();
let path = test_utils::write_config(
tempdir.path(),
json!({
"post_paths": {
"hostname.example": {
"path": "path",
"filters": [
{
"kind": "blah",
},
],
"header_name": "X-WebHook-Type",
},
},
}),
);
let config = Config::from_path(path).unwrap();
assert_eq!(config.post_paths.len(), 1);
}
}