Skip to content

Commit a7faa0f

Browse files
authored
Container - Validate docker domain (#87)
1 parent 16e20ba commit a7faa0f

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

artifactory/utils/container/buildinfo.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,11 @@ func getManifestPaths(imagePath, repo string, commandType CommandType) []string
159159

160160
// Search for image manifest and layers in Artifactory.
161161
func (builder *buildInfoBuilder) getManifestAndLayersDetails() (layers map[string]*utils.ResultItem, manifestContent *manifest, err error) {
162-
manifestPathsCandidates := getManifestPaths(builder.image.Path(), builder.getSearchableRepo(), builder.commandType)
162+
imagePath, err := builder.image.Path()
163+
if err != nil {
164+
return nil, nil, err
165+
}
166+
manifestPathsCandidates := getManifestPaths(imagePath, builder.getSearchableRepo(), builder.commandType)
163167
log.Debug("Start searching for image manifest.json")
164168
for _, path := range manifestPathsCandidates {
165169
log.Debug(`Searching in:"` + path + `"`)
@@ -171,11 +175,6 @@ func (builder *buildInfoBuilder) getManifestAndLayersDetails() (layers map[strin
171175
return nil, nil, errorutils.CheckError(errors.New(fmt.Sprintf(imageNotFoundErrorMessage, builder.image.tag)))
172176
}
173177

174-
func (builder *buildInfoBuilder) buildReverseProxyPathWithLibrary() string {
175-
endOfRepoNameIndex := strings.Index(builder.image.Path()[1:], "/")
176-
return path.Join(builder.getSearchableRepo(), "library", builder.image.Path()[endOfRepoNameIndex+1:])
177-
}
178-
179178
func (builder *buildInfoBuilder) handlePull(manifestDependency, configLayerDependency buildinfo.Dependency, imageManifest *manifest, searchResults map[string]*utils.ResultItem) error {
180179
// Add dependencies.
181180
builder.dependencies = append(builder.dependencies, manifestDependency)
@@ -281,7 +280,11 @@ func (builder *buildInfoBuilder) createBuildInfo(module string) (*buildinfo.Buil
281280
imageProperties["docker.image.id"] = builder.imageId
282281
imageProperties["docker.image.tag"] = builder.image.Tag()
283282
if module == "" {
284-
module = builder.image.Name()
283+
imageName, err := builder.image.Name()
284+
if err != nil {
285+
return nil, err
286+
}
287+
module = imageName
285288
}
286289
buildInfo := &buildinfo.BuildInfo{Modules: []buildinfo.Module{{
287290
Id: module,
Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
package container
22

33
import (
4+
"fmt"
45
"path"
56
"strings"
7+
8+
"github.com/jfrog/jfrog-client-go/utils/errorutils"
9+
"github.com/jfrog/jfrog-client-go/utils/log"
610
)
711

812
type Image struct {
@@ -15,21 +19,36 @@ func (image *Image) Tag() string {
1519
}
1620

1721
// Get image relative path in Artifactory.
18-
func (image *Image) Path() string {
22+
func (image *Image) Path() (string, error) {
23+
if err := image.validateTag(); err != nil {
24+
return "", err
25+
}
1926
indexOfFirstSlash := strings.Index(image.tag, "/")
2027
indexOfLastColon := strings.LastIndex(image.tag, ":")
2128
if indexOfLastColon < 0 || indexOfLastColon < indexOfFirstSlash {
22-
return path.Join(image.tag[indexOfFirstSlash:], "latest")
29+
log.Info("The image '%s' is does not include tag. Using the 'latest' tag.")
30+
return path.Join(image.tag[indexOfFirstSlash:], "latest"), nil
2331
}
24-
return path.Join(image.tag[indexOfFirstSlash:indexOfLastColon], image.tag[indexOfLastColon+1:])
32+
return path.Join(image.tag[indexOfFirstSlash:indexOfLastColon], image.tag[indexOfLastColon+1:]), nil
2533
}
2634

2735
// Get image name.
28-
func (image *Image) Name() string {
36+
func (image *Image) Name() (string, error) {
37+
if err := image.validateTag(); err != nil {
38+
return "", err
39+
}
2940
indexOfLastSlash := strings.LastIndex(image.tag, "/")
3041
indexOfLastColon := strings.LastIndex(image.tag, ":")
3142
if indexOfLastColon < 0 || indexOfLastColon < indexOfLastSlash {
32-
return image.tag[indexOfLastSlash+1:] + ":latest"
43+
log.Info("The image '%s' is does not include tag. Using the 'latest' tag.")
44+
return image.tag[indexOfLastSlash+1:] + ":latest", nil
45+
}
46+
return image.tag[indexOfLastSlash+1:], nil
47+
}
48+
49+
func (image *Image) validateTag() error {
50+
if !strings.Contains(image.tag, "/") {
51+
return errorutils.CheckError(fmt.Errorf("The image '%s' is missing '/' which indicates the image name/tag", image.tag))
3352
}
34-
return image.tag[indexOfLastSlash+1:]
53+
return nil
3554
}

artifactory/utils/container/image_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package container
22

33
import (
44
"testing"
5+
6+
"github.com/stretchr/testify/assert"
57
)
68

79
func TestGetImagePath(t *testing.T) {
@@ -18,11 +20,16 @@ func TestGetImagePath(t *testing.T) {
1820
}
1921

2022
for _, v := range imageTags {
21-
result := NewImage(v.in).Path()
23+
result, err := NewImage(v.in).Path()
24+
assert.NoError(t, err)
2225
if result != v.expected {
2326
t.Errorf("Path(\"%s\") => '%s', want '%s'", v.in, result, v.expected)
2427
}
2528
}
29+
// Validate failure upon missing image name
30+
_, err := NewImage("domain").Path()
31+
assert.Error(t, err)
32+
2633
}
2734

2835
func TestGetImageName(t *testing.T) {
@@ -39,11 +46,15 @@ func TestGetImageName(t *testing.T) {
3946
}
4047

4148
for _, v := range imageTags {
42-
result := NewImage(v.in).Name()
49+
result, err := NewImage(v.in).Name()
50+
assert.NoError(t, err)
4351
if result != v.expected {
4452
t.Errorf("Name(\"%s\") => '%s', want '%s'", v.in, result, v.expected)
4553
}
4654
}
55+
// Validate failure upon missing image name
56+
_, err := NewImage("domain").Name()
57+
assert.Error(t, err)
4758
}
4859

4960
func TestResolveRegistryFromTag(t *testing.T) {

0 commit comments

Comments
 (0)