Documentation ¶
Overview ¶
Package archive builds the backup archive.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var RandomSource = rand.Reader
RandomSource defines from where we are going to read random values to encrypt the archives.
var TARInfoFilename = "toglacier-info.json"
TARInfoFilename name of the file that is added to the tarball with the necessary information for an incremental archive.
Functions ¶
func ErrorEqual ¶
ErrorEqual compares two Error objects. This is useful to compare down to the low level errors.
func PathErrorEqual ¶
PathErrorEqual compares two PathError objects. This is useful to compare down to the low level errors.
Types ¶
type Archive ¶
type Archive interface { Build(lastArchiveInfo Info, ignorePatterns []*regexp.Regexp, backupPaths ...string) (string, Info, error) Extract(filename string, filter []string) (Info, error) FileChecksum(filename string) (string, error) }
Archive manages an archive joining all paths in a file, extracting and calculating Checksums.
type Envelop ¶
type Envelop interface { Encrypt(filename, secret string) (string, error) Decrypt(encryptedFilename, secret string) (string, error) }
Envelop manages the security of an archive encrypting and decrypting the content.
type Error ¶
Error stores error details from archive operations.
type ErrorCode ¶
type ErrorCode string
ErrorCode stores the error type that occurred to easy automatize an external action depending on the problem.
const ( // ErrorCodeTARCreation error while creating the TAR file. ErrorCodeTARCreation ErrorCode = "tar-creation" // ErrorCodeTARGeneration error adding all files to the TAR. ErrorCodeTARGeneration ErrorCode = "tar-generation" // ErrorCodeOpeningFile error while opening a file to encrypt or decrypt. ErrorCodeOpeningFile ErrorCode = "opening-file" // ErrorCodeTmpFileCreation error creating a new temporary file. ErrorCodeTmpFileCreation ErrorCode = "tmp-file-creation" // ErrorCodeCalculateHMACSHA256 error calculating the HMAC SHA256 of the file. ErrorCodeCalculateHMACSHA256 ErrorCode = "calculate-hmac-sha256" // ErrorCodeGenerateRandomNumbers error while trying to retrieve random // numbers for a encryption process. ErrorCodeGenerateRandomNumbers ErrorCode = "generate-random-numbers" // ErrorCodeWritingLabel error while adding the “encrypted” label to the file. // This label identify when the content is encrypted or not. ErrorCodeWritingLabel ErrorCode = "writing-label" // ErrorCodeReadingLabel error while reading the “encrypted” label from the // file. This label identify when the content is encrypted or not. ErrorCodeReadingLabel ErrorCode = "reading-label" // ErrorCodeWritingAuth error while writing the HMAC-SHA256 authentication to // the file. The authentication is necessary to verify if the encrypted // content wasn't modified. ErrorCodeWritingAuth ErrorCode = "writing-auth" // ErrorCodeReadingAuth error while reading the HMAC-SHA256 authentication // from the file. The authentication is necessary to verify if the encrypted // content wasn't modified. ErrorCodeReadingAuth ErrorCode = "reading-auth" // ErrorCodeWritingIV error while writing the IV that is a slice of random // numbers used as a encryption source. ErrorCodeWritingIV ErrorCode = "writing-iv" // ErrorCodeReadingIV error while reading the IV that is used as the source to // decrypt the content. ErrorCodeReadingIV ErrorCode = "reading-iv" // ErrorCodeInitCipher error initializing cipher that is used for the // encryption process. ErrorCodeInitCipher ErrorCode = "init-cipher" // ErrorCodeEncryptingFile error while encrypting file. ErrorCodeEncryptingFile ErrorCode = "encrypting-file" // ErrorCodeDecryptingFile error while decrypting file. ErrorCodeDecryptingFile ErrorCode = "decypting-file" // ErrorCodeAuthFailed error when the HMAC authentication from the encrypted // file failed. ErrorCodeAuthFailed ErrorCode = "auth-failed" // ErrorCodeRewindingFile error while moving back to the beginning of the // file. ErrorCodeRewindingFile ErrorCode = "rewinding-file" // ErrorCodeEncodingInfo failed to add the archive information to the tarball. ErrorCodeEncodingInfo ErrorCode = "encoding-info" // ErrorCodeDecodingInfo failed to extract the archive information from the // tarball. ErrorCodeDecodingInfo ErrorCode = "decoding-info" // ErrorCodeReadingTAR error while iterating over the TAR headers. ErrorCodeReadingTAR ErrorCode = "reading-tar" // ErrorCodeCreatingDirectories failed to create directories. ErrorCodeCreatingDirectories ErrorCode = "creating-directories" // ErrorCodeExtractingFile problem extracting file from TAR. ErrorCodeExtractingFile ErrorCode = "extracting-file" )
type Info ¶
Info stores extra information from the archive's items for allowing incremental archives.
func (Info) FilterByStatuses ¶
func (a Info) FilterByStatuses(statuses ...ItemInfoStatus) Info
FilterByStatuses returns the archive information only containing the items that have the desired statuses.
func (Info) Merge ¶
Merge adds all extra information that doesn't exist uet into the current storage.
func (Info) MergeLast ¶
MergeLast verifies all extra information that appeared in the last archive creation, but it doesn't appeared now. This is necessary to detect when items where deleted.
func (Info) Statistics ¶
func (a Info) Statistics() map[ItemInfoStatus]int
Statistics count the number of paths on each archive status.
type ItemInfo ¶
type ItemInfo struct { ID string Status ItemInfoStatus Checksum string }
ItemInfo stores all the necessary information to track the archive's item state.
type ItemInfoStatus ¶
type ItemInfoStatus string
ItemInfoStatus describes the current archive's item state.
const ( // ItemInfoStatusNew refers to an item that appeared for the first time in the // archive. ItemInfoStatusNew ItemInfoStatus = "new" // ItemInfoStatusModified refers to an item that was modified since the last // archive built. ItemInfoStatusModified ItemInfoStatus = "modified" // ItemInfoStatusUnmodified refers to an item that was not modified since the // last archive built. ItemInfoStatusUnmodified ItemInfoStatus = "unmodified" // ItemInfoStatusDeleted refers to an item that disappeared since the last // archive built. ItemInfoStatusDeleted ItemInfoStatus = "deleted" )
func (ItemInfoStatus) Useful ¶
func (i ItemInfoStatus) Useful() bool
Useful returns if the current status indicates that the archive item is useful or not.
type OFBEnvelop ¶
type OFBEnvelop struct {
// contains filtered or unexported fields
}
OFBEnvelop manages the security of an archive using block cipher with output feedback mode.
func NewOFBEnvelop ¶
func NewOFBEnvelop(logger log.Logger) *OFBEnvelop
NewOFBEnvelop build a new OFBEnvelop with all necessary initializations.
func (OFBEnvelop) Decrypt ¶
func (o OFBEnvelop) Decrypt(encryptedFilename, secret string) (string, error)
Decrypt do what we expect, decrypting the content with a shared secret. It authenticates the data using HMAC-SHA256. It will return the decrypted filename or an Error type encapsulated in a traceable error. To retrieve the desired error you can do:
type causer interface { Cause() error } if causeErr, ok := err.(causer); ok { switch specificErr := causeErr.Cause().(type) { case *archive.Error: // handle specifically default: // unknown error } }
func (OFBEnvelop) Encrypt ¶
func (o OFBEnvelop) Encrypt(filename, secret string) (string, error)
Encrypt do what we expect, encrypting the content with a shared secret. It adds authentication using HMAC-SHA256. It will return the encrypted filename or an Error type encapsulated in a traceable error. To retrieve the desired error you can do:
type causer interface { Cause() error } if causeErr, ok := err.(causer); ok { switch specificErr := causeErr.Cause().(type) { case *archive.Error: // handle specifically default: // unknown error } }
type PathError ¶
type PathError struct { Path string Code PathErrorCode Err error }
PathError stores error details detected while traversing the path.
type PathErrorCode ¶
type PathErrorCode string
PathErrorCode stores the error type that occurred to easy automatize an external actual depending on the problem.
const ( // PathErrorCodeInfo error retrieving the path information. PathErrorCodeInfo PathErrorCode = "info" // PathErrorCodeCreateTARHeader error while creating the TAR header from the // path information. PathErrorCodeCreateTARHeader PathErrorCode = "create-tar-header" // PathErrorCodeWritingTARHeader error while writing the header into the TAR // file. PathErrorCodeWritingTARHeader PathErrorCode = "writing-tar-header" // PathErrorCodeOpeningFile error while opening file. PathErrorCodeOpeningFile PathErrorCode = "opening-file" // PathErrorCodeWritingFile error while writing the file content to the TAR // file. PathErrorCodeWritingFile PathErrorCode = "writing-file" // PathErrorCodeSHA256 error calculating SHA256 hash from the file. PathErrorCodeSHA256 PathErrorCode = "sha-256" // PathErrorCodeRewindingFile error while moving back to the beginning of the // file. PathErrorCodeRewindingFile PathErrorCode = "rewinding-file" )
func (PathErrorCode) String ¶
func (p PathErrorCode) String() string
String translate the error code to a human readable text.
type TARBuilder ¶
type TARBuilder struct {
// contains filtered or unexported fields
}
TARBuilder join all paths into an archive using the TAR computer software utility.
func NewTARBuilder ¶
func NewTARBuilder(logger log.Logger) *TARBuilder
NewTARBuilder returns a TARBuilder with all necessary initializations.
func (TARBuilder) Build ¶
func (t TARBuilder) Build(lastArchiveInfo Info, ignorePatterns []*regexp.Regexp, backupPaths ...string) (string, Info, error)
Build builds a tarball containing all the desired files that you want to backup. A control file is added to the tarball root so we can control incremental archives (send only what was modified). Files and directories can be ignores in the backupPaths using the regular expressions in the ignorePatterns parameter. On success it will return an open file, so the caller is responsible for closing it. If no file was written to the tarball, an empty filename is returned. On error it will return an Error or PathError type encapsulated in a traceable error. To retrieve the desired error you can do:
type causer interface { Cause() error } if causeErr, ok := err.(causer); ok { switch specificErr := causeErr.Cause().(type) { case *archive.Error: // handle specifically case *archive.PathError: // handle specifically default: // unknown error } }
func (TARBuilder) Extract ¶
func (t TARBuilder) Extract(filename string, filter []string) (Info, error)
Extract uncompress all files from the tarball to the current path. You can select the files that are extracted with the filter parameter, if nil all files are extracted. On error it will return an Error type encapsulated in a traceable error. To retrieve the desired error you can do:
type causer interface { Cause() error } if causeErr, ok := err.(causer); ok { switch specificErr := causeErr.Cause().(type) { case *archive.Error: // handle specifically default: // unknown error } }
func (TARBuilder) FileChecksum ¶
func (t TARBuilder) FileChecksum(filename string) (string, error)
FileChecksum returns the file SHA256 hash encoded in base64. On error it will return a PathError type encapsulated in a traceable error. To retrieve the desired error you can do:
type causer interface { Cause() error } if causeErr, ok := err.(causer); ok { switch specificErr := causeErr.Cause().(type) { case *archive.PathError: // handle specifically default: // unknown error } }