Skip to content

Commit

Permalink
Merge pull request #1600 from felixfontein/no-keys
Browse files Browse the repository at this point in the history
Do not encrypt if a key group is empty, or there are no key groups
  • Loading branch information
felixfontein authored Sep 25, 2024
2 parents 15bed3e + 8c60d48 commit cd8a9fb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
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

0 comments on commit cd8a9fb

Please sign in to comment.