Skip to content

Commit

Permalink
Merge pull request #18 from trimble-oss/corrected_salt_guard
Browse files Browse the repository at this point in the history
Correctly handle the salt guard
  • Loading branch information
joel-rieke authored Aug 31, 2024
2 parents 7c50bdd + 5a13631 commit 8cc258e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 7 deletions.
55 changes: 55 additions & 0 deletions brimfeather/feather_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package main

import (
cap2 "github.com/trimble-oss/tierceron-hat/cap"
captiplib "github.com/trimble-oss/tierceron-hat/captip/captiplib"
"os"
"sync"
"testing"
)

func featherInterrupted(featherCtx *cap2.FeatherContext) error {
cap2.FeatherCtlEmit(featherCtx, string(cap2.MODE_PERCH), *featherCtx.SessionIdentifier, true)
os.Exit(-1)
return nil
}

func TestGetSaltyGuardian(t *testing.T) {
cap2.TapInitCodeSaltGuard(func() string { return "ExtraSaltPlease" })

var serverStart sync.WaitGroup
serverStart.Add(1)
go func() {
go cap2.Feather("Som18vhjqa72935h", "1cx7v89as7df89", "127.0.0.1:1832", "ThisIsACode", func(int, string) bool { return true })

cap2.TapFeather("I think", "therefore I am.")
cap2.TapFeather("It is not enough to have a good mind.", "The main thing is to use it well.")

cap2.TapFeather("Ponder", "a feather.")

go cap2.TapServer("127.0.0.1:1534")
serverStart.Done()
}()

serverStart.Wait()
localHostAddr := "localhost:1534"
encryptPass := "Som18vhjqa72935h"
encryptSalt := "1cx7v89as7df89"
hostAddr := "127.0.0.1:1832"
handshakeCode := "ThisIsACode"
sessionIdentifier := "FeatherSessionOne"
env := "SomeEnv"

var interruptChan chan os.Signal = make(chan os.Signal, 5)
featherCtx := captiplib.FeatherCtlInit(interruptChan, &localHostAddr, &encryptPass, &encryptSalt, &hostAddr, &handshakeCode, &sessionIdentifier, &env, captiplib.AcceptRemote, featherInterrupted)

expected := "therefore I am."
msg, err := captiplib.FeatherQueryCache(featherCtx, "I think")
if err != nil {
t.Fatalf("Expected '%s', got %s", expected, err.Error())
}

if msg != expected {
t.Fatalf("Expected '%s', got %s", expected, msg)
}
}
14 changes: 7 additions & 7 deletions cap/cap.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ var penseFeatherPluckMap = cmap.New[bool]()
var penseFeatherCtlCodeMap = cmap.New[string]()

// CodeSaltGuardFn is expected to return a hex.EncodeToString encoded salt
type CodeSaltGuardFn func() string
type CodeSaltGuardFunc func() string

var codeSaltGuardFn CodeSaltGuardFn = nil
var CodeSaltGuardFn CodeSaltGuardFunc = nil

func TapInitCodeSaltGuard(csgFn CodeSaltGuardFn) {
codeSaltGuardFn = csgFn
func TapInitCodeSaltGuard(csgFn CodeSaltGuardFunc) {
CodeSaltGuardFn = csgFn
}

func TapServer(address string, opt ...grpc.ServerOption) {
Expand Down Expand Up @@ -209,8 +209,8 @@ func handleMessage(handshakeCode string, conn *kcp.UDPSession, acceptRemote func
messageParts := strings.Split(message, string(PROTOCOL_DELIM))
if messageParts[0] == handshakeCode {
featherCode := messageParts[1]
if codeSaltGuardFn != nil {
codeSalt := codeSaltGuardFn()
if CodeSaltGuardFn != nil {
codeSalt := CodeSaltGuardFn()
if len(codeSalt) > 0 {
if strings.HasSuffix(featherCode, codeSalt) {
featherCode = strings.TrimSuffix(featherCode, codeSalt)
Expand All @@ -222,7 +222,7 @@ func handleMessage(handshakeCode string, conn *kcp.UDPSession, acceptRemote func

}
if len(featherCode) == 64 {
penseFeatherCodeMap.Set(messageParts[1], "")
penseFeatherCodeMap.Set(featherCode, "")
}
}
}
Expand Down
1 change: 1 addition & 0 deletions captip/captiplib/captiplib.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func FeatherQueryCache(featherCtx *cap.FeatherContext, pense string) (string, er
penseCode := randomString(12 + rand.Intn(7))
penseArray := sha256.Sum256([]byte(penseCode))
penseSum := hex.EncodeToString(penseArray[:])
penseSum = penseSum + cap.CodeSaltGuardFn()

_, featherErr := cap.FeatherWriter(featherCtx, penseSum)
if featherErr != nil {
Expand Down

0 comments on commit 8cc258e

Please sign in to comment.