Skip to content

Commit

Permalink
add initial cli
Browse files Browse the repository at this point in the history
  • Loading branch information
fzerorubigd committed Jun 22, 2019
1 parent 00bafe7 commit f2cf2f7
Show file tree
Hide file tree
Showing 6 changed files with 208 additions and 5 deletions.
6 changes: 5 additions & 1 deletion ciphers/secconf/sec_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ type cipher struct {
}

func (c *cipher) Decrypt(r io.Reader) ([]byte, error) {
return Decode(r, bytes.NewReader(c.secretKeyring))
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return Decode(data, bytes.NewReader(c.secretKeyring))
}

func NewCipher(secRing io.Reader) (onion.Cipher, error) {
Expand Down
5 changes: 3 additions & 2 deletions ciphers/secconf/secconf.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import (
)

// Decode decodes data using the secconf codec.
func Decode(data io.Reader, secretKeyring io.Reader) ([]byte, error) {
decoder := base64.NewDecoder(base64.StdEncoding, data)
func Decode(data []byte, secretKeyring io.Reader) ([]byte, error) {
r := bytes.NewReader(data)
decoder := base64.NewDecoder(base64.StdEncoding, r)
entityList, err := openpgp.ReadArmoredKeyRing(secretKeyring)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion ciphers/secconf/secconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestEncoding(t *testing.T) {
if err != nil {
t.Errorf(err.Error())
}
decoded, err := Decode(bytes.NewReader(encoded), bytes.NewBufferString(secring))
decoded, err := Decode(encoded, bytes.NewBufferString(secring))
if err != nil {
t.Errorf(err.Error())
}
Expand Down
191 changes: 191 additions & 0 deletions cli/onioncli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package main

import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"log"
"net/url"
"os"

"github.com/coreos/etcd/client"
"github.com/fzerorubigd/onion/ciphers/secconf"
"github.com/ogier/pflag"
)

var (
src = pflag.StringP("source", "s", "", "Source address to read from")
dst = pflag.StringP("destination", "d", "", "Destination address to write into")
srcKey = pflag.String("sk", "", "Source private key to use for reading data from source, if the source is plain leave it empty")
dstKey = pflag.String("pk", "", "Destination public key to use for writing data to destination, if the destination is plain leave it empty")
)

func open(path string) (io.ReadCloser, error) {
if path == "-" {
return os.Stdin, nil
}
return os.Open(path)
}

func create(path string) (io.WriteCloser, error) {
if path == "-" {
return os.Stdout, nil
}

return os.Create(path)
}

func readAllFile(path string) ([]byte, error) {
f, err := open(path)
if err != nil {
return nil, err
}
defer func() { _ = f.Close() }()

return ioutil.ReadAll(f)
}

func writeAllFile(path *url.URL, data []byte) error {
f, err := create(path.Path)
if err != nil {
return err
}
defer func() { _ = f.Close() }()

_, err = fmt.Fprint(f, string(data))
return err
}

func connectEtcd(u *url.URL) (client.Client, client.KeysAPI, error) {
cli, err := client.New(client.Config{
Endpoints: []string{"http://" + u.Host},
})
if err != nil {
return nil, nil, err
}

kv := client.NewKeysAPI(cli)
return cli, kv, nil
}

func readAllEtcd(u *url.URL) ([]byte, error) {
_, kv, err := connectEtcd(u)
if err != nil {
return nil, err
}
resp, err := kv.Get(context.TODO(), u.Path, nil)
if err != nil {
return nil, err
}

return []byte(resp.Node.Value), nil
}

func writeAllEtcd(u *url.URL, data []byte) error {
_, kv, err := connectEtcd(u)
if err != nil {
return err
}
if _, err = kv.Set(context.TODO(), u.Path, string(data), nil); err != nil {
return err
}

return nil
}

func read(path *url.URL) ([]byte, error) {
switch path.Scheme {
case "":
return readAllFile(path.Path)
case "etcd":
return readAllEtcd(path)
default:
return nil, fmt.Errorf("scheme %q is not valid", path.Scheme)
}
}

func write(path *url.URL, data []byte) error {
switch path.Scheme {
case "":
return writeAllFile(path, data)
case "etcd":
return writeAllEtcd(path, data)
default:
return fmt.Errorf("scheme %q is not valid", path.Scheme)
}
}

type transformer func([]byte) ([]byte, error)

func encrypt(fl string) (transformer, error) {
if fl == "" {
return func(in []byte) ([]byte, error) {
return in, nil
}, nil
}
data, err := readAllFile(fl)
if err != nil {
return nil, err
}
return func(in []byte) ([]byte, error) {
return secconf.Encode(in, bytes.NewReader(data))
}, nil
}

func decrypt(fl string) (transformer, error) {
if fl == "" {
return func(in []byte) ([]byte, error) {
return in, nil
}, nil
}
data, err := readAllFile(fl)
if err != nil {
return nil, err
}
return func(in []byte) ([]byte, error) {
return secconf.Decode(in, bytes.NewReader(data))
}, nil
}

func fatalIfErr(message string, err error) {
if err == nil {
return
}

log.Fatalf(message, err)

}

func main() {
pflag.Parse()
if *src == "" || *dst == "" {
pflag.Usage()
return
}

srcUrl, err := url.Parse(*src)
fatalIfErr("Parsing source url failed: %q", err)

dstUrl, err := url.Parse(*dst)
fatalIfErr("Parsing destination url failed: %q", err)

enc, err := encrypt(*dstKey)
fatalIfErr("Fail to create the encrypt function: %q", err)

dec, err := decrypt(*srcKey)
fatalIfErr("Fail to create the decrypt function: %q", err)

in, err := read(srcUrl)
fatalIfErr("Failed to read from source: %q", err)

in, err = dec(in)
fatalIfErr("Failed to decrypt the source: %q", err)

out, err := enc(in)
fatalIfErr("Failed to encrypt the data: %q", err)

err = write(dstUrl, out)
fatalIfErr("Failed to write data into destination: %q", err)
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.12
require (
github.com/BurntSushi/toml v0.3.1
github.com/coreos/bbolt v1.3.3 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/coreos/etcd v3.3.13+incompatible
github.com/coreos/go-semver v0.3.0 // indirect
github.com/coreos/go-systemd v0.0.0-20190620071333-e64a0ec8b42a // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
Expand All @@ -23,6 +23,7 @@ require (
github.com/jonboulle/clockwork v0.1.0 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/magiconair/properties v1.8.1
github.com/ogier/pflag v0.0.1
github.com/pkg/errors v0.8.1 // indirect
github.com/prometheus/common v0.6.0 // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
Expand All @@ -40,6 +41,7 @@ require (
golang.org/x/sys v0.0.0-20190621203818-d432491b9138 // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4 // indirect
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/genproto v0.0.0-20190620144150-6af8c5fc6601 // indirect
google.golang.org/grpc v1.21.1 // indirect
gopkg.in/yaml.v2 v2.2.2
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc h1:cAKDfWh5VpdgMhJosfJnn5/FoN2SRZ4p7fJNX58YPaU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf h1:qet1QNfXsQxTZqLG4oE62mJzwPIB8+Tee4RNCL9ulrY=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
Expand Down Expand Up @@ -86,6 +88,8 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/ogier/pflag v0.0.1 h1:RW6JSWSu/RkSatfcLtogGfFgpim5p7ARQ10ECk5O750=
github.com/ogier/pflag v0.0.1/go.mod h1:zkFki7tvTa0tafRvTBIZTvzYyAu6kQhPZFnshFFPE+g=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
Expand Down Expand Up @@ -190,6 +194,7 @@ google.golang.org/genproto v0.0.0-20190620144150-6af8c5fc6601/go.mod h1:z3L6/3dT
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.1 h1:j6XxA85m/6txkUCHvzlV5f+HBNl/1r5cZ2A/3IEFOO8=
google.golang.org/grpc v1.21.1/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6 h1:jMFz6MfLP0/4fUyZle81rXUoxOBFi19VUFKVDOQfozc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down

0 comments on commit f2cf2f7

Please sign in to comment.