ockam_api 0.93.0

Ockam's request-response API
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
use crate::influxdb::gateway::interceptor::HttpAuthInterceptorFactory;
use crate::influxdb::gateway::token_lease_refresher::TokenLeaseRefresher;
use crate::influxdb::{LeaseUsage, StartInfluxDBLeaseIssuerRequest};
use crate::nodes::models::portal::{
    CreateInlet, CreateOutlet, InletStatus, OutletAccessControl, OutletStatus,
};
use crate::nodes::service::tcp_inlets::create_inlet_payload;
use crate::nodes::{BackgroundNodeClient, NodeManagerWorker};
use crate::{ApiError, DefaultAddress};
use minicbor::{CborLen, Decode, Encode};
use ockam::flow_control::FlowControls;
use ockam::identity::Identifier;
use ockam::Message;
use ockam::{Address, Context, Result};
use ockam_abac::PolicyExpression;
use ockam_abac::{Action, Resource, ResourceType};
use ockam_core::api::{Error, Reply, Request, Response};
use ockam_core::{async_trait, Decodable, Encodable, Encoded};
use ockam_core::{cbor_encode_preallocate, route};
use ockam_multiaddr::proto::Service;
use ockam_multiaddr::MultiAddr;
use ockam_transport_core::HostnamePort;
use ockam_transport_tcp::{
    read_portal_payload_length, PortalInletInterceptor, PortalOutletInterceptor,
};
use std::sync::Arc;
use std::time::Duration;

impl NodeManagerWorker {
    pub(crate) async fn start_influxdb_outlet_service(
        &self,
        ctx: &Context,
        body: CreateInfluxDBOutlet,
    ) -> Result<Response<OutletStatus>, Response<Error>> {
        debug!("Starting InfluxDB Outlet service");
        let CreateOutlet {
            hostname_port,
            worker_addr,
            reachable_from_default_secure_channel,
            policy_expression,
            privileged,
            tls,
            skip_handshake,
            enable_nagle,
        } = body.tcp_outlet;
        let address = self
            .node_manager
            .registry
            .outlets
            .generate_worker_addr(worker_addr);
        let outlet_address = match body.influxdb_config {
            InfluxDBOutletConfig::OutletWithFixedToken(token) => {
                let outlet_addr: Address = format!("{}_outlet", address.address()).into();
                // Start the interceptor
                self.create_http_outlet_interceptor(
                    ctx,
                    address.clone(),
                    outlet_addr.clone(),
                    policy_expression.clone(),
                    token,
                )
                .await
                .map_err(|e| Response::bad_request_no_request(&format!("{e:?}")))?;
                outlet_addr
            }
            InfluxDBOutletConfig::StartLeaseManager(lease_manager_config) => {
                // Get the address of the lease issuer service
                let lease_issuer_address: Address =
                    format!("{}_{}", address.address(), DefaultAddress::LEASE_MANAGER).into();
                debug!(%address, ?policy_expression, "Using params");
                // Start the lease issuer service
                let req = StartInfluxDBLeaseIssuerRequest {
                    influxdb_address: hostname_port
                        .clone()
                        .into_url(if tls { "https" } else { "http" })?
                        .to_string(),
                    influxdb_org_id: lease_manager_config.influxdb_org_id,
                    influxdb_token: lease_manager_config.influxdb_token,
                    lease_permissions: lease_manager_config.lease_permissions,
                    expires_in: lease_manager_config.expires_in,
                    policy_expression: policy_expression.clone(),
                };
                self.node_manager
                    .start_influxdb_lease_issuer_service(ctx, lease_issuer_address.clone(), req)
                    .await
                    .map_err(|e| Response::bad_request_no_request(&format!("{e:?}")))?;
                address
            }
        };
        // Start the outlet
        match self
            .node_manager
            .create_outlet(
                ctx,
                hostname_port,
                tls,
                Some(outlet_address),
                reachable_from_default_secure_channel,
                OutletAccessControl::WithPolicyExpression(policy_expression),
                privileged,
                skip_handshake,
                enable_nagle,
            )
            .await
        {
            Ok(outlet_status) => Ok(Response::ok().body(outlet_status)),
            Err(e) => Err(Response::bad_request_no_request(&format!("{e:?}"))),
        }
    }

    pub(crate) async fn start_influxdb_inlet_service(
        &self,
        ctx: &Context,
        body: CreateInfluxDBInlet,
    ) -> Result<Response<InletStatus>, Response<Error>> {
        let CreateInlet {
            listen_addr,
            outlet_addr,
            alias,
            authorized,
            wait_for_outlet_duration,
            policy_expression,
            wait_connection,
            secure_channel_identifier,
            enable_udp_puncture,
            disable_tcp_fallback,
            privileged,
            tls_certificate_provider,
            skip_handshake,
            enable_nagle,
            ..
        } = body.tcp_inlet.clone();

        //TODO: should be an easier way to tweak the multiaddr
        let mut issuer_route = outlet_addr.clone();
        let outlet_addr_last_service = issuer_route
            .pop_back()
            .ok_or_else(|| Response::bad_request_no_request("The outlet address is invalid"))?;
        let outlet_addr_last_service = outlet_addr_last_service
            .cast::<Service>()
            .ok_or_else(|| Response::bad_request_no_request("The outlet address is invalid"))?;

        let lease_issuer_route = if let Some(s) = body.lease_issuer_address {
            s
        } else {
            // If outlet_addr = /A/B/C, then issuer is derived as /A/B/C_lease_issuer
            let lease_issuer_service = format!(
                "{}_{}",
                &*outlet_addr_last_service,
                DefaultAddress::LEASE_MANAGER
            );
            issuer_route
                .push_back(Service::new(lease_issuer_service))
                .map_err(|e| Response::bad_request_no_request(&format!("{e:?}")))?;
            issuer_route
        };

        let (prefix_route, suffix_route) = match body.lease_usage {
            LeaseUsage::PerClient => {
                // Start an interceptor pointing to the lease issuer service
                let interceptor_addr = self
                    .create_http_auth_interceptor(
                        ctx,
                        &alias,
                        policy_expression.clone(),
                        lease_issuer_route,
                    )
                    .await
                    .map_err(|e| Response::bad_request_no_request(&format!("{e:?}")))?;
                (route![interceptor_addr], route![])
            }
            LeaseUsage::Shared => {
                // Http interception is done on the outlet side.
                // The suffix route is derived from the given outlet addr `/A/B/C ⇒ /A/B/C_outlet`
                (
                    route![],
                    route![format!("{}_outlet", &*outlet_addr_last_service)],
                )
            }
        };

        match self
            .node_manager
            .create_inlet(
                ctx,
                listen_addr,
                prefix_route,
                suffix_route,
                outlet_addr,
                alias,
                policy_expression,
                wait_for_outlet_duration,
                authorized,
                wait_connection,
                secure_channel_identifier,
                enable_udp_puncture,
                disable_tcp_fallback,
                privileged,
                tls_certificate_provider,
                skip_handshake,
                enable_nagle,
            )
            .await
        {
            Ok(status) => Ok(Response::ok().body(status)),
            Err(e) => Err(Response::bad_request_no_request(&format!("{e:?}"))),
        }
    }

    async fn create_http_outlet_interceptor(
        &self,
        ctx: &Context,
        interceptor_address: Address,
        outlet_address: Address,
        outlet_policy_expression: Option<PolicyExpression>,
        token_to_use: String,
    ) -> Result<(), Error> {
        debug!(%interceptor_address, %outlet_address, ?outlet_policy_expression, %token_to_use, "Creating http outlet interceptor");
        let default_secure_channel_listener_flow_control_id = ctx
            .flow_controls()
            .get_flow_control_with_spawner(&DefaultAddress::SECURE_CHANNEL_LISTENER.into())
            .ok_or_else(|| {
                ApiError::core("Unable to get flow control for secure channel listener")
            })?;

        let policy_access_control = self
            .node_manager
            .policy_access_control(
                self.node_manager.project_authority().clone(),
                Resource::new(outlet_address.to_string(), ResourceType::TcpOutlet),
                Action::HandleMessage,
                outlet_policy_expression.clone(),
            )
            .await?;

        let spawner_flow_control_id = FlowControls::generate_flow_control_id();

        let token_refresher = TokenLeaseRefresher::new_with_fixed_token(token_to_use);
        let http_interceptor_factory = Arc::new(HttpAuthInterceptorFactory::new(token_refresher));

        PortalOutletInterceptor::create(
            ctx,
            interceptor_address.clone(),
            Some(spawner_flow_control_id.clone()),
            http_interceptor_factory,
            Arc::new(policy_access_control.create_outgoing(ctx)?),
            Arc::new(policy_access_control.create_incoming()),
            read_portal_payload_length(),
        )?;

        // every secure channel can reach this service
        let flow_controls = ctx.flow_controls();
        flow_controls.add_consumer(
            &interceptor_address,
            &default_secure_channel_listener_flow_control_id,
        );

        // this spawner flow control id is used to control communication with dynamically created
        // outlets
        flow_controls.add_spawner(&interceptor_address, &spawner_flow_control_id);

        // allow communication with the tcp outlet
        flow_controls.add_consumer(&outlet_address, &spawner_flow_control_id);
        Ok(())
    }

    async fn create_http_auth_interceptor(
        &self,
        ctx: &Context,
        inlet_alias: &String,
        inlet_policy_expression: Option<PolicyExpression>,
        lease_issuer_route: MultiAddr,
    ) -> Result<Address, Error> {
        let interceptor_address: Address = (inlet_alias.to_owned() + "_http_interceptor").into();
        let policy_access_control = self
            .node_manager
            .policy_access_control(
                self.node_manager.project_authority().clone(),
                Resource::new(interceptor_address.to_string(), ResourceType::TcpInlet),
                Action::HandleMessage,
                inlet_policy_expression,
            )
            .await?;

        let token_refresher =
            TokenLeaseRefresher::new(ctx, Arc::downgrade(&self.node_manager), lease_issuer_route)?;
        let http_interceptor_factory = Arc::new(HttpAuthInterceptorFactory::new(token_refresher));

        PortalInletInterceptor::start_listener(
            ctx,
            interceptor_address.clone(),
            http_interceptor_factory,
            Arc::new(policy_access_control.create_incoming()),
            Arc::new(policy_access_control.create_outgoing(ctx)?),
            read_portal_payload_length(),
        )?;
        Ok(interceptor_address)
    }
}

#[async_trait]
pub trait InfluxDBPortals {
    #[allow(clippy::too_many_arguments)]
    async fn create_influxdb_inlet(
        &self,
        ctx: &Context,
        listen_addr: &HostnamePort,
        outlet_addr: &MultiAddr,
        alias: &str,
        authorized_identifier: &Option<Identifier>,
        policy_expression: &Option<PolicyExpression>,
        wait_for_outlet_timeout: Duration,
        wait_connection: bool,
        secure_channel_identifier: &Option<Identifier>,
        enable_udp_puncture: bool,
        disable_tcp_fallback: bool,
        tls_certificate_provider: &Option<MultiAddr>,
        lease_usage: LeaseUsage,
        lease_issuer_route: Option<MultiAddr>,
    ) -> miette::Result<Reply<InletStatus>>;

    #[allow(clippy::too_many_arguments)]
    async fn create_influxdb_outlet(
        &self,
        ctx: &Context,
        to: HostnamePort,
        tls: bool,
        from: Option<&Address>,
        policy_expression: Option<PolicyExpression>,
        influxdb_config: InfluxDBOutletConfig,
    ) -> miette::Result<OutletStatus>;
}

#[async_trait]
impl InfluxDBPortals for BackgroundNodeClient {
    #[instrument(skip(self, ctx))]
    #[allow(clippy::too_many_arguments)]
    async fn create_influxdb_outlet(
        &self,
        ctx: &Context,
        to: HostnamePort,
        tls: bool,
        from: Option<&Address>,
        policy_expression: Option<PolicyExpression>,
        influxdb_config: InfluxDBOutletConfig,
    ) -> miette::Result<OutletStatus> {
        let mut outlet_payload =
            CreateOutlet::new(to, tls, from.cloned(), true, false, false, false);
        if let Some(policy_expression) = policy_expression {
            outlet_payload.set_policy_expression(policy_expression);
        }
        let payload = CreateInfluxDBOutlet::new(outlet_payload, influxdb_config);
        let req = Request::post("/node/influxdb_outlet").body(payload);
        self.ask(ctx, req).await
    }

    #[instrument(skip(self, ctx))]
    #[allow(clippy::too_many_arguments)]
    async fn create_influxdb_inlet(
        &self,
        ctx: &Context,
        listen_addr: &HostnamePort,
        outlet_addr: &MultiAddr,
        alias: &str,
        authorized_identifier: &Option<Identifier>,
        policy_expression: &Option<PolicyExpression>,
        wait_for_outlet_timeout: Duration,
        wait_connection: bool,
        secure_channel_identifier: &Option<Identifier>,
        enable_udp_puncture: bool,
        disable_tcp_fallback: bool,
        tls_certificate_provider: &Option<MultiAddr>,
        lease_usage: LeaseUsage,
        lease_issuer_route: Option<MultiAddr>,
    ) -> miette::Result<Reply<InletStatus>> {
        let request = {
            let inlet_payload = create_inlet_payload(
                listen_addr,
                outlet_addr,
                alias,
                authorized_identifier,
                policy_expression,
                wait_for_outlet_timeout,
                wait_connection,
                secure_channel_identifier,
                enable_udp_puncture,
                disable_tcp_fallback,
                false,
                tls_certificate_provider,
                false,
                false,
                route![],
            );
            let payload = CreateInfluxDBInlet::new(inlet_payload, lease_usage, lease_issuer_route);
            Request::post("/node/influxdb_inlet").body(payload)
        };
        self.ask_and_get_reply(ctx, request).await
    }
}

/// Request body to create an influxdb inlet
#[derive(Clone, Debug, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct CreateInfluxDBInlet {
    #[n(1)] pub(crate) tcp_inlet: CreateInlet,
    #[n(2)] pub(crate) lease_usage: LeaseUsage,
    /// Route to the lease issuer.
    /// If not given it's derived from the outlet route
    #[n(3)] pub(crate) lease_issuer_address: Option<MultiAddr>,
}

impl Encodable for CreateInfluxDBInlet {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for CreateInfluxDBInlet {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}

impl CreateInfluxDBInlet {
    pub fn new(
        tcp_inlet: CreateInlet,
        lease_usage: LeaseUsage,
        lease_issuer_address: Option<MultiAddr>,
    ) -> Self {
        Self {
            tcp_inlet,
            lease_usage,
            lease_issuer_address,
        }
    }
}

/// Request body to create an influxdb outlet
#[derive(Clone, Debug, Encode, Decode, CborLen, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct CreateInfluxDBOutlet {
    #[n(1)] pub(crate) tcp_outlet: CreateOutlet,
    #[n(2)] pub(crate) influxdb_config: InfluxDBOutletConfig,
}

impl Encodable for CreateInfluxDBOutlet {
    fn encode(self) -> Result<Encoded> {
        cbor_encode_preallocate(self)
    }
}

impl Decodable for CreateInfluxDBOutlet {
    fn decode(e: &[u8]) -> Result<Self> {
        Ok(minicbor::decode(e)?)
    }
}

#[derive(Clone, Debug, Encode, Decode, CborLen)]
#[rustfmt::skip]
#[cbor(map)]
pub struct LeaseManagerConfig {
    #[n(1)] pub(crate) influxdb_org_id: String,
    #[n(2)] pub(crate) influxdb_token: String,
    #[n(3)] pub(crate) lease_permissions: String,
    #[n(4)] pub(crate) expires_in: Duration,
}

#[derive(Clone, Debug, Encode, Decode, CborLen)]
#[rustfmt::skip]
#[cbor(map)]
pub enum InfluxDBOutletConfig {
    #[n(0)] StartLeaseManager(#[n(0)] LeaseManagerConfig),
    #[n(1)] OutletWithFixedToken(#[n(0)] String)
}

impl LeaseManagerConfig {
    pub fn new(
        influxdb_org_id: String,
        influxdb_token: String,
        lease_permissions: String,
        expires_in: Duration,
    ) -> Self {
        Self {
            influxdb_org_id,
            influxdb_token,
            lease_permissions,
            expires_in,
        }
    }
}

impl CreateInfluxDBOutlet {
    pub fn new(tcp_outlet: CreateOutlet, influxdb_config: InfluxDBOutletConfig) -> Self {
        Self {
            tcp_outlet,
            influxdb_config,
        }
    }
}