monitor_types/
config.rs

1use std::{collections::HashMap, net::IpAddr, path::PathBuf};
2
3use serde::{Deserialize, Serialize};
4use typeshare::typeshare;
5
6use crate::Timelength;
7
8pub type GithubUsername = String;
9pub type GithubToken = String;
10pub type GithubAccounts = HashMap<GithubUsername, GithubToken>;
11
12pub type DockerUsername = String;
13pub type DockerToken = String;
14pub type DockerAccounts = HashMap<DockerUsername, DockerToken>;
15
16pub type SecretsMap = HashMap<String, String>; // these are used for injection into deployments run commands
17
18#[derive(Serialize, Deserialize, Debug, Clone)]
19pub struct CoreConfig {
20    #[serde(default = "default_title")]
21    pub title: String,
22
23    // the host to use with oauth redirect url, whatever host the user hits to access monitor. eg 'https://monitor.mogh.tech'
24    pub host: String,
25
26    // port the core web server runs on
27    #[serde(default = "default_core_port")]
28    pub port: u16,
29
30    pub jwt_secret: String,
31
32    #[serde(default = "default_jwt_valid_for")]
33    pub jwt_valid_for: Timelength,
34
35    // interval at which to collect server stats and alert for out of bounds
36    pub monitoring_interval: Timelength,
37
38    // daily utc offset in hours to run daily update. eg 8:00 eastern time is 13:00 UTC, so offset should be 13. default of 0 runs at UTC midnight.
39    #[serde(default)]
40    pub daily_offset_hours: u8,
41
42    // number of days to keep stats, or 0 to disable pruning. stats older than this number of days are deleted on a daily cycle
43    #[serde(default)]
44    pub keep_stats_for_days: u64,
45
46    // used to verify validity from github webhooks
47    pub github_webhook_secret: String,
48
49    // used to form the frontend listener url, if None will use 'host'.
50    pub github_webhook_base_url: Option<String>,
51
52    // sent in auth header with req to periphery
53    pub passkey: String,
54
55    // integration with slack app
56    pub slack_url: Option<String>,
57
58    // enable login with local auth
59    pub local_auth: bool,
60
61    // allowed docker orgs used with monitor. first in this list will be default for build
62    #[serde(default)]
63    pub docker_organizations: Vec<String>,
64
65    pub mongo: MongoConfig,
66
67    #[serde(default)]
68    pub github_oauth: OauthCredentials,
69
70    #[serde(default)]
71    pub google_oauth: OauthCredentials,
72
73    #[serde(default)]
74    pub aws: AwsBuilderConfig,
75}
76
77fn default_title() -> String {
78    String::from("monitor")
79}
80
81fn default_core_port() -> u16 {
82    9000
83}
84
85fn default_jwt_valid_for() -> Timelength {
86    Timelength::OneWeek
87}
88
89#[derive(Serialize, Deserialize, Debug, Clone, Default)]
90pub struct OauthCredentials {
91    #[serde(default)]
92    pub enabled: bool,
93    #[serde(default)]
94    pub id: String,
95    #[serde(default)]
96    pub secret: String,
97}
98
99#[derive(Serialize, Deserialize, Debug, Clone)]
100pub struct MongoConfig {
101    pub uri: String,
102    #[serde(default = "default_core_mongo_app_name")]
103    pub app_name: String,
104    #[serde(default = "default_core_mongo_db_name")]
105    pub db_name: String,
106}
107
108fn default_core_mongo_app_name() -> String {
109    "monitor_core".to_string()
110}
111
112fn default_core_mongo_db_name() -> String {
113    "monitor".to_string()
114}
115
116#[typeshare]
117#[derive(Serialize, Deserialize, Debug, Clone, Default)]
118pub struct AwsBuilderConfig {
119    #[serde(skip_serializing)]
120    pub access_key_id: String,
121
122    #[serde(skip_serializing)]
123    pub secret_access_key: String,
124
125    pub default_ami_name: String,
126    pub default_subnet_id: String,
127    pub default_key_pair_name: String,
128
129    #[serde(default)]
130    pub available_ami_accounts: AvailableAmiAccounts,
131
132    #[serde(default = "default_aws_region")]
133    pub default_region: String,
134
135    #[serde(default = "default_volume_gb")]
136    pub default_volume_gb: i32,
137
138    #[serde(default = "default_instance_type")]
139    pub default_instance_type: String,
140
141    #[serde(default)]
142    pub default_security_group_ids: Vec<String>,
143
144    #[serde(default)]
145    pub default_assign_public_ip: bool,
146}
147
148fn default_aws_region() -> String {
149    String::from("us-east-1")
150}
151
152fn default_volume_gb() -> i32 {
153    8
154}
155
156fn default_instance_type() -> String {
157    String::from("m5.2xlarge")
158}
159
160#[typeshare]
161pub type AvailableAmiAccounts = HashMap<String, AmiAccounts>; // (ami_name, AmiAccounts)
162
163#[typeshare]
164#[derive(Serialize, Deserialize, Debug, Clone, Default)]
165pub struct AmiAccounts {
166    pub ami_id: String,
167    #[serde(default)]
168    pub github: Vec<String>,
169    #[serde(default)]
170    pub docker: Vec<String>,
171    #[serde(default)]
172    pub secrets: Vec<String>,
173}
174
175#[derive(Serialize, Deserialize, Debug, Clone)]
176pub struct PeripheryConfig {
177    #[serde(default = "default_periphery_port")]
178    pub port: u16,
179    #[serde(default = "default_repo_dir")]
180    pub repo_dir: PathBuf,
181    #[serde(default = "default_stats_refresh_interval")]
182    pub stats_polling_rate: Timelength,
183    #[serde(default)]
184    pub allowed_ips: Vec<IpAddr>,
185    #[serde(default)]
186    pub passkeys: Vec<String>,
187    #[serde(default)]
188    pub secrets: SecretsMap,
189    #[serde(default)]
190    pub github_accounts: GithubAccounts,
191    #[serde(default)]
192    pub docker_accounts: DockerAccounts,
193}
194
195fn default_periphery_port() -> u16 {
196    8000
197}
198
199fn default_repo_dir() -> PathBuf {
200    "/repos".parse().unwrap()
201}
202
203fn default_stats_refresh_interval() -> Timelength {
204    Timelength::FiveSeconds
205}