MCPcopy
hub / github.com/getsops/sops / ExtractMetadata

Function ExtractMetadata

stores/metadata.go:106–178  ·  view source on GitHub ↗

ExtractMetadata extracts SOPS metadata from the supplied tree branches.

(branches sops.TreeBranches, opts MetadataOpts)

Source from the content-addressed store, hash-verified

104
105// ExtractMetadata extracts SOPS metadata from the supplied tree branches.
106func ExtractMetadata(branches sops.TreeBranches, opts MetadataOpts) (sops.TreeBranches, sops.Metadata, error) {
107 var metadataTree sops.TreeBranch
108 if opts.Flatten != MetadataFlattenFull {
109 first := true
110 for bi, branch := range branches {
111 i := 0
112 for i < len(branch) {
113 if branch[i].Key == SopsMetadataKey {
114 if bi == 0 {
115 if !first {
116 return nil, sops.Metadata{}, fmt.Errorf("Found duplicate %v entry", SopsMetadataKey)
117 }
118 first = false
119 if tree, ok := branch[i].Value.(sops.TreeBranch); ok {
120 metadataTree = tree
121 } else {
122 return nil, sops.Metadata{}, fmt.Errorf("Found %v entry that is not a mapping", SopsMetadataKey)
123 }
124 }
125 branch = append(branch[:i], branch[i+1:]...)
126 } else {
127 i++
128 }
129 }
130 branches[bi] = branch
131 }
132 } else {
133 if len(branches) >= 1 {
134 branch := branches[0]
135 for i := 0; i < len(branch); i++ {
136 if key, ok := branch[i].Key.(string); ok {
137 if strings.HasPrefix(key, SopsPrefix) {
138 entry := branch[i]
139 entry.Key = key[len(SopsPrefix):]
140 metadataTree = append(metadataTree, entry)
141 branch = append(branch[:i], branch[i+1:]...)
142 i--
143 }
144 }
145 }
146 branches[0] = branch
147 }
148 }
149 if metadataTree == nil {
150 return nil, sops.Metadata{}, sops.MetadataNotFound
151 }
152 if opts.Flatten != MetadataFlattenNone {
153 var err error
154 if opts.EscapeNewlines {
155 for i, item := range metadataTree {
156 if value, ok := item.Value.(string); ok {
157 metadataTree[i] = sops.TreeItem{
158 Key: item.Key,
159 Value: strings.ReplaceAll(value, "\\n", "\n"),
160 }
161 }
162 }
163 }

Callers 5

LoadEncryptedFileMethod · 0.92
LoadEncryptedFileMethod · 0.92
LoadEncryptedFileMethod · 0.92
LoadEncryptedFileMethod · 0.92
TestExtractMetadataFunction · 0.85

Calls 3

unflattenTreeBranchFunction · 0.85
treeBranchToMetadataFunction · 0.85
ToInternalMethod · 0.80

Tested by 1

TestExtractMetadataFunction · 0.68