2019-11-15 23:57:40 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-08-11 15:50:01 +01:00
|
|
|
// ErrFileSource is an error class for file source errors.
|
2019-11-15 23:57:40 +00:00
|
|
|
ErrFileSource = errs.Class("file source")
|
|
|
|
)
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// FileSource represents a trust source contained in a file on disk.
|
2019-11-15 23:57:40 +00:00
|
|
|
type FileSource struct {
|
|
|
|
path string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFileSource creates a new FileSource that loads a trust list from the
|
|
|
|
// given path.
|
|
|
|
func NewFileSource(path string) *FileSource {
|
|
|
|
return &FileSource{
|
|
|
|
path: path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// String implements the Source interface and returns the FileSource URL.
|
2019-11-15 23:57:40 +00:00
|
|
|
func (source *FileSource) String() string {
|
|
|
|
return source.path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Static implements the Source interface. It returns true.
|
|
|
|
func (source *FileSource) Static() bool { return true }
|
|
|
|
|
|
|
|
// FetchEntries implements the Source interface and returns entries from a
|
|
|
|
// the file source on disk. The entries returned are authoritative.
|
|
|
|
func (source *FileSource) FetchEntries(ctx context.Context) (_ []Entry, err error) {
|
|
|
|
urls, err := LoadSatelliteURLList(ctx, source.path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var entries []Entry
|
|
|
|
for _, url := range urls {
|
|
|
|
entries = append(entries, Entry{
|
|
|
|
SatelliteURL: url,
|
|
|
|
Authoritative: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// LoadSatelliteURLList loads a list of Satellite URLs from a path on disk.
|
2019-11-15 23:57:40 +00:00
|
|
|
func LoadSatelliteURLList(ctx context.Context, path string) (_ []SatelliteURL, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFileSource.Wrap(err)
|
|
|
|
}
|
|
|
|
defer func() { err = errs.Combine(err, f.Close()) }()
|
|
|
|
|
|
|
|
urls, err := ParseSatelliteURLList(ctx, f)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrFileSource.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls, nil
|
|
|
|
}
|