innernet-server 1.7.1

A server to coordinate innernet networks.
Documentation
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
use std::collections::VecDeque;

use crate::{
    api::inject_endpoints,
    db::{DatabaseCidr, DatabasePeer},
    util::{form_body, json_response, status_response},
    Context, ServerError, Session,
};
use hyper::{Body, Method, Request, Response, StatusCode};
use innernet_shared::{
    Endpoint, EndpointContents, PeerContents, RedeemContents, ServerCapabilities, State,
    REDEEM_TRANSITION_WAIT,
};
use wireguard_control::{DeviceUpdate, PeerConfigBuilder};

pub async fn routes(
    req: Request<Body>,
    mut components: VecDeque<String>,
    session: Session,
) -> Result<Response<Body>, ServerError> {
    match (req.method(), components.pop_front().as_deref()) {
        (&Method::GET, Some("state")) => {
            if !session.user_capable() {
                return Err(ServerError::Unauthorized);
            }
            handlers::state(session).await
        },
        (&Method::GET, Some("capabilities")) => handlers::capabilities().await,
        (&Method::POST, Some("redeem")) => {
            if !session.redeemable() {
                return Err(ServerError::Unauthorized);
            }
            let form = form_body(req).await?;
            handlers::redeem(form, session).await
        },
        (&Method::PUT, Some("endpoint")) => {
            if !session.user_capable() {
                return Err(ServerError::Unauthorized);
            }
            let form = form_body(req).await?;
            handlers::endpoint(form, session).await
        },
        (&Method::PUT, Some("candidates")) => {
            if !session.user_capable() {
                return Err(ServerError::Unauthorized);
            }
            let form = form_body(req).await?;
            handlers::candidates(form, session).await
        },
        _ => Err(ServerError::NotFound),
    }
}

mod handlers {

    use super::*;

    /// Get the current state of the network, in the eyes of the current peer.
    ///
    /// This endpoint returns the visible CIDRs and Peers, providing all the necessary
    /// information for the peer to create connections to all of them.
    pub async fn state(session: Session) -> Result<Response<Body>, ServerError> {
        let conn = session.context.db.lock();
        let selected_peer = DatabasePeer::get(&conn, session.peer.id)?;

        let cidrs: Vec<_> = DatabaseCidr::list(&conn)?;

        let mut peers: Vec<_> = selected_peer
            .get_all_allowed_peers(&conn)?
            .into_iter()
            .map(|p| p.inner)
            .collect();
        inject_endpoints(&session, &mut peers);
        json_response(State { peers, cidrs })
    }

    pub async fn capabilities() -> Result<Response<Body>, ServerError> {
        let capabilities = ServerCapabilities {
            unspecified_ip_in_override_endpoint: true,
        };

        json_response(capabilities)
    }

    /// Redeems an invitation. An invitation includes a WireGuard keypair generated by either the server
    /// or a peer with admin rights.
    ///
    /// Redemption is the process of an invitee generating their own keypair and exchanging their temporary
    /// key with their permanent one.
    ///
    /// Until this API endpoint is called, the invited peer will not show up to other peers, and once
    /// it is called and succeeds, it cannot be called again.
    pub async fn redeem(
        form: RedeemContents,
        session: Session,
    ) -> Result<Response<Body>, ServerError> {
        let conn = session.context.db.lock();
        let mut selected_peer = DatabasePeer::get(&conn, session.peer.id)?;

        let old_public_key = wireguard_control::Key::from_base64(&selected_peer.public_key)
            .map_err(|_| ServerError::WireGuard)?;

        selected_peer.redeem(&conn, &form.public_key)?;

        if cfg!(not(test)) {
            let Context {
                interface, backend, ..
            } = session.context;

            // If we were to modify the WireGuard interface immediately, the HTTP response wouldn't
            // get through. Instead, we need to wait a reasonable amount for the HTTP response to
            // flush, then update the interface.
            //
            // The client is also expected to wait the same amount of time after receiving a success
            // response from /redeem.
            //
            // This might be avoidable if we were able to run code after we were certain the response
            // had flushed over the TCP socket, but that isn't easily accessible from this high-level
            // web framework.
            //
            // Related: https://github.com/hyperium/hyper/issues/2181
            tokio::task::spawn(async move {
                tokio::time::sleep(REDEEM_TRANSITION_WAIT).await;
                log::info!(
                    "WireGuard: adding new peer {}, removing old pubkey {}",
                    &*selected_peer,
                    old_public_key.to_base64()
                );
                DeviceUpdate::new()
                    .remove_peer_by_key(&old_public_key)
                    .add_peer(PeerConfigBuilder::from(&*selected_peer))
                    .apply(&interface, backend)
                    .map_err(|e| log::error!("{:?}", e))
                    .ok();
            });
        }
        status_response(StatusCode::NO_CONTENT)
    }

    /// Report any other endpoint candidates that can be tried by peers to connect.
    /// Currently limited to 10 candidates max.
    pub async fn candidates(
        contents: Vec<Endpoint>,
        session: Session,
    ) -> Result<Response<Body>, ServerError> {
        if contents.len() > 10 {
            return status_response(StatusCode::PAYLOAD_TOO_LARGE);
        }
        let conn = session.context.db.lock();
        let mut selected_peer = DatabasePeer::get(&conn, session.peer.id)?;
        selected_peer.update(
            &conn,
            PeerContents {
                candidates: contents,
                ..selected_peer.contents.clone()
            },
        )?;

        status_response(StatusCode::NO_CONTENT)
    }

    /// Force a specific endpoint to be reported by the server.
    pub async fn endpoint(
        contents: EndpointContents,
        session: Session,
    ) -> Result<Response<Body>, ServerError> {
        let conn = session.context.db.lock();
        let mut selected_peer = DatabasePeer::get(&conn, session.peer.id)?;
        selected_peer.update(
            &conn,
            PeerContents {
                endpoint: contents.into(),
                ..selected_peer.contents.clone()
            },
        )?;

        status_response(StatusCode::NO_CONTENT)
    }
}

#[cfg(test)]
mod tests {
    use std::time::{Duration, SystemTime};

    use super::*;
    use crate::{db::DatabaseAssociation, test};
    use bytes::Buf;
    use innernet_shared::{AssociationContents, CidrContents, Endpoint, EndpointContents, Error};

    #[tokio::test]
    async fn test_get_state_from_developer1() -> Result<(), Error> {
        let server = test::Server::new()?;
        let res = server
            .request(test::DEVELOPER1_PEER_IP, "GET", "/v1/user/state")
            .await;

        assert_eq!(res.status(), StatusCode::OK);

        let whole_body = hyper::body::aggregate(res).await?;
        let State { peers, .. } = serde_json::from_reader(whole_body.reader())?;
        let mut peer_names = peers.iter().map(|p| &*p.contents.name).collect::<Vec<_>>();
        peer_names.sort_unstable();
        // Developers should see only peers in infra CIDR and developer CIDR.
        assert_eq!(
            &["developer1", "developer2", "innernet-server"],
            &peer_names[..]
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_override_endpoint() -> Result<(), Error> {
        let server = test::Server::new()?;
        assert_eq!(
            server
                .form_request(
                    test::DEVELOPER1_PEER_IP,
                    "PUT",
                    "/v1/user/endpoint",
                    &EndpointContents::Set("1.1.1.1:51820".parse().unwrap())
                )
                .await
                .status(),
            StatusCode::NO_CONTENT
        );

        println!("{}", serde_json::to_string(&EndpointContents::Unset)?);
        assert_eq!(
            server
                .form_request(
                    test::DEVELOPER1_PEER_IP,
                    "PUT",
                    "/v1/user/endpoint",
                    &EndpointContents::Unset,
                )
                .await
                .status(),
            StatusCode::NO_CONTENT
        );

        assert_eq!(
            server
                .form_request(
                    test::DEVELOPER1_PEER_IP,
                    "PUT",
                    "/v1/user/endpoint",
                    "endpoint=blah",
                )
                .await
                .status(),
            StatusCode::BAD_REQUEST
        );

        Ok(())
    }

    #[tokio::test]
    async fn test_list_peers_from_unknown_ip() -> Result<(), Error> {
        let server = test::Server::new()?;

        // Request comes from an unknown IP.
        let res = server.request("10.80.80.80", "GET", "/v1/user/state").await;

        assert_eq!(res.status(), StatusCode::UNAUTHORIZED);

        Ok(())
    }

    #[tokio::test]
    async fn test_list_peers_for_developer_subcidr() -> Result<(), Error> {
        let server = test::Server::new()?;
        {
            let db = server.db.lock();
            let cidr = DatabaseCidr::create(
                &db,
                CidrContents {
                    name: "experiment cidr".to_string(),
                    cidr: test::EXPERIMENTAL_CIDR.parse()?,
                    parent: Some(test::ROOT_CIDR_ID),
                },
            )?;
            let subcidr = DatabaseCidr::create(
                &db,
                CidrContents {
                    name: "experiment subcidr".to_string(),
                    cidr: test::EXPERIMENTAL_SUBCIDR.parse()?,
                    parent: Some(cidr.id),
                },
            )?;
            DatabasePeer::create(
                &db,
                test::peer_contents(
                    "experiment-peer",
                    test::EXPERIMENT_SUBCIDR_PEER_IP,
                    subcidr.id,
                    false,
                )?,
            )?;

            // Add a peering between the developer's CIDR and the experimental *parent* cidr.
            DatabaseAssociation::create(
                &db,
                AssociationContents {
                    cidr_id_1: test::DEVELOPER_CIDR_ID,
                    cidr_id_2: cidr.id,
                },
            )?;
            DatabaseAssociation::create(
                &db,
                AssociationContents {
                    cidr_id_1: test::INFRA_CIDR_ID,
                    cidr_id_2: cidr.id,
                },
            )?;
        }

        for ip in &[test::DEVELOPER1_PEER_IP, test::EXPERIMENT_SUBCIDR_PEER_IP] {
            let res = server.request(ip, "GET", "/v1/user/state").await;
            assert_eq!(res.status(), StatusCode::OK);
            let whole_body = hyper::body::aggregate(res).await?;
            let State { peers, .. } = serde_json::from_reader(whole_body.reader())?;
            let mut peer_names = peers.iter().map(|p| &*p.contents.name).collect::<Vec<_>>();
            peer_names.sort_unstable();
            // Developers should see only peers in infra CIDR and developer CIDR.
            assert_eq!(
                &[
                    "developer1",
                    "developer2",
                    "experiment-peer",
                    "innernet-server"
                ],
                &peer_names[..]
            );
        }

        Ok(())
    }

    #[tokio::test]
    async fn test_redeem() -> Result<(), Error> {
        let server = test::Server::new()?;

        let experimental_cidr = DatabaseCidr::create(
            &server.db().lock(),
            CidrContents {
                name: "experimental".to_string(),
                cidr: test::EXPERIMENTAL_CIDR.parse()?,
                parent: Some(test::ROOT_CIDR_ID),
            },
        )?;

        let mut peer_contents = test::peer_contents(
            "experiment-peer",
            test::EXPERIMENT_SUBCIDR_PEER_IP,
            experimental_cidr.id,
            false,
        )?;
        peer_contents.is_redeemed = false;
        peer_contents.invite_expires = Some(SystemTime::now() + Duration::from_secs(100));
        let _experiment_peer = DatabasePeer::create(&server.db().lock(), peer_contents)?;

        // Step 1: Ensure that before redeeming, other endpoints aren't yet accessible.
        let res = server
            .request(test::EXPERIMENT_SUBCIDR_PEER_IP, "GET", "/v1/user/state")
            .await;
        assert_eq!(res.status(), StatusCode::UNAUTHORIZED);

        // Step 2: Ensure that redemption works.
        let body = RedeemContents {
            public_key: "YBVIgpfLbi/knrMCTEb0L6eVy0daiZnJJQkxBK9s+2I=".into(),
        };
        let res = server
            .form_request(
                test::EXPERIMENT_SUBCIDR_PEER_IP,
                "POST",
                "/v1/user/redeem",
                &body,
            )
            .await;
        assert!(res.status().is_success());

        // Step 3: Ensure that a second attempt at redemption DOESN'T work.
        let res = server
            .form_request(
                test::EXPERIMENT_SUBCIDR_PEER_IP,
                "POST",
                "/v1/user/redeem",
                &body,
            )
            .await;
        assert!(res.status().is_client_error());

        // Step 3: Ensure that after redemption, fetching state works.
        let res = server
            .request(test::EXPERIMENT_SUBCIDR_PEER_IP, "GET", "/v1/user/state")
            .await;
        assert_eq!(res.status(), StatusCode::OK);
        Ok(())
    }

    #[tokio::test]
    async fn test_redeem_expired() -> Result<(), Error> {
        let server = test::Server::new()?;

        let experimental_cidr = DatabaseCidr::create(
            &server.db().lock(),
            CidrContents {
                name: "experimental".to_string(),
                cidr: test::EXPERIMENTAL_CIDR.parse()?,
                parent: Some(test::ROOT_CIDR_ID),
            },
        )?;

        let mut peer_contents = test::peer_contents(
            "experiment-peer",
            test::EXPERIMENT_SUBCIDR_PEER_IP,
            experimental_cidr.id,
            false,
        )?;
        peer_contents.is_redeemed = false;
        peer_contents.invite_expires = Some(SystemTime::now() - Duration::from_secs(1));
        let _experiment_peer = DatabasePeer::create(&server.db().lock(), peer_contents)?;

        // Step 1: Ensure that before redeeming, other endpoints aren't yet accessible.
        let res = server
            .request(test::EXPERIMENT_SUBCIDR_PEER_IP, "GET", "/v1/user/state")
            .await;
        assert_eq!(res.status(), StatusCode::UNAUTHORIZED);

        // Step 2: Ensure that redemption works.
        let body = RedeemContents {
            public_key: "YBVIgpfLbi/knrMCTEb0L6eVy0daiZnJJQkxBK9s+2I=".into(),
        };
        let res = server
            .form_request(
                test::EXPERIMENT_SUBCIDR_PEER_IP,
                "POST",
                "/v1/user/redeem",
                &body,
            )
            .await;
        assert_eq!(res.status(), StatusCode::UNAUTHORIZED);
        Ok(())
    }

    #[tokio::test]
    async fn test_candidates() -> Result<(), Error> {
        let server = test::Server::new()?;

        let peer = DatabasePeer::get(&server.db().lock(), test::DEVELOPER1_PEER_ID)?;
        assert_eq!(peer.candidates, vec![]);

        let candidates = vec!["1.1.1.1:51820".parse::<Endpoint>().unwrap()];
        assert_eq!(
            server
                .form_request(
                    test::DEVELOPER1_PEER_IP,
                    "PUT",
                    "/v1/user/candidates",
                    &candidates
                )
                .await
                .status(),
            StatusCode::NO_CONTENT
        );

        let res = server
            .request(test::DEVELOPER1_PEER_IP, "GET", "/v1/user/state")
            .await;

        assert_eq!(res.status(), StatusCode::OK);

        let peer = DatabasePeer::get(&server.db().lock(), test::DEVELOPER1_PEER_ID)?;
        assert_eq!(peer.candidates, candidates);
        Ok(())
    }

    #[tokio::test]
    async fn test_endpoint_in_candidates() -> Result<(), Error> {
        // We want to verify that the current wireguard endpoint always shows up
        // either in the peer.endpoint field, or the peer.candidates field (in the
        // case that the peer has specified an endpoint override).
        let server = test::Server::new()?;

        let peer = DatabasePeer::get(&server.db().lock(), test::DEVELOPER1_PEER_ID)?;
        assert_eq!(peer.candidates, vec![]);

        // Specify one NAT candidate. At this point, we have an unspecified
        // endpoint and one NAT candidate specified.
        let candidates = vec!["1.1.1.1:51820".parse::<Endpoint>().unwrap()];
        assert_eq!(
            server
                .form_request(
                    test::DEVELOPER1_PEER_IP,
                    "PUT",
                    "/v1/user/candidates",
                    &candidates
                )
                .await
                .status(),
            StatusCode::NO_CONTENT
        );

        let res = server
            .request(test::DEVELOPER1_PEER_IP, "GET", "/v1/user/state")
            .await;

        assert_eq!(res.status(), StatusCode::OK);

        let whole_body = hyper::body::aggregate(res).await?;
        let State { peers, .. } = serde_json::from_reader(whole_body.reader())?;

        let developer_1 = peers
            .into_iter()
            .find(|p| p.id == test::DEVELOPER1_PEER_ID)
            .unwrap();
        assert_eq!(
            developer_1.endpoint,
            Some(test::DEVELOPER1_PEER_ENDPOINT.parse().unwrap())
        );
        assert_eq!(developer_1.candidates, candidates);

        // Now, explicitly set an endpoint with the override-endpoint API
        // and check that the original wireguard endpoint still shows up
        // in the list of NAT candidates.
        assert_eq!(
            server
                .form_request(
                    test::DEVELOPER1_PEER_IP,
                    "PUT",
                    "/v1/user/endpoint",
                    &EndpointContents::Set("1.2.3.4:51820".parse().unwrap())
                )
                .await
                .status(),
            StatusCode::NO_CONTENT
        );

        let res = server
            .request(test::DEVELOPER1_PEER_IP, "GET", "/v1/user/state")
            .await;

        assert_eq!(res.status(), StatusCode::OK);

        let whole_body = hyper::body::aggregate(res).await?;
        let State { peers, .. } = serde_json::from_reader(whole_body.reader())?;

        let developer_1 = peers
            .into_iter()
            .find(|p| p.id == test::DEVELOPER1_PEER_ID)
            .unwrap();

        // The peer endpoint should be the one we just specified in the override-endpoint request.
        assert_eq!(developer_1.endpoint, Some("1.2.3.4:51820".parse().unwrap()));

        // The list of candidates should now contain the one we specified at the beginning of the
        // test, and the wireguard-reported endpoint of the peer.
        let nat_candidate_1 = "1.1.1.1:51820".parse().unwrap();
        let nat_candidate_2 = test::DEVELOPER1_PEER_ENDPOINT.parse().unwrap();
        assert_eq!(developer_1.candidates.len(), 2);
        assert!(developer_1.candidates.contains(&nat_candidate_1));
        assert!(developer_1.candidates.contains(&nat_candidate_2));

        Ok(())
    }
}