this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

restructure config and add encryption

Alex Ottr 019a53a0 ba8f513c

+95 -33
+32 -22
cmd/nox/main.go
··· 68 68 // }, 69 69 // }, 70 70 { 71 + Name: "encrypt", 72 + Aliases: []string{"enc"}, 73 + Usage: "Encrypt a file", 74 + Flags: []cli.Flag{ 75 + &cli.StringFlag{ 76 + Name: "input", 77 + Usage: "path to input file", 78 + Aliases: []string{"i"}, 79 + Value: constants.StandardInput, 80 + Destination: &inputPath, 81 + }, 82 + &cli.StringFlag{ 83 + Name: "output", 84 + Usage: "path to output file", 85 + Aliases: []string{"o"}, 86 + Value: constants.StandardOutput, 87 + Destination: &outputPath, 88 + }, 89 + &cli.StringSliceFlag{ 90 + Name: "recipient", 91 + Usage: "age public key of recipient (repeatable)", 92 + Aliases: []string{"r"}, 93 + }, 94 + }, 95 + Action: func(ctx context.Context, cmd *cli.Command) error { 96 + fmt.Println("encrypting file", inputPath) 97 + fmt.Println("writing to", outputPath) 98 + fmt.Println(cmd.StringSlice("recipient")) 99 + return nil 100 + }, 101 + }, 102 + { 71 103 Name: "export", 72 104 Aliases: []string{"e"}, 73 105 Usage: "Export all secrets to a single file", ··· 110 142 return processor.SyncApp(rtx) 111 143 } 112 144 return processor.SyncApps(rtx) 113 - }, 114 - }, 115 - { 116 - Name: "encrypt", 117 - Aliases: []string{"enc"}, 118 - Usage: "Encrypt a file", 119 - Flags: []cli.Flag{ 120 - &cli.StringFlag{ 121 - Name: "input", 122 - Usage: "path to input file", 123 - Destination: &inputPath, 124 - }, 125 - &cli.StringFlag{ 126 - Name: "output", 127 - Usage: "path to output file", 128 - Destination: &outputPath, 129 - }, 130 - }, 131 - Action: func(ctx context.Context, cmd *cli.Command) error { 132 - fmt.Println("encrypting file", inputPath) 133 - fmt.Println("writing to", outputPath) 134 - return nil 135 145 }, 136 146 }, 137 147 {
+4 -1
config.yaml .nox.yaml
··· 1 1 interval: "10m" 2 - ageKeyPath: "keys/key.txt" 2 + age: 3 + identity: "keys/key.txt" 4 + recipients: 5 + - "age1nuxu3q9wr5wrd53dj8hj5flhz86q2dpjyuq7agseh0wzwq5t696s2dm0ht" 3 6 statePath: ".nox-state.json" 4 7 defaultRepo: git@github.com:ShorkBytes/nox-secrets.git 5 8 secrets:
+11 -5
internal/config/config.go
··· 12 12 } 13 13 14 14 type FileConfig struct { 15 - Path string `yaml:"path"` 16 - Output string `yaml:"output,omitempty"` 15 + Path string `yaml:"path"` 16 + Output string `yaml:"output,omitempty"` 17 17 } 18 18 19 19 type AppConfig struct { 20 - Repo string `yaml:"repo,omitempty"` 21 - Branch string `yaml:"branch"` 20 + Repo string `yaml:"repo,omitempty"` 21 + Branch string `yaml:"branch"` 22 22 Files []FileConfig `yaml:"files"` 23 23 } 24 24 25 + type AgeConfig struct { 26 + Identity string `yaml:"identity"` 27 + Identities []string `yaml:"identities,omitempty"` 28 + Recipients []string `yaml:"recipients,omitempty"` 29 + } 30 + 25 31 type Config struct { 26 32 Interval string `yaml:"interval"` 27 - AgeKeyPath string `yaml:"ageKeyPath"` 33 + Age AgeConfig `yaml:"age"` 28 34 StatePath string `yaml:"statePath"` 29 35 DefaultRepo string `yaml:"defaultRepo"` 30 36 Secrets []SecretMapping `yaml:"secrets"`
+1 -1
internal/config/context.go
··· 49 49 50 50 identityPath := opts.IdentityPath 51 51 if identityPath == "" { 52 - identityPath = cfg.AgeKeyPath 52 + identityPath = cfg.Age.Identity 53 53 } 54 54 ids, err := crypto.LoadAgeIdentities(identityPath) 55 55 if err != nil {
+5 -3
internal/constants/constants.go
··· 1 1 package constants 2 2 3 3 const ( 4 - DefaultStatePath = ".nox-state.json" 5 - DefaultConfigPath = "config.yaml" 6 - ) 4 + DefaultStatePath = ".nox-state.json" 5 + DefaultConfigPath = ".nox.yaml" 6 + StandardOutput = "<stdout>" 7 + StandardInput = "<stdin>" 8 + )
+41
internal/crypto/encrypt.go
··· 1 + package crypto 2 + 3 + import ( 4 + "bytes" 5 + "fmt" 6 + "io" 7 + "os" 8 + 9 + "filippo.io/age" 10 + ) 11 + 12 + // EncryptFile encrypts the given file using the given identities 13 + func EncryptFile(path string, recipients []age.Recipient) ([]byte, error) { 14 + data, err := os.ReadFile(path) 15 + if err != nil { 16 + return nil, fmt.Errorf("failed to read file: %w", err) 17 + } 18 + return EncryptBytes(data, recipients) 19 + } 20 + 21 + func EncryptBytes(data []byte, recipients []age.Recipient) ([]byte, error) { 22 + 23 + src := bytes.NewReader(data) 24 + dst := new(bytes.Buffer) 25 + 26 + enc, err := age.Encrypt(dst, recipients...) 27 + if err != nil { 28 + return nil, fmt.Errorf("initializing age enc failed: %w", err) 29 + } 30 + 31 + if _, err := io.Copy(enc, src); err != nil { 32 + enc.Close() // ensure resources are freed 33 + return nil, fmt.Errorf("encryption failed: %w", err) 34 + } 35 + 36 + if err := enc.Close(); err != nil { 37 + return nil, fmt.Errorf("finalizing encryption failed: %w", err) 38 + } 39 + 40 + return dst.Bytes(), nil 41 + }
+1 -1
internal/processor/validate.go
··· 11 11 ) 12 12 13 13 func ValidateConfig(cfg *config.Config) error { 14 - if cfg.AgeKeyPath == "" { 14 + if cfg.Age.Identity == "" { 15 15 return fmt.Errorf("age key path is required") 16 16 } 17 17