interprex_github/
config.rs1use std::{collections::BTreeMap, fmt};
2
3use interprex::{ConfigurationSource, ProviderError, Result};
4use secrecy::SecretString;
5use serde::Deserialize;
6
7#[derive(Clone)]
8pub struct AppCredentials {
9 pub app_id: u64,
10 pub installation_id: u64,
11 pub private_key: SecretString,
12}
13
14impl fmt::Debug for AppCredentials {
15 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
16 formatter
17 .debug_struct("AppCredentials")
18 .field("app_id", &self.app_id)
19 .field("installation_id", &self.installation_id)
20 .field("private_key", &"[REDACTED]")
21 .finish()
22 }
23}
24
25#[derive(Clone, Default)]
26pub struct GithubConfig {
27 pub gh_token: Option<SecretString>,
28 pub apps: BTreeMap<String, AppCredentials>,
29 pub base_uri: Option<String>,
30 pub upload_uri: Option<String>,
31}
32
33impl fmt::Debug for GithubConfig {
34 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
35 formatter
36 .debug_struct("GithubConfig")
37 .field("gh_token", &self.gh_token.as_ref().map(|_| "[REDACTED]"))
38 .field("apps", &self.apps.keys().collect::<Vec<_>>())
39 .field("base_uri", &self.base_uri)
40 .field("upload_uri", &self.upload_uri)
41 .finish()
42 }
43}
44
45pub(crate) fn parse_project_config(
46 contents: &str,
47 source: &ConfigurationSource,
48) -> Result<GithubConfig> {
49 let file: ProjectFile =
50 toml::from_str(contents).map_err(|error| ProviderError::Configuration {
51 origin: source.clone(),
52 reason: error.message().to_owned(),
53 })?;
54 Ok(file.provider.github.into())
55}
56
57#[derive(Deserialize)]
58struct ProjectFile {
59 provider: ProviderTable,
60}
61
62#[derive(Deserialize)]
63struct ProviderTable {
64 github: GithubTable,
65}
66
67#[derive(Deserialize)]
68struct GithubTable {
69 #[serde(rename = "GH_TOKEN")]
70 gh_token: Option<String>,
71 #[serde(default)]
72 apps: BTreeMap<String, AppTable>,
73 #[serde(rename = "BASE_URI")]
74 base_uri: Option<String>,
75 #[serde(rename = "UPLOAD_URI")]
76 upload_uri: Option<String>,
77}
78
79#[derive(Deserialize)]
80struct AppTable {
81 #[serde(rename = "APP_ID")]
82 app_id: u64,
83 #[serde(rename = "INSTALLATION_ID")]
84 installation_id: u64,
85 #[serde(rename = "PRIVATE_KEY")]
86 private_key: String,
87}
88
89impl From<GithubTable> for GithubConfig {
90 fn from(table: GithubTable) -> Self {
91 Self {
92 gh_token: table.gh_token.map(Into::into),
93 apps: table
94 .apps
95 .into_iter()
96 .map(|(name, app)| {
97 (
98 name,
99 AppCredentials {
100 app_id: app.app_id,
101 installation_id: app.installation_id,
102 private_key: app.private_key.into(),
103 },
104 )
105 })
106 .collect(),
107 base_uri: table.base_uri,
108 upload_uri: table.upload_uri,
109 }
110 }
111}
112
113#[cfg(test)]
114mod tests {
115 use interprex::{ConfigurationSource, ProviderError};
116 use secrecy::{ExposeSecret, SecretString};
117
118 use super::{AppCredentials, GithubConfig, parse_project_config};
119
120 #[test]
121 fn file_and_direct_forms_preserve_the_same_credentials() {
122 let from_file = parse_project_config(
123 r#"
124 [provider.github]
125 GH_TOKEN = "user-secret"
126
127 [provider.github.apps.automation]
128 APP_ID = 12
129 INSTALLATION_ID = 34
130 PRIVATE_KEY = "app-secret"
131 "#,
132 &ConfigurationSource::File(".interprex.toml".to_owned()),
133 )
134 .expect("valid file");
135 let direct = GithubConfig {
136 gh_token: Some(SecretString::from("user-secret")),
137 apps: [(
138 "automation".to_owned(),
139 AppCredentials {
140 app_id: 12,
141 installation_id: 34,
142 private_key: SecretString::from("app-secret"),
143 },
144 )]
145 .into_iter()
146 .collect(),
147 ..GithubConfig::default()
148 };
149
150 assert_eq!(
151 from_file.gh_token.as_ref().map(ExposeSecret::expose_secret),
152 direct.gh_token.as_ref().map(ExposeSecret::expose_secret)
153 );
154 let file_app = &from_file.apps["automation"];
155 let direct_app = &direct.apps["automation"];
156 assert_eq!(file_app.app_id, direct_app.app_id);
157 assert_eq!(file_app.installation_id, direct_app.installation_id);
158 assert_eq!(
159 file_app.private_key.expose_secret(),
160 direct_app.private_key.expose_secret()
161 );
162 }
163
164 #[test]
165 fn malformed_project_config_retains_its_source() {
166 let source = ConfigurationSource::File("/workspace/project/.interprex.toml".to_owned());
167 let error = parse_project_config("[provider.github\nGH_TOKEN = broken", &source)
168 .expect_err("malformed file");
169 match error {
170 ProviderError::Configuration { origin, reason } => {
171 assert_eq!(origin, source);
172 assert!(!reason.is_empty());
173 }
174 other => panic!("unexpected error: {other:?}"),
175 }
176 }
177
178 #[test]
179 fn debug_output_never_contains_credentials() {
180 let config = GithubConfig {
181 gh_token: Some(SecretString::from("sensitive-user-token")),
182 ..GithubConfig::default()
183 };
184 let debug = format!("{config:?}");
185 assert!(!debug.contains("sensitive-user-token"));
186 assert!(debug.contains("REDACTED"));
187 }
188}