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
use std::fmt;
use std::str::FromStr;

use serde::Serialize;

use rpki::ca::idexchange;

use crate::{
    commons::{
        api::{
            import::ExportChild, AllCertAuthIssues, AspaDefinitionList, BgpSecCsrInfoList, CaCommandDetails,
            CaRepoDetails, CertAuthInfo, CertAuthIssues, CertAuthList, ChildCaInfo, ChildrenConnectionStats,
            CommandHistory, ConfiguredRoas, IdCertInfo, ParentCaContact, ParentStatuses, PublisherDetails,
            PublisherList, RepoStatus, RepositoryContact, RtaList, RtaPrepResponse, ServerInfo,
        },
        bgp::{BgpAnalysisAdvice, BgpAnalysisReport, BgpAnalysisSuggestion},
    },
    daemon::ca::ResourceTaggedAttestation,
    pubd::RepoStats,
    ta::{TrustAnchorProxySignerExchanges, TrustAnchorSignedRequest, TrustAnchorSignedResponse, TrustAnchorSignerInfo},
};

//------------ ApiResponse ---------------------------------------------------

/// This type defines all supported responses for the api
#[derive(Clone, Debug, Eq, PartialEq)]
#[allow(clippy::large_enum_variant)]
pub enum ApiResponse {
    Health,
    Info(ServerInfo),

    CertAuthInfo(CertAuthInfo),
    CertAuthHistory(CommandHistory),
    CertAuthAction(CaCommandDetails),
    CertAuths(CertAuthList),

    // ROA related
    RouteAuthorizations(ConfiguredRoas),
    BgpAnalysisAdvice(BgpAnalysisAdvice),
    BgpAnalysisFull(BgpAnalysisReport),
    BgpAnalysisSuggestions(BgpAnalysisSuggestion),

    // ASPA related
    AspaDefinitions(AspaDefinitionList),

    // BGPSec related
    BgpSecDefinitions(BgpSecCsrInfoList),

    ParentCaContact(ParentCaContact),
    ParentStatuses(ParentStatuses),

    ChildInfo(ChildCaInfo),
    ChildExported(ExportChild),
    ChildrenStats(ChildrenConnectionStats),

    PublisherDetails(PublisherDetails),
    PublisherList(PublisherList),
    RepoStats(RepoStats),

    Rfc8183ParentResponse(idexchange::ParentResponse),
    Rfc8183RepositoryResponse(idexchange::RepositoryResponse),
    Rfc8183ChildRequest(idexchange::ChildRequest),
    Rfc8183PublisherRequest(idexchange::PublisherRequest),

    RepoDetails(CaRepoDetails),
    RepoStatus(RepoStatus),

    CertAuthIssues(CertAuthIssues),
    AllCertAuthIssues(AllCertAuthIssues),

    RtaList(RtaList),
    RtaMultiPrep(RtaPrepResponse),
    Rta(ResourceTaggedAttestation),

    Empty,               // Typically a successful post just gets an empty 200 response
    GenericBody(String), // For when the server echos Json to a successful post
}

impl ApiResponse {
    pub fn report(&self, fmt: ReportFormat) -> Result<Option<String>, ReportError> {
        if fmt == ReportFormat::None {
            Ok(None)
        } else {
            match self {
                ApiResponse::Health => Ok(None),
                ApiResponse::Info(info) => Ok(Some(info.report(fmt)?)),
                ApiResponse::CertAuths(list) => Ok(Some(list.report(fmt)?)),
                ApiResponse::CertAuthInfo(info) => Ok(Some(info.report(fmt)?)),
                ApiResponse::CertAuthHistory(history) => Ok(Some(history.report(fmt)?)),
                ApiResponse::CertAuthAction(details) => Ok(Some(details.report(fmt)?)),
                ApiResponse::CertAuthIssues(issues) => Ok(Some(issues.report(fmt)?)),
                ApiResponse::AllCertAuthIssues(issues) => Ok(Some(issues.report(fmt)?)),
                ApiResponse::RouteAuthorizations(definitions) => Ok(Some(definitions.report(fmt)?)),
                ApiResponse::BgpAnalysisAdvice(analysis) => Ok(Some(analysis.report(fmt)?)),
                ApiResponse::BgpAnalysisFull(table) => Ok(Some(table.report(fmt)?)),
                ApiResponse::BgpAnalysisSuggestions(suggestions) => Ok(Some(suggestions.report(fmt)?)),
                ApiResponse::AspaDefinitions(definitions) => Ok(Some(definitions.report(fmt)?)),
                ApiResponse::BgpSecDefinitions(definitions) => Ok(Some(definitions.report(fmt)?)),
                ApiResponse::ParentCaContact(contact) => Ok(Some(contact.report(fmt)?)),
                ApiResponse::ParentStatuses(statuses) => Ok(Some(statuses.report(fmt)?)),
                ApiResponse::ChildInfo(info) => Ok(Some(info.report(fmt)?)),
                ApiResponse::ChildExported(child) => Ok(Some(child.report(fmt)?)),
                ApiResponse::ChildrenStats(stats) => Ok(Some(stats.report(fmt)?)),
                ApiResponse::PublisherList(list) => Ok(Some(list.report(fmt)?)),
                ApiResponse::PublisherDetails(details) => Ok(Some(details.report(fmt)?)),
                ApiResponse::RepoStats(stats) => Ok(Some(stats.report(fmt)?)),
                ApiResponse::Rfc8183ParentResponse(res) => Ok(Some(res.report(fmt)?)),
                ApiResponse::Rfc8183ChildRequest(req) => Ok(Some(req.report(fmt)?)),
                ApiResponse::Rfc8183PublisherRequest(req) => Ok(Some(req.report(fmt)?)),
                ApiResponse::Rfc8183RepositoryResponse(res) => Ok(Some(res.report(fmt)?)),
                ApiResponse::RepoDetails(details) => Ok(Some(details.report(fmt)?)),
                ApiResponse::RepoStatus(status) => Ok(Some(status.report(fmt)?)),
                ApiResponse::Rta(rta) => Ok(Some(rta.report(fmt)?)),
                ApiResponse::RtaList(list) => Ok(Some(list.report(fmt)?)),
                ApiResponse::RtaMultiPrep(res) => Ok(Some(res.report(fmt)?)),
                ApiResponse::GenericBody(body) => Ok(Some(body.clone())),
                ApiResponse::Empty => Ok(None),
            }
        }
    }
}

//------------ ReportFormat --------------------------------------------------

/// This type defines the format to use when representing the api response
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum ReportFormat {
    None,
    Json,
    Text,
}

impl FromStr for ReportFormat {
    type Err = ReportError;

    fn from_str(s: &str) -> Result<Self, ReportError> {
        match s {
            "none" => Ok(ReportFormat::None),
            "json" => Ok(ReportFormat::Json),
            "text" => Ok(ReportFormat::Text),
            _ => Err(ReportError::UnrecognizedFormat(s.to_string())),
        }
    }
}

//------------ ReportError ---------------------------------------------------

/// This type defines possible Errors for KeyStore
#[derive(Debug)]
pub enum ReportError {
    UnsupportedFormat,
    UnrecognizedFormat(String),
}

impl fmt::Display for ReportError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ReportError::UnsupportedFormat => write!(f, "This report format is not supported for this data"),
            ReportError::UnrecognizedFormat(s) => write!(f, "This report format is not recognized: {}", s),
        }
    }
}

//------------ Report --------------------------------------------------------

/// This trait should be implemented by all api responses, so that the
/// response can be formatted for users.
pub trait Report: Serialize + ToString {
    fn text(&self) -> Result<String, ReportError> {
        Ok(self.to_string())
    }

    fn json(&self) -> String {
        serde_json::to_string_pretty(self).unwrap()
    }

    fn report(&self, format: ReportFormat) -> Result<String, ReportError> {
        match format {
            ReportFormat::None => Ok("".to_string()),
            ReportFormat::Json => Ok(self.json()),
            ReportFormat::Text => self.text(),
        }
    }
}

impl Report for CertAuthList {}
impl Report for CertAuthInfo {}
impl Report for IdCertInfo {}
impl Report for RepositoryContact {}

impl Report for ChildCaInfo {}
impl Report for ExportChild {}

impl Report for ParentCaContact {}
impl Report for ParentStatuses {}

impl Report for CommandHistory {}
impl Report for CaCommandDetails {}

impl Report for PublisherList {}

impl Report for RepoStats {}
impl Report for ChildrenConnectionStats {}

impl Report for PublisherDetails {}

impl Report for idexchange::RepositoryResponse {
    fn text(&self) -> Result<String, ReportError> {
        Ok(self.to_xml_string())
    }
}

impl Report for idexchange::ParentResponse {
    fn text(&self) -> Result<String, ReportError> {
        Ok(self.to_xml_string())
    }
}

impl Report for idexchange::ChildRequest {
    fn text(&self) -> Result<String, ReportError> {
        Ok(self.to_xml_string())
    }
}

impl Report for idexchange::PublisherRequest {
    fn text(&self) -> Result<String, ReportError> {
        Ok(self.to_xml_string())
    }
}

impl Report for ConfiguredRoas {}

impl Report for BgpAnalysisAdvice {}
impl Report for BgpAnalysisReport {}
impl Report for BgpAnalysisSuggestion {}

impl Report for AspaDefinitionList {}

impl Report for BgpSecCsrInfoList {}

impl Report for CaRepoDetails {}
impl Report for RepoStatus {}

impl Report for CertAuthIssues {}

impl Report for AllCertAuthIssues {}

impl Report for ServerInfo {}

impl Report for ResourceTaggedAttestation {}
impl Report for RtaList {}
impl Report for RtaPrepResponse {}

impl Report for TrustAnchorSignerInfo {}
impl Report for TrustAnchorSignedRequest {}
impl Report for TrustAnchorSignedResponse {}
impl Report for TrustAnchorProxySignerExchanges {}