-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
imageutil: validate source on resolving existing blob by digest
Signed-off-by: Tonis Tiigi <[email protected]> (cherry picked from commit 04fc13480d8ef253e0c3cc4c1f889455aefbdcf7)
- Loading branch information
1 parent
3a1eeca
commit 450155e
Showing
7 changed files
with
281 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package contentutil | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/reference" | ||
) | ||
|
||
func HasSource(info content.Info, refspec reference.Spec) (bool, error) { | ||
u, err := url.Parse("dummy://" + refspec.Locator) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
if info.Labels == nil { | ||
return false, nil | ||
} | ||
|
||
source, target := u.Hostname(), strings.TrimPrefix(u.Path, "/") | ||
repoLabel, ok := info.Labels["containerd.io/distribution.source."+source] | ||
if !ok || repoLabel == "" { | ||
return false, nil | ||
} | ||
|
||
for _, repo := range strings.Split(repoLabel, ",") { | ||
// the target repo is not a candidate | ||
if repo == target { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package contentutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/containerd/containerd/content" | ||
"github.com/containerd/containerd/reference" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestHasSource(t *testing.T) { | ||
info := content.Info{ | ||
Labels: map[string]string{ | ||
"containerd.io/distribution.source.docker.io": "library/alpine", | ||
}, | ||
} | ||
ref, err := reference.Parse("docker.io/library/alpine:latest") | ||
require.NoError(t, err) | ||
b, err := HasSource(info, ref) | ||
require.NoError(t, err) | ||
require.True(t, b) | ||
|
||
info = content.Info{ | ||
Labels: map[string]string{ | ||
"containerd.io/distribution.source.docker.io": "library/alpine,library/ubuntu", | ||
}, | ||
} | ||
b, err = HasSource(info, ref) | ||
require.NoError(t, err) | ||
require.True(t, b) | ||
|
||
info = content.Info{} | ||
b, err = HasSource(info, ref) | ||
require.NoError(t, err) | ||
require.False(t, b) | ||
|
||
info = content.Info{Labels: map[string]string{}} | ||
b, err = HasSource(info, ref) | ||
require.NoError(t, err) | ||
require.False(t, b) | ||
|
||
info = content.Info{ | ||
Labels: map[string]string{ | ||
"containerd.io/distribution.source.docker.io": "library/ubuntu", | ||
}, | ||
} | ||
b, err = HasSource(info, ref) | ||
require.NoError(t, err) | ||
require.False(t, b) | ||
|
||
info = content.Info{Labels: map[string]string{ | ||
"containerd.io/distribution.source.ghcr.io": "library/alpine", | ||
}} | ||
b, err = HasSource(info, ref) | ||
require.NoError(t, err) | ||
require.False(t, b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters