diff options
| author | Shulhan <ms@kilabit.info> | 2023-12-16 18:28:59 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-12-16 18:28:59 +0700 |
| commit | cf8caf8844b51d71e39f5b454fc7c79a9bec3227 (patch) | |
| tree | 07c59becb58fa485c598b3067ffd4f26f8b2cf73 /session.go | |
| parent | 17a19677b325ce9911570db0988cd18ee996f46a (diff) | |
| download | awwan-cf8caf8844b51d71e39f5b454fc7c79a9bec3227.tar.xz | |
all: make the magic line "#put:" use explicit source for encrypted file
Previously, the magic line "#put" detect whether the source file is
encrypted or not automatically, so we did not need to put ".vault"
suffix in the source path.
This changes make it to be always explicit.
If we want to copy encrypted file than the source file must be the path
to encrypted file.
Implements: https://todo.sr.ht/~shulhan/awwan/7
Diffstat (limited to 'session.go')
| -rw-r--r-- | session.go | 48 |
1 files changed, 18 insertions, 30 deletions
@@ -541,19 +541,20 @@ func (ses *Session) executeScriptOnRemote(req *ExecRequest, pos linePosition) (e func (ses *Session) generateFileInput(in string) (out string, isVault bool, err error) { // Check if the file is binary first, since binary file will not get // encrypted. - if libos.IsBinary(in) { + if !strings.HasSuffix(in, defEncryptExt) && libos.IsBinary(in) { return in, false, nil } var ( - logp = `generateFileInput` + logp = `generateFileInput` + relPathInput = relativePath(ses.BaseDir, in) contentInput []byte ) contentInput, isVault, err = ses.loadFileInput(in) if err != nil { - return ``, false, err + return ``, false, fmt.Errorf(`%s %q: %w`, logp, relPathInput, err) } var contentOut []byte @@ -704,43 +705,30 @@ func (ses *Session) loadFileEnv(awwanEnv string, isVault bool) (err error) { } // loadFileInput read the input file for Copy or Put operation. -// If the original input file does not exist, try loading the encrypted file -// with ".vault" extension. +// +// If the input path end with ".vault" suffix, the file will be decrypted +// first. // // On success, it will return the content of file and true if the file is // from encrypted file .vault. func (ses *Session) loadFileInput(path string) (content []byte, isVault bool, err error) { - var ( - logp = `loadFileInput` - relPath = relativePath(ses.BaseDir, path) - ) - - content, err = os.ReadFile(path) - if err == nil { - return content, false, nil - } - if !errors.Is(err, fs.ErrNotExist) { - return nil, false, err + if strings.HasSuffix(path, defEncryptExt) { + content, err = os.ReadFile(path) + if err != nil { + return nil, true, err + } + content, err = ses.cryptoc.decrypt(content) + if err != nil { + return nil, true, err + } + return content, true, nil } - log.Printf(`??? %s %q: not exist`, logp, relPath) - - path = path + defEncryptExt - relPath += defEncryptExt content, err = os.ReadFile(path) if err != nil { - if errors.Is(err, fs.ErrNotExist) { - return nil, false, fmt.Errorf(`%s %q: %w`, logp, relPath, fs.ErrNotExist) - } return nil, false, err } - - content, err = ses.cryptoc.decrypt(content) - if err != nil { - return nil, false, fmt.Errorf(`%s %q: %s`, logp, relPath, err) - } - - return content, true, nil + return content, false, nil } func (ses *Session) loadRawEnv(content []byte) (err error) { |
