fix(api): fix an issue with RegistryUpdate operation (#3137)

pull/3148/head
Anthony Lapenna 2019-09-10 10:55:27 +12:00 committed by GitHub
parent 2b48f1e49a
commit 628d4960cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 30 deletions

View File

@ -3,7 +3,6 @@ package registries
import ( import (
"net/http" "net/http"
"github.com/asaskevich/govalidator"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request" "github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response" "github.com/portainer/libhttp/response"
@ -11,19 +10,16 @@ import (
) )
type registryUpdatePayload struct { type registryUpdatePayload struct {
Name string Name *string
URL string URL *string
Authentication bool Authentication *bool
Username string Username *string
Password string Password *string
UserAccessPolicies portainer.UserAccessPolicies UserAccessPolicies portainer.UserAccessPolicies
TeamAccessPolicies portainer.TeamAccessPolicies TeamAccessPolicies portainer.TeamAccessPolicies
} }
func (payload *registryUpdatePayload) Validate(r *http.Request) error { func (payload *registryUpdatePayload) Validate(r *http.Request) error {
if payload.Authentication && (govalidator.IsNull(payload.Username) || govalidator.IsNull(payload.Password)) {
return portainer.Error("Invalid credentials. Username and password must be specified when authentication is enabled")
}
return nil return nil
} }
@ -47,32 +43,41 @@ func (handler *Handler) registryUpdate(w http.ResponseWriter, r *http.Request) *
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a registry with the specified identifier inside the database", err} return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find a registry with the specified identifier inside the database", err}
} }
registries, err := handler.RegistryService.Registries() if payload.Name != nil {
if err != nil { registry.Name = *payload.Name
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve registries from the database", err}
} }
for _, r := range registries {
if r.URL == payload.URL && r.ID != registry.ID { if payload.URL != nil {
return &httperror.HandlerError{http.StatusConflict, "Another registry with the same URL already exists", portainer.ErrRegistryAlreadyExists} registries, err := handler.RegistryService.Registries()
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve registries from the database", err}
} }
for _, r := range registries {
if r.URL == *payload.URL && r.ID != registry.ID {
return &httperror.HandlerError{http.StatusConflict, "Another registry with the same URL already exists", portainer.ErrRegistryAlreadyExists}
}
}
registry.URL = *payload.URL
} }
if payload.Name != "" { if payload.Authentication != nil {
registry.Name = payload.Name if *payload.Authentication {
} registry.Authentication = true
if payload.URL != "" { if payload.Username != nil {
registry.URL = payload.URL registry.Username = *payload.Username
} }
if payload.Authentication { if payload.Password != nil {
registry.Authentication = true registry.Password = *payload.Password
registry.Username = payload.Username }
registry.Password = payload.Password
} else { } else {
registry.Authentication = false registry.Authentication = false
registry.Username = "" registry.Username = ""
registry.Password = "" registry.Password = ""
}
} }
if payload.UserAccessPolicies != nil { if payload.UserAccessPolicies != nil {

View File

@ -64,7 +64,7 @@
<!-- !authentication-credentials --> <!-- !authentication-credentials -->
<div class="form-group"> <div class="form-group">
<div class="col-sm-12"> <div class="col-sm-12">
<button type="button" class="btn btn-primary btn-sm" ng-disabled="state.actionInProgress || !registry.Name || !registry.URL || (registry.Authentication && (!registry.Username || !formValues.Password))" ng-click="updateRegistry()" button-spinner="state.actionInProgress"> <button type="button" class="btn btn-primary btn-sm" ng-disabled="state.actionInProgress || !registry.Name || !registry.URL" ng-click="updateRegistry()" button-spinner="state.actionInProgress">
<span ng-hide="state.actionInProgress">Update registry</span> <span ng-hide="state.actionInProgress">Update registry</span>
<span ng-show="state.actionInProgress">Updating registry...</span> <span ng-show="state.actionInProgress">Updating registry...</span>
</button> </button>