Skip to content

Commit

Permalink
ISSUE-2130: Fixing the dockerhub validation and error message
Browse files Browse the repository at this point in the history
  • Loading branch information
varunsharma0286 committed Feb 14, 2024
1 parent d3b5b0d commit 6c6fe4e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
24 changes: 18 additions & 6 deletions deepfence_server/handler/container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"strconv"
Expand Down Expand Up @@ -325,14 +326,22 @@ func (h *Handler) UpdateRegistry(w http.ResponseWriter, r *http.Request) {
func (h *Handler) AddGoogleContainerRegistry(w http.ResponseWriter, r *http.Request) {

defer r.Body.Close()
failureMsg := "Failed to add registry, Error: %s"

if err := r.ParseMultipartForm(1024 * 1024); err != nil {
h.respondError(&BadDecoding{err}, w)
log.Error().Msgf("%v", err)
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, err.Error())}, w)
return
}

file, fileHeader, err := r.FormFile("service_account_json")
if err != nil {
h.respondError(&BadDecoding{err}, w)
log.Error().Msgf("%v", err)
if err == http.ErrMissingFile {
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, "Missing file")}, w)
} else {
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, err.Error())}, w)
}
return
}
defer file.Close()
Expand All @@ -347,7 +356,8 @@ func (h *Handler) AddGoogleContainerRegistry(w http.ResponseWriter, r *http.Requ

fileBytes, err := io.ReadAll(file)
if err != nil {
h.respondError(&BadDecoding{err}, w)
log.Error().Msgf("%v", err)
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, err.Error())}, w)
return
}

Expand All @@ -371,7 +381,8 @@ func (h *Handler) AddGoogleContainerRegistry(w http.ResponseWriter, r *http.Requ

var sa gcr.ServiceAccountJSON
if err := json.Unmarshal(fileBytes, &sa); err != nil {
h.respondError(&BadDecoding{err}, w)
log.Error().Msgf("%v", err)
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, err.Error())}, w)
return
}

Expand All @@ -387,14 +398,14 @@ func (h *Handler) AddGoogleContainerRegistry(w http.ResponseWriter, r *http.Requ
b, err := json.Marshal(req)
if err != nil {
log.Error().Msgf("%v", err)
h.respondError(&BadDecoding{err}, w)
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, err.Error())}, w)
return
}

registry, err := registry.GetRegistry(constants.GCR, b)
if err != nil {
log.Error().Msgf("%v", err)
h.respondError(&BadDecoding{err}, w)
h.respondError(&BadDecoding{fmt.Errorf(failureMsg, err.Error())}, w)
return
}

Expand All @@ -420,6 +431,7 @@ func (h *Handler) AddGoogleContainerRegistry(w http.ResponseWriter, r *http.Requ
ctx := r.Context()
pgClient, err := directory.PostgresClient(ctx)
if err != nil {
log.Error().Msgf("%v", err)
h.respondError(&InternalServerError{err}, w)
return
}
Expand Down
6 changes: 5 additions & 1 deletion deepfence_server/pkg/registry/dockerhub/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ func (d *RegistryDockerHub) ValidateFields(v *validator.Validate) error {

func (d *RegistryDockerHub) IsValidCredential() bool {
if d.NonSecret.DockerHubUsername == "" {
return true
if d.NonSecret.IsPublic == "true" {
return true
} else {
return false
}
}

jsonData := map[string]interface{}{"username": d.NonSecret.DockerHubUsername, "password": d.Secret.DockerHubPassword}
Expand Down
1 change: 1 addition & 0 deletions deepfence_server/pkg/registry/dockerhub/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type RegistryDockerHub struct {
}

type NonSecret struct {
IsPublic string `json:"is_public" validate:"required"`
DockerHubNamespace string `json:"docker_hub_namespace" validate:"required,min=2"`
DockerHubUsername string `json:"docker_hub_username" validate:"omitempty,min=2"`
}
Expand Down

0 comments on commit 6c6fe4e

Please sign in to comment.