diff options
Diffstat (limited to 'pkg')
-rw-r--r-- | pkg/dns/dns.go | 40 | ||||
-rw-r--r-- | pkg/instance/endpoint.go | 2 | ||||
-rw-r--r-- | pkg/instance/endpoint_test.go | 48 | ||||
-rw-r--r-- | pkg/instance/instance.go | 10 | ||||
-rw-r--r-- | pkg/mocks/sigsum_dns.go | 49 |
5 files changed, 131 insertions, 18 deletions
diff --git a/pkg/dns/dns.go b/pkg/dns/dns.go new file mode 100644 index 0000000..7979119 --- /dev/null +++ b/pkg/dns/dns.go @@ -0,0 +1,40 @@ +package dns + +import ( + "context" + "fmt" + "net" + + "encoding/hex" + + "git.sigsum.org/sigsum-log-go/pkg/types" +) + +// Verifier can verify that a domain name is aware of a public key +type Verifier interface { + Verify(ctx context.Context, name string, key *[types.VerificationKeySize]byte) error +} + +// DefaultResolver implements the Verifier interface with Go's default resolver +type DefaultResolver struct { + resolver net.Resolver +} + +func NewDefaultResolver() Verifier { + return &DefaultResolver{} +} + +func (dr *DefaultResolver) Verify(ctx context.Context, name string, key *[types.VerificationKeySize]byte) error { + rsp, err := dr.resolver.LookupTXT(ctx, name) + if err != nil { + return fmt.Errorf("domain name look-up failed: %v", err) + } + + want := hex.EncodeToString(types.Hash(key[:])[:]) + for _, got := range rsp { + if got == want { + return nil + } + } + return fmt.Errorf("%q is not aware of key hash %q", name, want) +} diff --git a/pkg/instance/endpoint.go b/pkg/instance/endpoint.go index 2387263..a6d424d 100644 --- a/pkg/instance/endpoint.go +++ b/pkg/instance/endpoint.go @@ -9,7 +9,7 @@ import ( func addLeaf(ctx context.Context, i *Instance, w http.ResponseWriter, r *http.Request) (int, error) { glog.V(3).Info("handling add-entry request") - req, err := i.leafRequestFromHTTP(r) + req, err := i.leafRequestFromHTTP(ctx, r) if err != nil { return http.StatusBadRequest, err } diff --git a/pkg/instance/endpoint_test.go b/pkg/instance/endpoint_test.go index 3ca72b2..29d5a8e 100644 --- a/pkg/instance/endpoint_test.go +++ b/pkg/instance/endpoint_test.go @@ -10,9 +10,9 @@ import ( "net/http/httptest" "testing" - "github.com/golang/mock/gomock" "git.sigsum.org/sigsum-log-go/pkg/mocks" "git.sigsum.org/sigsum-log-go/pkg/types" + "github.com/golang/mock/gomock" ) var ( @@ -72,11 +72,13 @@ func TestAddLeaf(t *testing.T) { )) } for _, table := range []struct { - description string - ascii io.Reader // buffer used to populate HTTP request - expect bool // set if a mock answer is expected - err error // error from Trillian client - wantCode int // HTTP status ok + description string + ascii io.Reader // buffer used to populate HTTP request + expectTrillian bool // expect Trillian client code path + errTrillian error // error from Trillian client + expectDNS bool // expect DNS verifier code path + errDNS error // error from DNS verifier + wantCode int // HTTP status ok }{ // XXX introduce helper so that test params are not hardcoded { @@ -103,7 +105,7 @@ func TestAddLeaf(t *testing.T) { wantCode: http.StatusBadRequest, }, { - description: "invalid: bad request (shard hint is before shard start)", + description: "invalid: bad request (shard hint is after shard end)", ascii: buf(21, "0000000000000000000000000000000000000000000000000000000000000000", "79c14f0ad9ab24ab98fe9d5ff59c3b91348789758aa092c6bfab2ac8890b41fb1d44d985e723184f9de42edb82b5ada14f494a96e361914d5366dd92379a1d04", @@ -112,15 +114,27 @@ func TestAddLeaf(t *testing.T) { wantCode: http.StatusBadRequest, }, { + description: "invalid: failed verifying domain hint", + ascii: buf(10, + "0000000000000000000000000000000000000000000000000000000000000000", + "7df253d2578c6c20b90832245ad6f981077454667796b3d507336a89ee878a2eae6b96e6d8de84fe8c1acf4b3aaffd482b657b65d94ed5e6be6320492147f90c", + "f6eef8e94ddf1396682871257e670a1d9b627cf460daade7c36d218b2866befb", + ), + expectDNS: true, + errDNS: fmt.Errorf("something went wrong"), + wantCode: http.StatusBadRequest, + }, + { description: "invalid: backend failure", ascii: buf(10, "0000000000000000000000000000000000000000000000000000000000000000", "7df253d2578c6c20b90832245ad6f981077454667796b3d507336a89ee878a2eae6b96e6d8de84fe8c1acf4b3aaffd482b657b65d94ed5e6be6320492147f90c", "f6eef8e94ddf1396682871257e670a1d9b627cf460daade7c36d218b2866befb", ), - expect: true, - err: fmt.Errorf("something went wrong"), - wantCode: http.StatusInternalServerError, + expectDNS: true, + expectTrillian: true, + errTrillian: fmt.Errorf("something went wrong"), + wantCode: http.StatusInternalServerError, }, { description: "valid", @@ -129,21 +143,27 @@ func TestAddLeaf(t *testing.T) { "7df253d2578c6c20b90832245ad6f981077454667796b3d507336a89ee878a2eae6b96e6d8de84fe8c1acf4b3aaffd482b657b65d94ed5e6be6320492147f90c", "f6eef8e94ddf1396682871257e670a1d9b627cf460daade7c36d218b2866befb", ), - expect: true, - wantCode: http.StatusOK, + expectDNS: true, + expectTrillian: true, + wantCode: http.StatusOK, }, } { // Run deferred functions at the end of each iteration func() { ctrl := gomock.NewController(t) defer ctrl.Finish() + dns := mocks.NewMockVerifier(ctrl) + if table.expectDNS { + dns.EXPECT().Verify(gomock.Any(), gomock.Any(), gomock.Any()).Return(table.errDNS) + } client := mocks.NewMockClient(ctrl) - if table.expect { - client.EXPECT().AddLeaf(gomock.Any(), gomock.Any()).Return(table.err) + if table.expectTrillian { + client.EXPECT().AddLeaf(gomock.Any(), gomock.Any()).Return(table.errTrillian) } i := Instance{ Config: testConfig, Client: client, + DNS: dns, } // Create HTTP request diff --git a/pkg/instance/instance.go b/pkg/instance/instance.go index 31a9b73..fbfe4df 100644 --- a/pkg/instance/instance.go +++ b/pkg/instance/instance.go @@ -8,10 +8,11 @@ import ( "net/http" "time" - "github.com/golang/glog" + "git.sigsum.org/sigsum-log-go/pkg/dns" "git.sigsum.org/sigsum-log-go/pkg/state" "git.sigsum.org/sigsum-log-go/pkg/trillian" "git.sigsum.org/sigsum-log-go/pkg/types" + "github.com/golang/glog" ) // Config is a collection of log parameters @@ -35,6 +36,7 @@ type Instance struct { Client trillian.Client // provides access to the Trillian backend Signer crypto.Signer // provides access to Ed25519 private key Stateman state.StateManager // coordinates access to (co)signed tree heads + DNS dns.Verifier // checks if domain name knows a public key } // Handler implements the http.Handler interface, and contains a reference @@ -92,7 +94,7 @@ func (a Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { } } -func (i *Instance) leafRequestFromHTTP(r *http.Request) (*types.LeafRequest, error) { +func (i *Instance) leafRequestFromHTTP(ctx context.Context, r *http.Request) (*types.LeafRequest, error) { var req types.LeafRequest if err := req.UnmarshalASCII(r.Body); err != nil { return nil, fmt.Errorf("UnmarshalASCII: %v", err) @@ -110,7 +112,9 @@ func (i *Instance) leafRequestFromHTTP(r *http.Request) (*types.LeafRequest, err if req.ShardHint > i.ShardEnd { return nil, fmt.Errorf("invalid shard hint: %d not in [%d, %d]", req.ShardHint, i.ShardStart, i.ShardEnd) } - // TODO: check domain hint + if err := i.DNS.Verify(ctx, req.DomainHint, req.VerificationKey); err != nil { + return nil, fmt.Errorf("invalid domain hint: %v", err) + } return &req, nil } diff --git a/pkg/mocks/sigsum_dns.go b/pkg/mocks/sigsum_dns.go new file mode 100644 index 0000000..ede237e --- /dev/null +++ b/pkg/mocks/sigsum_dns.go @@ -0,0 +1,49 @@ +// Code generated by MockGen. DO NOT EDIT. +// Source: git.sigsum.org/sigsum-log-go/pkg/dns (interfaces: Verifier) + +// Package mocks is a generated GoMock package. +package mocks + +import ( + context "context" + reflect "reflect" + + gomock "github.com/golang/mock/gomock" +) + +// MockVerifier is a mock of Verifier interface. +type MockVerifier struct { + ctrl *gomock.Controller + recorder *MockVerifierMockRecorder +} + +// MockVerifierMockRecorder is the mock recorder for MockVerifier. +type MockVerifierMockRecorder struct { + mock *MockVerifier +} + +// NewMockVerifier creates a new mock instance. +func NewMockVerifier(ctrl *gomock.Controller) *MockVerifier { + mock := &MockVerifier{ctrl: ctrl} + mock.recorder = &MockVerifierMockRecorder{mock} + return mock +} + +// EXPECT returns an object that allows the caller to indicate expected use. +func (m *MockVerifier) EXPECT() *MockVerifierMockRecorder { + return m.recorder +} + +// Verify mocks base method. +func (m *MockVerifier) Verify(arg0 context.Context, arg1 string, arg2 *[32]byte) error { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "Verify", arg0, arg1, arg2) + ret0, _ := ret[0].(error) + return ret0 +} + +// Verify indicates an expected call of Verify. +func (mr *MockVerifierMockRecorder) Verify(arg0, arg1, arg2 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Verify", reflect.TypeOf((*MockVerifier)(nil).Verify), arg0, arg1, arg2) +} |