olai-uc-server 0.0.1

Unity Catalog REST and gRPC server with pluggable storage backends.
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
480
481
482
483
484
485
use itertools::Itertools;

use unitycatalog_common::models::ObjectLabel;
use unitycatalog_common::models::agent_skills::v0alpha1::*;
use unitycatalog_common::models::{ResourceIdent, ResourceName, ResourceRef};

use super::staging_tables::{child_location, resolve_managed_parent_location};
use super::{RequestContext, SecuredAction};
pub use crate::codegen::agent_skills::AgentSkillHandler;
use crate::policy::{Permission, Policy, process_resources};
use crate::services::location::StorageLocationUrl;
use crate::services::object_store::validate_external_storage_location;
use crate::services::{ProvidesLocalStoragePolicy, ProvidesManagedStorageRoot};
use crate::store::ResourceStore;
use crate::{Error, Result};

#[async_trait::async_trait]
impl<
    T: ResourceStore
        + Policy<RequestContext>
        + ProvidesLocalStoragePolicy
        + ProvidesManagedStorageRoot,
> AgentSkillHandler<RequestContext> for T
{
    #[tracing::instrument(skip(self, context), fields(resource_name))]
    async fn create_agent_skill(
        &self,
        request: CreateAgentSkillRequest,
        context: RequestContext,
    ) -> Result<AgentSkill> {
        tracing::Span::current().record("resource_name", &request.name);
        self.check_required(&request, &context).await?;

        let skill_type = request.agent_skill_type.as_known().unwrap_or_default();

        // Pre-allocate the id so the created record carries it (the store honors a
        // pre-set id, else mints a v7) and a managed skill can embed it in its
        // storage path. This mirrors managed `Volume`, since an agent skill is
        // just a storage-backed directory.
        let agent_skill_id = uuid::Uuid::now_v7().hyphenated().to_string();
        let storage_location = match skill_type {
            AgentSkillType::EXTERNAL => {
                // External skills MUST have an explicit storage location that
                // lives within a registered external location and does not
                // overlap any existing table or volume.
                let location = request
                    .storage_location
                    .filter(|s| !s.is_empty())
                    .ok_or_else(|| {
                        Error::invalid_argument(
                            "storage_location is required for EXTERNAL agent skills",
                        )
                    })?;
                let parsed = StorageLocationUrl::parse(&location)?;
                validate_external_storage_location(self, &parsed).await?;
                location
            }
            AgentSkillType::MANAGED => {
                // Managed skills derive their storage location from the managed
                // parent location resolved for the schema/catalog, appending an
                // `agent_skills/{id}` segment, mirroring managed volumes. The id
                // is allocated here and persisted so the path equals the skill's
                // id and survives renames. A caller cannot supply a location.
                let parent = resolve_managed_parent_location(
                    self,
                    &request.catalog_name,
                    &request.schema_name,
                )
                .await?;
                child_location(&parent, "agent_skills", &agent_skill_id)
            }
            AgentSkillType::AGENT_SKILL_TYPE_UNSPECIFIED => {
                return Err(Error::invalid_argument(
                    "agent_skill_type must be specified (EXTERNAL or MANAGED)",
                ));
            }
        };

        let full_name = format!(
            "{}.{}.{}",
            request.catalog_name, request.schema_name, request.name
        );
        let resource = AgentSkill {
            full_name,
            name: request.name,
            catalog_name: request.catalog_name,
            schema_name: request.schema_name,
            agent_skill_type: request.agent_skill_type,
            storage_location,
            agent_skill_id,
            description: request.description,
            license: request.license,
            allowed_tools: request.allowed_tools,
            metadata: request.metadata,
            comment: request.comment,
            ..Default::default()
        };
        Ok(self.create(resource.into()).await?.0.try_into()?)
    }

    #[tracing::instrument(skip(self, context))]
    async fn list_agent_skills(
        &self,
        request: ListAgentSkillsRequest,
        context: RequestContext,
    ) -> Result<ListAgentSkillsResponse> {
        self.check_required(&request, &context).await?;
        let (mut resources, next_page_token) = self
            .list(
                &ObjectLabel::AgentSkill,
                Some(&ResourceName::new([
                    &request.catalog_name,
                    &request.schema_name,
                ])),
                request.max_results.map(|v| v as usize),
                request.page_token,
            )
            .await?;
        process_resources(self, &context, &Permission::Read, &mut resources).await?;
        Ok(ListAgentSkillsResponse {
            agent_skills: resources.into_iter().map(|r| r.try_into()).try_collect()?,
            next_page_token,
            ..Default::default()
        })
    }

    #[tracing::instrument(skip(self, context), fields(resource_name))]
    async fn get_agent_skill(
        &self,
        request: GetAgentSkillRequest,
        context: RequestContext,
    ) -> Result<AgentSkill> {
        tracing::Span::current().record("resource_name", &request.name);
        self.check_required(&request, &context).await?;
        Ok(self.get(&request.resource()).await?.0.try_into()?)
    }

    #[tracing::instrument(skip(self, context), fields(resource_name))]
    async fn update_agent_skill(
        &self,
        request: UpdateAgentSkillRequest,
        context: RequestContext,
    ) -> Result<AgentSkill> {
        tracing::Span::current().record("resource_name", &request.name);
        self.check_required(&request, &context).await?;
        let ident = request.resource();
        let name = ResourceName::from_naive_str_split(request.name.as_str());
        let [catalog_name, schema_name, skill_name] = name.as_ref() else {
            return Err(Error::invalid_argument(
                "Invalid agent skill name - expected <catalog_name>.<schema_name>.<skill_name>",
            ));
        };
        // Load the current record so omitted update fields (and the immutable
        // storage location / type / id) are preserved across the rename-style update.
        let current: AgentSkill = self.get(&ident).await?.0.try_into()?;
        let new_name = request.new_name.as_deref().unwrap_or(skill_name);
        let resource = AgentSkill {
            name: new_name.to_owned(),
            catalog_name: catalog_name.to_owned(),
            schema_name: schema_name.to_owned(),
            full_name: format!("{}.{}.{}", catalog_name, schema_name, new_name),
            agent_skill_type: current.agent_skill_type,
            storage_location: current.storage_location,
            description: request.description.or(current.description),
            license: current.license,
            allowed_tools: if request.allowed_tools.is_empty() {
                current.allowed_tools
            } else {
                request.allowed_tools
            },
            metadata: current.metadata,
            comment: request.comment.or(current.comment),
            owner: request.owner.or(current.owner),
            ..Default::default()
        };
        Ok(self.update(&ident, resource.into()).await?.0.try_into()?)
    }

    #[tracing::instrument(skip(self, context), fields(resource_name))]
    async fn delete_agent_skill(
        &self,
        request: DeleteAgentSkillRequest,
        context: RequestContext,
    ) -> Result<()> {
        tracing::Span::current().record("resource_name", &request.name);
        self.check_required(&request, &context).await?;
        Ok(self.delete(&request.resource()).await?)
    }
}

impl SecuredAction for CreateAgentSkillRequest {
    fn resource(&self) -> ResourceIdent {
        ResourceIdent::agent_skill(ResourceName::new([
            self.catalog_name.as_str(),
            self.schema_name.as_str(),
            self.name.as_str(),
        ]))
    }

    fn permission(&self) -> &'static Permission {
        &Permission::Create
    }
}

impl SecuredAction for ListAgentSkillsRequest {
    fn resource(&self) -> ResourceIdent {
        ResourceIdent::agent_skill(ResourceRef::Undefined)
    }

    fn permission(&self) -> &'static Permission {
        &Permission::Read
    }
}

impl SecuredAction for GetAgentSkillRequest {
    fn resource(&self) -> ResourceIdent {
        ResourceIdent::agent_skill(ResourceName::from_naive_str_split(self.name.as_str()))
    }

    fn permission(&self) -> &'static Permission {
        &Permission::Read
    }
}

impl SecuredAction for UpdateAgentSkillRequest {
    fn resource(&self) -> ResourceIdent {
        ResourceIdent::agent_skill(ResourceName::from_naive_str_split(self.name.as_str()))
    }

    fn permission(&self) -> &'static Permission {
        &Permission::Manage
    }
}

impl SecuredAction for DeleteAgentSkillRequest {
    fn resource(&self) -> ResourceIdent {
        ResourceIdent::agent_skill(ResourceName::from_naive_str_split(self.name.as_str()))
    }

    fn permission(&self) -> &'static Permission {
        &Permission::Manage
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use unitycatalog_common::models::catalogs::v1::CreateCatalogRequest;
    use unitycatalog_common::models::credentials::v1::{
        AwsIamRoleConfig, CreateCredentialRequest, Purpose,
    };
    use unitycatalog_common::models::external_locations::v1::CreateExternalLocationRequest;
    use unitycatalog_common::models::schemas::v1::CreateSchemaRequest;
    use unitycatalog_common::services::encryption::{EnvelopeEncryptor, LocalKeyProvider};

    use super::*;
    use crate::api::{CatalogHandler, CredentialHandler, ExternalLocationHandler, SchemaHandler};
    use crate::memory::InMemoryResourceStore;
    use crate::policy::ConstantPolicy;
    use crate::services::ServerHandler;

    fn handler() -> ServerHandler<RequestContext> {
        let encryptor =
            EnvelopeEncryptor::local(LocalKeyProvider::single("test", vec![0x42; 32]).unwrap());
        let store = Arc::new(InMemoryResourceStore::new(encryptor));
        let policy: Arc<dyn Policy<RequestContext>> = Arc::new(ConstantPolicy::default());
        ServerHandler::try_new_tokio(policy, store).unwrap()
    }

    fn ctx() -> RequestContext {
        RequestContext {
            recipient: crate::policy::Principal::anonymous(),
        }
    }

    /// Register a credential + external location at `url`.
    async fn make_covering_location(h: &ServerHandler<RequestContext>, tag: &str, url: &str) {
        h.create_credential(
            CreateCredentialRequest {
                name: format!("{tag}-cred"),
                purpose: Purpose::Storage.into(),
                aws_iam_role: Some(AwsIamRoleConfig {
                    role_arn: "arn:aws:iam::123456789012:role/test".to_string(),
                    ..Default::default()
                })
                .into(),
                ..Default::default()
            },
            ctx(),
        )
        .await
        .unwrap();
        h.create_external_location(
            CreateExternalLocationRequest {
                name: format!("{tag}-el"),
                url: url.to_string(),
                credential_name: format!("{tag}-cred"),
                ..Default::default()
            },
            ctx(),
        )
        .await
        .unwrap();
    }

    /// Create catalog `cat` (rooted at `storage_root`) and schema `sch` so a
    /// managed skill can resolve a managed storage root.
    async fn setup_managed_namespace(h: &ServerHandler<RequestContext>, storage_root: &str) {
        make_covering_location(h, "cat", storage_root).await;
        h.create_catalog(
            CreateCatalogRequest {
                name: "cat".to_string(),
                storage_root: Some(storage_root.to_string()),
                ..Default::default()
            },
            ctx(),
        )
        .await
        .unwrap();
        h.create_schema(
            CreateSchemaRequest {
                name: "sch".to_string(),
                catalog_name: "cat".to_string(),
                ..Default::default()
            },
            ctx(),
        )
        .await
        .unwrap();
    }

    fn create_external_skill(name: &str, location: Option<&str>) -> CreateAgentSkillRequest {
        CreateAgentSkillRequest {
            catalog_name: "cat".to_string(),
            schema_name: "sch".to_string(),
            name: name.to_string(),
            agent_skill_type: AgentSkillType::External.into(),
            storage_location: location.map(str::to_string),
            description: Some("formats things".to_string()),
            license: Some("MIT".to_string()),
            allowed_tools: vec!["bash".to_string()],
            metadata: Default::default(),
            comment: None,
            ..Default::default()
        }
    }

    fn create_managed_skill(name: &str) -> CreateAgentSkillRequest {
        CreateAgentSkillRequest {
            catalog_name: "cat".to_string(),
            schema_name: "sch".to_string(),
            name: name.to_string(),
            agent_skill_type: AgentSkillType::Managed.into(),
            storage_location: None,
            description: None,
            license: None,
            allowed_tools: vec![],
            metadata: Default::default(),
            comment: None,
            ..Default::default()
        }
    }

    #[tokio::test]
    async fn managed_skill_location_uses_skill_id() {
        let h = handler();
        setup_managed_namespace(&h, "s3://bucket/cat").await;

        let s = h
            .create_agent_skill(create_managed_skill("fmt"), ctx())
            .await
            .unwrap();

        assert!(uuid::Uuid::parse_str(&s.agent_skill_id).is_ok());
        assert!(
            s.storage_location
                .ends_with(&format!("/agent_skills/{}", s.agent_skill_id)),
            "got {}",
            s.storage_location
        );
        assert!(
            s.storage_location
                .starts_with("s3://bucket/cat/__unitystorage/catalogs/"),
            "got {}",
            s.storage_location
        );

        let got = h
            .get_agent_skill(
                GetAgentSkillRequest {
                    name: "cat.sch.fmt".to_string(),
                    ..Default::default()
                },
                ctx(),
            )
            .await
            .unwrap();
        assert_eq!(got.agent_skill_id, s.agent_skill_id);
        assert_eq!(got.storage_location, s.storage_location);
    }

    #[tokio::test]
    async fn managed_skill_recreate_yields_new_path() {
        let h = handler();
        setup_managed_namespace(&h, "s3://bucket/cat").await;
        let first = h
            .create_agent_skill(create_managed_skill("fmt"), ctx())
            .await
            .unwrap();
        h.delete_agent_skill(
            DeleteAgentSkillRequest {
                name: "cat.sch.fmt".to_string(),
                ..Default::default()
            },
            ctx(),
        )
        .await
        .unwrap();
        let second = h
            .create_agent_skill(create_managed_skill("fmt"), ctx())
            .await
            .unwrap();
        assert_ne!(first.agent_skill_id, second.agent_skill_id);
        assert_ne!(first.storage_location, second.storage_location);
    }

    #[tokio::test]
    async fn external_skill_within_location_succeeds_and_round_trips_metadata() {
        let h = handler();
        // Managed namespace (catalog/schema) plus a separate external location
        // covering the skill's own path.
        setup_managed_namespace(&h, "s3://bucket/cat").await;
        make_covering_location(&h, "ext", "s3://bucket/ext").await;

        let created = h
            .create_agent_skill(
                create_external_skill("fmt", Some("s3://bucket/ext/skill")),
                ctx(),
            )
            .await
            .unwrap();
        assert_eq!(created.storage_location, "s3://bucket/ext/skill");
        assert_eq!(created.license.as_deref(), Some("MIT"));
        assert_eq!(created.allowed_tools, vec!["bash".to_string()]);
        assert_eq!(created.description.as_deref(), Some("formats things"));
    }

    #[tokio::test]
    async fn external_skill_outside_location_is_rejected() {
        let h = handler();
        setup_managed_namespace(&h, "s3://bucket/cat").await;
        let res = h
            .create_agent_skill(
                create_external_skill("fmt", Some("s3://bucket/other/skill")),
                ctx(),
            )
            .await;
        assert!(matches!(res, Err(Error::InvalidArgument(_))), "{res:?}");
    }

    #[tokio::test]
    async fn external_skill_requires_storage_location() {
        let h = handler();
        setup_managed_namespace(&h, "s3://bucket/cat").await;
        let res = h
            .create_agent_skill(create_external_skill("fmt", None), ctx())
            .await;
        assert!(matches!(res, Err(Error::InvalidArgument(_))), "{res:?}");
    }

    #[tokio::test]
    async fn managed_skill_duplicate_name_is_rejected() {
        let h = handler();
        setup_managed_namespace(&h, "s3://bucket/cat").await;
        h.create_agent_skill(create_managed_skill("fmt"), ctx())
            .await
            .unwrap();
        let err = h
            .create_agent_skill(create_managed_skill("fmt"), ctx())
            .await
            .expect_err("duplicate name must be rejected");
        assert_eq!(err.error_code(), "RESOURCE_ALREADY_EXISTS", "{err:?}");
    }
}