2019-11-12 23:41:42 +00:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
2020-07-29 09:10:46 +00:00
|
|
|
"bytes"
|
2019-11-12 23:41:42 +00:00
|
|
|
"context"
|
2020-07-29 09:10:46 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"io/ioutil"
|
2019-11-12 23:41:42 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/client"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/http/proxy/factory/responseutils"
|
2020-06-16 07:58:16 +00:00
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
2019-11-12 23:41:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
serviceObjectIdentifier = "ID"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getInheritedResourceControlFromServiceLabels(dockerClient *client.Client, serviceID string, resourceControls []portainer.ResourceControl) (*portainer.ResourceControl, error) {
|
|
|
|
service, _, err := dockerClient.ServiceInspectWithRaw(context.Background(), serviceID, types.ServiceInspectOptions{})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
swarmStackName := service.Spec.Labels[resourceLabelForDockerSwarmStackName]
|
|
|
|
if swarmStackName != "" {
|
2020-06-16 07:58:16 +00:00
|
|
|
return authorization.GetResourceControlByResourceIDAndType(swarmStackName, portainer.StackResourceControl, resourceControls), nil
|
2019-11-12 23:41:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// serviceListOperation extracts the response as a JSON array, loop through the service array
|
|
|
|
// decorate and/or filter the services based on resource controls before rewriting the response.
|
|
|
|
func (transport *Transport) serviceListOperation(response *http.Response, executor *operationExecutor) error {
|
|
|
|
// ServiceList response is a JSON array
|
|
|
|
// https://docs.docker.com/engine/api/v1.28/#operation/ServiceList
|
|
|
|
responseArray, err := responseutils.GetResponseAsJSONArray(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceOperationParameters := &resourceOperationParameters{
|
|
|
|
resourceIdentifierAttribute: serviceObjectIdentifier,
|
|
|
|
resourceType: portainer.ServiceResourceControl,
|
|
|
|
labelsObjectSelector: selectorServiceLabels,
|
|
|
|
}
|
|
|
|
|
|
|
|
responseArray, err = transport.applyAccessControlOnResourceList(resourceOperationParameters, responseArray, executor)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return responseutils.RewriteResponse(response, responseArray, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// serviceInspectOperation extracts the response as a JSON object, verify that the user
|
|
|
|
// has access to the service based on resource control and either rewrite an access denied response or a decorated service.
|
|
|
|
func (transport *Transport) serviceInspectOperation(response *http.Response, executor *operationExecutor) error {
|
|
|
|
//ServiceInspect response is a JSON object
|
|
|
|
//https://docs.docker.com/engine/api/v1.28/#operation/ServiceInspect
|
|
|
|
responseObject, err := responseutils.GetResponseAsJSONOBject(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceOperationParameters := &resourceOperationParameters{
|
|
|
|
resourceIdentifierAttribute: serviceObjectIdentifier,
|
|
|
|
resourceType: portainer.ServiceResourceControl,
|
|
|
|
labelsObjectSelector: selectorServiceLabels,
|
|
|
|
}
|
|
|
|
|
|
|
|
return transport.applyAccessControlOnResource(resourceOperationParameters, responseObject, response, executor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// selectorServiceLabels retrieve the labels object associated to the service object.
|
|
|
|
// Labels are available under the "Spec.Labels" property.
|
|
|
|
// API schema references:
|
|
|
|
// https://docs.docker.com/engine/api/v1.28/#operation/ServiceInspect
|
|
|
|
// https://docs.docker.com/engine/api/v1.28/#operation/ServiceList
|
|
|
|
func selectorServiceLabels(responseObject map[string]interface{}) map[string]interface{} {
|
|
|
|
serviceSpecObject := responseutils.GetJSONObject(responseObject, "Spec")
|
|
|
|
if serviceSpecObject != nil {
|
|
|
|
return responseutils.GetJSONObject(serviceSpecObject, "Labels")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-29 09:10:46 +00:00
|
|
|
|
|
|
|
func (transport *Transport) decorateServiceCreationOperation(request *http.Request) (*http.Response, error) {
|
|
|
|
type PartialService struct {
|
|
|
|
TaskTemplate struct {
|
|
|
|
ContainerSpec struct {
|
|
|
|
Mounts []struct {
|
|
|
|
Type string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
forbiddenResponse := &http.Response{
|
|
|
|
StatusCode: http.StatusForbidden,
|
|
|
|
}
|
|
|
|
|
|
|
|
isAdminOrEndpointAdmin, err := transport.isAdminOrEndpointAdmin(request)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !isAdminOrEndpointAdmin {
|
|
|
|
settings, err := transport.dataStore.Settings().Settings()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
body, err := ioutil.ReadAll(request.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
partialService := &PartialService{}
|
|
|
|
err = json.Unmarshal(body, partialService)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !settings.AllowBindMountsForRegularUsers && (len(partialService.TaskTemplate.ContainerSpec.Mounts) > 0) {
|
|
|
|
for _, mount := range partialService.TaskTemplate.ContainerSpec.Mounts {
|
|
|
|
if mount.Type == "bind" {
|
|
|
|
return forbiddenResponse, errors.New("forbidden to use bind mounts")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
request.Body = ioutil.NopCloser(bytes.NewBuffer(body))
|
|
|
|
}
|
|
|
|
|
|
|
|
return transport.replaceRegistryAuthenticationHeader(request)
|
|
|
|
}
|