This repository was archived by the owner on Jul 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathkeyring.rs
More file actions
201 lines (190 loc) · 7.17 KB
/
keyring.rs
File metadata and controls
201 lines (190 loc) · 7.17 KB
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
//! Utilities for interacting with keyring and performing signing operations.
use crate::RegistryUrl;
use anyhow::{bail, Context, Result};
use indexmap::IndexSet;
use secrecy::Secret;
use warg_crypto::signing::PrivateKey;
/// Gets the auth token entry for the given registry and key name.
pub fn get_auth_token_entry(registry_url: &RegistryUrl) -> Result<keyring::Entry> {
let label = format!("warg-auth-token:{}", registry_url.safe_label());
keyring::Entry::new(&label, ®istry_url.safe_label()).context("failed to get keyring entry")
}
/// Gets the auth token
pub fn get_auth_token(registry_url: &RegistryUrl) -> Result<Option<Secret<String>>> {
let entry = get_auth_token_entry(registry_url)?;
match entry.get_password() {
Ok(secret) => Ok(Some(Secret::from(secret))),
Err(keyring::Error::NoEntry) => Ok(None),
Err(keyring::Error::Ambiguous(_)) => {
bail!("more than one auth token for registry `{registry_url}`");
}
Err(e) => {
bail!("failed to get auth token for registry `{registry_url}`: {e}");
}
}
}
/// Deletes the auth token
pub fn delete_auth_token(registry_url: &RegistryUrl) -> Result<()> {
let entry = get_auth_token_entry(registry_url)?;
match entry.delete_password() {
Ok(()) => Ok(()),
Err(keyring::Error::NoEntry) => {
bail!("no auth token found for registry `{registry_url}`");
}
Err(keyring::Error::Ambiguous(_)) => {
bail!("more than one auth token found for registry `{registry_url}`");
}
Err(e) => {
bail!("failed to delete auth torkn for registry `{registry_url}`: {e}");
}
}
}
/// Sets the auth token
pub fn set_auth_token(registry_url: &RegistryUrl, token: &str) -> Result<()> {
let entry = get_auth_token_entry(registry_url)?;
match entry.set_password(token) {
Ok(()) => Ok(()),
Err(keyring::Error::NoEntry) => {
bail!("no auth token found for registry `{registry_url}`");
}
Err(keyring::Error::Ambiguous(_)) => {
bail!("more than one auth token for registry `{registry_url}`");
}
Err(e) => {
bail!("failed to set auth token for registry `{registry_url}`: {e}");
}
}
}
/// Gets the signing key entry for the given registry and key name.
pub fn get_signing_key_entry(
registry_url: Option<&str>,
keys: &IndexSet<String>,
home_url: Option<&str>,
) -> Result<keyring::Entry> {
if let Some(registry_url) = registry_url {
if keys.contains(registry_url) {
keyring::Entry::new("warg-signing-key", registry_url)
.context("failed to get keyring entry")
} else {
keyring::Entry::new("warg-signing-key", "default")
.context("failed to get keyring entry")
}
} else {
if let Some(url) = home_url {
if keys.contains(url) {
return keyring::Entry::new(
"warg-signing-key",
&RegistryUrl::new(url)?.safe_label(),
)
.context("failed to get keyring entry");
}
}
if keys.contains("default") {
keyring::Entry::new("warg-signing-key", "default")
.context("failed to get keyring entry")
} else {
bail!(
"error: Please set a default signing key by typing `warg key set <alg:base64>` or `warg key new`"
)
}
}
}
/// Gets the signing key for the given registry registry_label and key name.
pub fn get_signing_key(
// 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 = get_signing_key_entry(registry_url, keys, home_url)?;
match entry.get_password() {
Ok(secret) => PrivateKey::decode(secret).context("failed to parse signing key"),
Err(keyring::Error::NoEntry) => {
if let Some(registry_url) = registry_url {
bail!("no signing key found for registry `{registry_url}`");
} else {
bail!("no signing key found");
}
}
Err(keyring::Error::Ambiguous(_)) => {
if let Some(registry_url) = registry_url {
bail!("more than one signing key found for registry `{registry_url}`");
} else {
bail!("more than one signing key found");
}
}
Err(e) => {
if let Some(registry_url) = registry_url {
bail!("failed to get signing key for registry `{registry_url}`: {e}");
} else {
bail!("failed to get signing key`");
}
}
}
}
/// Sets the signing key for the given registry host and key name.
pub fn set_signing_key(
registry_url: Option<&str>,
key: &PrivateKey,
keys: &mut IndexSet<String>,
home_url: Option<&str>,
) -> Result<()> {
let entry = get_signing_key_entry(registry_url, keys, home_url)?;
match entry.set_password(&key.encode()) {
Ok(()) => Ok(()),
Err(keyring::Error::NoEntry) => {
if let Some(registry_url) = registry_url {
bail!("no signing key found for registry `{registry_url}`");
} else {
bail!("no signing key found`");
}
}
Err(keyring::Error::Ambiguous(_)) => {
if let Some(registry_url) = registry_url {
bail!("more than one signing key found for registry `{registry_url}`");
} else {
bail!("more than one signing key found");
}
}
Err(e) => {
if let Some(registry_url) = registry_url {
bail!("failed to get signing key for registry `{registry_url}`: {e}");
} else {
bail!("failed to get signing: {e}");
}
}
}
}
/// Deletes the signing key for the given registry host and key name.
pub fn delete_signing_key(
registry_url: Option<&str>,
keys: &IndexSet<String>,
home_url: Option<&str>,
) -> Result<()> {
let entry = get_signing_key_entry(registry_url, keys, home_url)?;
match entry.delete_password() {
Ok(()) => Ok(()),
Err(keyring::Error::NoEntry) => {
if let Some(registry_url) = registry_url {
bail!("no signing key found for registry `{registry_url}`");
} else {
bail!("no signing key found");
}
}
Err(keyring::Error::Ambiguous(_)) => {
if let Some(registry_url) = registry_url {
bail!("more than one signing key found for registry `{registry_url}`");
} else {
bail!("more than one signing key found`");
}
}
Err(e) => {
if let Some(registry_url) = registry_url {
bail!("failed to delete signing key for registry `{registry_url}`: {e}");
} else {
bail!("failed to delete signing key");
}
}
}
}