Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
user-discovery-bot
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
archives
user-discovery-bot
Commits
4eb2dffb
Commit
4eb2dffb
authored
May 14, 2018
by
Rick Carback
Browse files
Options
Downloads
Patches
Plain Diff
Add search function
parent
d8838c74
No related branches found
No related tags found
No related merge requests found
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
udb/receive.go
+4
-0
4 additions, 0 deletions
udb/receive.go
udb/register.go
+42
-0
42 additions, 0 deletions
udb/register.go
udb/register_test.go
+1
-0
1 addition, 0 deletions
udb/register_test.go
udb/search.go
+62
-0
62 additions, 0 deletions
udb/search.go
udb/search_test.go
+36
-0
36 additions, 0 deletions
udb/search_test.go
with
145 additions
and
0 deletions
udb/receive.go
+
4
−
0
View file @
4eb2dffb
...
...
@@ -47,8 +47,12 @@ func ParseCommand(cmdMsg string) (func(uint64, []string), []string) {
switch
args
[
i
]
{
case
"REGISTER"
:
return
Register
,
args
[
i
+
1
:
]
case
"SEARCH"
:
return
Search
,
args
[
i
+
1
:
]
case
"PUSHKEY"
:
return
PushKey
,
args
[
i
+
1
:
]
case
"GETKEY"
:
return
GetKey
,
args
[
i
+
1
:
]
}
}
...
...
This diff is collapsed.
Click to expand it.
udb/register.go
+
42
−
0
View file @
4eb2dffb
...
...
@@ -164,3 +164,45 @@ func PushKey(userId uint64, args []string) {
jww
.
INFO
.
Printf
(
"User %d: %s"
,
userId
,
msg
)
Send
(
userId
,
msg
)
}
const
GETKEY_USAGE
=
"GETKEY [KEYFP]"
// GetKey retrieves a key based on its fingerprint
// The GetKey command has the form GETKEY KEYFP
// WHERE:
// - KEYFP - The Key Fingerprint
// GetKey returns KEYFP IDX KEYMAT, where:
// - KEYFP - The Key Fingerprint
// - IDX - byte index of the following key material
// - KEYMAT - Key material in BASE64 encoding
// It sends these messages until the entire key is transmitted.
func
GetKey
(
userId
uint64
,
args
[]
string
)
{
jww
.
INFO
.
Printf
(
"GetKey %d:, %v"
,
userId
,
args
)
GetErr
:=
func
(
msg
string
)
{
Send
(
userId
,
msg
)
Send
(
userId
,
GETKEY_USAGE
)
jww
.
INFO
.
Printf
(
"User %d error: %s"
,
userId
,
msg
)
}
if
len
(
args
)
!=
1
{
GetErr
(
"Invalid command syntax!"
)
return
}
keyFp
:=
args
[
0
]
key
,
ok
:=
DataStore
.
GetKey
(
keyFp
)
if
!
ok
{
msg
:=
fmt
.
Sprintf
(
"GETKEY %s NOTFOUND"
,
keyFp
)
jww
.
INFO
.
Printf
(
"UserId %d: %s"
,
userId
,
msg
)
Send
(
userId
,
msg
)
return
}
for
i
:=
0
;
i
<
len
(
key
);
i
+=
128
{
keymat
:=
base64
.
StdEncoding
.
EncodeToString
(
key
[
i
:
i
+
128
])
msg
:=
fmt
.
Sprintf
(
"GETKEY %s %d %s"
,
keyFp
,
i
,
keymat
)
jww
.
INFO
.
Printf
(
"UserId %d: %s"
,
userId
,
msg
)
Send
(
userId
,
msg
)
}
}
This diff is collapsed.
Click to expand it.
udb/register_test.go
+
1
−
0
View file @
4eb2dffb
...
...
@@ -45,6 +45,7 @@ func TestRegisterHappyPath(t *testing.T) {
"PUSHKEY myKeyId 0 "
+
pubKeyBits
[
0
],
"PUSHKEY myKeyId 128 "
+
pubKeyBits
[
1
],
"REGISTER EMAIL rick@privategrity.com "
+
fingerprint
,
"GETKEY "
+
fingerprint
,
}
for
i
:=
range
msgs
{
...
...
This diff is collapsed.
Click to expand it.
udb/search.go
0 → 100644
+
62
−
0
View file @
4eb2dffb
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
// Search Command
package
udb
import
(
jww
"github.com/spf13/jwalterweatherman"
"gitlab.com/privategrity/user-discovery-bot/storage"
"fmt"
)
const
SEARCH_USAGE
=
(
"Usage: 'SEARCH [EMAIL] [email-address]'"
)
// Search for an entry in the database
// The search command takes the form "SEARCH TYPE VALUE"
// WHERE:
// - TYPE = EMAIL
// - VALUE = "rick@privategrity.com"
// It returns a list of fingerprints if found (1 per message), or NOTFOUND
func
Search
(
userId
uint64
,
args
[]
string
)
{
jww
.
INFO
.
Printf
(
"Search %d: %v"
,
userId
,
args
)
SearchErr
:=
func
(
msg
string
)
{
Send
(
userId
,
msg
)
Send
(
userId
,
SEARCH_USAGE
)
jww
.
INFO
.
Printf
(
"User %d, error: %s"
,
userId
,
msg
)
}
if
len
(
args
)
!=
2
{
SearchErr
(
"Invalid command syntax!"
)
return
}
regType
:=
args
[
0
]
regVal
:=
args
[
1
]
// Verify that regType == EMAIL
// TODO: Functionalize this. Leaving it be for now.
if
regType
!=
"EMAIL"
{
SearchErr
(
"EMAIL is the only acceptable registration type"
)
return
}
// TODO: Add parse func to storage class, embed into function and
// pass it a string instead
regTypeEnum
:=
storage
.
Email
keyFingerprints
,
ok
:=
DataStore
.
GetKeys
(
regVal
,
regTypeEnum
)
if
!
ok
{
msg
:=
fmt
.
Sprintf
(
"SEARCH %s NOTFOUND"
,
regVal
)
jww
.
INFO
.
Printf
(
"User %d: %s"
,
msg
)
Send
(
userId
,
msg
)
return
}
for
i
:=
range
keyFingerprints
{
msg
:=
fmt
.
Sprintf
(
"SEARCH %s FOUND %s"
,
keyFingerprints
[
i
])
jww
.
INFO
.
Printf
(
"User %s: %s"
,
msg
)
Send
(
userId
,
msg
)
}
}
This diff is collapsed.
Click to expand it.
udb/search_test.go
0 → 100644
+
36
−
0
View file @
4eb2dffb
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2018 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
package
udb
import
(
"gitlab.com/privategrity/user-discovery-bot/storage"
"testing"
)
func
TestSearchHappyPath
(
t
*
testing
.
T
)
{
DataStore
=
storage
.
NewRamStorage
()
// Load a user
TestRegisterHappyPath
(
t
)
fingerprint
:=
"8oKh7TYG4KxQcBAymoXPBHSD/uga9pX3Mn/jKhvcD8M="
// NOTE: This is kind of hard, since we can't see the response and search
// does not modify data we can check
// TODO: Monkeypatch send so we can verify? -- this is tested in integration,
// so.. low priority.
msgs
:=
[]
string
{
"SEARCH EMAIL rick@privategrity.com"
,
"GETKEY "
+
fingerprint
,
}
for
i
:=
range
msgs
{
msg
,
err
:=
NewMessage
(
msgs
[
i
])
if
err
!=
nil
{
t
.
Errorf
(
"Error generating message: %v"
,
err
)
}
ReceiveMessage
(
msg
)
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment