MCPcopy Create free account
hub / github.com/atomicdotdev/atomic / run

Method run

atomic-cli/src/commands/identity/delegate.rs:128–248  ·  view source on GitHub ↗
(&self)

Source from the content-addressed store, hash-verified

126
127impl Command for Delegate {
128 fn run(&self) -> CliResult<()> {
129 let store = IdentityStore::open_default().map_err(|e| {
130 CliError::Internal(anyhow::anyhow!("Failed to open identity store: {e}"))
131 })?;
132
133 let delegator = super::load_identity_or_default(&store, self.identity.as_deref())?;
134 if delegator.identity_type.is_delegated() || delegator.identity_type.is_agent() {
135 return Err(CliError::InvalidArgument {
136 message: format!("'{}' is an agent and cannot delegate", delegator.name),
137 });
138 }
139
140 // Resolve the delegate: either an identity in the local store, or the
141 // subject of a countersigned request.
142 let delegate = match (&self.request, &self.agent) {
143 (Some(path), _) => self.delegate_from_request(path)?,
144 (None, Some(name)) => store
145 .load_by_name(name)
146 .map_err(|_| CliError::IdentityNotFound(name.clone()))?,
147 (None, None) => {
148 return Err(CliError::InvalidArgument {
149 message: "name an agent identity, or pass --request <file> to countersign \
150 a request"
151 .to_string(),
152 })
153 }
154 };
155
156 let scope = self.build_scope()?;
157 let expires = self
158 .expires
159 .as_deref()
160 .map(parse_duration)
161 .transpose()?
162 .unwrap_or_else(|| chrono::Duration::days(DEFAULT_EXPIRY_DAYS));
163
164 let mut terms = Delegation::new(&delegator, &delegate, scope).expires_in(expires);
165 if let Some(agent_urn) = delegate
166 .metadata
167 .description
168 .as_deref()
169 .and_then(software_agent_from_description)
170 {
171 terms = terms.with_software_agent(agent_urn);
172 }
173
174 let keypair = store.load_keypair(&delegator.id, None).map_err(|e| {
175 CliError::Internal(anyhow::anyhow!(
176 "Failed to load the signing key for '{}': {e}",
177 delegator.name
178 ))
179 })?;
180 let certificate = cert::mint(&delegator, &keypair, &terms);
181 // Admitted before it is stored, exported or printed: a grant every
182 // reader on this machine would refuse is not worth signing.
183 let document =
184 cert::encode_for_storage(&certificate).map_err(|e| CliError::InvalidArgument {
185 message: format!("this grant would be refused when it is read back: {e}"),

Callers

nothing calls this directly

Calls 15

load_identity_or_defaultFunction · 0.85
mintFunction · 0.85
encode_for_storageFunction · 0.85
print_successFunction · 0.85
print_hintFunction · 0.85
is_delegatedMethod · 0.80
is_agentMethod · 0.80
delegate_from_requestMethod · 0.80
load_by_nameMethod · 0.80
expires_inMethod · 0.80
with_software_agentMethod · 0.80
load_keypairMethod · 0.80

Tested by

no test coverage detected