locket 0.17.3

Helper tool for secret injection as a process dependency
Documentation
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
//! Defines the Infisical secret reference syntax and parsing logic.
//!
//! Since infisical does not have a native secret reference syntax, this defines
//! a URI based scheme
//!
//! `infisical:///<secret-key>?env=<env-slug>&path=<path>&project_id=<project-uuid>&type=<secret-type>`
//!
//! * The URI prefix is used to disambiguate from other providers.
//! * The secret key is required and is encoded in the path component.
//! * The environment slug, path, project ID, and secret type are optional query parameters, which override defaults.
//!
use super::{Extract, ReferenceSyntax, SecretReference};
use clap::ValueEnum;
use percent_encoding::percent_decode_str;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use std::sync::LazyLock;
use thiserror::Error;
use url::Url;
use uuid::Uuid;

// slugs can be lowercase, numbers, or hyphens only
static SLUG_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^[a-z0-9-]+$").expect("regex must be valid"));

// keys cannot contain slashes, control characters, or colon
static KEY_INVALID_CHARS: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"[:/?\x00-\x1f]").expect("regex must be valid"));

// paths must begin with / and contain only alphanumerics and dashes
static PATH_RE: LazyLock<Regex> =
    LazyLock::new(|| Regex::new(r"^/[a-zA-Z0-9_/-]*$").expect("regex must be valid"));

#[derive(Debug, Error)]
pub enum InfisicalParseError {
    #[error("reference must start with 'infisical://'")]
    InvalidScheme,

    #[error("missing secret key in path")]
    MissingKey,

    #[error("'infisical://{0}' is missing secret key. Did you mean 'infisical:///{0}'?")]
    KeyAsHost(String),

    #[error("invalid URL format: {0}")]
    Url(#[from] url::ParseError),

    #[error("utf8 decode error: {0}")]
    Utf8(#[from] std::str::Utf8Error),

    #[error("validation error: {0}")]
    Validation(#[from] ValidationError),

    #[error("query param error: {0}")]
    QueryParam(#[from] serde_urlencoded::de::Error),
}

#[derive(Debug, Error)]
pub enum ValidationError {
    #[error("invalid slug '{0}': must contain only lowercase letters, numbers, and hyphens")]
    Slug(String),

    #[error("invalid secret key '{0}': cannot contain slashes, colons, or control characters")]
    Key(String),

    #[error("invalid project ID '{0}': must be a valid UUID")]
    ProjectId(String),

    #[error("invalid path '{0}': must start with '/' and contain only alphanumerics and dashes")]
    Path(String),
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct InfisicalReference {
    pub key: InfisicalSecretKey,
    pub options: InfisicalOptions,
}

impl From<InfisicalReference> for SecretReference {
    fn from(r: InfisicalReference) -> Self {
        Self::Infisical(r)
    }
}

impl ReferenceSyntax for InfisicalReference {
    fn try_parse(raw: &str) -> Option<Self> {
        Self::from_str(raw)
            .inspect_err(|e| {
                if !matches!(e, InfisicalParseError::InvalidScheme) {
                    tracing::warn!("Invalid Infisical reference '{}': {}", raw, e);
                }
            })
            .ok()
    }
}

impl Extract for InfisicalReference {
    fn extract(r: &SecretReference) -> Option<&Self> {
        #[allow(unreachable_patterns)]
        match r {
            SecretReference::Infisical(inner) => Some(inner),
            _ => None,
        }
    }
}

impl FromStr for InfisicalReference {
    type Err = InfisicalParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if !s.starts_with("infisical://") {
            return Err(InfisicalParseError::InvalidScheme);
        }

        let url = Url::parse(s)?;

        if let Some(host) = url.host_str()
            && (url.path() == "/" || url.path().is_empty())
        {
            return Err(InfisicalParseError::KeyAsHost(host.to_string()));
        }

        let path = url.path();
        let raw_key = path.strip_prefix('/').unwrap_or(path);

        if raw_key.is_empty() {
            return Err(InfisicalParseError::MissingKey);
        }

        let decoded_key = percent_decode_str(raw_key)
            .decode_utf8()
            .map_err(InfisicalParseError::Utf8)?
            .to_string();

        let key =
            InfisicalSecretKey::parse(decoded_key).map_err(InfisicalParseError::Validation)?;

        let options: InfisicalOptions = serde_urlencoded::from_str(url.query().unwrap_or(""))?;

        Ok(Self { key, options })
    }
}

impl fmt::Display for InfisicalReference {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut url = Url::parse("infisical://").map_err(|_| fmt::Error)?;

        url.set_path(self.key.as_str());

        let query = serde_urlencoded::to_string(&self.options).map_err(|_| fmt::Error)?;
        if !query.is_empty() {
            url.set_query(Some(&query));
        }

        write!(f, "{}", url)
    }
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct InfisicalOptions {
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub env: Option<InfisicalSlug>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub path: Option<InfisicalPath>,

    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub project_id: Option<InfisicalProjectId>,

    #[serde(default, rename = "type", skip_serializing_if = "Option::is_none")]
    pub secret_type: Option<InfisicalSecretType>,
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct InfisicalSlug(String);

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct InfisicalSecretKey(String);

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct InfisicalProjectId(Uuid);

#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct InfisicalPath(String);

#[derive(Debug, Serialize, Default, Deserialize, Clone, PartialEq, Eq, Hash, ValueEnum, Copy)]
#[serde(rename_all = "lowercase")]
pub enum InfisicalSecretType {
    #[default]
    Shared,
    Personal,
}

impl std::fmt::Display for InfisicalSecretType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.to_possible_value()
            .expect("no values are skipped")
            .get_name()
            .fmt(f)
    }
}

impl TryFrom<String> for InfisicalSlug {
    type Error = ValidationError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if !SLUG_RE.is_match(&value) {
            return Err(ValidationError::Slug(value));
        }
        Ok(Self(value))
    }
}

impl FromStr for InfisicalSlug {
    type Err = ValidationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from(s.to_string())
    }
}

impl From<InfisicalSlug> for String {
    fn from(slug: InfisicalSlug) -> Self {
        slug.0
    }
}

impl AsRef<str> for InfisicalSlug {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for InfisicalSlug {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl InfisicalSecretKey {
    pub fn parse(s: impl Into<String>) -> Result<Self, ValidationError> {
        let s = s.into();
        if KEY_INVALID_CHARS.is_match(&s) {
            return Err(ValidationError::Key(s));
        }
        Ok(Self(s))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl fmt::Display for InfisicalSecretKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl TryFrom<String> for InfisicalProjectId {
    type Error = ValidationError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        let uuid = Uuid::parse_str(&value)
            .map_err(|_| ValidationError::ProjectId(format!("'{}' is not a valid UUID", value)))?;
        Ok(Self(uuid))
    }
}

impl FromStr for InfisicalProjectId {
    type Err = ValidationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from(s.to_string())
    }
}

impl From<Uuid> for InfisicalProjectId {
    fn from(uuid: Uuid) -> Self {
        Self(uuid)
    }
}

impl From<InfisicalProjectId> for String {
    fn from(pid: InfisicalProjectId) -> Self {
        pid.0.to_string()
    }
}

impl fmt::Display for InfisicalProjectId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl InfisicalPath {
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl FromStr for InfisicalPath {
    type Err = ValidationError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::try_from(s.to_string())
    }
}

impl TryFrom<String> for InfisicalPath {
    type Error = ValidationError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        if !PATH_RE.is_match(&value) {
            return Err(ValidationError::Path(value));
        }
        Ok(Self(value))
    }
}

impl AsRef<str> for InfisicalPath {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl From<InfisicalPath> for String {
    fn from(p: InfisicalPath) -> Self {
        p.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_full_reference() {
        let raw = format!(
            "infisical:///my-secret-key?env=prod&path=/app/backend&project_id={}",
            InfisicalProjectId::from(Uuid::new_v4())
        );
        let reference =
            InfisicalReference::from_str(raw.as_str()).expect("should parse valid reference");

        assert_eq!(reference.key.as_str(), "my-secret-key");

        assert_eq!(reference.options.env.unwrap().as_ref(), "prod");
        assert_eq!(
            reference.options.path,
            Some(InfisicalPath::try_from("/app/backend".to_string()).unwrap())
        );
    }

    #[test]
    fn test_parse_minimal_reference() {
        let raw = "infisical:///simple-key";
        let reference = InfisicalReference::from_str(raw).expect("should parse minimal reference");

        assert_eq!(reference.key.as_str(), "simple-key");
        assert_eq!(reference.options.env, None);
    }

    #[test]
    fn test_url_encoding_handling() {
        let raw = "infisical:///My%20Secret%20Key?env=staging-env";
        let reference = InfisicalReference::from_str(raw).expect("should handle encoding");

        assert_eq!(reference.key.as_str(), "My Secret Key");
        assert_eq!(reference.options.env.unwrap().as_ref(), "staging-env");
    }

    #[test]
    fn test_display_round_trip() {
        let original = InfisicalReference {
            key: InfisicalSecretKey::parse("complex* -_key name").unwrap(),
            options: InfisicalOptions {
                env: Some(InfisicalSlug::try_from("production".to_string()).unwrap()),
                path: Some(InfisicalPath::try_from("/deeply/nested/path".to_string()).unwrap()),
                secret_type: Some(InfisicalSecretType::default()),
                project_id: None,
            },
        };

        let serialized = original.to_string();

        assert!(serialized.starts_with("infisical:///complex*%20-_key%20name"));
        let deserialized = InfisicalReference::from_str(&serialized).expect("should re-parse");
        assert_eq!(original, deserialized);
    }

    #[test]
    fn test_reject_colon() {
        let raw = "infisical:///Key:With:Colon";
        let err = InfisicalReference::from_str(raw);

        assert!(matches!(
            err,
            Err(InfisicalParseError::Validation(ValidationError::Key(_)))
        ));
    }

    #[test]
    fn test_reject_slash_in_key() {
        let raw = "infisical:///folder/key";
        let err = InfisicalReference::from_str(raw);
        assert!(matches!(
            err,
            Err(InfisicalParseError::Validation(ValidationError::Key(_)))
        ));
    }

    #[test]
    fn test_slug_enforcement() {
        assert!(InfisicalSlug::try_from("prod-v1".to_string()).is_ok());
        assert!(InfisicalSlug::try_from("Prod".to_string()).is_err());

        let raw = "infisical:///key?env=Bad_Slug";
        let res = InfisicalReference::from_str(raw);

        assert!(res.is_err());
    }

    #[test]
    fn test_path_validation() {
        assert!(InfisicalPath::try_from("/prod/backend-service/v1".to_string()).is_ok());
        assert!(InfisicalPath::try_from("/TEST_AREA/my_folder".to_string()).is_ok());
        let err = InfisicalPath::try_from("/prod/my folder".to_string()).unwrap_err();
        assert!(matches!(err, ValidationError::Path(_)));

        let err = InfisicalPath::try_from("/prod/v1.0".to_string()).unwrap_err();
        assert!(matches!(err, ValidationError::Path(_)));

        let err = InfisicalPath::try_from("prod/db".to_string()).unwrap_err();
        assert!(matches!(err, ValidationError::Path(_)));
    }

    #[test]
    fn test_project_id_validation() {
        assert!(InfisicalProjectId::try_from(Uuid::new_v4().to_string()).is_ok());
        let err = InfisicalProjectId::try_from("invalid-uuid".to_string()).unwrap_err();
        assert!(matches!(err, ValidationError::ProjectId(_)));
    }

    #[test]
    fn test_secret_type_validation() {
        let raw = "infisical:///key?type=shared";
        let reference = InfisicalReference::from_str(raw).expect("should parse shared");
        assert_eq!(
            reference.options.secret_type,
            Some(InfisicalSecretType::Shared)
        );

        let raw = "infisical:///key?type=personal";
        let reference = InfisicalReference::from_str(raw).expect("should parse personal");
        assert_eq!(
            reference.options.secret_type,
            Some(InfisicalSecretType::Personal)
        );

        let raw = "infisical:///key?type=user";
        let err = InfisicalReference::from_str(raw);

        assert!(err.is_err());

        assert!(matches!(
            err.unwrap_err(),
            InfisicalParseError::QueryParam(_)
        ));

        let raw = "infisical:///key?type=SHARED";
        assert!(InfisicalReference::from_str(raw).is_err());
    }
}