-
Notifications
You must be signed in to change notification settings - Fork 92
/
Copy pathexpansion.rs
216 lines (195 loc) · 7.12 KB
/
expansion.rs
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
207
208
209
210
211
212
213
214
215
216
//! The code in this file is borrowed from the router for consistent syntax. As such, it is covered
//! by the [ELv2 license](https://www.apollographql.com/docs/resources/elastic-license-v2-faq/).
//! Before calling this code from other functions, make sure that the license is accepted (like
//! `supergraph compose`)
use anyhow::{anyhow, bail, Context, Error};
use serde_yaml::{Mapping, Sequence, Value};
use std::env;
use std::path::Path;
use rover_std::Fs;
use shellexpand::env_with_context;
use crate::RoverResult;
/// Implements router-config-style
/// [variable expansion](https://www.apollographql.com/docs/router/configuration/overview/#variable-expansion)
/// for a YAML mapping (e.g., an entire `supergraph.yaml` or `router.yaml`).
pub(crate) fn expand(value: Value) -> RoverResult<Value> {
match value {
Value::String(s) => expand_str(&s).map(Value::String),
Value::Null | Value::Bool(_) | Value::Number(_) | Value::Tagged(_) => Ok(value),
Value::Sequence(inner) => inner
.into_iter()
.map(expand)
.collect::<RoverResult<Sequence>>()
.map(Value::Sequence),
Value::Mapping(inner) => inner
.into_iter()
.map(|(key, value)| expand(value).map(|value| (key, value)))
.collect::<RoverResult<Mapping>>()
.map(Value::Mapping),
}
}
#[cfg(test)]
mod test_expand {
use serde_yaml::Value;
#[test]
fn mapping() {
let yaml = "supergraph:\n introspection: true\n listen: 0.0.0.0:${env.PORT:-4000}";
let value = serde_yaml::from_str(yaml).unwrap();
let expanded = super::expand(value).unwrap();
let expected: Value =
serde_yaml::from_str("supergraph:\n introspection: true\n listen: 0.0.0.0:4000")
.unwrap();
assert_eq!(expanded, expected);
}
/// A realish world test of complex expansion
#[test]
fn supergraph_config_header_injection() {
let yaml = r#"federation_version: =2.4.7
subgraphs:
products:
routing_url: http://localhost:4001
schema:
subgraph_url: http://localhost:4001
introspection_headers:
Router-Authorization: ${env.PRODUCTS_AUTHORIZATION:-test}
users:
routing_url: http://localhost:4002
schema:
subgraph_url: http://localhost:4002
introspection_headers:
Router-Authorization: ${env.USERS_AUTHORIZATION:-test2}"#;
let value = serde_yaml::from_str(yaml).unwrap();
let expanded = super::expand(value).unwrap();
let expected: Value = serde_yaml::from_str(
r#"federation_version: =2.4.7
subgraphs:
products:
routing_url: http://localhost:4001
schema:
subgraph_url: http://localhost:4001
introspection_headers:
Router-Authorization: test
users:
routing_url: http://localhost:4002
schema:
subgraph_url: http://localhost:4002
introspection_headers:
Router-Authorization: test2"#,
)
.unwrap();
assert_eq!(expanded, expected);
}
}
/// Implements router-config-style
/// [variable expansion](https://www.apollographql.com/docs/router/configuration/overview/#variable-expansion)
/// for a single value.
fn expand_str(value: &str) -> RoverResult<String> {
env_with_context(value, context)
.map_err(|e| anyhow!(e).context("While expanding variables").into())
.map(|cow| cow.into_owned())
}
fn context(key: &str) -> Result<Option<String>, Error> {
if let Some(env_var_key) = key.strip_prefix("env.") {
env::var(env_var_key).map(Some).with_context(|| {
format!(
"While reading env var {} for variable expansion",
env_var_key
)
})
} else if let Some(file_name) = key.strip_prefix("file.") {
if !Path::new(file_name).exists() {
Ok(None)
} else {
Ok(Fs::read_file(file_name).map(Some)?)
}
} else {
bail!("Invalid variable expansion key: {}", key)
}
}
#[cfg(test)]
mod test_expand_str {
use super::*;
use assert_fs::fixture::{FileWriteBin, FileWriteStr, NamedTempFile};
// Env vars are global, so if you're going to reuse them you'd better make them constants
// These point at each other for testing nested values
const ENV_VAR_KEY_1: &str = "RESOLVE_HEADER_VALUE_TEST_VAR_1";
const ENV_VAR_VALUE_1: &str = "RESOLVE_HEADER_VALUE_TEST_VAR_2";
const ENV_VAR_KEY_2: &str = "RESOLVE_HEADER_VALUE_TEST_VAR_2";
const ENV_VAR_VALUE_2: &str = "RESOLVE_HEADER_VALUE_TEST_VAR_1";
#[test]
fn valid_env_var() {
let value = format!("${{env.{}}}", ENV_VAR_KEY_1);
env::set_var(ENV_VAR_KEY_1, ENV_VAR_VALUE_1);
assert_eq!(expand_str(&value).unwrap(), ENV_VAR_VALUE_1);
}
#[test]
fn partial_env_var_partial_static() {
let value = format!("static-part-${{env.{}}}", ENV_VAR_KEY_1);
env::set_var(ENV_VAR_KEY_1, ENV_VAR_VALUE_1);
assert_eq!(
expand_str(&value).unwrap(),
format!("static-part-{}", ENV_VAR_VALUE_1)
);
}
#[test]
fn multiple_env_vars() {
let value = format!(
"${{env.{}}}-static-part-${{env.{}}}",
ENV_VAR_KEY_1, ENV_VAR_KEY_2
);
env::set_var(ENV_VAR_KEY_1, ENV_VAR_VALUE_1);
env::set_var(ENV_VAR_KEY_2, ENV_VAR_VALUE_2);
assert_eq!(
expand_str(&value).unwrap(),
format!("{}-static-part-{}", ENV_VAR_VALUE_1, ENV_VAR_VALUE_2)
);
}
#[test]
fn nested_env_vars() {
let value = format!("${{env.${{env.{}}}}}", ENV_VAR_KEY_1);
env::set_var(ENV_VAR_KEY_1, ENV_VAR_VALUE_1);
env::set_var(ENV_VAR_KEY_2, ENV_VAR_VALUE_2);
assert!(expand_str(&value).is_err());
}
#[test]
fn not_env_var() {
let value = "test_value";
assert_eq!(expand_str(value).unwrap(), value);
}
#[test]
fn env_var_not_found() {
let value = "${env.RESOLVE_HEADER_VALUE_TEST_VAR_DOES_NOT_EXIST}";
assert!(expand_str(value).is_err());
}
#[test]
fn missing_end_brace() {
let value = "${env.RESOLVE_HEADER_VALUE_TEST_VAR_DOES_NOT_EXIST";
assert_eq!(expand_str(value).unwrap(), value);
}
#[test]
fn missing_start_section() {
let value = "RESOLVE_HEADER_VALUE_TEST_VAR_DOES_NOT_EXIST}";
assert_eq!(expand_str(value).unwrap(), value);
}
#[test]
fn content_from_file() {
let temp = NamedTempFile::new("variable.txt").unwrap();
temp.write_str("test_value").unwrap();
let value = format!("${{file.{}}}", temp.path().to_str().unwrap());
assert_eq!(expand_str(&value).unwrap(), "test_value");
}
/// This behavior is copied from Router
#[test]
fn missing_file() {
let value = "${file.afilethatdefinitelydoesntexisthere}";
assert_eq!(expand_str(value).unwrap(), value);
}
#[test]
fn invalid_file() {
let temp = NamedTempFile::new("variable.txt").unwrap();
// Invalid UTF-8
temp.write_binary(&[0x80]).unwrap();
let value = format!("${{file.{}}}", temp.path().to_str().unwrap());
assert!(expand_str(&value).is_err());
}
}