drogue_client/registry/v1/data/app/
mod.rs

1mod downstream;
2mod kafka;
3mod knative;
4mod process;
5
6pub use downstream::*;
7pub use kafka::*;
8pub use knative::*;
9pub use process::*;
10use std::time::Duration;
11
12use crate::{
13    dialect,
14    meta::v1::{CommonMetadata, CommonMetadataMut, NonScopedMetadata},
15    serde::{is_default, Base64Standard},
16    translator, Section, Translator,
17};
18use chrono::{DateTime, Utc};
19use serde::{Deserialize, Serialize};
20use serde_json::{Map, Value};
21
22/// An application, owning devices.
23#[derive(Clone, Default, Debug, Deserialize, Serialize, Eq, PartialEq)]
24pub struct Application {
25    pub metadata: NonScopedMetadata,
26    #[serde(default)]
27    #[serde(skip_serializing_if = "Map::is_empty")]
28    pub spec: Map<String, Value>,
29    #[serde(default)]
30    #[serde(skip_serializing_if = "Map::is_empty")]
31    pub status: Map<String, Value>,
32}
33
34translator!(Application);
35
36impl AsRef<dyn CommonMetadata> for Application {
37    fn as_ref(&self) -> &(dyn CommonMetadata + 'static) {
38        &self.metadata
39    }
40}
41
42impl AsMut<dyn CommonMetadataMut> for Application {
43    fn as_mut(&mut self) -> &mut (dyn CommonMetadataMut + 'static) {
44        &mut self.metadata
45    }
46}
47
48impl Application {
49    /// Create an minimal application object from the an application name
50    pub fn new<A>(name: A) -> Self
51    where
52        A: AsRef<str>,
53    {
54        Application {
55            metadata: NonScopedMetadata {
56                name: name.as_ref().into(),
57                ..Default::default()
58            },
59            ..Default::default()
60        }
61    }
62
63    /// Insert a trust anchor entry to an application
64    /// If there are no trust anchors already existing an array is created
65    /// if there is an error deserializing the existing data an error is returned
66    pub fn add_trust_anchor(
67        &mut self,
68        anchor: ApplicationSpecTrustAnchorEntry,
69    ) -> Result<(), serde_json::Error> {
70        self.update_section::<ApplicationSpecTrustAnchors, _>(|mut credentials| {
71            credentials.anchors.push(anchor);
72            credentials
73        })
74    }
75}
76
77/// The application's trust-anchors.
78#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
79pub struct ApplicationSpecTrustAnchors {
80    #[serde(skip_serializing_if = "Vec::is_empty")]
81    pub anchors: Vec<ApplicationSpecTrustAnchorEntry>,
82}
83
84dialect!(ApplicationSpecTrustAnchors [Section::Spec => "trustAnchors"]);
85
86/// A single trust-anchor entry.
87#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
88pub struct ApplicationSpecTrustAnchorEntry {
89    #[serde(with = "Base64Standard")]
90    pub certificate: Vec<u8>,
91}
92
93/// The status of the trust-anchors.
94#[derive(Clone, Debug, Default, Deserialize, Serialize, Eq, PartialEq)]
95pub struct ApplicationStatusTrustAnchors {
96    pub anchors: Vec<ApplicationStatusTrustAnchorEntry>,
97}
98
99dialect!(ApplicationStatusTrustAnchors [Section::Status => "trustAnchors"]);
100
101/// A single trust-anchor status.
102#[derive(Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
103#[serde(rename_all = "camelCase")]
104pub enum ApplicationStatusTrustAnchorEntry {
105    #[serde(rename_all = "camelCase")]
106    Valid {
107        subject: String,
108        #[serde(with = "Base64Standard")]
109        certificate: Vec<u8>,
110        not_before: DateTime<Utc>,
111        not_after: DateTime<Utc>,
112    },
113    Invalid {
114        error: String,
115        message: String,
116    },
117}
118
119#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
120#[serde(rename_all = "camelCase")]
121pub struct ExternalEndpoint {
122    #[serde(default, skip_serializing_if = "Option::is_none")]
123    pub method: Option<String>,
124    pub url: String,
125    #[serde(default)]
126    pub tls: Option<TlsOptions>,
127    #[serde(default)]
128    pub auth: Authentication,
129    #[serde(default)]
130    pub headers: Vec<Header>,
131    #[serde(default)]
132    #[serde(with = "humantime_serde")]
133    pub timeout: Option<Duration>,
134}
135
136#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
137#[serde(rename_all = "camelCase")]
138pub struct Header {
139    pub name: String,
140    pub value: String,
141}
142
143#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Hash)]
144#[serde(rename_all = "camelCase")]
145pub struct TlsOptions {
146    #[serde(default)]
147    pub insecure: bool,
148    #[serde(default, skip_serializing_if = "is_default")]
149    pub certificate: Option<String>,
150}
151
152#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
153#[serde(rename_all = "camelCase")]
154pub enum Authentication {
155    None,
156    Basic {
157        username: String,
158        #[serde(default, skip_serializing_if = "Option::is_none")]
159        password: Option<String>,
160    },
161    Bearer {
162        token: String,
163    },
164}
165
166impl Default for Authentication {
167    fn default() -> Self {
168        Self::None
169    }
170}
171
172#[cfg(test)]
173mod test;