Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
G
go-ipfs
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
jihao
go-ipfs
Commits
dbedee59
提交
dbedee59
authored
1月 31, 2018
作者:
Dirk McCormick
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
namesys: differentiate between validation errors
License: MIT Signed-off-by:
Dirk McCormick
<
dirkmdev@gmail.com
>
上级
e980e68f
隐藏空白字符变更
内嵌
并排
正在显示
4 个修改的文件
包含
78 行增加
和
6 行删除
+78
-6
ipns_validate_test.go
namesys/ipns_validate_test.go
+57
-1
routing.go
namesys/routing.go
+3
-1
selector.go
namesys/selector.go
+2
-0
validator.go
namesys/validator.go
+16
-4
没有找到文件。
namesys/ipns_validate_test.go
浏览文件 @
dbedee59
...
...
@@ -2,6 +2,7 @@ package namesys
import
(
"context"
"fmt"
"testing"
"time"
...
...
@@ -21,7 +22,62 @@ import (
ci
"gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
)
func
TestValidation
(
t
*
testing
.
T
)
{
func
testValidatorCase
(
t
*
testing
.
T
,
priv
ci
.
PrivKey
,
kbook
pstore
.
KeyBook
,
ns
string
,
key
string
,
val
[]
byte
,
eol
time
.
Time
,
exp
error
)
{
validChecker
:=
NewIpnsRecordValidator
(
kbook
)
p
:=
path
.
Path
(
"/ipfs/QmfM2r8seH2GiRaC4esTjeraXEachRt8ZsSeGaWTPLyMoG"
)
entry
,
err
:=
CreateRoutingEntryData
(
priv
,
p
,
1
,
eol
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
data
:=
val
if
data
==
nil
{
data
,
err
=
proto
.
Marshal
(
entry
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
rec
:=
&
record
.
ValidationRecord
{
Namespace
:
ns
,
Key
:
key
,
Value
:
data
,
}
err
=
validChecker
.
Func
(
rec
)
if
err
!=
exp
{
params
:=
fmt
.
Sprintf
(
"namespace: %s
\n
key: %s
\n
eol: %s
\n
"
,
ns
,
key
,
eol
)
if
exp
==
nil
{
t
.
Fatalf
(
"Unexpected error %s for params %s"
,
err
,
params
)
}
else
if
err
==
nil
{
t
.
Fatalf
(
"Expected error %s but there was no error for params %s"
,
exp
,
params
)
}
else
{
t
.
Fatalf
(
"Expected error %s but got %s for params %s"
,
exp
,
err
,
params
)
}
}
}
func
TestValidator
(
t
*
testing
.
T
)
{
ts
:=
time
.
Now
()
priv
,
id
,
_
,
_
:=
genKeys
(
t
)
priv2
,
id2
,
_
,
_
:=
genKeys
(
t
)
kbook
:=
pstore
.
NewPeerstore
()
kbook
.
AddPubKey
(
id
,
priv
.
GetPublic
())
emptyKbook
:=
pstore
.
NewPeerstore
()
testValidatorCase
(
t
,
priv
,
kbook
,
"ipns"
,
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
),
nil
)
testValidatorCase
(
t
,
priv
,
kbook
,
"ipns"
,
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
*-
1
),
ErrExpiredRecord
)
testValidatorCase
(
t
,
priv
,
kbook
,
"ipns"
,
string
(
id
),
[]
byte
(
"bad data"
),
ts
.
Add
(
time
.
Hour
),
ErrBadRecord
)
testValidatorCase
(
t
,
priv
,
kbook
,
"ipns"
,
"bad key"
,
nil
,
ts
.
Add
(
time
.
Hour
),
ErrKeyFormat
)
testValidatorCase
(
t
,
priv
,
emptyKbook
,
"ipns"
,
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
),
ErrPublicKeyNotFound
)
testValidatorCase
(
t
,
priv2
,
kbook
,
"ipns"
,
string
(
id2
),
nil
,
ts
.
Add
(
time
.
Hour
),
ErrPublicKeyNotFound
)
testValidatorCase
(
t
,
priv2
,
kbook
,
"ipns"
,
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
),
ErrSignature
)
testValidatorCase
(
t
,
priv
,
kbook
,
""
,
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
),
ErrInvalidPath
)
testValidatorCase
(
t
,
priv
,
kbook
,
"wrong"
,
string
(
id
),
nil
,
ts
.
Add
(
time
.
Hour
),
ErrInvalidPath
)
}
func
TestResolverValidation
(
t
*
testing
.
T
)
{
ctx
:=
context
.
Background
()
rid
:=
testutil
.
RandIdentityOrFatal
(
t
)
dstore
:=
dssync
.
MutexWrap
(
ds
.
NewMapDatastore
())
...
...
namesys/routing.go
浏览文件 @
dbedee59
...
...
@@ -147,7 +147,9 @@ func (r *routingResolver) resolveOnce(ctx context.Context, name string) (path.Pa
return
""
,
err
}
// use the routing system to get the name.
// Use the routing system to get the name.
// Note that the DHT will call the ipns validator when retrieving
// the value, which in turn verifies the ipns record signature
_
,
ipnsKey
:=
IpnsKeysForID
(
pid
)
val
,
err
:=
r
.
routing
.
GetValue
(
ctx
,
ipnsKey
)
if
err
!=
nil
{
...
...
namesys/selector.go
浏览文件 @
dbedee59
...
...
@@ -42,11 +42,13 @@ func selectRecord(recs []*pb.IpnsEntry, vals [][]byte) (int, error) {
}
else
if
r
.
GetSequence
()
==
bestSeq
{
rt
,
err
:=
u
.
ParseRFC3339
(
string
(
r
.
GetValidity
()))
if
err
!=
nil
{
log
.
Errorf
(
"failed to parse ipns record EOL %s"
,
r
.
GetValidity
())
continue
}
bestt
,
err
:=
u
.
ParseRFC3339
(
string
(
recs
[
besti
]
.
GetValidity
()))
if
err
!=
nil
{
log
.
Errorf
(
"failed to parse ipns record EOL %s"
,
recs
[
besti
]
.
GetValidity
())
continue
}
...
...
namesys/validator.go
浏览文件 @
dbedee59
...
...
@@ -29,6 +29,18 @@ var ErrInvalidPath = errors.New("record path invalid")
// signature verification
var
ErrSignature
=
errors
.
New
(
"record signature verification failed"
)
// ErrBadRecord should be returned when an ipns record cannot be unmarshalled
var
ErrBadRecord
=
errors
.
New
(
"record could not be unmarshalled"
)
// ErrKeyFormat should be returned when an ipns record key is
// incorrectly formatted (not a peer ID)
var
ErrKeyFormat
=
errors
.
New
(
"record key could not be parsed into peer ID"
)
// ErrPublicKeyNotFound should be returned when the public key
// corresponding to the ipns record path cannot be retrieved
// from the peer store
var
ErrPublicKeyNotFound
=
errors
.
New
(
"public key not found in peer store"
)
// NewIpnsRecordValidator returns a ValidChecker for IPNS records
// The validator function will get a public key from the KeyBook
// to verify the record's signature
...
...
@@ -44,19 +56,19 @@ func NewIpnsRecordValidator(kbook pstore.KeyBook) *record.ValidChecker {
entry
:=
new
(
pb
.
IpnsEntry
)
err
:=
proto
.
Unmarshal
(
r
.
Value
,
entry
)
if
err
!=
nil
{
return
err
return
ErrBadRecord
}
// Get the public key defined by the ipns path
pid
,
err
:=
peer
.
IDFromString
(
r
.
Key
)
if
err
!=
nil
{
log
.
Debugf
(
"failed to parse ipns record key %s into p
ublic key hash
"
,
r
.
Key
)
return
Err
Signature
log
.
Debugf
(
"failed to parse ipns record key %s into p
eer ID
"
,
r
.
Key
)
return
Err
KeyFormat
}
pubk
:=
kbook
.
PubKey
(
pid
)
if
pubk
==
nil
{
log
.
Debugf
(
"public key with hash %s not found in peer store"
,
pid
)
return
Err
Signature
return
Err
PublicKeyNotFound
}
// Check the ipns record signature with the public key
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论