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
//! Utilities for interacting with keyring and performing signing operations.

use crate::config::Config;
use crate::RegistryUrl;
use indexmap::IndexSet;
use secrecy::Secret;
use warg_crypto::signing::PrivateKey;

mod error;
use error::KeyringAction;
pub use error::KeyringError;

pub mod flatfile;

/// Interface to a pluggable keyring backend
#[derive(Debug)]
pub struct Keyring {
    imp: Box<keyring::CredentialBuilder>,
    name: &'static str,
}

/// Result type for keyring errors.
pub type Result<T, E = KeyringError> = std::result::Result<T, E>;

impl Keyring {
    #[cfg(target_os = "linux")]
    /// List of supported credential store backends
    pub const SUPPORTED_BACKENDS: &'static [&'static str] =
        &["secret-service", "flat-file", "linux-keyutils", "mock"];
    #[cfg(any(target_os = "freebsd", target_os = "openbsd"))]
    /// List of supported credential store backends
    pub const SUPPORTED_BACKENDS: &'static [&'static str] =
        &["secret-service", "flat-file", "mock"];
    #[cfg(target_os = "windows")]
    /// List of supported credential store backends
    pub const SUPPORTED_BACKENDS: &'static [&'static str] = &["windows", "flat-file", "mock"];
    #[cfg(target_os = "macos")]
    /// List of supported credential store backends
    pub const SUPPORTED_BACKENDS: &'static [&'static str] = &["macos", "flat-file", "mock"];
    #[cfg(target_os = "ios")]
    /// List of supported credential store backends
    pub const SUPPORTED_BACKENDS: &'static [&'static str] = &["ios", "flat-file", "mock"];
    #[cfg(not(any(
        target_os = "linux",
        target_os = "freebsd",
        target_os = "openbsd",
        target_os = "macos",
        target_os = "ios",
        target_os = "windows",
    )))]
    /// List of supported credential store backends
    pub const SUPPORTED_BACKENDS: &'static [&'static str] = &["flat-file", "mock"];

    /// The default backend when no configuration option is set
    pub const DEFAULT_BACKEND: &'static str = Self::SUPPORTED_BACKENDS[0];

    /// Returns a human-readable description of a keyring backend.
    pub fn describe_backend(backend: &str) -> &'static str {
        match backend {
            "secret-service" => "Freedesktop.org secret service (GNOME Keyring or KWallet)",
            "linux-keyutils" => "Linux kernel memory-based keystore (lacks persistence, not suitable for desktop use)",
            "windows" => "Windows Credential Manager",
            "macos" => "MacOS Keychain",
            "ios" => "Apple iOS Keychain",
            "flat-file" => "Unencrypted flat files in your warg config directory",
            "mock" => "Mock credential store with no persistence (for testing only)",
            _ => "(no description available)"
        }
    }

    fn load_backend(backend: &str) -> Result<Box<keyring::CredentialBuilder>> {
        if !Self::SUPPORTED_BACKENDS.contains(&backend) {
            return Err(KeyringError::unknown_backend(backend.to_owned()));
        }

        #[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "openbsd"))]
        if backend == "secret-service" {
            return Ok(keyring::secret_service::default_credential_builder());
        }

        #[cfg(target_os = "linux")]
        if backend == "linux-keyutils" {
            return Ok(keyring::keyutils::default_credential_builder());
        }

        #[cfg(target_os = "macos")]
        if backend == "macos" {
            return Ok(keyring::macos::default_credential_builder());
        }

        #[cfg(target_os = "ios")]
        if backend == "ios" {
            return Ok(keyring::ios::default_credential_builder());
        }

        #[cfg(target_os = "windows")]
        if backend == "windows" {
            return Ok(keyring::windows::default_credential_builder());
        }

        if backend == "flat-file" {
            return Ok(Box::new(
                flatfile::FlatfileCredentialBuilder::new()
                    .map_err(|e| KeyringError::backend_init_failure("flat-file", e))?,
            ));
        }

        if backend == "mock" {
            return Ok(keyring::mock::default_credential_builder());
        }

        unreachable!("missing logic for backend {backend}")
    }

    /// Instantiate a new keyring.
    ///
    /// The argument should be an element of [Self::SUPPORTED_BACKENDS].
    pub fn new(backend: &str) -> Result<Self> {
        Self::load_backend(backend).map(|imp| Self {
            imp,
            // Get an equivalent &'static str from our &str
            name: Self::SUPPORTED_BACKENDS
                .iter()
                .find(|s| **s == backend)
                .expect("successfully-loaded backend should be found in SUPPORTED_BACKENDS"),
        })
    }

    /// Instantiate a new keyring using the backend specified in a configuration file.
    pub fn from_config(config: &Config) -> Result<Self> {
        if let Some(ref backend) = config.keyring_backend {
            Self::new(backend.as_str())
        } else {
            Self::new(Self::DEFAULT_BACKEND)
        }
    }

    /// Gets the auth token entry for the given registry and key name.
    pub fn get_auth_token_entry(&self, registry_url: &RegistryUrl) -> Result<keyring::Entry> {
        let label = format!("warg-auth-token:{}", registry_url.safe_label());
        let cred = self
            .imp
            .build(None, &label, &registry_url.safe_label())
            .map_err(|e| {
                KeyringError::auth_token_access_error(
                    self.name,
                    registry_url,
                    KeyringAction::Open,
                    e,
                )
            })?;
        Ok(keyring::Entry::new_with_credential(cred))
    }

    /// Gets the auth token
    pub fn get_auth_token(&self, registry_url: &RegistryUrl) -> Result<Option<Secret<String>>> {
        let entry = self.get_auth_token_entry(registry_url)?;
        match entry.get_password() {
            Ok(secret) => Ok(Some(Secret::from(secret))),
            Err(keyring::Error::NoEntry) => Ok(None),
            Err(e) => Err(KeyringError::auth_token_access_error(
                self.name,
                registry_url,
                KeyringAction::Get,
                e,
            )),
        }
    }

    /// Deletes the auth token
    pub fn delete_auth_token(&self, registry_url: &RegistryUrl) -> Result<()> {
        let entry = self.get_auth_token_entry(registry_url)?;
        entry.delete_password().map_err(|e| {
            KeyringError::auth_token_access_error(self.name, registry_url, KeyringAction::Delete, e)
        })
    }

    /// Sets the auth token
    pub fn set_auth_token(&self, registry_url: &RegistryUrl, token: &str) -> Result<()> {
        let entry = self.get_auth_token_entry(registry_url)?;
        entry.set_password(token).map_err(|e| {
            KeyringError::auth_token_access_error(self.name, registry_url, KeyringAction::Set, e)
        })
    }

    /// Gets the signing key entry for the given registry and key name.
    pub fn get_signing_key_entry(
        &self,
        registry_url: Option<&str>,
        keys: &IndexSet<String>,
        home_url: Option<&str>,
    ) -> Result<keyring::Entry> {
        if let Some(registry_url) = registry_url {
            let user = if keys.contains(registry_url) {
                registry_url
            } else {
                "default"
            };
            let cred = self
                .imp
                .build(None, "warg-signing-key", user)
                .map_err(|e| {
                    KeyringError::signing_key_access_error(
                        self.name,
                        Some(registry_url),
                        KeyringAction::Open,
                        e,
                    )
                })?;
            Ok(keyring::Entry::new_with_credential(cred))
        } else {
            if let Some(url) = home_url {
                if keys.contains(url) {
                    let cred = self
                        .imp
                        .build(
                            None,
                            "warg-signing-key",
                            &RegistryUrl::new(url)
                                .map_err(|e| {
                                    KeyringError::signing_key_access_error(
                                        self.name,
                                        Some(url),
                                        KeyringAction::Open,
                                        e,
                                    )
                                })?
                                .safe_label(),
                        )
                        .map_err(|e| {
                            KeyringError::signing_key_access_error(
                                self.name,
                                Some(url),
                                KeyringAction::Open,
                                e,
                            )
                        })?;
                    return Ok(keyring::Entry::new_with_credential(cred));
                }
            }

            if keys.contains("default") {
                let cred = self
                    .imp
                    .build(None, "warg-signing-key", "default")
                    .map_err(|e| {
                        KeyringError::signing_key_access_error(
                            self.name,
                            None::<&str>,
                            KeyringAction::Open,
                            e,
                        )
                    })?;
                return Ok(keyring::Entry::new_with_credential(cred));
            }

            Err(KeyringError::no_default_signing_key(self.name))
        }
    }

    /// Gets the signing key for the given registry registry_label and key name.
    pub fn get_signing_key(
        &self,
        // If being called by a cli key command, this will always be a cli flag
        // If being called by a client publish command, this could also be supplied by namespace map config
        registry_url: Option<&str>,
        keys: &IndexSet<String>,
        home_url: Option<&str>,
    ) -> Result<PrivateKey> {
        let entry = self.get_signing_key_entry(registry_url, keys, home_url)?;

        match entry.get_password() {
            Ok(secret) => PrivateKey::decode(secret).map_err(|e| {
                KeyringError::signing_key_access_error(
                    self.name,
                    registry_url,
                    KeyringAction::Get,
                    anyhow::Error::from(e),
                )
            }),
            Err(e) => Err(KeyringError::signing_key_access_error(
                self.name,
                registry_url,
                KeyringAction::Get,
                e,
            )),
        }
    }

    /// Sets the signing key for the given registry host and key name.
    pub fn set_signing_key(
        &self,
        registry_url: Option<&str>,
        key: &PrivateKey,
        keys: &mut IndexSet<String>,
        home_url: Option<&str>,
    ) -> Result<()> {
        let entry = self.get_signing_key_entry(registry_url, keys, home_url)?;
        entry.set_password(&key.encode()).map_err(|e| {
            KeyringError::signing_key_access_error(self.name, registry_url, KeyringAction::Set, e)
        })
    }

    /// Deletes the signing key for the given registry host and key name.
    pub fn delete_signing_key(
        &self,
        registry_url: Option<&str>,
        keys: &IndexSet<String>,
        home_url: Option<&str>,
    ) -> Result<()> {
        let entry = self.get_signing_key_entry(registry_url, keys, home_url)?;
        entry.delete_password().map_err(|e| {
            KeyringError::signing_key_access_error(
                self.name,
                registry_url,
                KeyringAction::Delete,
                e,
            )
        })
    }
}