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

Function encrypt

cmd/sops/encrypt.go:94–144  ·  view source on GitHub ↗
(opts encryptOpts)

Source from the content-addressed store, hash-verified

92}
93
94func encrypt(opts encryptOpts) (encryptedFile []byte, err error) {
95 // Load the file
96 var fileBytes []byte
97 if opts.ReadFromStdin {
98 fileBytes, err = io.ReadAll(os.Stdin)
99 if err != nil {
100 return nil, common.NewExitError(fmt.Sprintf("Error reading from stdin: %s", err), codes.CouldNotReadInputFile)
101 }
102 } else {
103 fileBytes, err = os.ReadFile(opts.InputPath)
104 if err != nil {
105 return nil, common.NewExitError(fmt.Sprintf("Error reading file: %s", err), codes.CouldNotReadInputFile)
106 }
107 }
108 branches, err := opts.InputStore.LoadPlainFile(fileBytes)
109 if err != nil {
110 return nil, common.NewExitError(fmt.Sprintf("Error unmarshalling file: %s", err), codes.CouldNotReadInputFile)
111 }
112 if err, code := validateFileForEncryption(opts.OutputStore, branches); err != nil {
113 return nil, common.NewExitError(err, code)
114 }
115 path, err := filepath.Abs(opts.InputPath)
116 if err != nil {
117 return nil, err
118 }
119 tree := sops.Tree{
120 Branches: branches,
121 Metadata: metadataFromEncryptionConfig(opts.encryptConfig),
122 FilePath: path,
123 }
124 dataKey, errs := tree.GenerateDataKeyWithKeyServices(opts.KeyServices)
125 if len(errs) > 0 {
126 err = fmt.Errorf("Could not generate data key: %s", errs)
127 return nil, err
128 }
129
130 err = common.EncryptTree(common.EncryptTreeOpts{
131 DataKey: dataKey,
132 Tree: &tree,
133 Cipher: opts.Cipher,
134 })
135 if err != nil {
136 return nil, err
137 }
138
139 encryptedFile, err = opts.OutputStore.EmitEncryptedFile(tree)
140 if err != nil {
141 return nil, common.NewExitError(fmt.Sprintf("Could not marshal tree: %s", err), codes.ErrorDumpingTree)
142 }
143 return
144}

Callers 1

mainFunction · 0.85

Calls 7

NewExitErrorFunction · 0.92
EncryptTreeFunction · 0.92
LoadPlainFileMethod · 0.65
EmitEncryptedFileMethod · 0.65

Tested by

no test coverage detected