Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not encrypt if a key group is empty, or there are no key groups #1600

Merged
merged 1 commit into from
Sep 25, 2024
Merged
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
6 changes: 6 additions & 0 deletions functional-tests/.sops.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ creation_rules:
- FBC7B9E2A4F9289AC0C1D4843D16CEE4A27381B4
- pgp:
- B611A2F9F11D0FF82568805119F9B5DAEA91FF86
- path_regex: test_no_keygroups.yaml
- path_regex: test_zero_keygroups.yaml
key_groups: []
- path_regex: test_empty_keygroup.yaml
key_groups:
- {}
- pgp: FBC7B9E2A4F9289AC0C1D4843D16CEE4A27381B4
destination_rules:
- s3_bucket: "sops-publish-functional-tests"
Expand Down
60 changes: 60 additions & 0 deletions functional-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,66 @@ b: ba"#
);
}

#[test]
fn test_no_keygroups() {
// The .sops.yaml file ensures this file is encrypted by zero keygroups
let file_path = prepare_temp_file("test_no_keygroups.yaml", "a: secret".as_bytes());
let output = Command::new(SOPS_BINARY_PATH)
.arg("encrypt")
.arg("-i")
.arg(file_path.clone())
.output()
.expect("Error running sops");
assert!(
!output.status.success(),
"SOPS succeeded encrypting a file without a key group"
);
assert_eq!(
std::str::from_utf8(&output.stderr).unwrap(),
"Could not generate data key: [empty key group provided]\n"
);
}

#[test]
fn test_zero_keygroups() {
// The .sops.yaml file ensures this file is encrypted by zero keygroups
let file_path = prepare_temp_file("test_zero_keygroups.yaml", "a: secret".as_bytes());
let output = Command::new(SOPS_BINARY_PATH)
.arg("encrypt")
.arg("-i")
.arg(file_path.clone())
.output()
.expect("Error running sops");
assert!(
!output.status.success(),
"SOPS succeeded encrypting a file without a key group"
);
assert_eq!(
std::str::from_utf8(&output.stderr).unwrap(),
"Could not generate data key: [empty key group provided]\n"
);
}

#[test]
fn test_empty_keygroup() {
// The .sops.yaml file ensures this file is encrypted by zero keygroups
let file_path = prepare_temp_file("test_empty_keygroup.yaml", "a: secret".as_bytes());
let output = Command::new(SOPS_BINARY_PATH)
.arg("encrypt")
.arg("-i")
.arg(file_path.clone())
.output()
.expect("Error running sops");
assert!(
!output.status.success(),
"SOPS succeeded encrypting a file without a key group"
);
assert_eq!(
std::str::from_utf8(&output.stderr).unwrap(),
"Could not generate data key: [empty key group provided]\n"
);
}

#[test]
fn extract_string() {
let file_path = prepare_temp_file(
Expand Down
10 changes: 10 additions & 0 deletions sops.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,11 @@ func (m *Metadata) UpdateMasterKeysWithKeyServices(dataKey []byte, svcs []keyser
fmt.Errorf("no key services provided, cannot update master keys"),
}
}
if len(m.KeyGroups) == 0 {
return []error{
fmt.Errorf("no key groups provided"),
}
}
var parts [][]byte
if len(m.KeyGroups) == 1 {
// If there's only one key group, we can't do Shamir. All keys
Expand All @@ -726,6 +731,11 @@ func (m *Metadata) UpdateMasterKeysWithKeyServices(dataKey []byte, svcs []keyser
}
for i, group := range m.KeyGroups {
part := parts[i]
if len(group) == 0 {
return []error{
fmt.Errorf("empty key group provided"),
}
}
for _, key := range group {
svcKey := keyservice.KeyFromMasterKey(key)
var keyErrs []error
Expand Down
Loading