1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
package kms
import (
"bytes"
"encoding/pem"
"io"
"io/fs"
"sync"
"time"
"go.step.sm/crypto/pemutil"
)
// object implements the fs.File and fs.FileMode interfaces.
type object struct {
Path string
Object interface{}
once sync.Once
err error
pemData *bytes.Buffer
}
// FileMode implementation
func (o *object) Name() string { return o.Path }
func (o *object) Size() int64 { return int64(o.pemData.Len()) }
func (o *object) Mode() fs.FileMode { return 0400 }
func (o *object) ModTime() time.Time { return time.Time{} }
func (o *object) IsDir() bool { return false }
func (o *object) Sys() interface{} { return o.Object }
func (o *object) load() error {
o.once.Do(func() {
b, err := pemutil.Serialize(o.Object)
if err != nil {
o.err = &fs.PathError{
Op: "open",
Path: o.Path,
Err: err,
}
return
}
o.pemData = bytes.NewBuffer(pem.EncodeToMemory(b))
})
return o.err
}
func (o *object) Stat() (fs.FileInfo, error) {
if err := o.load(); err != nil {
return nil, err
}
return o, nil
}
func (o *object) Read(b []byte) (int, error) {
if err := o.load(); err != nil {
return 0, err
}
return o.pemData.Read(b)
}
func (o *object) Close() error {
o.Object = nil
o.pemData = nil
if o.err == nil {
o.err = io.EOF
return nil
}
return o.err
}
|