Skip to content

Commit

Permalink
cli option to decrypt
Browse files Browse the repository at this point in the history
  • Loading branch information
reshke committed Nov 13, 2023
1 parent 46d7f90 commit d08e07e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
4 changes: 3 additions & 1 deletion cmd/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

var cfgPath string
var logLevel string
var decrypt bool

var rootCmd = &cobra.Command{
Use: "",
Expand All @@ -32,7 +33,7 @@ var rootCmd = &cobra.Command{
}

defer con.Close()
msg := proc.ConstructMessage(args[0])
msg := proc.ConstructMessage(args[0], decrypt)
_, err = con.Write(msg)
if err != nil {
return err
Expand All @@ -57,6 +58,7 @@ var rootCmd = &cobra.Command{
func init() {
rootCmd.PersistentFlags().StringVarP(&cfgPath, "config", "c", "/etc/yproxy/yproxy.yaml", "path to yproxy config file")
rootCmd.PersistentFlags().StringVarP(&logLevel, "log-level", "l", "", "log level")
rootCmd.PersistentFlags().BoolVarP(&decrypt, "decrypt", "d", false, "decrypt external object or not")
}

func main() {
Expand Down
8 changes: 7 additions & 1 deletion pkg/proc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error {
case MessageTypeCat:
// omit first byte
name := GetCatName(body[4:])
ylogger.Zero.Debug().Str("object-path", name).Msg("cat object ")
ylogger.Zero.Debug().Str("object-path", name).Msg("cat object")
r, err := s.CatFileFromStorage(name)
if err != nil {

Expand All @@ -34,8 +34,14 @@ func ProcConn(s storage.StorageReader, cr crypt.Crypter, c net.Conn) error {
return err
}
if body[1] == byte(DecryptMessage) {
ylogger.Zero.Debug().Str("object-path", name).Msg("decrypt object ")
r, err = cr.Decrypt(r)
if err != nil {

_, _ = c.Write([]byte(
fmt.Sprintf("failed to compelete request: %v", err),
))

return err
}
}
Expand Down
16 changes: 12 additions & 4 deletions pkg/proc/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type MessageType byte
type RequestEncryption byte

const (
MessageTypeCat = MessageType(42)
DecryptMessage = RequestEncryption(1)
MessageTypeCat = MessageType(42)
DecryptMessage = RequestEncryption(1)
NoDecryptMessage = RequestEncryption(0)
)

func (m MessageType) String() string {
Expand Down Expand Up @@ -71,14 +72,21 @@ func GetCatName(b []byte) string {
return buff.String()
}

func ConstructMessage(name string) []byte {
func ConstructMessage(name string, decrypt bool) []byte {

bt := []byte{
byte(MessageTypeCat),
byte(DecryptMessage),
0,
0,
0,
}

if decrypt {
bt[1] = byte(DecryptMessage)
} else {
bt[1] = byte(NoDecryptMessage)
}

bt = append(bt, []byte(name)...)
bt = append(bt, 0)
ln := len(bt)
Expand Down

0 comments on commit d08e07e

Please sign in to comment.