Skip to content

Commit 8feccf0

Browse files
authored
Merge pull request #726 from bkreitch/try-offline-methods-first
Sort SOPS masterkeys so offline decrypt methods are tried first
2 parents 9e0930c + 5fe7910 commit 8feccf0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

controllers/kustomization_decryptor.go

+8
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io/fs"
2626
"os"
2727
"path/filepath"
28+
"sort"
2829
"strings"
2930
"sync"
3031
"time"
@@ -280,6 +281,13 @@ func (d *KustomizeDecryptor) SopsDecryptWithFormat(data []byte, inputFormat, out
280281
return nil, sopsUserErr(fmt.Sprintf("failed to load encrypted %s data", sopsFormatToString[inputFormat]), err)
281282
}
282283

284+
for _, group := range tree.Metadata.KeyGroups {
285+
// Sort MasterKeys in the group so offline ones are tried first
286+
sort.SliceStable(group, func(i, j int) bool {
287+
return intkeyservice.IsOfflineMethod(group[i]) && !intkeyservice.IsOfflineMethod(group[j])
288+
})
289+
}
290+
283291
metadataKey, err := tree.Metadata.GetDataKeyWithKeyServices(d.keyServiceServer())
284292
if err != nil {
285293
return nil, sopsUserErr("cannot get sops data key", err)
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (C) 2022 The Flux authors
2+
//
3+
// This Source Code Form is subject to the terms of the Mozilla Public
4+
// License, v. 2.0. If a copy of the MPL was not distributed with this
5+
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
6+
7+
package keyservice
8+
9+
import (
10+
"go.mozilla.org/sops/v3/age"
11+
"go.mozilla.org/sops/v3/keys"
12+
"go.mozilla.org/sops/v3/pgp"
13+
)
14+
15+
// IsOfflineMethod returns true for offline decrypt methods or false otherwise
16+
func IsOfflineMethod(mk keys.MasterKey) bool {
17+
switch mk.(type) {
18+
case *pgp.MasterKey, *age.MasterKey:
19+
return true
20+
default:
21+
return false
22+
}
23+
}

0 commit comments

Comments
 (0)