Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(keyring): fail closed on key metadata errors

Steven Enamakel59348163871f
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
index 52cf952f8b..a159e88d47 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend.rs
@@ -264,20 +264,21 @@ fn read_master_key_file(path: &Path) -> Result<String, String> {
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
- if let Ok(metadata) = std::fs::metadata(path) {
- let mode = metadata.permissions().mode() & 0o777;
- if key_file_mode_is_private(mode) {
- return Err(format!(
- "master key file ({MASTER_KEY_FILE_ENV}) has insecure permissions \
- {mode:04o}; restrict it to owner-readable 0400 or 0600"
- ));
- }
- if key_file_mode_is_other_writable(mode) {
- return Err(format!(
- "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; \
- restrict it to a read-only secret mount"
- ));
- }
+ let metadata = std::fs::metadata(path).map_err(|e| {
+ format!("cannot inspect master key file permissions ({MASTER_KEY_FILE_ENV}): {e}")
+ })?;
+ let mode = metadata.permissions().mode() & 0o777;
+ if key_file_mode_is_private(mode) {
+ return Err(format!(
+ "master key file ({MASTER_KEY_FILE_ENV}) has insecure permissions \
+ {mode:04o}; restrict it to owner-readable 0400 or 0600"
+ ));
+ }
+ if key_file_mode_is_other_writable(mode) {
+ return Err(format!(
+ "master key file ({MASTER_KEY_FILE_ENV}) is writable by other users; \
+ restrict it to a read-only secret mount"
+ ));
}
}
std::fs::read_to_string(path).map_err(|e| format!("cannot read master key file: {e}"))
diff --git a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
index 11ea8e9780..3fa0597ec2 100644
--- a/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
+++ b/crates/openhuman-core/src/security/keyring/encrypted_file_backend_tests.rs
@@ -255,7 +255,10 @@ fn read_master_key_file_returns_contents_and_reports_a_missing_file() {
assert_eq!(contents.trim(), hex_key(0x33));
let err = read_master_key_file(&tmp.path().join("missing.key")).expect_err("missing file");
- assert!(err.contains("cannot read master key file"), "{err}");
+ assert!(
+ err.contains("cannot inspect master key file permissions"),
+ "{err}"
+ );
}
#[cfg(unix)]
diff --git a/gitbooks/features/cloud-deploy.md b/gitbooks/features/cloud-deploy.md
index e6c07516ac..fdb75496b5 100644
--- a/gitbooks/features/cloud-deploy.md
+++ b/gitbooks/features/cloud-deploy.md
@@ -492,10 +492,47 @@ Mount the key file owner-only. The core rejects group- or world-readable files
because any process able to read the file can decrypt the entire keyring.
Restrict the mode where the mount is declared:
-- Kubernetes: set `defaultMode: 0400` on the secret volume (or `mode: 0400` on
- the item), and set the secret item's `runAsUser`/`uid` to the core's user.
- Do not use `fsGroup` to grant access: kubelet can turn the file into `0440`,
- which the core rejects.
+- Kubernetes: Secret volumes are root-owned, and `runAsUser` does not change
+ the owner of a projected Secret. For a non-root core, use an init container
+ running as root to copy the Secret into an `emptyDir`, `chown` the copy to
+ the core UID, and `chmod 0400` it before the core starts. Mount the original
+ Secret read-only in the init container and the `emptyDir` read-only in the
+ core container. For example:
+
+ ```yaml
+ volumes:
+ - name: master-key-secret
+ secret:
+ secretName: openhuman-master-key
+ defaultMode: 0400
+ - name: master-key
+ emptyDir: {}
+ initContainers:
+ - name: prepare-master-key
+ image: busybox:1.36
+ command: ["sh", "-c", "cp /source/master.key /target/master.key && chown 10001:10001 /target/master.key && chmod 0400 /target/master.key"]
+ volumeMounts:
+ - name: master-key-secret
+ mountPath: /source
+ readOnly: true
+ - name: master-key
+ mountPath: /target
+ containers:
+ - name: openhuman
+ securityContext:
+ runAsUser: 10001
+ env:
+ - name: OPENHUMAN_KEYRING_MASTER_KEY_FILE
+ value: /run/openhuman-master-key/master.key
+ volumeMounts:
+ - name: master-key
+ mountPath: /run/openhuman-master-key
+ readOnly: true
+ ```
+
+ Set the `emptyDir` medium and the core UID to match the deployment's
+ security policy. Do not use `fsGroup` to grant access: kubelet can turn the
+ projected file into `0440`, which the core rejects.
- Docker Swarm: set `mode: 0400` (with `uid`/`gid` for a non-root user) on the
service's secret.
- A plain file: `chmod 600` it, owned by the user the core runs as.