2019-01-02 10:23:25 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/peertls"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Options holds config, identity, and peer verification function data for use with a grpc server.
|
|
|
|
type Options struct {
|
|
|
|
Config Config
|
|
|
|
Ident *identity.FullIdentity
|
|
|
|
RevDB *peertls.RevocationDB
|
|
|
|
PCVFuncs []peertls.PeerCertVerificationFunc
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewOptions is a constructor for `serverOptions` given an identity and config
|
|
|
|
func NewOptions(i *identity.FullIdentity, c Config) (*Options, error) {
|
|
|
|
opts := &Options{
|
|
|
|
Config: c,
|
|
|
|
Ident: i,
|
|
|
|
}
|
|
|
|
|
|
|
|
err := opts.configure(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return opts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (opts *Options) grpcOpts() (grpc.ServerOption, error) {
|
|
|
|
return opts.Ident.ServerOption(opts.PCVFuncs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// configure adds peer certificate verification functions and revocation
|
|
|
|
// database to the config.
|
|
|
|
func (opts *Options) configure(c Config) (err error) {
|
|
|
|
var pcvs []peertls.PeerCertVerificationFunc
|
|
|
|
parseOpts := peertls.ParseExtOptions{}
|
|
|
|
|
2019-01-17 17:36:45 +00:00
|
|
|
if c.UsePeerCAWhitelist {
|
|
|
|
whitelist := []byte(DefaultPeerCAWhitelist)
|
|
|
|
if c.PeerCAWhitelistPath != "" {
|
|
|
|
whitelist, err = ioutil.ReadFile(c.PeerCAWhitelistPath)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("unable to find whitelist file %v: %v", c.PeerCAWhitelistPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
parsed, err := identity.DecodeAndParseChainPEM(whitelist)
|
2019-01-02 10:23:25 +00:00
|
|
|
if err != nil {
|
2019-01-17 17:36:45 +00:00
|
|
|
return Error.Wrap(err)
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
2019-01-17 17:36:45 +00:00
|
|
|
parseOpts.CAWhitelist = parsed
|
|
|
|
pcvs = append(pcvs, peertls.VerifyCAWhitelist(parsed))
|
2019-01-02 10:23:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.Extensions.Revocation {
|
|
|
|
opts.RevDB, err = peertls.NewRevDB(c.RevocationDBURL)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pcvs = append(pcvs, peertls.VerifyUnrevokedChainFunc(opts.RevDB))
|
|
|
|
}
|
|
|
|
|
|
|
|
exts := peertls.ParseExtensions(c.Extensions, parseOpts)
|
|
|
|
pcvs = append(pcvs, exts.VerifyFunc())
|
|
|
|
|
|
|
|
// NB: remove nil elements
|
|
|
|
for i, f := range pcvs {
|
|
|
|
if f == nil {
|
|
|
|
copy(pcvs[i:], pcvs[i+1:])
|
|
|
|
pcvs = pcvs[:len(pcvs)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
opts.PCVFuncs = pcvs
|
|
|
|
return nil
|
|
|
|
}
|