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
|
use crate::ISSUE_TRACKER_LOC;
use clap::{Parser, Subcommand, ValueEnum};
use colored::*;
use serde::{Deserialize, Serialize};
use thiserror;
use url::Url;
use uuid::Uuid;
#[derive(Debug, thiserror::Error)]
pub enum CliError {
#[error("{} {}", "Failed to initialize an iam store.".red(), format!("An invariant was likely broken and should be reported as a bug here: {}", ISSUE_TRACKER_LOC))]
AdminInitializationError,
#[error("{} {}", "Failed to recieve incoming request.".red(), .0.white())]
DevOauthServer(String),
#[error("{} {} {}", "An unknown error occurred.".red(), format!("An invariant was likely broken and should be reported as a bug here: {}", ISSUE_TRACKER_LOC), .0.yellow())]
InternalError(String),
#[error("{} {}", "The provided validation id and code is invalid or has expired.".red(), "You may recieve at most one session with a valid code, after which a new validation is required.")]
InvalidCode,
#[error("{}", "iam failed to read a valid configuration profile. Initialize an iam store with `iam admin init`".red())]
InvalidProfile,
#[error("{} {}", "Failed to initialize secd: ".red(), .0.yellow())]
SecdInitializationFailure(String),
}
#[derive(Parser)]
#[command(
name = "iam",
author = "benabel",
version = "0.0.1",
long_about = "SecD IAM\nIdentity and access management secured by secd and user controlled. Get started with `iam init`"
)]
pub struct Args {
#[command(subcommand)]
pub command: Command,
/// IAM backend profile as defined in the iam config or as a connection string to an auth store.
#[arg(long, short)]
pub profile: Option<String>,
/// The store type for the associated connection string
#[arg(long, short)]
pub store_type: Option<StoreType>,
/// Connection string for the IAM store
#[arg(long, short)]
pub connection_string: Option<String>,
}
#[derive(Clone, ValueEnum)]
pub enum StoreType {
Dynamo,
Memory,
Mysql,
Postgres,
Redis,
Sqlite,
}
#[derive(Subcommand)]
pub enum Command {
#[command(
about = "Administrative actions for this IAM instance",
long_about = "Admin\n\nAdministrative actions for this IAM instance. Each IAM instance is defined by the specified backend in the iam config or manually as an optional argument"
)]
Admin {
#[command(subcommand)]
action: AdminAction,
},
#[command(
about = "Create a new IAM object which may be used to enforce authorization schemes.",
long_about = "Create\n\nEntities which define the structure of an IAM instance. These entities may be rendered as an IAM graph, or within a web view, to more easily visualize and manipulate the IAM instance."
)]
Create {
#[command(subcommand)]
object: CreateObject,
},
#[command(
about = "Utility and convenience commands while developing against secd",
long_about = "Dev\n\nUtility and convenience commands while developing against secd. Easily retrieve local mail, monitor secd logs, and otherwise inspect or interact with the system."
)]
Dev {
#[command(subcommand)]
object: DevObject,
},
#[command(
about = "List and filter IAM objects",
long_about = "Get\n\nGet details for a specific IAM object"
)]
Get {
#[command(subcommand)]
object: GetObject,
},
/// Start the iam repl to more easily interact with iam and its primitives
Repl,
#[command(
about = "Update specified IAM object",
long_about = "Update\n\nUpdate details for the specified IAM object"
)]
Update {
#[command(subcommand)]
object: UpdateObject,
},
#[command(
about = "Validate an IAM credential, optionally returning a session from the validation",
long_about = "Validate\n\nCredentials which have been created for identities may be validated and optionally exchanged for sessions."
)]
Validate {
#[command(subcommand)]
object: ValidateObject,
},
}
#[derive(Subcommand)]
pub enum AdminAction {
/// Aliased as `iam init`
Init {
/// If true, interactively initialize an IAM store. Otherwise output a template config.
#[arg(long, short, action)]
interactive: bool,
},
/// Configure, describe, or rotate the default IAM store.
Backend {
#[command(subcommand)]
action: AdminBackendAction,
},
/// Create a new administrative entity for an IAM store.
Create {
#[command(subcommand)]
object: AdminObject,
},
/// Seal the configured IAM store to prevent administrative changes
Seal,
/// Unseal the configured IAM store to make administrative changes
Unseal {
/// The secret key used to seal this store
secret_key: String,
},
}
#[derive(Subcommand)]
pub enum AdminBackendAction {
Configure {
name: String,
store: StoreType,
connection: String,
},
Switch {
name: String,
},
}
#[derive(Subcommand)]
pub enum AdminObject {
/// An email template used for IAM procedures, including identity validation
EmailTemplate {
template_type: EmailTemplateType,
template: String,
},
/// A selected provider capable of sending email messages
EmailProvider {
provider: EmailProvider,
secret_key: String,
public_key: Option<String>,
},
/// A selected Oauth2.0 provider capable of authenticating identities
Oauth2Provider {
client_id: String,
secret: String,
redirect_url: Url,
},
/// A selected provider capable of sending SMS
SmsProvider {
provider: SmsProvider,
secret_key: String,
public_key: Option<String>,
},
/// A new secret which may be used to unseal the IAM store
StoreSecret,
/// A selected provider capable of sending automated voice messages
VoiceProvider {
provider: VoiceProvider,
secret_key: String,
public_key: Option<String>,
},
}
#[derive(Clone, ValueEnum)]
pub enum EmailTemplateType {
Login,
SignUp,
}
#[derive(Clone, ValueEnum)]
pub enum EmailProvider {
Custom,
Mailgun,
Sendgrid,
Ses,
}
#[derive(Clone, ValueEnum)]
pub enum SmsProvider {
AwsSns,
Custom,
Twilio,
}
#[derive(Clone, ValueEnum)]
pub enum VoiceProvider {
Custom,
Twilio,
}
#[derive(Subcommand)]
pub enum CreateObject {
Credential {
#[command(subcommand)]
method: CredentialMethod,
/// The identity against which to associate this credential. A new identity will be created if no identity is provided.
#[arg(long, short)]
identity_id: Option<Uuid>,
},
Impersonator {
/// The identity which will be the source impersonator.
impersonator_id: Uuid,
/// The identity id which will be the target for impersonation, and for whom a credential will be created.
target_id: Uuid,
},
Validation {
/// Method by which the validation will occur
#[command(subcommand)]
method: ValidationMethod,
/// The identity against which to associate this validation. A new identity will be created if no identity is provided.
#[arg(long, short)]
identity_id: Option<Uuid>,
},
#[command(
about = "An action which completes an address validation",
long_about = "Validation Completion\n\nA validation completion depends on an existing address validation, which is validated based on the provided validation id and secret token or secret code"
)]
ValidationCompletion {
/// The validation id against which to complete the validation.
validation_id: Uuid,
/// The secret token for the validation. A token or code must be provided.
#[arg(long, short)]
token: Option<String>,
/// The secret code for the validation. A code or token must be provided.
#[arg(long, short)]
code: Option<String>,
},
}
#[derive(Subcommand)]
pub enum DevObject {
#[command(
about = "Create a temporary server to easily receive oauth validation during development.",
long_about = "Oauth2\n\nCreate a temporary server to easily receive oauth validation during development."
)]
Oauth2Server {
/// The port on which the server should listen. You must specify this exact port with your oauth provider. Defaults to 1337
#[arg(long, short)]
port: Option<u16>,
},
}
#[derive(Subcommand)]
pub enum CredentialMethod {
ApiToken {
#[arg(long)]
expires_at: Option<i64>,
},
/// Unique username and passphrase credential. Each username may have at most one Passprhase credential.
Passphrase {
/// The username associated with this credential
username: String,
/// The secret passphrase for this credential
passphrase: String,
},
}
#[derive(Subcommand)]
pub enum ValidationMethod {
/// An email address to which the validation will be sent
Email {
/// Email address which will receive the validation
address: String,
},
/// A phone which an identity may authenticate via SMS or Voice
Phone {
/// Whether to use a voice code. Otherwise, uses SMS
#[arg(long, short, action)]
use_voice: bool,
},
}
#[derive(Subcommand)]
pub enum GetObject {
Identity {
/// The unique id corresponding to this identity.
#[arg(long, short)]
identity_id: Option<Uuid>,
/// The credential corresponding to this identity.
#[command(subcommand)]
credential: Option<ValidateObject>,
},
}
#[derive(Subcommand)]
pub enum UpdateObject {
Identity {
/// Unique identifier for this identity.
id: Uuid,
/// Metadata for this identity. Note, structured metadata must be configured to be enforced.
#[arg(long, short)]
metadata: Option<String>,
},
Credential {
/// Unique identifier for this credential.
/// Note: You can validate the credential to find it's id.
id: Uuid,
/// Whether to revoke this credential. Once revoked, the credential may no longer be used
/// and may not be un-revoked.
#[arg(long, short, action)]
revoke: bool,
},
}
#[derive(Subcommand)]
pub enum ValidateObject {
ApiToken {
/// Api token to validate
token: String,
},
Passphrase {
/// The username associated with this credential
username: String,
/// The secret passphrase for this credential
passphrase: String,
},
Session {
/// The secret token associated with this session.
token: String,
},
}
#[derive(Serialize, Deserialize)]
pub struct Config {
pub profile: Vec<ConfigProfile>,
}
#[derive(Serialize, Deserialize)]
pub struct ConfigProfile {
pub name: String,
pub store: secd::AuthStore,
pub store_conn: String,
pub emailer: secd::AuthEmailMessenger,
pub email_template_login: Option<String>,
pub email_template_signup: Option<String>,
}
#[derive(Serialize, Deserialize)]
pub struct Validation {
pub validation_id: Uuid,
#[serde(skip_serializing_if = "Option::is_none")]
pub oauth_auth_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub note: Option<String>,
}
pub type ValidationSecretCode = String;
|