/
encrypt.go
111 lines (99 loc) · 3.15 KB
/
encrypt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright © 2017 Sam Elliott <me@sam-e.co.uk>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"encoding/base64"
"fmt"
"io/ioutil"
"log"
"os"
"github.com/elliottsam/vault-config/crypto"
"github.com/spf13/cobra"
)
var inline bool
// encryptCmd represents the encrypt command
var encryptCmd = &cobra.Command{
Use: "encrypt",
Short: "Encrypts a file using the specified key",
Long: `Encrypts a file using the specified key, if the
delete flag is set to true, this will also delete
the original file
The key specified needs to be 32 bytes long and
base 64 encoded, this can be generated with the
keygen command
If the key is not specified it will be requested
from the command line
i.e.
vault-config encrypt -i config.vc -k mSskqBC85rA65lofPOaQcVtjjnHJ16rI+/rqZfkBwqs=
Will encrypt the file 'config.vc' to 'config.vc.enc'`,
Run: func(cmd *cobra.Command, args []string) {
if input == "" {
log.Fatalf("No input file specified, use paramter -input")
}
var err error
e := crypto.EncryptionObject{}
if key == "" {
e.Key, err = crypto.GetPassword()
if err != nil {
log.Fatal(err)
}
} else {
e.Key, err = base64.StdEncoding.DecodeString(key)
if err != nil {
log.Fatalf("Error decoding base64 key: %v", err)
}
}
if len(e.Key) != 32 {
log.Fatalln("Key must be 32 bytes")
}
e.PlainText, err = ioutil.ReadFile(input)
if err != nil {
log.Fatal(err)
}
if inline {
err = e.InlineEncryptMap("secret/data")
if err != nil {
log.Fatalf("Error performing inline encryption")
}
if output == "" {
output = input
}
if err := ioutil.WriteFile(output, []byte(e.CipherText), 0644); err != nil {
log.Fatalf("Error writing encrypted file to disk: %v", err)
}
} else {
if err := e.Encrypt(); err != nil {
log.Fatalf("Error encrypting file: %v", err)
}
e.WrapCrypto()
if deleteFile {
os.Remove(input)
}
if output == "" {
output = fmt.Sprintf("%s.enc", input)
}
if err := ioutil.WriteFile(output, []byte(e.WrappedData), 0644); err != nil {
log.Fatalf("Error writing encrypted file to disk: %v", err)
}
}
},
}
func init() {
RootCmd.AddCommand(encryptCmd)
encryptCmd.Flags().StringVarP(&input, "input", "i", "", "Name of file to encrypt - required")
encryptCmd.Flags().StringVarP(&output, "output", "o", "", "Name of encrypted file to output")
encryptCmd.Flags().StringVarP(&key, "key", "k", "", "Key to use for encryption")
encryptCmd.Flags().BoolVarP(&deleteFile, "delete", "d", false, "Delete original file after encryption, if successful")
encryptCmd.Flags().BoolVarP(&inline, "inline", "l", false, "Use inline encryption - this only works on secrets")
}