safe

package
v0.0.0-...-43c9aad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 32 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DefaultChangeLogWatch = time.Minute
	DefaultReplicaWatch   = 10 * time.Minute
)
View Source
const (
	MaxSizeForCompression = 1 << 26   // 64 MB
	MaxThumbnailSize      = 64 * 1024 // 64 KB
	MaxThumbnailWidth     = 512       // 512 px

	ErrInvalidName = "invalid name: %s should not start with /"
)
View Source
const (
	ConfigFolder   = "config"
	DataFolder     = "data"
	InitiateFolder = "initiate"
	HeaderFolder   = "h"
	BodyFolder     = "b"
	BucketFile     = ".bucket"
)
View Source
const (
	StoreMaster = 1
	StoreCDN    = 2
)
View Source
const (
	TestMemStoreUrl   = "mem://0"
	TestLocalStoreUrl = "file:///tmp"
)
View Source
const (
	ErrZoneNotExist    = "zone '%s' does not exist"               // Returned when a zone does not exist
	ErrZoneNameTooLong = "zone name '%s' too long, max length 32" // Returned when a zone name is too long
	ErrZoneExist       = "zone '%s' already exists"               //
	ErrNoAuth          = "user '%s['%s']' has not authorization for box '%s'"
	ErrUnauthorized    = "unauthorized: user '%s' has not rights to change permission for user '%s' from %d to %d for safe '%s'"
)
View Source
const KeySize = 32
View Source
const TestSafeName = "test-safe"

Variables

View Source
var CacheFolder string
View Source
var DefaultSyncUsersRefreshRate = 10 * time.Minute
View Source
var DelayForDestination = 100 * time.Millisecond // Delay before checking if the file is downloaded to the specified destination
View Source
var ErrFileNotExist = fmt.Errorf("file does not exist") // Returned when a file does not exist
View Source
var ErrInvalidHeaders = fmt.Errorf("headers are invalid")
View Source
var ErrInvalidTag = fmt.Errorf("invalid tag. Only alphanumeric characters are allowed")
View Source
var ErrNoEncryptionKey = fmt.Errorf("no encryption key")
View Source
var ErrNoStoreAvailable = fmt.Errorf("no store available")
View Source
var ErrSignatureMismatch = fmt.Errorf("provided signature does not match the expected identity")
View Source
var HousekeepingMaxDuration = time.Hour * 23
View Source
var IdentitiesFolder = ".identities"
View Source
var Identity1, Identity2 security.Identity
View Source
var MaxACLFilesInZone = 4
View Source
var MaxCacheSize = int64(128 * 1024 * 1024) // Maximum size of the cache
View Source
var MaxHeaderFileSize = 1024 * 1024 * 4 // 4MB
View Source
var MaxHeadersFiles = 16
View Source
var MergeBatchSize = 4

Functions

func AddStore

func AddStore(s *Safe, storeConfig StoreConfig) error

func CheckForUpdates

func CheckForUpdates(s *Safe, dir string, after time.Time, depth int) ([]string, error)

func Close

func Close(s *Safe)

func DeleteFile

func DeleteFile(s *Safe, bucket string, fileId uint64) error

func Federate

func Federate(names []string, buckets []string, options FederateOptions) error

func GetCached

func GetCached(name string, store storage.Store, key string, data any, creatorId string) (synced bool, err error)

GetCached returns the modification time of the guard file. If the creatorID is not empty, it returns true if the guard file creator has the same ID.

func InitTest

func InitTest()

func ListDirs

func ListDirs(s *Safe, bucket string, listDirsOptions ListDirsOptions) ([]string, error)

func SetCached

func SetCached(name string, store storage.Store, key string, value any, creatorId string) error

func SetUsers

func SetUsers(s *Safe, users Users, options SetUsersOptions) error

SetUsers sets some users with corresponding permissions for a zone.

func StartTestDB

func StartTestDB(t *testing.T, dbPath string)

func SyncBucket

func SyncBucket(s *Safe, bucket string, SyncOptions SyncOptions, async func(int, error)) (changes int, err error)

func SyncUsers

func SyncUsers(s *Safe) (int, error)

Types

type Admins

type Admins map[string]Level2

Admins defines the users who are administrators of a box and for each those that have level2, i.e. can add or remove other administrators

type Attributes

type Attributes struct {
	Hash        []byte         `json:"ha,omitempty"` // Hash of the file
	ContentType string         `json:"co,omitempty"` // Content type of the file
	Thumbnail   []byte         `json:"th,omitempty"` // Thumbnail of the file
	Tags        []string       `json:"ta,omitempty"` // Tags of the file
	Meta        map[string]any `json:"mt,omitempty"` // Extra attributes of the file
}

type Change

type Change struct {
	Type      ChangeType `json:"type"`
	By        string     `json:"by"`
	What      []byte     `json:"what"`
	ModTime   time.Time  `json:"modTime"`
	Signature []byte     `json:"signature"`
}

type ChangeLog

type ChangeLog struct {
	Changes []Change `json:"changes"`
}

type ChangeType

type ChangeType string
const (
	ChangePermission ChangeType = "permission"
	ChangeReplicas   ChangeType = "replicas"
)

type CompactHeader

type CompactHeader struct {
	BucketDir string
	NewKey    bool
}

type CreateOptions

type CreateOptions struct {
	Wipe            bool          `json:"wipe"`            // Wipe is true if the safe should be wiped before creating it
	Description     string        `json:"description"`     // Description of the safe
	ChangeLogWatch  time.Duration `json:"changeLogWatch"`  // ChangeLogWatch is the period for watching changes in the change log
	ReplicaWatch    time.Duration `json:"replicaWatch"`    // ReplicaWatch is the period for synchronizing replicas
	MinimalSyncTime time.Duration `json:"minimalSyncTime"` // MinimalSyncTime is the minimal time between syncs
}

type FederateOptions

type FederateOptions struct {
}

type GetOptions

type GetOptions struct {
	Progress    chan int64     `json:"progress"`    // Send progress updates to the channel
	FileId      uint64         `json:"fileId"`      // Get the file with the specified body ID
	NoCache     bool           `json:"noCache"`     // Do not cache the file
	CacheExpire time.Duration  `json:"cacheExpire"` // Cache expiration time
	Range       *storage.Range `json:"range"`       // Range of bytes to read
	NoSync      bool           `json:"noSync"`      // Do not sync the headers before getting the file
}
type Header struct {
	Name                string               `json:"na"`            // Full path of the file
	Creator             string               `json:"cr"`            // Creator of the file
	Size                int64                `json:"si"`            //	Size of the file
	ModTime             time.Time            `json:"mo"`            // Last modification time of the file
	FileId              uint64               `json:"fi"`            // ID used in the storage to identify the file
	IV                  []byte               `json:"iv"`            // IV used to encrypt the attributes
	Zip                 bool                 `json:"zi,omitempty"`  // True if the file is zipped
	Attributes          Attributes           `json:"at,omitempty"`  // Attributes of the file
	EncryptedAttributes []byte               `json:"en,omitempty"`  // Encrypted attributes of the file
	BodyKey             []byte               `json:"bo,omitempty"`  // Key used to encrypt the body
	PrivateId           string               `json:"pr,omitempty"`  // ID of the user in case of private encryption
	Deleted             bool                 `json:"de,omitempty"`  // True if the file is deleted
	Cached              string               `json:"ca,omitempty"`  // Location where the file is cached
	CachedExpires       time.Time            `json:"cac,omitempty"` // Time when the cache expires
	Uploading           bool                 `json:"up,omitempty"`  // Number of uploads retries
	SourceFile          string               `json:"so,omitempty"`  // Source of the file
	ReplaceId           uint64               `json:"re,omitempty"`  // ID of the file to replace
	Replace             bool                 `json:"rp,omitempty"`  // True if the file is replacing another file
	Downloads           map[string]time.Time `json:"do,omitempty"`  // Map of download locations and times
}

func Get

func Get(s *Safe, bucket, name string, dest any, options GetOptions) (Header, error)

Get reads a file from a bucket. The destination can be a filename (string) or an io.Writer. If the destination is nil, the file is not written but the header is returned.

func ListFiles

func ListFiles(s *Safe, bucket string, listOptions ListOptions) ([]Header, error)

func Patch

func Patch(s *Safe, bucket string, header Header, options PatchOptions) (Header, error)

func Put

func Put(s *Safe, bucket, name string, src any, options PutOptions, onComplete func(Header, error)) (Header, error)

type HeadersFile

type HeadersFile struct {
	KeyId   uint64   `json:"-"`
	Bucket  string   `json:"b"`
	Headers []Header `json:"h"`
}

type Index

type Index struct {
}

type Initiate

type Initiate struct {
	Secret   string            `json:"secret"`
	Identity security.Identity `json:"identity"`
}

Initiates is a map of user id to secret message.

func GetInitiates

func GetInitiates(s *Safe) ([]Initiate, error)

type Keys

type Keys map[uint64][]byte

type Keystore

type Keystore struct {
	LastKeyId uint64            `json:"lastKeyId"`
	Keys      map[uint64][]byte `json:"keys"`
}

type KeystoreFile

type KeystoreFile struct {
	KeyId uint64            `json:"keyId"`
	Keys  map[string][]byte `json:"keys"`
}

KeystoreFile is the file that contains the primary key of box encrypted for each user

type Level2

type Level2 bool

type ListDirsOptions

type ListDirsOptions struct {
	Dir             string `json:"dir"`             // Filter on the directory
	Depth           int    `json:"depth"`           // Level of depth into subfolders
	ErrorIfNotExist bool   `json:"errorIfNotExist"` // Return an error if the directory does not exist. Otherwise, return empty list
}

type ListOptions

type ListOptions struct {
	Name            string    `json:"name"`            // Filter on the file name
	Dir             string    `json:"dir"`             // Filter on the directory
	NoSync          bool      `json:"noSync"`          // Skip syncing with the store
	Recursive       bool      `json:"recursive"`       // Recursively list files in subfolders
	Prefix          string    `json:"prefix"`          // Filter on the file prefix
	Suffix          string    `json:"suffix"`          // Filter on the file suffix
	ContentType     string    `json:"contentType"`     // Filter on the content type
	FileId          uint64    `json:"bodyId"`          // Filter on the body ID
	Tags            []string  `json:"tags"`            // Filter on the tags
	Before          time.Time `json:"before"`          // Filter on the modification time
	After           time.Time `json:"after"`           // Filter on the modification time
	KnownSince      time.Time `json:"knownSince"`      // Filter on the sync time
	OnlyChanges     bool      `json:"onlyChanges"`     // Only return files that have changed since the last sync
	Offset          int       `json:"offset"`          // Offset of the first file to return
	Limit           int       `json:"limit"`           // Maximum number of files to return
	IncludeDeleted  bool      `json:"includeDeleted"`  // Include deleted files
	Creator         string    `json:"creator"`         // Filter on the creator
	NoPrivate       bool      `json:"noPrivate"`       // Ignore private files
	PrivateId       string    `json:"privateId"`       // Filter on private files either created by the current user or the specified user
	Prefetch        bool      `json:"prefetch"`        // Prefetch the file bodies
	ErrorIfNotExist bool      `json:"errorIfNotExist"` // Return an error if the directory does not exist. Otherwise, return empty list
	OrderBy         string    `json:"orderBy"`         // Order by name or modTime. Default is name
	ReverseOrder    bool      `json:"reverseOrder"`    // Order descending when true. Default is false
}

type OpenOptions

type OpenOptions struct {
	//InitiateSecret is the information the admin receives when a user requests access to a safe
	InitiateSecret string

	//Reset cleans the DB before opening the safe
	Reset bool

	//Notification is
	Notification chan Header
}

type PatchOptions

type PatchOptions struct {
	ByName bool `json:"byName"` // Patch by name instead of fileId. Hash and size will be ignored in the patch
	Async  bool `json:"async"`  // Patch asynchronously
}

type Permission

type Permission int
const (
	Suspended Permission = 1
	Reader    Permission = 2
	Standard  Permission = 4
	Admin     Permission = 16
	Creator   Permission = 32
)

type PermissionChange

type PermissionChange struct {
	UserId     string     `json:"userId"`
	Permission Permission `json:"permission"`
}

type PutOptions

type PutOptions struct {
	Progress chan int64 // Progress channel

	Async          bool           `json:"async"`          // Do not wait for the file to be uploaded
	Replace        bool           `json:"replace"`        // Replace all other files with the same name
	ReplaceID      uint64         `json:"replaceId"`      // Replace the file with the specified ID
	Tags           []string       `json:"tags"`           // Tags associated with the file
	Thumbnail      []byte         `json:"thumbnail"`      // Thumbnail associated with the file
	ThumbnailWidth int            `json:"thumbnailWidth"` // Thumbnail width
	AutoThumbnail  bool           `json:"autoThumbnail"`  // Generate a thumbnail from the file
	ContentType    string         `json:"contentType"`    // Content type of the file
	Zip            bool           `json:"zip"`            // Zip the file if it is smaller than 64MB
	Meta           map[string]any `json:"meta"`           // Metadata associated with the file
	Private        string         `json:"private"`        // Id of the target user in case of private message
}

type Safe

type Safe struct {
	Hnd             int               `json:"hnd"`             // Handle of the safe
	CurrentUser     security.Identity `json:"currentUser"`     // Current user
	Permission      Permission        `json:"permission"`      // Permission of the current user
	CreatorId       string            `json:"creatorId"`       // Creator of the safe
	Name            string            `json:"name"`            // Name of the safe including the path
	Description     string            `json:"description"`     // Description of the safe
	Size            int64             `json:"size"`            // Size of the safe in bytes
	StoreConfigs    []StoreConfig     `json:"storeConfigs"`    // Stores of the safe
	MinimalSyncTime time.Duration     `json:"minimalSyncTime"` // Minimal time between syncs
	// contains filtered or unexported fields
}

func Create

func Create(currentUser security.Identity, name string, storeConfig StoreConfig, users Users, options CreateOptions) (*Safe, error)

Create creates a new safe with the given name and store. The current user is the creator of the safe and it is automatically added to the list of users with creator permissions. Other users are optional.

func Open

func Open(currentUser security.Identity, name string, storeUrl string, creatorId string, options OpenOptions) (*Safe, error)

type SetUsersOptions

type SetUsersOptions struct {
	SyncAlign bool `json:"syncAlign"` // SyncAlign is true if the keys are refreshed before ending the function
}

type StoreConfig

type StoreConfig struct {
	Name      string `json:"name"`
	Url       string `json:"url"`
	Quota     int64  `json:"quota"`
	Primary   bool   `json:"primary"`
	CreatorId string `json:"creatorId"`
}

type StoreType

type StoreType int

type SyncOptions

type SyncOptions struct {
	Replicate bool `json:"replicate"`
}

type UploadTask

type UploadTask struct {
	Bucket     string
	Header     Header
	HeaderFile uint64
}

type Users

type Users map[string]Permission

func GetUsers

func GetUsers(s *Safe) (Users, error)

func (Users) Is

func (users Users) Is(userId string, permission Permission) bool

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL