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
//! A filesystem directory based implementation of a `KeyManager`

use std::{
    ops::Deref,
    path::{Path, PathBuf},
};

use anyhow::Result;
use nkeys::KeyPair;

use super::KeyManager;

pub const KEY_FILE_EXTENSION: &str = "nk";

pub struct KeyDir(PathBuf);

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

impl Deref for KeyDir {
    type Target = Path;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl KeyDir {
    /// Creates a new KeyDir, erroring if it is unable to access or create the given directory.
    pub fn new(path: impl AsRef<Path>) -> Result<KeyDir> {
        let p = path.as_ref();
        let exists = p.exists();
        if exists && !p.is_dir() {
            anyhow::bail!("{} is not a directory (or cannot be accessed)", p.display())
        } else if !exists {
            std::fs::create_dir_all(p)?;
        }
        // Always ensure the directory has the proper permissions, even if it exists
        set_permissions_keys(p)?;
        // Make sure we have the fully qualified path at this point
        Ok(KeyDir(p.canonicalize()?))
    }

    /// Returns a list of paths to all keyfiles in the directory
    pub fn list_paths(&self) -> Result<Vec<PathBuf>> {
        let paths = std::fs::read_dir(&self.0)?;

        Ok(paths
            .filter_map(|p| {
                if let Ok(entry) = p {
                    let path = entry.path();
                    match path.extension().map(|os| os.to_str()).unwrap_or_default() {
                        Some(KEY_FILE_EXTENSION) => Some(path),
                        _ => None,
                    }
                } else {
                    None
                }
            })
            .collect())
    }

    fn generate_file_path(&self, name: &str) -> PathBuf {
        self.0.join(format!("{name}.{KEY_FILE_EXTENSION}"))
    }
}

impl KeyManager for KeyDir {
    fn get(&self, name: &str) -> Result<Option<KeyPair>> {
        let path = self.generate_file_path(name);
        match read_key(path) {
            Ok(k) => Ok(Some(k)),
            Err(e) if matches!(e.kind(), std::io::ErrorKind::NotFound) => Ok(None),
            Err(e) => Err(anyhow::anyhow!("Unable to load key from disk: {}", e)),
        }
    }

    fn list_names(&self) -> Result<Vec<String>> {
        Ok(self
            .list_paths()?
            .into_iter()
            .filter_map(|p| {
                p.file_stem()
                    .unwrap_or_default()
                    .to_os_string()
                    .into_string()
                    .ok()
            })
            .collect())
    }

    fn list(&self) -> Result<Vec<KeyPair>> {
        self.list_paths()?
            .into_iter()
            .map(|p| {
                read_key(p).map_err(|e| anyhow::anyhow!("Unable to load key from disk: {}", e))
            })
            .collect()
    }

    fn delete(&self, name: &str) -> Result<()> {
        match std::fs::remove_file(self.generate_file_path(name)) {
            Ok(_) => Ok(()),
            Err(e) if matches!(e.kind(), std::io::ErrorKind::NotFound) => Ok(()),
            Err(e) => Err(anyhow::anyhow!("Unable to delete key from disk: {}", e)),
        }
    }

    fn save(&self, name: &str, key: &KeyPair) -> Result<()> {
        let path = self.generate_file_path(name);
        std::fs::write(&path, key.seed()?.as_bytes())
            .map_err(|e| anyhow::anyhow!("Unable to write key to disk: {}", e))?;
        set_permissions_keys(path)
    }
}

/// Helper function for reading a key from disk
pub fn read_key(p: impl AsRef<Path>) -> std::io::Result<KeyPair> {
    let raw = std::fs::read_to_string(p)?;

    KeyPair::from_seed(&raw).map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))
}

#[cfg(all(unix))]
/// Set file and folder permissions for keys.
fn set_permissions_keys(path: impl AsRef<Path>) -> Result<()> {
    use std::os::unix::fs::PermissionsExt;

    let metadata = path.as_ref().metadata()?;
    match metadata.file_type().is_dir() {
        true => std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o700))?,
        false => std::fs::set_permissions(path, std::fs::Permissions::from_mode(0o600))?,
    };
    Ok(())
}

#[cfg(target_os = "windows")]
fn set_permissions_keys(_path: impl AsRef<Path>) -> Result<()> {
    Ok(())
}

#[cfg(test)]
mod test {
    use nkeys::KeyPairType;

    use super::*;

    const TEST_KEY: &str = "SMAAGJ4DY4FNV4VJWA6QU7UQIL7DKJR4Z3UH7NBMNTH22V6VEIJGJUBQN4";

    #[test]
    fn round_trip_happy_path() {
        let tempdir = tempfile::tempdir().expect("Unable to create temp dir");
        let key_dir = KeyDir::new(&tempdir).expect("Should be able to create key dir");

        let key1 = KeyPair::new(KeyPairType::Account);
        let key2 = KeyPair::new(KeyPairType::Module);

        key_dir
            .save("foobar_account", &key1)
            .expect("Should be able to save key");
        key_dir
            .save("foobar_module", &key2)
            .expect("Should be able to save key");

        assert_eq!(
            tempdir.path().read_dir().unwrap().count(),
            2,
            "Directory should have 2 entries"
        );

        let names = key_dir.list_names().expect("Should be able to list names");
        assert_eq!(names.len(), 2, "Should have listed 2 names");
        for name in names.into_iter() {
            assert!(
                name == "foobar_account" || name == "foobar_module",
                "Should only have the newly created keys in the list"
            );
        }

        let key = key_dir
            .get("foobar_module")
            .expect("Shouldn't error while reading key")
            .expect("Key should exist");
        assert_eq!(
            key.public_key(),
            key2.public_key(),
            "Should have fetched the right key from disk"
        );

        assert_eq!(
            key_dir
                .list()
                .expect("Should be able to load all keys")
                .len(),
            2,
            "Should have loaded 2 keys from disk"
        );

        key_dir
            .delete("foobar_account")
            .expect("Should be able to delete key");
        assert_eq!(
            tempdir.path().read_dir().unwrap().count(),
            1,
            "Directory should have 1 entry"
        );
    }

    #[test]
    fn can_read_existing() {
        let tempdir = tempfile::tempdir().expect("Unable to create temp dir");
        std::fs::write(tempdir.path().join("foobar_module.nk"), TEST_KEY)
            .expect("Unable to write test file");
        // Write a file that should be skipped
        std::fs::write(tempdir.path().join("blah"), TEST_KEY).expect("Unable to write test file");

        let key_dir = KeyDir::new(&tempdir).expect("Should be able to create key dir");

        assert_eq!(
            key_dir
                .list_names()
                .expect("Should be able to list existing keys")
                .len(),
            1,
            "Should only have 1 key on disk"
        );

        let key = key_dir
            .get("foobar_module")
            .expect("Should be able to load key from disk")
            .expect("Key should exist");
        assert_eq!(
            key.seed().unwrap(),
            TEST_KEY,
            "Should load the correct key from disk"
        );
    }

    #[test]
    fn delete_of_nonexistent_key_should_succeed() {
        let tempdir = tempfile::tempdir().expect("Unable to create temp dir");
        let key_dir = KeyDir::new(&tempdir).expect("Should be able to create key dir");

        key_dir
            .delete("foobar")
            .expect("Non-existent key shouldn't error");
    }
}