Addressing all Comments; Renaming ID to CertID

This commit is contained in:
Diogo Monica 2015-06-21 18:23:43 -04:00
parent ac881bb381
commit 2e96f13ded
5 changed files with 43 additions and 43 deletions

View File

@ -21,12 +21,14 @@ type FileStore interface {
ListGUN(gun string) []string
}
// fileStore implements FileStore
type fileStore struct {
baseDir string
fileExt string
perms os.FileMode
}
// NewFileStore creates a directory with 755 permissions
func NewFileStore(baseDir string, fileExt string) (FileStore, error) {
if err := CreateDirectory(baseDir); err != nil {
return nil, err
@ -39,6 +41,7 @@ func NewFileStore(baseDir string, fileExt string) (FileStore, error) {
}, nil
}
// NewPrivateFileStore creates a directory with 700 permissions
func NewPrivateFileStore(baseDir string, fileExt string) (FileStore, error) {
if err := CreatePrivateDirectory(baseDir); err != nil {
return nil, err
@ -51,25 +54,21 @@ func NewPrivateFileStore(baseDir string, fileExt string) (FileStore, error) {
}, nil
}
// Add writes data to a file with a given name
func (f *fileStore) Add(name string, data []byte) error {
filePath := f.genFilePath(name)
createDirectory(filepath.Dir(filePath), f.perms)
if err := ioutil.WriteFile(filePath, data, f.perms); err != nil {
return err
}
return nil
return ioutil.WriteFile(filePath, data, f.perms)
}
// Remove removes a file identified by a name
// TODO (diogo): We can get rid of RemoveGUN by merging with Remove
func (f *fileStore) Remove(name string) error {
filePath := f.genFilePath(name)
if err := os.Remove(filePath); err != nil {
return err
}
return nil
return os.Remove(filePath)
}
// RemoveGUN removes a directory identified by the Global Unique Name
func (f *fileStore) RemoveGUN(gun string) error {
dirPath := filepath.Join(f.baseDir, gun)
@ -84,13 +83,10 @@ func (f *fileStore) RemoveGUN(gun string) error {
return fmt.Errorf("GUN not found: %s", gun)
}
if err := os.RemoveAll(dirPath); err != nil {
return err
}
return nil
return os.RemoveAll(dirPath)
}
// GetData returns the data given a file name
func (f *fileStore) GetData(name string) ([]byte, error) {
filePath := f.genFilePath(name)
data, err := ioutil.ReadFile(filePath)
@ -101,22 +97,27 @@ func (f *fileStore) GetData(name string) ([]byte, error) {
return data, nil
}
// GetPath returns the full final path of a file with a given name
func (f *fileStore) GetPath(name string) string {
return f.genFilePath(name)
}
// List lists all the files inside of a store
func (f *fileStore) List() []string {
return f.listGUN(f.baseDir)
return f.list(f.baseDir)
}
// ListGUN lists all the files inside of a directory identified by a Global Unique Name.
// TODO (diogo): We can get rid of ListGUN by merging with List
func (f *fileStore) ListGUN(gun string) []string {
gunPath := filepath.Join(f.baseDir, gun)
return f.listGUN(gunPath)
return f.list(gunPath)
}
func (f *fileStore) listGUN(gunPath string) []string {
// listGUN lists all the files in a directory given a full path
func (f *fileStore) list(path string) []string {
files := make([]string, 0, 0)
filepath.Walk(gunPath, func(fp string, fi os.FileInfo, err error) error {
filepath.Walk(path, func(fp string, fi os.FileInfo, err error) error {
// If there are errors, ignore this particular file
if err != nil {
return nil
@ -136,16 +137,18 @@ func (f *fileStore) listGUN(gunPath string) []string {
return files
}
// genFilePath returns the full path with extension given a file name
func (f *fileStore) genFilePath(name string) string {
fileName := fmt.Sprintf("%s.%s", name, f.fileExt)
filePath := filepath.Join(f.baseDir, fileName)
return filePath
return filepath.Join(f.baseDir, fileName)
}
// CreateDirectory uses createDirectory to create a chmod 755 Directory
func CreateDirectory(dir string) error {
return createDirectory(dir, visible)
}
// CreatePrivateDirectory uses createDirectory to create a chmod 700 Directory
func CreatePrivateDirectory(dir string) error {
return createDirectory(dir, private)
}
@ -157,8 +160,5 @@ func createDirectory(dir string, perms os.FileMode) error {
// This prevents someone passing /path/to/dir and 'dir' not being created
// If two '//' exist, MkdirAll deals it with correctly
dir = dir + "/"
if err := os.MkdirAll(dir, perms); err != nil {
return err
}
return nil
return os.MkdirAll(dir, perms)
}

View File

@ -10,9 +10,9 @@ import (
// X509FileStore implements X509Store that persists on disk
type X509FileStore struct {
validate Validator
fileMap map[ID]string
fingerprintMap map[ID]*x509.Certificate
nameMap map[string][]ID
fileMap map[CertID]string
fingerprintMap map[CertID]*x509.Certificate
nameMap map[string][]CertID
fileStore FileStore
}
@ -36,9 +36,9 @@ func newX509FileStore(directory string, validate func(*x509.Certificate) bool) (
s := &X509FileStore{
validate: ValidatorFunc(validate),
fileMap: make(map[ID]string),
fingerprintMap: make(map[ID]*x509.Certificate),
nameMap: make(map[string][]ID),
fileMap: make(map[CertID]string),
fingerprintMap: make(map[CertID]*x509.Certificate),
nameMap: make(map[string][]CertID),
fileStore: fileStore,
}
@ -178,7 +178,7 @@ func (s X509FileStore) GetCertificateBykID(hexkID string) (*x509.Certificate, er
}
// Check to see if this subject key identifier exists
if cert, ok := s.fingerprintMap[ID(hexkID)]; ok {
if cert, ok := s.fingerprintMap[CertID(hexkID)]; ok {
return cert, nil
}

View File

@ -9,8 +9,8 @@ import (
// X509MemStore implements X509Store as an in-memory object with no persistence
type X509MemStore struct {
validate Validator
fingerprintMap map[ID]*x509.Certificate
nameMap map[string][]ID
fingerprintMap map[CertID]*x509.Certificate
nameMap map[string][]CertID
}
// NewX509MemStore returns a new X509MemStore.
@ -19,8 +19,8 @@ func NewX509MemStore() *X509MemStore {
return &X509MemStore{
validate: validate,
fingerprintMap: make(map[ID]*x509.Certificate),
nameMap: make(map[string][]ID),
fingerprintMap: make(map[CertID]*x509.Certificate),
nameMap: make(map[string][]CertID),
}
}
@ -30,8 +30,8 @@ func NewX509FilteredMemStore(validate func(*x509.Certificate) bool) *X509MemStor
s := &X509MemStore{
validate: ValidatorFunc(validate),
fingerprintMap: make(map[ID]*x509.Certificate),
nameMap: make(map[string][]ID),
fingerprintMap: make(map[CertID]*x509.Certificate),
nameMap: make(map[string][]CertID),
}
return s
@ -147,7 +147,7 @@ func (s X509MemStore) GetCertificateBykID(hexkID string) (*x509.Certificate, err
}
// Check to see if this subject key identifier exists
if cert, ok := s.fingerprintMap[ID(hexkID)]; ok {
if cert, ok := s.fingerprintMap[CertID(hexkID)]; ok {
return cert, nil
}

View File

@ -16,7 +16,7 @@ type X509Store interface {
GetVerifyOptions(dnsName string) (x509.VerifyOptions, error)
}
type ID string
type CertID string
// Validator is a convenience type to create validating function that filters
// certificates that get added to the store

View File

@ -83,14 +83,14 @@ func loadCertFromPEM(pemBytes []byte) (*x509.Certificate, error) {
return nil, errors.New("no certificates found in PEM data")
}
func FingerprintCert(cert *x509.Certificate) ID {
func FingerprintCert(cert *x509.Certificate) CertID {
block := pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}
pemdata := string(pem.EncodeToMemory(&block))
// Create new TUF Key so we can compute the TUF-compliant ID
// Create new TUF Key so we can compute the TUF-compliant CertID
tufKey := data.NewTUFKey("RSA", pemdata, "")
return ID(tufKey.ID())
return CertID(tufKey.ID())
}
// loadCertsFromDir receives a store AddCertFromFile for each certificate found