2018-02-05 21:05:59 +00:00
package reference // import "github.com/docker/docker/reference"
2015-11-18 22:16:21 +00:00
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
2015-12-08 01:55:35 +00:00
"sort"
2015-11-18 22:16:21 +00:00
"sync"
2017-01-26 00:54:18 +00:00
"github.com/docker/distribution/reference"
2016-04-21 00:08:47 +00:00
"github.com/docker/docker/pkg/ioutils"
2022-03-04 13:49:42 +00:00
"github.com/opencontainers/go-digest"
2017-07-19 14:20:13 +00:00
"github.com/pkg/errors"
2015-11-18 22:16:21 +00:00
)
var (
// ErrDoesNotExist is returned if a reference is not found in the
// store.
2017-07-19 14:20:13 +00:00
ErrDoesNotExist notFoundError = "reference does not exist"
2015-11-18 22:16:21 +00:00
)
// An Association is a tuple associating a reference with an image ID.
type Association struct {
2017-01-26 00:54:18 +00:00
Ref reference . Named
2016-09-15 23:37:32 +00:00
ID digest . Digest
2015-11-18 22:16:21 +00:00
}
2017-08-17 22:43:36 +00:00
// Store provides the set of methods which can operate on a reference store.
2015-11-18 22:16:21 +00:00
type Store interface {
2017-01-26 00:54:18 +00: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 22:16:21 +00:00
}
2022-11-14 14:35:30 +00:00
type refStore struct {
2015-11-18 22:16:21 +00:00
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-26 00:54:18 +00:00
referencesByIDCache map [ digest . Digest ] map [ string ] reference . Named
2015-11-18 22:16:21 +00:00
}
2016-09-15 23:37:32 +00:00
// Repository maps tags to digests. The key is a stringified Reference,
2015-11-18 22:16:21 +00:00
// including the repository name.
2016-09-15 23:37:32 +00:00
type repository map [ string ] digest . Digest
2015-11-18 22:16:21 +00:00
2017-01-26 00:54:18 +00:00
type lexicalRefs [ ] reference . Named
2015-12-08 01:55:35 +00:00
2017-01-26 00:54:18 +00: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-08 01:55:35 +00:00
type lexicalAssociations [ ] Association
2017-01-26 00:54:18 +00: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-08 01:55:35 +00:00
2015-12-04 21:55:15 +00: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 22:43:36 +00:00
func NewReferenceStore ( jsonPath string ) ( Store , error ) {
2015-11-18 22:16:21 +00:00
abspath , err := filepath . Abs ( jsonPath )
if err != nil {
return nil , err
}
2022-11-14 14:35:30 +00:00
store := & refStore {
2015-11-18 22:16:21 +00:00
jsonPath : abspath ,
Repositories : make ( map [ string ] repository ) ,
2017-01-26 00:54:18 +00:00
referencesByIDCache : make ( map [ digest . Digest ] map [ string ] reference . Named ) ,
2015-11-18 22:16:21 +00: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 21:55:15 +00:00
// AddTag adds a tag reference to the store. If force is set to true, existing
2015-11-18 22:16:21 +00:00
// references can be overwritten. This only works for tags, not digests.
2022-11-14 14:35:30 +00:00
func ( store * refStore ) AddTag ( ref reference . Named , id digest . Digest , force bool ) error {
2017-01-26 00:54:18 +00:00
if _ , isCanonical := ref . ( reference . Canonical ) ; isCanonical {
2017-07-19 14:20:13 +00:00
return errors . WithStack ( invalidTagError ( "refusing to create a tag with a digest reference" ) )
2015-11-25 20:42:40 +00:00
}
2017-01-26 00:54:18 +00:00
return store . addReference ( reference . TagNameOnly ( ref ) , id , force )
2015-11-25 20:42:40 +00:00
}
2015-12-04 21:55:15 +00:00
// AddDigest adds a digest reference to the store.
2022-11-14 14:35:30 +00:00
func ( store * refStore ) AddDigest ( ref reference . Canonical , id digest . Digest , force bool ) error {
2015-11-25 20:42:40 +00:00
return store . addReference ( ref , id , force )
}
2017-02-16 09:48:40 +00:00
func favorDigest ( originalRef reference . Named ) ( reference . Named , error ) {
ref := originalRef
2017-02-10 00:30:46 +00:00
// If the reference includes a digest and a tag, we must store only the
// digest.
2017-02-16 09:48:40 +00:00
canonical , isCanonical := originalRef . ( reference . Canonical )
_ , isNamedTagged := originalRef . ( reference . NamedTagged )
2017-02-10 00:30:46 +00:00
if isCanonical && isNamedTagged {
trimmed , err := reference . WithDigest ( reference . TrimNamed ( canonical ) , canonical . Digest ( ) )
if err != nil {
// should never happen
2017-02-16 09:48:40 +00:00
return originalRef , err
2017-02-10 00:30:46 +00:00
}
ref = trimmed
}
2017-02-16 09:48:40 +00:00
return ref , nil
}
2022-11-14 14:35:30 +00:00
func ( store * refStore ) addReference ( ref reference . Named , id digest . Digest , force bool ) error {
2017-02-16 09:48:40 +00:00
ref , err := favorDigest ( ref )
if err != nil {
return err
}
2017-02-10 00:30:46 +00:00
2017-01-26 00:54:18 +00:00
refName := reference . FamiliarName ( ref )
refStr := reference . FamiliarString ( ref )
if refName == string ( digest . Canonical ) {
2017-07-19 14:20:13 +00:00
return errors . WithStack ( invalidTagError ( "refusing to create an ambiguous tag using digest algorithm as name" ) )
2015-11-25 20:42:40 +00:00
}
2015-11-18 22:16:21 +00:00
store . mu . Lock ( )
defer store . mu . Unlock ( )
2022-11-14 14:35:30 +00:00
repo , exists := store . Repositories [ refName ]
if ! exists || repo == nil {
repo = make ( map [ string ] digest . Digest )
store . Repositories [ refName ] = repo
2015-11-18 22:16:21 +00:00
}
2022-11-14 14:35:30 +00:00
oldID , exists := repo [ refStr ]
2015-11-18 22:16:21 +00:00
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 21:24:06 +00: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 22:16:21 +00:00
// force only works for tags
2017-01-26 00:54:18 +00:00
if digested , isDigest := ref . ( reference . Canonical ) ; isDigest {
2017-07-19 14:20:13 +00:00
return errors . WithStack ( conflictingTagError ( "Cannot overwrite digest " + digested . Digest ( ) . String ( ) ) )
2015-11-18 22:16:21 +00:00
}
if ! force {
2017-07-19 14:20:13 +00: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 22:16:21 +00:00
}
if store . referencesByIDCache [ oldID ] != nil {
delete ( store . referencesByIDCache [ oldID ] , refStr )
if len ( store . referencesByIDCache [ oldID ] ) == 0 {
delete ( store . referencesByIDCache , oldID )
}
}
}
2022-11-14 14:35:30 +00:00
repo [ refStr ] = id
2015-11-18 22:16:21 +00:00
if store . referencesByIDCache [ id ] == nil {
2017-01-26 00:54:18 +00:00
store . referencesByIDCache [ id ] = make ( map [ string ] reference . Named )
2015-11-18 22:16:21 +00: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.
2022-11-14 14:35:30 +00:00
func ( store * refStore ) Delete ( ref reference . Named ) ( bool , error ) {
2017-02-16 09:48:40 +00:00
ref , err := favorDigest ( ref )
if err != nil {
return false , err
}
2017-01-26 00:54:18 +00:00
ref = reference . TagNameOnly ( ref )
refName := reference . FamiliarName ( ref )
refStr := reference . FamiliarString ( ref )
2015-11-18 22:16:21 +00:00
store . mu . Lock ( )
defer store . mu . Unlock ( )
2022-11-14 14:35:30 +00:00
repo , exists := store . Repositories [ refName ]
2015-11-18 22:16:21 +00:00
if ! exists {
return false , ErrDoesNotExist
}
2022-11-14 14:35:30 +00:00
if id , exists := repo [ refStr ] ; exists {
delete ( repo , refStr )
if len ( repo ) == 0 {
2017-01-26 00:54:18 +00:00
delete ( store . Repositories , refName )
2015-11-18 22:16:21 +00: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 23:37:32 +00:00
// Get retrieves an item from the store by reference
2022-11-14 14:35:30 +00:00
func ( store * refStore ) Get ( ref reference . Named ) ( digest . Digest , error ) {
2017-01-26 00:54:18 +00:00
if canonical , ok := ref . ( reference . Canonical ) ; ok {
// If reference contains both tag and digest, only
2017-05-21 23:24:07 +00:00
// lookup by digest as it takes precedence over
2017-01-26 00:54:18 +00: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 22:16:21 +00:00
store . mu . RLock ( )
defer store . mu . RUnlock ( )
2022-11-14 14:35:30 +00:00
repo , exists := store . Repositories [ refName ]
if ! exists || repo == nil {
2015-11-18 22:16:21 +00:00
return "" , ErrDoesNotExist
}
2022-11-14 14:35:30 +00:00
id , exists := repo [ refStr ]
2015-11-18 22:16:21 +00:00
if ! exists {
return "" , ErrDoesNotExist
}
return id , nil
}
2016-09-15 23:37:32 +00:00
// References returns a slice of references to the given ID. The slice
// will be nil if there are no references to this ID.
2022-11-14 14:35:30 +00:00
func ( store * refStore ) References ( id digest . Digest ) [ ] reference . Named {
2015-11-18 22:16:21 +00:00
store . mu . RLock ( )
defer store . mu . RUnlock ( )
// Convert the internal map to an array for two reasons:
2015-12-04 21:55:15 +00:00
// 1) We must not return a mutable
2015-11-18 22:16:21 +00:00
// 2) It would be ugly to expose the extraneous map keys to callers.
2017-01-26 00:54:18 +00:00
var references [ ] reference . Named
2015-11-18 22:16:21 +00:00
for _ , ref := range store . referencesByIDCache [ id ] {
references = append ( references , ref )
}
2015-12-08 01:55:35 +00:00
sort . Sort ( lexicalRefs ( references ) )
2015-11-18 22:16:21 +00: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.
2022-11-14 14:35:30 +00:00
func ( store * refStore ) ReferencesByName ( ref reference . Named ) [ ] Association {
2017-01-26 00:54:18 +00:00
refName := reference . FamiliarName ( ref )
2015-11-18 22:16:21 +00:00
store . mu . RLock ( )
defer store . mu . RUnlock ( )
2022-11-14 14:35:30 +00:00
repo , exists := store . Repositories [ refName ]
2015-11-18 22:16:21 +00:00
if ! exists {
return nil
}
var associations [ ] Association
2022-11-14 14:35:30 +00:00
for refStr , refID := range repo {
2017-01-26 00:54:18 +00:00
ref , err := reference . ParseNormalizedNamed ( refStr )
2015-11-18 22:16:21 +00:00
if err != nil {
// Should never happen
return nil
}
associations = append ( associations ,
Association {
2016-09-15 23:37:32 +00:00
Ref : ref ,
ID : refID ,
2015-11-18 22:16:21 +00:00
} )
}
2015-12-08 01:55:35 +00:00
sort . Sort ( lexicalAssociations ( associations ) )
2015-11-18 22:16:21 +00:00
return associations
}
2022-11-14 14:35:30 +00:00
func ( store * refStore ) save ( ) error {
2015-11-18 22:16:21 +00:00
// Store the json
jsonData , err := json . Marshal ( store )
if err != nil {
return err
}
2016-04-21 00:08:47 +00:00
return ioutils . AtomicWriteFile ( store . jsonPath , jsonData , 0600 )
2015-11-18 22:16:21 +00:00
}
2022-11-14 14:35:30 +00:00
func ( store * refStore ) reload ( ) error {
2015-11-18 22:16:21 +00:00
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
}
2022-11-14 14:35:30 +00:00
for _ , repo := range store . Repositories {
for refStr , refID := range repo {
2017-01-26 00:54:18 +00:00
ref , err := reference . ParseNormalizedNamed ( refStr )
2015-11-18 22:16:21 +00:00
if err != nil {
// Should never happen
continue
}
if store . referencesByIDCache [ refID ] == nil {
2017-01-26 00:54:18 +00:00
store . referencesByIDCache [ refID ] = make ( map [ string ] reference . Named )
2015-11-18 22:16:21 +00:00
}
store . referencesByIDCache [ refID ] [ refStr ] = ref
}
}
return nil
}