Skip to content

Commit 84c03e1

Browse files
committed
First import
0 parents  commit 84c03e1

File tree

252 files changed

+70312
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

252 files changed

+70312
-0
lines changed

cmd/sec/config.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"text/tabwriter"
9+
10+
"gopkg.in/alecthomas/kingpin.v2"
11+
)
12+
13+
func config(app *kingpin.Application, configFile string) {
14+
data, err := ioutil.ReadFile(configFile)
15+
app.FatalIfError(err, "sec")
16+
17+
var conf Config
18+
err = json.Unmarshal(data, &conf)
19+
app.FatalIfError(err, "sec")
20+
21+
w := tabwriter.NewWriter(os.Stdout, 8, 8, 2, ' ', 0)
22+
defer w.Flush()
23+
24+
fmt.Fprintf(w, "%s\t%s\n", "master", conf.MasterKey)
25+
w.Flush()
26+
27+
for name, key := range conf.ReceiverKeys {
28+
fmt.Fprintf(w, "%s\t%s\t%s\n", "receiver", name, key)
29+
}
30+
}

cmd/sec/dec.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
"strings"
10+
11+
"github.com/fd/sec-utils/pkg/box"
12+
"gopkg.in/alecthomas/kingpin.v2"
13+
)
14+
15+
func dec(app *kingpin.Application, creds string) {
16+
keys := strings.SplitN(creds, ":", 2)
17+
if len(keys) != 2 {
18+
err := errors.New("invalid credentials")
19+
app.FatalIfError(err, "sec")
20+
}
21+
22+
data, err := ioutil.ReadAll(os.Stdin)
23+
app.FatalIfError(err, "sec")
24+
25+
decdata, err := box.Open(data, keys[0], keys[1])
26+
app.FatalIfError(err, "sec")
27+
28+
_, err = io.Copy(os.Stdout, bytes.NewReader(decdata))
29+
app.FatalIfError(err, "sec")
30+
}

cmd/sec/enc.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"io/ioutil"
8+
"os"
9+
10+
"github.com/fd/sec-utils/pkg/box"
11+
"gopkg.in/alecthomas/kingpin.v2"
12+
)
13+
14+
func enc(app *kingpin.Application, configFile string) {
15+
data, err := ioutil.ReadFile(configFile)
16+
app.FatalIfError(err, "sec")
17+
18+
var conf Config
19+
err = json.Unmarshal(data, &conf)
20+
app.FatalIfError(err, "sec")
21+
22+
var rKeys []string
23+
for _, k := range conf.ReceiverKeys {
24+
rKeys = append(rKeys, k)
25+
}
26+
27+
data, err = ioutil.ReadAll(os.Stdin)
28+
app.FatalIfError(err, "sec")
29+
30+
encdata, err := box.Seal(data, conf.MasterKey, rKeys)
31+
app.FatalIfError(err, "sec")
32+
33+
_, err = io.Copy(os.Stdout, bytes.NewReader(encdata))
34+
app.FatalIfError(err, "sec")
35+
}

cmd/sec/init.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
9+
"github.com/fd/sec-utils/pkg/box"
10+
"gopkg.in/alecthomas/kingpin.v2"
11+
)
12+
13+
func doInit(app *kingpin.Application, configFile string) {
14+
if _, err := os.Stat(configFile); !os.IsNotExist(err) {
15+
err := fmt.Errorf("config file already exists")
16+
app.FatalIfError(err, "sec")
17+
}
18+
19+
m, err := box.GenerateKey()
20+
app.FatalIfError(err, "sec")
21+
22+
conf := &Config{
23+
MasterKey: m,
24+
ReceiverKeys: map[string]string{},
25+
}
26+
27+
data, err := json.MarshalIndent(&conf, "", " ")
28+
app.FatalIfError(err, "sec")
29+
30+
err = ioutil.WriteFile(configFile, data, 0600)
31+
app.FatalIfError(err, "sec")
32+
}
33+
34+
type Config struct {
35+
MasterKey string
36+
ReceiverKeys map[string]string
37+
}

cmd/sec/main.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package main
2+
3+
import (
4+
"os"
5+
6+
"gopkg.in/alecthomas/kingpin.v2"
7+
"limbo.services/version"
8+
)
9+
10+
func main() {
11+
var (
12+
configFile string
13+
receiverName string
14+
receiverKey string
15+
)
16+
17+
app := kingpin.New("sec", "Simple secure data").
18+
Version(version.Get().String()).
19+
Author(version.Get().ReleasedBy)
20+
21+
initCmd := app.Command("init", "Initialize a config file")
22+
initCmd.Flag("config", "path to the config file").Short('c').
23+
Default("./sec-config.json").PlaceHolder("FILE").Envar("SEC_CONFIG").
24+
StringVar(&configFile)
25+
26+
configCmd := app.Command("config", "Show the content of a config file")
27+
configCmd.Flag("config", "path to the config file").Short('c').
28+
Default("./sec-config.json").PlaceHolder("FILE").Envar("SEC_CONFIG").
29+
ExistingFileVar(&configFile)
30+
31+
receiverAddCmd := app.Command("receiver-add", "Register a new receiver")
32+
receiverAddCmd.Arg("name", "name of the receiver").Required().
33+
StringVar(&receiverName)
34+
receiverAddCmd.Flag("config", "path to the config file").Short('c').
35+
Default("./sec-config.json").PlaceHolder("FILE").Envar("SEC_CONFIG").
36+
ExistingFileVar(&configFile)
37+
38+
receiverRemoveCmd := app.Command("receiver-remove", "Remove a receiver")
39+
receiverRemoveCmd.Arg("name", "name of the receiver").Required().
40+
StringVar(&receiverName)
41+
receiverRemoveCmd.Flag("config", "path to the config file").Short('c').
42+
Default("./sec-config.json").PlaceHolder("FILE").Envar("SEC_CONFIG").
43+
ExistingFileVar(&configFile)
44+
45+
encCmd := app.Command("enc", "Encrypt data from STDIN")
46+
encCmd.Flag("config", "path to the config file").Short('c').
47+
Default("./sec-config.json").PlaceHolder("FILE").Envar("SEC_CONFIG").
48+
ExistingFileVar(&configFile)
49+
50+
decCmd := app.Command("dec", "Decrypt data from STDIN")
51+
decCmd.Flag("key", "receiver key").Short('k').
52+
PlaceHolder("KEY").Envar("SEC_KEY").
53+
Required().StringVar(&receiverKey)
54+
55+
switch kingpin.MustParse(app.Parse(os.Args[1:])) {
56+
case initCmd.FullCommand():
57+
doInit(app, configFile)
58+
case configCmd.FullCommand():
59+
config(app, configFile)
60+
case receiverAddCmd.FullCommand():
61+
receiverAdd(app, configFile, receiverName)
62+
case receiverRemoveCmd.FullCommand():
63+
receiverRemove(app, configFile, receiverName)
64+
case encCmd.FullCommand():
65+
enc(app, configFile)
66+
case decCmd.FullCommand():
67+
dec(app, receiverKey)
68+
}
69+
}

cmd/sec/receiver_add.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
8+
"github.com/fd/sec-utils/pkg/box"
9+
"gopkg.in/alecthomas/kingpin.v2"
10+
)
11+
12+
func receiverAdd(app *kingpin.Application, configFile, name string) {
13+
data, err := ioutil.ReadFile(configFile)
14+
app.FatalIfError(err, "sec")
15+
16+
r, err := box.GenerateKey()
17+
app.FatalIfError(err, "sec")
18+
19+
rPub, err := box.PublicKey(r)
20+
app.FatalIfError(err, "sec")
21+
22+
var conf Config
23+
err = json.Unmarshal(data, &conf)
24+
app.FatalIfError(err, "sec")
25+
26+
mPub, err := box.PublicKey(conf.MasterKey)
27+
app.FatalIfError(err, "sec")
28+
29+
if conf.ReceiverKeys == nil {
30+
conf.ReceiverKeys = make(map[string]string)
31+
}
32+
if _, f := conf.ReceiverKeys[name]; f {
33+
err := fmt.Errorf("receiver %q already exists", name)
34+
app.FatalIfError(err, "sec")
35+
}
36+
37+
conf.ReceiverKeys[name] = rPub
38+
39+
data, err = json.MarshalIndent(&conf, "", " ")
40+
app.FatalIfError(err, "sec")
41+
42+
err = ioutil.WriteFile(configFile, data, 0600)
43+
app.FatalIfError(err, "sec")
44+
45+
fmt.Printf("%s:%s\n", r, mPub)
46+
}

cmd/sec/receiver_remove.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"io/ioutil"
6+
7+
"gopkg.in/alecthomas/kingpin.v2"
8+
)
9+
10+
func receiverRemove(app *kingpin.Application, configFile, name string) {
11+
data, err := ioutil.ReadFile(configFile)
12+
app.FatalIfError(err, "sec")
13+
14+
var conf Config
15+
err = json.Unmarshal(data, &conf)
16+
app.FatalIfError(err, "sec")
17+
18+
if conf.ReceiverKeys == nil {
19+
conf.ReceiverKeys = make(map[string]string)
20+
}
21+
22+
delete(conf.ReceiverKeys, name)
23+
24+
data, err = json.MarshalIndent(&conf, "", " ")
25+
app.FatalIfError(err, "sec")
26+
27+
err = ioutil.WriteFile(configFile, data, 0600)
28+
app.FatalIfError(err, "sec")
29+
}

0 commit comments

Comments
 (0)