Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions crates/enclava-api/src/deploy/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,27 @@ fn tenant_image_pull_secret_scope_requires_every_container_to_match() {
));
}

#[test]
fn tenant_image_pull_secret_scope_matches_tag_and_digest_images() {
let config = pull_secret_config(vec![
parse_image_pull_repository_scope("ghcr.io/enclava-ai/private-template").unwrap(),
parse_image_pull_repository_scope("ghcr.io/enclava-ai/templates/*").unwrap(),
]);
let containers = vec![
test_container(
"ghcr.io/enclava-ai/private-template:v1@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
),
test_container(
"ghcr.io/enclava-ai/templates/ssh:2.0@sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb",
),
];

assert!(tenant_image_pull_secret_applies_to_containers(
&containers,
&config
));
}

#[test]
fn signed_deploy_with_local_artifacts_still_reconciles_global_kbs_policy() {
let mut attestation = test_attestation_config();
Expand Down
12 changes: 12 additions & 0 deletions crates/enclava-api/src/source_provider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,18 @@ mod tests {
.unwrap();
}

#[test]
fn gitlab_source_context_accepts_tag_and_digest_image() {
validate_source_context(
SourceProvider::GitLab,
"acme/platform/confidential-app",
"registry.gitlab.com/acme/platform/confidential-app:v1@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"https://gitlab.com/acme/platform/confidential-app/-/blob/main/.gitlab-ci.yml@refs/heads/main",
"https://gitlab.com",
)
.unwrap();
}

#[test]
fn github_provider_rejects_gitlab_registry() {
let err = validate_source_context(
Expand Down
17 changes: 16 additions & 1 deletion crates/enclava-api/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3718,6 +3718,21 @@ async fn generic_github_deployment_validates_and_reaches_strict_deploy_gate() {

#[tokio::test]
async fn generic_gitlab_deployment_validates_and_reaches_strict_deploy_gate() {
assert_generic_gitlab_deployment_reaches_strict_deploy_gate(
"registry.gitlab.com/acme/platform/confidential-app/app@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
.await;
}

#[tokio::test]
async fn generic_gitlab_tag_and_digest_deployment_reaches_strict_deploy_gate() {
assert_generic_gitlab_deployment_reaches_strict_deploy_gate(
"registry.gitlab.com/acme/platform/confidential-app:v1@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
)
.await;
}

async fn assert_generic_gitlab_deployment_reaches_strict_deploy_gate(image: &str) {
let (mut state, pool) = setup_test_state().await;
state.require_customer_signed_policy_artifact = true;
let app = test_router(state);
Expand All @@ -3736,7 +3751,7 @@ async fn generic_gitlab_deployment_validates_and_reaches_strict_deploy_gate() {
&app_name,
"gitlab",
"acme/platform/confidential-app",
"registry.gitlab.com/acme/platform/confidential-app/app@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
image,
"https://gitlab.com/acme/platform/confidential-app/-/blob/main/.gitlab-ci.yml@refs/heads/main",
"https://gitlab.com",
))
Expand Down
70 changes: 44 additions & 26 deletions crates/enclava-common/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,39 +19,25 @@ pub enum ImageRefError {

impl ImageRef {
pub fn parse(raw: &str) -> Result<Self, ImageRefError> {
// Split on @ first (digest takes priority)
if let Some((name, digest)) = raw.split_once('@') {
crate::validate::validate_image_digest(digest)
.map_err(|e| ImageRefError::Invalid(e.to_string()))?;
let (registry, repository) = split_name(name)?;
return Ok(Self {
registry,
repository,
tag: None,
digest: digest.to_string(),
});
}

// Split on : for tag -- find the last : after the first /
let (name, tag) = if let Some(slash_pos) = raw.find('/') {
if let Some(colon_pos) = raw[slash_pos..].rfind(':') {
let abs_pos = slash_pos + colon_pos;
(&raw[..abs_pos], Some(raw[abs_pos + 1..].to_string()))
} else {
(raw, None)
let (name, digest) = match raw.split_once('@') {
Some((name, digest)) => {
crate::validate::validate_image_digest(digest)
.map_err(|e| ImageRefError::Invalid(e.to_string()))?;
(name, digest.to_string())
}
} else if let Some((name, tag)) = raw.split_once(':') {
(name, Some(tag.to_string()))
} else {
(raw, None)
None => (raw, String::new()),
};

let (name, tag) = split_tag(name);
if let Some(tag) = tag {
validate_tag(tag)?;
}
let (registry, repository) = split_name(name)?;
Ok(Self {
registry,
repository,
tag,
digest: String::new(),
tag: tag.map(str::to_string),
digest,
})
}

Expand Down Expand Up @@ -103,6 +89,38 @@ impl ImageRef {
}
}

/// Splits an optional `:tag` off a `registry/repository[:tag]` name. The first
/// colon after the first `/` starts a tag; one before it is a registry port.
fn split_tag(name: &str) -> (&str, Option<&str>) {
let tag_search_start = name.find('/').unwrap_or(0);
match name[tag_search_start..].find(':') {
Some(pos) => {
let colon = tag_search_start + pos;
(&name[..colon], Some(&name[colon + 1..]))
}
None => (name, None),
}
}

/// OCI distribution-spec tag grammar: `[A-Za-z0-9_][A-Za-z0-9._-]{0,127}`.
fn validate_tag(tag: &str) -> Result<(), ImageRefError> {
let valid = tag.len() <= 128
&& tag
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphanumeric() || c == '_')
&& tag
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '.' || c == '-' || c == '_');
if valid {
Ok(())
} else {
Err(ImageRefError::Invalid(format!(
"invalid image tag: {tag:?}"
)))
}
}

fn split_name(name: &str) -> Result<(String, String), ImageRefError> {
if name.len() > 255 {
return Err(ImageRefError::Invalid(
Expand Down
35 changes: 35 additions & 0 deletions crates/enclava-common/tests/image_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,38 @@ fn parse_rejects_short_sha256_digest() {
let err = ImageRef::parse("ghcr.io/user/app@sha256:abc").unwrap_err();
assert!(err.to_string().contains("64"));
}

#[test]
fn parse_tag_and_digest_ref_keeps_tag_out_of_repository() {
let digest = format!("sha256:{}", "a".repeat(64));
let img = ImageRef::parse(&format!("ghcr.io/acme/app:v1@{digest}")).unwrap();
assert_eq!(img.registry(), "ghcr.io");
assert_eq!(img.repository(), "acme/app");
assert_eq!(img.tag(), Some("v1"));
assert_eq!(img.digest(), digest);
assert_eq!(img.digest_ref(), format!("ghcr.io/acme/app@{digest}"));

let bare = ImageRef::parse(&format!("nginx:1.27@{digest}")).unwrap();
assert_eq!(bare.registry(), "docker.io");
assert_eq!(bare.repository(), "library/nginx");
assert_eq!(bare.tag(), Some("1.27"));

let with_port =
ImageRef::parse(&format!("registry.example.com:5000/acme/app:v1@{digest}")).unwrap();
assert_eq!(with_port.registry(), "registry.example.com:5000");
assert_eq!(with_port.repository(), "acme/app");
assert_eq!(with_port.tag(), Some("v1"));
}

#[test]
fn parse_tag_and_digest_ref_rejects_invalid_tag() {
let digest = format!("sha256:{}", "a".repeat(64));
assert!(ImageRef::parse(&format!("ghcr.io/acme/app:v$1@{digest}")).is_err());
}

#[test]
fn parse_rejects_extra_colon_in_repository() {
let digest = format!("sha256:{}", "a".repeat(64));
assert!(ImageRef::parse(&format!("ghcr.io/acme/app:tag:extra@{digest}")).is_err());
assert!(ImageRef::parse("ghcr.io/acme/app:tag:extra").is_err());
}
Loading