tag2upload_service_manager/
webhook.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
//! webhook handling (forge-agnostic)
//!
//! Processing happens as follows:
//!
//!  * HHTP POST data
//!    1. processed by Rocket route handler
//!    2. `FromData for RawSpecificWebhookPayload<P>`
//!      1. `P` is `f::Payload` where `f` is eg` gitlab`
//!      2. `P::Deserialize` gives us `P`
//!      3. client IP address extracted from Rocket
//!  * `RawSpecificWebhookPayload<P>`
//!    1. processed by `RawSpecificWebhookPayload::webhook_impl`
//!      1. Find the forge details, from `P as SomeWebhookPayload`
//!      2. `TryFrom<Payload> for RawWebhookPayloadData<DbData>`
//!         (`DbData` is the forge-specific data)
//!      3. Converted to `RawWebhookPayloadData<DbData>`
//!         and `RawWebhookMetadata`
//!    2. processed by `RawWebhookMetadata::webhook_impl`
//!       (with `RawWebhookPayloadData<_>` argument)
//!      1. Processed by `UnvalidatedWebhookRequest::validate_payload`
//!        1. Check forge host (based on repo url)
//!           and IP address permission
//!        2. Check tag name etc.
//!      2. Make a `JobRow`
//!  * `JobRow`, in `RawWebhookMetadata::webhook_impl`
//!    * inserted in db
//
// ^ TOOD there should be comments explaining why each of these is required

use crate::prelude::*;

use rocket::data::{Data, FromData, Outcome};
use rocket::http::Status;
use rocket::request::Request;
use rocket::serde::json::Json;

/// Not validated
#[derive(Deftly, Debug)]
#[derive_deftly_adhoc]
pub struct RawWebhookPayloadData<FD> {
    pub repo_git_url: String,
    pub tag_name: String,
    pub tag_objectid: GitObjectId,
    pub tag_meta: t2umeta::Parsed,
    #[deftly(validate_special)]
    pub forge_data: FD,
}

struct UnvalidatedWebhookRequest<'g, FD> {
    globals: &'g Globals,
    meta: RawWebhookMetadata,
    data: RawWebhookPayloadData<FD>,
}

struct RawWebhookMetadata {
    forge_namever: ForgeNamever,
    client_ip: IpAddr,
    kind_name: &'static str,
}

#[async_trait]
pub trait SomeWebhookPayload: for <'a> Deserialize<'a>
    + TryInto<RawWebhookPayloadData<<Self::Forge as SomeForge>::DbData>,
              Error=WebError>
{
    type Forge: SomeForge + Default;
}

pub struct RawSpecificWebhookPayload<P> {
    data: P,
    client_ip: IpAddr,
}

#[async_trait]
impl<'r, P> FromData<'r> for RawSpecificWebhookPayload<P>
where
    P: SomeWebhookPayload
{
    type Error = anyhow::Error;
    
    async fn from_data(
        req: &'r Request<'_>,
        data: Data<'r>,
    ) -> Outcome<'r, Self, Self::Error> {
        use Outcome as O;
        let client_ip = req.client_ip();

        let r = async {
            let data = match FromData::from_data(req, data).await {
                O::Success(Json(data)) => data,
                O::Error((s, e)) => return O::Error(
                    (s, anyhow!("body parsing failed: {e}"))
                ),
                x @ O::Forward(_) => return O::Error(
                    (Status::InternalServerError, anyhow!("forwarded?! {x:?}"))
                )
            };
            let Some(client_ip) = client_ip
            else { return O::Error((
                Status::InternalServerError,
                anyhow!("missing client IP addr")
            )) };
            O::Success(RawSpecificWebhookPayload { data, client_ip })
        }.await;

        match &r {
            O::Error((_s, error)) =>
                debug!(?client_ip, %error, "unprocessable reqeust"),
            O::Success { .. } | O::Forward { .. } => {}
        }
        r
    }
}

impl<P: SomeWebhookPayload> RawSpecificWebhookPayload<P> {
    pub async fn webhook_impl(self) -> Result<String, WebError> {
        let some_forge = P::Forge::default();
        let forge_namever = some_forge
            .namever_str().to_owned().into();
        let data = self.data.try_into();

        let log_info = if let Ok(d) = &data {
            format!(
                "source={} version={} repo={:?} tag_objid={}",
                d.tag_meta.source, d.tag_meta.version,
                d.repo_git_url, d.tag_objectid,
            )
        } else {
            format!("unprocessable")
        };

        let r = async {
            let meta = RawWebhookMetadata {
                forge_namever,
                kind_name: P::Forge::default().kind_name(),
                client_ip: self.client_ip,
            };
            meta.webhook_impl(&some_forge, data).await
        }.await;

        if let Err(e) = &r {
            match e {
                WE::MisconfiguredWebhook { .. } |
                WE::NetworkError { .. } =>
                    debug!("rejected, {log_info}"),
                WE::MalfunctioningWebhook { .. } |
                WE::NotForUs { .. } |
                // reported as Error when we generated it
                WE::InternalError { .. } => 
                    info!("rejected, {log_info}")
            }
        }
        r
    }
}

impl RawWebhookMetadata {
    async fn webhook_impl<SF: SomeForge>(
        self,
        _some_forge: &SF,
        data: Result<RawWebhookPayloadData<SF::DbData>, WebError>,
    ) -> Result<String, WebError> {
        let globals = globals();

        let data = match async {
            let data = data?;

            let erased_payload = UnvalidatedWebhookRequest {
                meta: self,
                globals: &globals,
                data,
            };

            erased_payload.validate_payload().await
        }.await {
            Ok(y) => y,
            Err(e) => return Err(e),
        };

        let now = globals.now();

        let job_row = JobRow {
            jid: JobId::none(),
            data: data,
            received: now,
            last_update: now,
            tag_data: None.into(),
            status: JobStatus::Noticed,
            info: format!("job received, tag not yet fetched"),
            processing: None.into(),
            duplicate_of: None,
        };

        let jid = db_transaction(TN::Update { 
            this_jid: None,
            tag_objectid: &job_row.data.tag_objectid,
        }, |dbt| {
            let jid = dbt.bsql_insert(bsql!(
                "INSERT INTO jobs " +~(job_row) ""
            )).into_internal("insert into jobs failed")?;

            Ok::<_, WebError>(jid)
        })??;

        let msg = format!("job received, jid={jid}");

        info!(jid=%jid, now=?job_row.status, info=%job_row.info,
              "[{}] received", job_row.data.forge_host);

        Ok(msg)
    }
}

impl<FD: Display> UnvalidatedWebhookRequest<'_, FD> {
    async fn check_permission(&self) -> Result<Hostname, WE> {
        let forge_host = (|| {
            let rhs = if let Some(fake) = &self.globals.config
                .testing.fake_https_dir
            {
                let strip = format!("file://{fake}/");
                self.data.repo_git_url
                    .strip_prefix(&strip)
                    .ok_or_else(|| anyhow!(
 "failed to strip expected faked {strip:?} from {:?}", self.data.repo_git_url
                    ))?
            } else {
                self.data.repo_git_url
                    .strip_prefix("https://")
                    .ok_or_else(|| anyhow!("scheme not https"))?
            };
            let (host, rhs) = rhs.split_once('/')
                .ok_or_else(|| anyhow!("missing / after host"))?;

            rhs.chars().all(|c| c.is_ascii_graphic()).then_some(())
                .ok_or_else(|| anyhow!("nonprintable characters in url"))?;

            let host: Hostname = host.parse()?;

            Ok::<_, AE>(host)
        })()
            .context("bad project repository URL")
            .map_err(WE::MisconfiguredWebhook)?;

        let correct_host_forges = self.globals.config.t2u.forges.iter()
            .filter(|cf| cf.host == forge_host);

        let check_kind = |cf: &config::Forge| {
            (cf.kind == self.meta.kind_name).then(|| ())
                .ok_or_else(|| anyhow!(
                    "wrong webhook path used, expected /hook/{}",
                    cf.kind,
                ))
        };

        let forge: &config::Forge =
            correct_host_forges.clone()
            .find(|cf| check_kind(cf).is_ok())
            .ok_or_else(|| {
                let mut emsg = format!("no matching forge in config");
                for cf in correct_host_forges.clone() {
                    let wrong = check_kind(cf).expect_err("suddenly good?");
                    write!(emsg, "; forge host {:?}: {wrong}", cf.host)
                        .unwrap();
                }
                if correct_host_forges.clone().next().is_none() {
                    write!(emsg, "; no matching forge hosts")
                        .unwrap();
                }
                anyhow!("{}", emsg)
            })
            // TODO this is perhaps anthe actual permission denied variant,
            // log them at lower severity ?
            .map_err(WE::MisconfiguredWebhook)?;

        let _: IsAllowedCaller = AllowedCaller::list_contains(
            &forge.allow,
            self.meta.client_ip,
        )
            // TODO these are the actual permission denied variants,
            // log them at lower severity ?
            .await.map_err(WE::NetworkError)?
            .map_err(|wrong| WE::MisconfiguredWebhook(wrong.into()))?;

        Ok(forge.host.clone())
    }

    fn check_tag_name(&self) -> Result<(), NotForUsReason> {
        let app_config = &self.globals.config.t2u;

        let (distro, version) = self.data.tag_name.split('/').collect_tuple()
            .ok_or_else(|| NFR::TagNameUnexpectedSyntax)?;
        (distro == app_config.distro).then(||())
            .ok_or_else(|| NFR::TagNameNotOurDistro)?;
        if !version.chars().all(
            |c| c.is_ascii_alphanumeric() || ".+-%_#".chars().contains(&c)
        ) {
            return Err(NFR::TagNameUnexpectedSyntax)
        }
        if version == "." || version == ".." {
            return Err(NFR::TagNameUnexpectedSyntax)
        }
        Ok(())
    }

    async fn validate_payload(self) -> Result<JobData, WE> {
        let forge_host = self.check_permission().await?;

        self.check_tag_name()?;

        let forge_data = ForgeData::from_raw_string(
            self.data.forge_data.to_string()
        );

        let validated = derive_deftly_adhoc! {
            RawWebhookPayloadData expect expr:
            JobData {
              $(
                ${when not(fmeta(validate_special))}
                $fname: self.data.$fname,
              )
                forge_host,
                forge_data,
                forge_namever: self.meta.forge_namever,
            }
        };

        Ok(validated)
    }
}