@@ -146,22 +146,10 @@ func (c *versionCommand) Run() error {
146146}
147147
148148func (c * putCommand ) Run () error {
149- backend , err := store . NewS3 (c .AccessKey , c . SecretKey , c .Token , c . Region , c .Endpoint )
149+ app , err := newClient (c .awsFlags , cipherOpts { symKey : c .SymKey , pemKey : c . PemKey } , c .SkipHash )
150150 if err != nil {
151151 return err
152152 }
153- cipher , err := clientCipher (c .SymKey , c .PemKey )
154- if err != nil {
155- return err
156- }
157- app := & client.Client {
158- Hash : crypto .NewHash (),
159- Cipher : cipher ,
160- Store : backend ,
161- }
162- if c .SkipHash {
163- app .Hash = nil
164- }
165153 remote , local , err := checkPaths (c .RemotePath , c .LocalPath )
166154 if err != nil {
167155 return err
@@ -170,22 +158,10 @@ func (c *putCommand) Run() error {
170158}
171159
172160func (c * getCommand ) Run () error {
173- backend , err := store .NewS3 (c .AccessKey , c .SecretKey , c .Token , c .Region , c .Endpoint )
174- if err != nil {
175- return err
176- }
177- cipher , err := clientCipher (c .SymKey , c .PemKey )
161+ app , err := newClient (c .awsFlags , cipherOpts {symKey : c .SymKey , pemKey : c .PemKey }, c .SkipHash )
178162 if err != nil {
179163 return err
180164 }
181- app := & client.Client {
182- Hash : crypto .NewHash (),
183- Cipher : cipher ,
184- Store : backend ,
185- }
186- if c .SkipHash {
187- app .Hash = nil
188- }
189165 remote , local , err := checkPaths (c .RemotePath , c .LocalPath )
190166 if err != nil {
191167 return err
@@ -251,7 +227,10 @@ func (c *genRsaCommand) Run() error {
251227}
252228
253229func (c * encryptCommand ) Run () error {
254- cipher , err := clientCipher (c .SymKey , c .PemKey )
230+ cipher , err := newCipher (cipherOpts {
231+ symKey : c .SymKey ,
232+ pemKey : c .PemKey ,
233+ })
255234 if err != nil {
256235 return err
257236 }
@@ -262,7 +241,10 @@ func (c *encryptCommand) Run() error {
262241}
263242
264243func (c * decryptCommand ) Run () error {
265- cipher , err := clientCipher (c .SymKey , c .PemKey )
244+ cipher , err := newCipher (cipherOpts {
245+ symKey : c .SymKey ,
246+ pemKey : c .PemKey ,
247+ })
266248 if err != nil {
267249 return err
268250 }
@@ -272,6 +254,41 @@ func (c *decryptCommand) Run() error {
272254 return cipher .Decrypt (c .InputFile , c .OutputFile )
273255}
274256
257+ func newClient (af awsFlags , co cipherOpts , skipHash bool ) (* client.Client , error ) {
258+ backend , err := store .NewS3 (af .AccessKey , af .SecretKey , af .Token , af .Region , af .Endpoint )
259+ if err != nil {
260+ return nil , err
261+ }
262+ cipher , err := newCipher (co )
263+ if err != nil {
264+ return nil , err
265+ }
266+ app := & client.Client {
267+ Hash : crypto .NewHash (),
268+ Cipher : cipher ,
269+ Store : backend ,
270+ }
271+ if skipHash {
272+ app .Hash = nil
273+ }
274+ return app , nil
275+ }
276+
277+ type cipherOpts struct {
278+ symKey string
279+ pemKey string
280+ }
281+
282+ func newCipher (co cipherOpts ) (client.Cipher , error ) {
283+ if co .symKey != "" {
284+ return crypto .NewAESCipher (co .symKey )
285+ }
286+ if co .pemKey != "" {
287+ return crypto .NewRSACipher (co .pemKey )
288+ }
289+ return nil , nil
290+ }
291+
275292func checkPaths (inRemote , inLocal string ) (outRemote string , outLocal string , err error ) {
276293 if store .IsRemote (inRemote ) && store .IsRemote (inLocal ) {
277294 err = errors .New ("cannot have two remote paths" )
@@ -284,22 +301,12 @@ func checkPaths(inRemote, inLocal string) (outRemote string, outLocal string, er
284301 outLocal = inLocal
285302 outRemote = inRemote
286303 if store .IsRemote (inLocal ) {
287- outLocal = inRemote
288304 outRemote = inLocal
305+ outLocal = inRemote
289306 }
290307 return
291308}
292309
293- func clientCipher (symKey , pemKey string ) (client.Cipher , error ) {
294- if symKey != "" {
295- return crypto .NewAESCipher (symKey )
296- }
297- if pemKey != "" {
298- return crypto .NewRSACipher (pemKey )
299- }
300- return nil , nil
301- }
302-
303310func vaultConfig (f vaultFlags ) (* config.Config , error ) {
304311 log .Println ("Fetching configuration from vault" )
305312 ctx := context .Background ()
0 commit comments