Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhancing webhook regexp for registry check #1187

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pkg/webhook/pod/containerregistry_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ func (r registry) Tag() string {

func NewRegistry(value string) Registry {
reg := make(registry)
r := regexp.MustCompile(`((?P<registry>[a-zA-Z0-9-._]+(:\d+)?)\/)?(?P<repository>.*\/)?(?P<image>[a-zA-Z0-9-._]+:(?P<tag>[a-zA-Z0-9-._]+))?`)
// registry name should start with a-zA-Z0-9 and end with a-zA-Z0-9
// @see https://github.com/kubernetes/kubernetes/blob/v1.31.0/pkg/apis/core/validation/validation.go#L7281-L7284
r := regexp.MustCompile(`((?P<registry>[a-zA-Z0-9][a-zA-Z0-9-.]+[a-zA-Z0-9](:\d+)?)\/)?(?P<repository>.*\/)?(?P<image>[a-zA-Z0-9-._]+:(?P<tag>[a-zA-Z0-9-._]+))?`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oliverbaehler it seems a good improvement to me, WDYT?

match := r.FindStringSubmatch(value)

for i, name := range r.SubexpNames() {
Expand Down
48 changes: 48 additions & 0 deletions pkg/webhook/pod/containerregistry_registry_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package pod

import (
"testing"

"github.com/stretchr/testify/assert"
)

// struct indicates [value(url that should check), bool(expect result)]
type RegistryValueAndExpected struct {
Value string
Expected bool
}

func checkContainerRegistryContainerRegexp(value string) bool {
checkRegistry := NewRegistry(value)
if checkRegistry.Registry() == "" || checkRegistry.Image() == "" || checkRegistry.Tag() == "" {
return false
}
return true
}

func TestContainerRegistry_Registry_Regexp(t *testing.T) {
resgistryValueAndExpectValue := []RegistryValueAndExpected{
{Value: "some-Domain.domain.name/linux:latest", Expected: true},
{Value: "some-Domain.domain.name/repository/linux:latest", Expected: true},
{Value: "111.some-Domain.domain.com/linux:latest", Expected: true},
{Value: "some-Domain.domain.com:8080/linux:latest", Expected: true},
// check whether registry starts with valid characters
{Value: "-111.some-Domain.domain.com/linux:latest", Expected: false},
{Value: ".111.some-Domain.domain.com/linux:latest", Expected: false},
{Value: "_111.some-Domain.domain.com/linux:latest", Expected: false},
// check whether registry content should not include invalid characters
{Value: "111.some_Domain.domain.com/linux:latest", Expected: false},
// check whether registry ends with valid characters
{Value: "111.some-Domain.domain.co-/linux:latest", Expected: false},
{Value: "111.some-Domain.domain.co./linux:latest", Expected: false},
{Value: "111.some-Domain.domain.co_/linux:latest", Expected: false},
}
for _, testStruct := range resgistryValueAndExpectValue {
actualValue := checkContainerRegistryContainerRegexp(testStruct.Value)
if actualValue == true {
assert.True(t, actualValue)
} else {
assert.False(t, actualValue)
}
}
}
Loading