2018-06-11 15:13:19 +02:00
package registries
import (
2020-07-08 00:57:52 +03:00
"errors"
2021-12-01 13:18:57 +13:00
"github.com/portainer/portainer/api/internal/endpointutils"
2018-06-11 15:13:19 +02:00
"net/http"
2018-09-10 12:01:38 +02:00
httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response"
2021-01-18 02:59:57 +02:00
portainer "github.com/portainer/portainer/api"
2020-07-08 00:57:52 +03:00
bolterrors "github.com/portainer/portainer/api/bolt/errors"
2021-07-14 11:15:21 +02:00
httperrors "github.com/portainer/portainer/api/http/errors"
"github.com/portainer/portainer/api/http/security"
2018-06-11 15:13:19 +02:00
)
type registryUpdatePayload struct {
2021-07-14 11:15:21 +02:00
// Name that will be used to identify this registry
Name * string ` validate:"required" example:"my-registry" `
// URL or IP address of the Docker registry
URL * string ` validate:"required" example:"registry.mydomain.tld:2375" `
// BaseURL is used for quay registry
BaseURL * string ` json:",omitempty" example:"registry.mydomain.tld:2375" `
// Is authentication against this registry enabled
Authentication * bool ` example:"false" validate:"required" `
// Username used to authenticate against this registry. Required when Authentication is true
Username * string ` example:"registry_user" `
// Password used to authenticate against this registry. required when Authentication is true
Password * string ` example:"registry_password" `
2021-12-01 13:18:57 +13:00
// Quay data
2021-07-14 11:15:21 +02:00
Quay * portainer . QuayRegistryData
2021-12-01 13:18:57 +13:00
// Registry access control
RegistryAccesses * portainer . RegistryAccesses ` json:",omitempty" `
// ECR data
Ecr * portainer . EcrData ` json:",omitempty" `
2018-06-11 15:13:19 +02:00
}
func ( payload * registryUpdatePayload ) Validate ( r * http . Request ) error {
return nil
}
2021-02-23 05:21:39 +02:00
// @id RegistryUpdate
// @summary Update a registry
// @description Update a registry
2021-10-12 12:12:08 +13:00
// @description **Access policy**: restricted
2021-02-23 05:21:39 +02:00
// @tags registries
2021-11-30 15:31:16 +13:00
// @security ApiKeyAuth
2021-02-23 05:21:39 +02:00
// @security jwt
// @accept json
// @produce json
// @param id path int true "Registry identifier"
// @param body body registryUpdatePayload true "Registry details"
// @success 200 {object} portainer.Registry "Success"
// @failure 400 "Invalid request"
// @failure 404 "Registry not found"
// @failure 409 "Another registry with the same URL already exists"
// @failure 500 "Server error"
// @router /registries/{id} [put]
2018-06-11 15:13:19 +02:00
func ( handler * Handler ) registryUpdate ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
2021-07-14 11:15:21 +02:00
securityContext , err := security . RetrieveRestrictedRequestContext ( r )
2018-06-11 15:13:19 +02:00
if err != nil {
2021-07-14 11:15:21 +02:00
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to retrieve info from request context" , err }
}
2021-12-01 13:18:57 +13:00
2021-07-14 11:15:21 +02:00
if ! securityContext . IsAdmin {
return & httperror . HandlerError { http . StatusForbidden , "Permission denied to update registry" , httperrors . ErrResourceAccessDenied }
2018-06-11 15:13:19 +02:00
}
2021-07-14 11:15:21 +02:00
registryID , err := request . RetrieveNumericRouteVariableValue ( r , "id" )
2018-06-11 15:13:19 +02:00
if err != nil {
2021-07-14 11:15:21 +02:00
return & httperror . HandlerError { http . StatusBadRequest , "Invalid registry identifier route variable" , err }
2018-06-11 15:13:19 +02:00
}
2020-05-20 17:23:15 +12:00
registry , err := handler . DataStore . Registry ( ) . Registry ( portainer . RegistryID ( registryID ) )
2020-07-08 00:57:52 +03:00
if err == bolterrors . ErrObjectNotFound {
2018-06-11 15:13:19 +02:00
return & httperror . HandlerError { http . StatusNotFound , "Unable to find a registry with the specified identifier inside the database" , err }
} else if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to find a registry with the specified identifier inside the database" , err }
}
2021-07-14 11:15:21 +02:00
var payload registryUpdatePayload
err = request . DecodeAndValidateJSONPayload ( r , & payload )
if err != nil {
return & httperror . HandlerError { http . StatusBadRequest , "Invalid request payload" , err }
}
2019-09-10 10:55:27 +12:00
if payload . Name != nil {
registry . Name = * payload . Name
2018-06-11 15:13:19 +02:00
}
2019-09-10 10:55:27 +12:00
2021-07-01 14:57:15 +12:00
if registry . Type == portainer . ProGetRegistry && payload . BaseURL != nil {
registry . BaseURL = * payload . BaseURL
}
2021-12-01 13:18:57 +13:00
shouldUpdateSecrets := false
2019-09-10 10:55:27 +12:00
if payload . Authentication != nil {
2021-12-01 13:18:57 +13:00
shouldUpdateSecrets = shouldUpdateSecrets || ( registry . Authentication != * payload . Authentication )
2019-09-10 10:55:27 +12:00
if * payload . Authentication {
registry . Authentication = true
if payload . Username != nil {
2021-12-01 13:18:57 +13:00
shouldUpdateSecrets = shouldUpdateSecrets || ( registry . Username != * payload . Username )
2019-09-10 10:55:27 +12:00
registry . Username = * payload . Username
}
2018-06-11 15:13:19 +02:00
2021-01-18 02:59:57 +02:00
if payload . Password != nil && * payload . Password != "" {
2021-12-01 13:18:57 +13:00
shouldUpdateSecrets = shouldUpdateSecrets || ( registry . Password != * payload . Password )
2019-09-10 10:55:27 +12:00
registry . Password = * payload . Password
}
2021-12-01 13:18:57 +13:00
if registry . Type == portainer . EcrRegistry && payload . Ecr != nil && payload . Ecr . Region != "" {
shouldUpdateSecrets = shouldUpdateSecrets || ( registry . Ecr . Region != payload . Ecr . Region )
registry . Ecr . Region = payload . Ecr . Region
}
2019-09-10 10:55:27 +12:00
} else {
registry . Authentication = false
registry . Username = ""
registry . Password = ""
2021-12-01 13:18:57 +13:00
registry . Ecr . Region = ""
registry . AccessToken = ""
registry . AccessTokenExpiry = 0
2019-09-10 10:55:27 +12:00
}
2018-06-11 15:13:19 +02:00
}
2021-07-14 11:15:21 +02:00
if payload . URL != nil {
shouldUpdateSecrets = shouldUpdateSecrets || ( * payload . URL != registry . URL )
registry . URL = * payload . URL
registries , err := handler . DataStore . Registry ( ) . Registries ( )
if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to retrieve registries from the database" , err }
}
2021-12-01 13:18:57 +13:00
2021-07-14 11:15:21 +02:00
for _ , r := range registries {
if r . ID != registry . ID && handler . registriesHaveSameURLAndCredentials ( & r , registry ) {
return & httperror . HandlerError { http . StatusConflict , "Another registry with the same URL and credentials already exists" , errors . New ( "A registry is already defined for this URL and credentials" ) }
}
}
2019-05-24 18:04:58 +12:00
}
2021-07-14 11:15:21 +02:00
if shouldUpdateSecrets {
2021-12-01 13:18:57 +13:00
registry . AccessToken = ""
registry . AccessTokenExpiry = 0
2021-07-14 11:15:21 +02:00
for endpointID , endpointAccess := range registry . RegistryAccesses {
endpoint , err := handler . DataStore . Endpoint ( ) . Endpoint ( endpointID )
if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to update access to registry" , err }
}
2021-12-01 13:18:57 +13:00
if endpointutils . IsKubernetesEndpoint ( endpoint ) {
2021-07-14 11:15:21 +02:00
err = handler . updateEndpointRegistryAccess ( endpoint , registry , endpointAccess )
if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to update access to registry" , err }
}
}
}
2019-05-24 18:04:58 +12:00
}
2021-03-13 12:47:35 +13:00
if payload . Quay != nil {
registry . Quay = * payload . Quay
}
2020-05-20 17:23:15 +12:00
err = handler . DataStore . Registry ( ) . UpdateRegistry ( registry . ID , registry )
2018-06-11 15:13:19 +02:00
if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to persist registry changes inside the database" , err }
}
return response . JSON ( w , registry )
}
2020-06-04 09:50:02 +03:00
2021-07-14 11:15:21 +02:00
func ( handler * Handler ) updateEndpointRegistryAccess ( endpoint * portainer . Endpoint , registry * portainer . Registry , endpointAccess portainer . RegistryAccessPolicies ) error {
cli , err := handler . K8sClientFactory . GetKubeClient ( endpoint )
if err != nil {
return err
}
for _ , namespace := range endpointAccess . Namespaces {
err := cli . DeleteRegistrySecret ( registry , namespace )
if err != nil {
return err
}
err = cli . CreateRegistrySecret ( registry , namespace )
if err != nil {
return err
}
2020-06-04 09:50:02 +03:00
}
2021-07-14 11:15:21 +02:00
return nil
2020-06-04 09:50:02 +03:00
}