2018-02-05 16:05:59 -05:00
package reference // import "github.com/docker/docker/reference"
2015-11-18 17:16:21 -05:00
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
2015-12-07 20:55:35 -05:00
"sort"
2015-11-18 17:16:21 -05:00
"sync"
2017-01-25 19:54:18 -05:00
"github.com/docker/distribution/reference"
2016-04-20 20:08:47 -04:00
"github.com/docker/docker/pkg/ioutils"
2019-08-05 10:37:47 -04:00
digest "github.com/opencontainers/go-digest"
2017-07-19 10:20:13 -04:00
"github.com/pkg/errors"
2015-11-18 17:16:21 -05:00
)
var (
// ErrDoesNotExist is returned if a reference is not found in the
// store.
2017-07-19 10:20:13 -04:00
ErrDoesNotExist notFoundError = "reference does not exist"
2015-11-18 17:16:21 -05:00
)
// An Association is a tuple associating a reference with an image ID.
type Association struct {
2017-01-25 19:54:18 -05:00
Ref reference . Named
2016-09-15 19:37:32 -04:00
ID digest . Digest
2015-11-18 17:16:21 -05:00
}
2017-08-17 18:43:36 -04:00
// Store provides the set of methods which can operate on a reference store.
2015-11-18 17:16:21 -05:00
type Store interface {
2017-01-25 19:54:18 -05:00
References ( id digest . Digest ) [ ] reference . Named
ReferencesByName ( ref reference . Named ) [ ] Association
AddTag ( ref reference . Named , id digest . Digest , force bool ) error
AddDigest ( ref reference . Canonical , id digest . Digest , force bool ) error
Delete ( ref reference . Named ) ( bool , error )
Get ( ref reference . Named ) ( digest . Digest , error )
2015-11-18 17:16:21 -05:00
}
type store struct {
mu sync . RWMutex
// jsonPath is the path to the file where the serialized tag data is
// stored.
jsonPath string
// Repositories is a map of repositories, indexed by name.
Repositories map [ string ] repository
// referencesByIDCache is a cache of references indexed by ID, to speed
// up References.
2017-01-25 19:54:18 -05:00
referencesByIDCache map [ digest . Digest ] map [ string ] reference . Named
2015-11-18 17:16:21 -05:00
}
2016-09-15 19:37:32 -04:00
// Repository maps tags to digests. The key is a stringified Reference,
2015-11-18 17:16:21 -05:00
// including the repository name.
2016-09-15 19:37:32 -04:00
type repository map [ string ] digest . Digest
2015-11-18 17:16:21 -05:00
2017-01-25 19:54:18 -05:00
type lexicalRefs [ ] reference . Named
2015-12-07 20:55:35 -05:00
2017-01-25 19:54:18 -05:00
func ( a lexicalRefs ) Len ( ) int { return len ( a ) }
func ( a lexicalRefs ) Swap ( i , j int ) { a [ i ] , a [ j ] = a [ j ] , a [ i ] }
func ( a lexicalRefs ) Less ( i , j int ) bool {
return a [ i ] . String ( ) < a [ j ] . String ( )
}
2015-12-07 20:55:35 -05:00
type lexicalAssociations [ ] Association
2017-01-25 19:54:18 -05:00
func ( a lexicalAssociations ) Len ( ) int { return len ( a ) }
func ( a lexicalAssociations ) Swap ( i , j int ) { a [ i ] , a [ j ] = a [ j ] , a [ i ] }
func ( a lexicalAssociations ) Less ( i , j int ) bool {
return a [ i ] . Ref . String ( ) < a [ j ] . Ref . String ( )
}
2015-12-07 20:55:35 -05:00
2015-12-04 16:55:15 -05:00
// NewReferenceStore creates a new reference store, tied to a file path where
// the set of references are serialized in JSON format.
2017-08-17 18:43:36 -04:00
func NewReferenceStore ( jsonPath string ) ( Store , error ) {
2015-11-18 17:16:21 -05:00
abspath , err := filepath . Abs ( jsonPath )
if err != nil {
return nil , err
}
store := & store {
jsonPath : abspath ,
Repositories : make ( map [ string ] repository ) ,
2017-01-25 19:54:18 -05:00
referencesByIDCache : make ( map [ digest . Digest ] map [ string ] reference . Named ) ,
2015-11-18 17:16:21 -05:00
}
// Load the json file if it exists, otherwise create it.
if err := store . reload ( ) ; os . IsNotExist ( err ) {
if err := store . save ( ) ; err != nil {
return nil , err
}
} else if err != nil {
return nil , err
}
return store , nil
}
2015-12-04 16:55:15 -05:00
// AddTag adds a tag reference to the store. If force is set to true, existing
2015-11-18 17:16:21 -05:00
// references can be overwritten. This only works for tags, not digests.
2017-01-25 19:54:18 -05:00
func ( store * store ) AddTag ( ref reference . Named , id digest . Digest , force bool ) error {
if _ , isCanonical := ref . ( reference . Canonical ) ; isCanonical {
2017-07-19 10:20:13 -04:00
return errors . WithStack ( invalidTagError ( "refusing to create a tag with a digest reference" ) )
2015-11-25 15:42:40 -05:00
}
2017-01-25 19:54:18 -05:00
return store . addReference ( reference . TagNameOnly ( ref ) , id , force )
2015-11-25 15:42:40 -05:00
}
2015-12-04 16:55:15 -05:00
// AddDigest adds a digest reference to the store.
2017-01-25 19:54:18 -05:00
func ( store * store ) AddDigest ( ref reference . Canonical , id digest . Digest , force bool ) error {
2015-11-25 15:42:40 -05:00
return store . addReference ( ref , id , force )
}
2017-02-16 04:48:40 -05:00
func favorDigest ( originalRef reference . Named ) ( reference . Named , error ) {
ref := originalRef
2017-02-09 19:30:46 -05:00
// If the reference includes a digest and a tag, we must store only the
// digest.
2017-02-16 04:48:40 -05:00
canonical , isCanonical := originalRef . ( reference . Canonical )
_ , isNamedTagged := originalRef . ( reference . NamedTagged )
2017-02-09 19:30:46 -05:00
if isCanonical && isNamedTagged {
trimmed , err := reference . WithDigest ( reference . TrimNamed ( canonical ) , canonical . Digest ( ) )
if err != nil {
// should never happen
2017-02-16 04:48:40 -05:00
return originalRef , err
2017-02-09 19:30:46 -05:00
}
ref = trimmed
}
2017-02-16 04:48:40 -05:00
return ref , nil
}
func ( store * store ) addReference ( ref reference . Named , id digest . Digest , force bool ) error {
ref , err := favorDigest ( ref )
if err != nil {
return err
}
2017-02-09 19:30:46 -05:00
2017-01-25 19:54:18 -05:00
refName := reference . FamiliarName ( ref )
refStr := reference . FamiliarString ( ref )
if refName == string ( digest . Canonical ) {
2017-07-19 10:20:13 -04:00
return errors . WithStack ( invalidTagError ( "refusing to create an ambiguous tag using digest algorithm as name" ) )
2015-11-25 15:42:40 -05:00
}
2015-11-18 17:16:21 -05:00
store . mu . Lock ( )
defer store . mu . Unlock ( )
2017-01-25 19:54:18 -05:00
repository , exists := store . Repositories [ refName ]
2015-11-18 17:16:21 -05:00
if ! exists || repository == nil {
2016-09-15 19:37:32 -04:00
repository = make ( map [ string ] digest . Digest )
2017-01-25 19:54:18 -05:00
store . Repositories [ refName ] = repository
2015-11-18 17:16:21 -05:00
}
oldID , exists := repository [ refStr ]
if exists {
Don't fail on two concurrent reference.store.AddDigest calls
reference.store.addReference fails when adding a digest reference
that already exists (regardless of the reference target). Both
callers (via reference.store.AddDigest) do check in advance, using
reference.store.Get, whether the digest reference exists before
calling AddDigest, but the reference store lock is released between
the two calls, so if another thread sets the reference in the meantime,
AddDigest may fail with
> Cannot overwrite digest ...
.
Handle this by checking that the pre-existing reference points at the
same image, i.e. that there is nothing to do, and succeeding immediately
in that case. This is even cheaper, avoids a reference.store.save() call.
(In principle, the same failure could have happened via
reference.store.AddTag, as
> Conflict: Tag %s is already set to image %s, if you want to replace it, please use -f option
but almost all callers (except for migrate/v1.Migrate, which is run
single-threaded anyway) set the "force" parameter of AddTag to true,
which makes the race invisible. This commit does not change the behavior
of that case, except for speeding it up by avoiding the
reference.store.save() call.)
The existing reference.store.Get checks are now, in a sense, redundant
as such, but their existence allows the callers to provide nice
context-dependent error messages, so this commit leaves them unchanged.
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2018-09-06 17:24:06 -04:00
if oldID == id {
// Nothing to do. The caller may have checked for this using store.Get in advance, but store.mu was unlocked in the meantime, so this can legitimately happen nevertheless.
return nil
}
2015-11-18 17:16:21 -05:00
// force only works for tags
2017-01-25 19:54:18 -05:00
if digested , isDigest := ref . ( reference . Canonical ) ; isDigest {
2017-07-19 10:20:13 -04:00
return errors . WithStack ( conflictingTagError ( "Cannot overwrite digest " + digested . Digest ( ) . String ( ) ) )
2015-11-18 17:16:21 -05:00
}
if ! force {
2017-07-19 10:20:13 -04:00
return errors . WithStack (
conflictingTagError (
fmt . Sprintf ( "Conflict: Tag %s is already set to image %s, if you want to replace it, please use the force option" , refStr , oldID . String ( ) ) ,
) ,
)
2015-11-18 17:16:21 -05:00
}
if store . referencesByIDCache [ oldID ] != nil {
delete ( store . referencesByIDCache [ oldID ] , refStr )
if len ( store . referencesByIDCache [ oldID ] ) == 0 {
delete ( store . referencesByIDCache , oldID )
}
}
}
repository [ refStr ] = id
if store . referencesByIDCache [ id ] == nil {
2017-01-25 19:54:18 -05:00
store . referencesByIDCache [ id ] = make ( map [ string ] reference . Named )
2015-11-18 17:16:21 -05:00
}
store . referencesByIDCache [ id ] [ refStr ] = ref
return store . save ( )
}
// Delete deletes a reference from the store. It returns true if a deletion
// happened, or false otherwise.
2017-01-25 19:54:18 -05:00
func ( store * store ) Delete ( ref reference . Named ) ( bool , error ) {
2017-02-16 04:48:40 -05:00
ref , err := favorDigest ( ref )
if err != nil {
return false , err
}
2017-01-25 19:54:18 -05:00
ref = reference . TagNameOnly ( ref )
refName := reference . FamiliarName ( ref )
refStr := reference . FamiliarString ( ref )
2015-11-18 17:16:21 -05:00
store . mu . Lock ( )
defer store . mu . Unlock ( )
2017-01-25 19:54:18 -05:00
repository , exists := store . Repositories [ refName ]
2015-11-18 17:16:21 -05:00
if ! exists {
return false , ErrDoesNotExist
}
if id , exists := repository [ refStr ] ; exists {
delete ( repository , refStr )
if len ( repository ) == 0 {
2017-01-25 19:54:18 -05:00
delete ( store . Repositories , refName )
2015-11-18 17:16:21 -05:00
}
if store . referencesByIDCache [ id ] != nil {
delete ( store . referencesByIDCache [ id ] , refStr )
if len ( store . referencesByIDCache [ id ] ) == 0 {
delete ( store . referencesByIDCache , id )
}
}
return true , store . save ( )
}
return false , ErrDoesNotExist
}
2016-09-15 19:37:32 -04:00
// Get retrieves an item from the store by reference
2017-01-25 19:54:18 -05:00
func ( store * store ) Get ( ref reference . Named ) ( digest . Digest , error ) {
if canonical , ok := ref . ( reference . Canonical ) ; ok {
// If reference contains both tag and digest, only
2017-05-21 19:24:07 -04:00
// lookup by digest as it takes precedence over
2017-01-25 19:54:18 -05:00
// tag, until tag/digest combos are stored.
if _ , ok := ref . ( reference . Tagged ) ; ok {
var err error
ref , err = reference . WithDigest ( reference . TrimNamed ( canonical ) , canonical . Digest ( ) )
if err != nil {
return "" , err
}
}
} else {
ref = reference . TagNameOnly ( ref )
}
refName := reference . FamiliarName ( ref )
refStr := reference . FamiliarString ( ref )
2015-11-18 17:16:21 -05:00
store . mu . RLock ( )
defer store . mu . RUnlock ( )
2017-01-25 19:54:18 -05:00
repository , exists := store . Repositories [ refName ]
2015-11-18 17:16:21 -05:00
if ! exists || repository == nil {
return "" , ErrDoesNotExist
}
2017-01-25 19:54:18 -05:00
id , exists := repository [ refStr ]
2015-11-18 17:16:21 -05:00
if ! exists {
return "" , ErrDoesNotExist
}
return id , nil
}
2016-09-15 19:37:32 -04:00
// References returns a slice of references to the given ID. The slice
// will be nil if there are no references to this ID.
2017-01-25 19:54:18 -05:00
func ( store * store ) References ( id digest . Digest ) [ ] reference . Named {
2015-11-18 17:16:21 -05:00
store . mu . RLock ( )
defer store . mu . RUnlock ( )
// Convert the internal map to an array for two reasons:
2015-12-04 16:55:15 -05:00
// 1) We must not return a mutable
2015-11-18 17:16:21 -05:00
// 2) It would be ugly to expose the extraneous map keys to callers.
2017-01-25 19:54:18 -05:00
var references [ ] reference . Named
2015-11-18 17:16:21 -05:00
for _ , ref := range store . referencesByIDCache [ id ] {
references = append ( references , ref )
}
2015-12-07 20:55:35 -05:00
sort . Sort ( lexicalRefs ( references ) )
2015-11-18 17:16:21 -05:00
return references
}
// ReferencesByName returns the references for a given repository name.
// If there are no references known for this repository name,
// ReferencesByName returns nil.
2017-01-25 19:54:18 -05:00
func ( store * store ) ReferencesByName ( ref reference . Named ) [ ] Association {
refName := reference . FamiliarName ( ref )
2015-11-18 17:16:21 -05:00
store . mu . RLock ( )
defer store . mu . RUnlock ( )
2017-01-25 19:54:18 -05:00
repository , exists := store . Repositories [ refName ]
2015-11-18 17:16:21 -05:00
if ! exists {
return nil
}
var associations [ ] Association
for refStr , refID := range repository {
2017-01-25 19:54:18 -05:00
ref , err := reference . ParseNormalizedNamed ( refStr )
2015-11-18 17:16:21 -05:00
if err != nil {
// Should never happen
return nil
}
associations = append ( associations ,
Association {
2016-09-15 19:37:32 -04:00
Ref : ref ,
ID : refID ,
2015-11-18 17:16:21 -05:00
} )
}
2015-12-07 20:55:35 -05:00
sort . Sort ( lexicalAssociations ( associations ) )
2015-11-18 17:16:21 -05:00
return associations
}
func ( store * store ) save ( ) error {
// Store the json
jsonData , err := json . Marshal ( store )
if err != nil {
return err
}
2016-04-20 20:08:47 -04:00
return ioutils . AtomicWriteFile ( store . jsonPath , jsonData , 0600 )
2015-11-18 17:16:21 -05:00
}
func ( store * store ) reload ( ) error {
f , err := os . Open ( store . jsonPath )
if err != nil {
return err
}
defer f . Close ( )
if err := json . NewDecoder ( f ) . Decode ( & store ) ; err != nil {
return err
}
for _ , repository := range store . Repositories {
for refStr , refID := range repository {
2017-01-25 19:54:18 -05:00
ref , err := reference . ParseNormalizedNamed ( refStr )
2015-11-18 17:16:21 -05:00
if err != nil {
// Should never happen
continue
}
if store . referencesByIDCache [ refID ] == nil {
2017-01-25 19:54:18 -05:00
store . referencesByIDCache [ refID ] = make ( map [ string ] reference . Named )
2015-11-18 17:16:21 -05:00
}
store . referencesByIDCache [ refID ] [ refStr ] = ref
}
}
return nil
}