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

ISSUE-2139: Fixing few issues and code workflow #1996

Merged
merged 1 commit into from
Feb 26, 2024
Merged
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
30 changes: 15 additions & 15 deletions deepfence_server/pkg/registry/acr/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ import (

var client = &http.Client{Timeout: 10 * time.Second}

func listImagesRegistryV2(url, namespace, userName, password string) ([]model.IngestedContainerImage, error) {

var (
images []model.IngestedContainerImage
)
func listImagesRegistryV2(url, namespace, userName,
password string) ([]model.IngestedContainerImage, error) {
var images []model.IngestedContainerImage

repos, err := listCatalogRegistryV2(url, namespace, userName, password)
if err != nil {
Expand All @@ -38,11 +36,10 @@ func listImagesRegistryV2(url, namespace, userName, password string) ([]model.In
return images, nil
}

func listCatalogRegistryV2(url, namespace, userName, password string) ([]string, error) {
var (
repositories []string
err error
)
func listCatalogRegistryV2(url, namespace, userName,
password string) ([]string, error) {

var repositories []string

listReposURL := "%s/v2/_catalog"
queryURL := fmt.Sprintf(listReposURL, url)
Expand All @@ -57,34 +54,37 @@ func listCatalogRegistryV2(url, namespace, userName, password string) ([]string,
resp, err := client.Do(req)
if err != nil {
log.Error().Msg(err.Error())
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Msg(err.Error())
return nil, err
}

if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("error bad status code %d", resp.StatusCode)
log.Error().Msg(err.Error())
return nil, err
}

var repos ReposResp
if err := json.Unmarshal(body, &repos); err != nil {
log.Error().Msg(err.Error())
return nil, err
}

repositories = append(repositories, repos.Repositories...)

return repositories, err
}

func listRepoTagsV2(url, namespace, userName, password, repoName string) (RepoTagsResp, error) {
var (
err error
repoTags RepoTagsResp
)
func listRepoTagsV2(url, namespace, userName, password,
repoName string) (RepoTagsResp, error) {

var repoTags RepoTagsResp

listRepoTagsURL := "%s/v2/%s/tags/list"
queryURL := fmt.Sprintf(listRepoTagsURL, url, repoName)
Expand Down
4 changes: 2 additions & 2 deletions deepfence_server/pkg/registry/acr/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ type RegistryACR struct {

type NonSecret struct {
AzureRegistryURL string `json:"azure_registry_url" validate:"required,url"`
AzureRegistryUsername string `json:"azure_registry_username" validate:"omitempty,min=1"`
AzureRegistryUsername string `json:"azure_registry_username" validate:"required,min=1"`
}

type Secret struct {
AzureRegistryPassword string `json:"azure_registry_password" validate:"omitempty,min=1"`
AzureRegistryPassword string `json:"azure_registry_password" validate:"required,min=1"`
}

type ReposResp struct {
Expand Down
15 changes: 14 additions & 1 deletion deepfence_server/pkg/registry/dockerhub/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,20 @@ func New(requestByte []byte) (*RegistryDockerHub, error) {
}

func (d *RegistryDockerHub) ValidateFields(v *validator.Validate) error {
return v.Struct(d)
err := v.Struct(d)
if (err != nil) || d.NonSecret.IsPublic == "true" {
return err
}

type AuthInfo struct {
DockerHubUsername string `json:"docker_hub_username" validate:"required,min=2"`
DockerHubPassword string `json:"docker_hub_password" validate:"required,min=2"`
}

auth := AuthInfo{}
auth.DockerHubUsername = d.NonSecret.DockerHubUsername
auth.DockerHubPassword = d.Secret.DockerHubPassword
return v.Struct(auth)
}

func (d *RegistryDockerHub) IsValidCredential() bool {
Expand Down
16 changes: 15 additions & 1 deletion deepfence_server/pkg/registry/dockerprivate/dockerprivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,21 @@ func New(requestByte []byte) (*RegistryDockerPrivate, error) {
}

func (d *RegistryDockerPrivate) ValidateFields(v *validator.Validate) error {
return v.Struct(d)
err := v.Struct(d)
if (err != nil) || d.NonSecret.IsPublic == "true" {
return err
}

type AuthInfo struct {
DockerUsername string `json:"docker_username" validate:"required,min=2"`
DockerPassword string `json:"docker_password" validate:"required,min=2"`
}

auth := AuthInfo{}
auth.DockerUsername = d.NonSecret.DockerUsername
auth.DockerPassword = d.Secret.DockerPassword
return v.Struct(auth)

}

func (d *RegistryDockerPrivate) IsValidCredential() bool {
Expand Down
1 change: 1 addition & 0 deletions deepfence_server/pkg/registry/dockerprivate/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type RegistryDockerPrivate struct {
}

type NonSecret struct {
IsPublic string `json:"is_public" validate:"required"`
DockerRegistryURL string `json:"docker_registry_url" validate:"required,url"`
DockerUsername string `json:"docker_username" validate:"omitempty,min=2"`
}
Expand Down
13 changes: 5 additions & 8 deletions deepfence_server/pkg/registry/harbor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ var client = &http.Client{

func listImages(url, project, username, password string) ([]model.IngestedContainerImage, error) {

var (
images []model.IngestedContainerImage
)
var images []model.IngestedContainerImage

repos, err := listRepos(url, project, username, password)
if err != nil {
Expand Down Expand Up @@ -89,13 +87,12 @@ func listRepos(url, project, username, password string) ([]Repository, error) {
}

func listArtifacts(url, username, password, project, repo string) ([]Artifact, error) {
var (
err error
artifacts []Artifact
)
var artifacts []Artifact

listRepoTagsURL := "%s/api/v2.0/projects/%s/repositories/%s/artifacts"
queryURL := fmt.Sprintf(listRepoTagsURL, url, project, strings.TrimPrefix(repo, project))
repoName := strings.TrimPrefix(repo, project)
repoName = strings.TrimPrefix(repoName, "/")
queryURL := fmt.Sprintf(listRepoTagsURL, url, project, repoName)
req, err := http.NewRequest(http.MethodGet, queryURL, nil)
if err != nil {
log.Error().Msg(err.Error())
Expand Down
7 changes: 4 additions & 3 deletions deepfence_server/pkg/registry/quay/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ func listRepos(url, namespace, token string) ([]Repositories, error) {
resp, err := client.Do(req)
if err != nil {
log.Error().Msg(err.Error())
break
return nil, err
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Msg(err.Error())
break
return nil, err
}

if resp.StatusCode != http.StatusOK {
Expand All @@ -76,7 +76,8 @@ func listRepos(url, namespace, token string) ([]Repositories, error) {
}

var repos ReposResp
if err := json.Unmarshal(body, &repos); err != nil {
err = json.Unmarshal(body, &repos)
if err != nil {
log.Error().Msg(err.Error())
break
}
Expand Down
Loading