2017-05-23 18:56:10 +00:00
|
|
|
package proxy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/portainer/portainer"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// ErrDockerContainerIdentifierNotFound defines an error raised when Portainer is unable to find a container identifier
|
2018-06-11 13:13:19 +00:00
|
|
|
ErrDockerContainerIdentifierNotFound = portainer.Error("Docker container identifier not found")
|
|
|
|
containerIdentifier = "Id"
|
|
|
|
containerLabelForServiceIdentifier = "com.docker.swarm.service.id"
|
|
|
|
containerLabelForSwarmStackIdentifier = "com.docker.stack.namespace"
|
|
|
|
containerLabelForComposeStackIdentifier = "com.docker.compose.project"
|
2017-05-23 18:56:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// containerListOperation extracts the response as a JSON object, loop through the containers array
|
|
|
|
// decorate and/or filter the containers based on resource controls before rewriting the response
|
2018-05-06 07:15:57 +00:00
|
|
|
func containerListOperation(response *http.Response, executor *operationExecutor) error {
|
2017-05-23 18:56:10 +00:00
|
|
|
var err error
|
|
|
|
// ContainerList response is a JSON array
|
|
|
|
// https://docs.docker.com/engine/api/v1.28/#operation/ContainerList
|
|
|
|
responseArray, err := getResponseAsJSONArray(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
if executor.operationContext.isAdmin {
|
|
|
|
responseArray, err = decorateContainerList(responseArray, executor.operationContext.resourceControls)
|
2017-05-23 18:56:10 +00:00
|
|
|
} else {
|
2017-10-15 17:24:40 +00:00
|
|
|
responseArray, err = filterContainerList(responseArray, executor.operationContext)
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-01 08:14:55 +00:00
|
|
|
if executor.labelBlackList != nil {
|
|
|
|
responseArray, err = filterContainersWithBlackListedLabels(responseArray, executor.labelBlackList)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-23 18:56:10 +00:00
|
|
|
return rewriteResponse(response, responseArray, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// containerInspectOperation extracts the response as a JSON object, verify that the user
|
|
|
|
// has access to the container based on resource control (check are done based on the containerID and optional Swarm service ID)
|
|
|
|
// and either rewrite an access denied response or a decorated container.
|
2018-05-06 07:15:57 +00:00
|
|
|
func containerInspectOperation(response *http.Response, executor *operationExecutor) error {
|
2017-05-23 18:56:10 +00:00
|
|
|
// ContainerInspect response is a JSON object
|
|
|
|
// https://docs.docker.com/engine/api/v1.28/#operation/ContainerInspect
|
|
|
|
responseObject, err := getResponseAsJSONOBject(response)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseObject[containerIdentifier] == nil {
|
|
|
|
return ErrDockerContainerIdentifierNotFound
|
|
|
|
}
|
|
|
|
|
2017-10-15 17:24:40 +00:00
|
|
|
containerID := responseObject[containerIdentifier].(string)
|
|
|
|
responseObject, access := applyResourceAccessControl(responseObject, containerID, executor.operationContext)
|
|
|
|
if !access {
|
|
|
|
return rewriteAccessDeniedResponse(response)
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
containerLabels := extractContainerLabelsFromContainerInspectObject(responseObject)
|
2017-10-15 17:24:40 +00:00
|
|
|
responseObject, access = applyResourceAccessControlFromLabel(containerLabels, responseObject, containerLabelForServiceIdentifier, executor.operationContext)
|
|
|
|
if !access {
|
|
|
|
return rewriteAccessDeniedResponse(response)
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
responseObject, access = applyResourceAccessControlFromLabel(containerLabels, responseObject, containerLabelForSwarmStackIdentifier, executor.operationContext)
|
|
|
|
if !access {
|
|
|
|
return rewriteAccessDeniedResponse(response)
|
|
|
|
}
|
|
|
|
|
|
|
|
responseObject, access = applyResourceAccessControlFromLabel(containerLabels, responseObject, containerLabelForComposeStackIdentifier, executor.operationContext)
|
2017-10-15 17:24:40 +00:00
|
|
|
if !access {
|
|
|
|
return rewriteAccessDeniedResponse(response)
|
2017-05-23 18:56:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rewriteResponse(response, responseObject, http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractContainerLabelsFromContainerInspectObject retrieve the Labels of the container if present.
|
|
|
|
// Container schema reference: https://docs.docker.com/engine/api/v1.28/#operation/ContainerInspect
|
|
|
|
func extractContainerLabelsFromContainerInspectObject(responseObject map[string]interface{}) map[string]interface{} {
|
|
|
|
// Labels are stored under Config.Labels
|
|
|
|
containerConfigObject := extractJSONField(responseObject, "Config")
|
|
|
|
if containerConfigObject != nil {
|
|
|
|
containerLabelsObject := extractJSONField(containerConfigObject, "Labels")
|
|
|
|
return containerLabelsObject
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// extractContainerLabelsFromContainerListObject retrieve the Labels of the container if present.
|
|
|
|
// Container schema reference: https://docs.docker.com/engine/api/v1.28/#operation/ContainerList
|
|
|
|
func extractContainerLabelsFromContainerListObject(responseObject map[string]interface{}) map[string]interface{} {
|
|
|
|
// Labels are stored under Labels
|
|
|
|
containerLabelsObject := extractJSONField(responseObject, "Labels")
|
|
|
|
return containerLabelsObject
|
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
// decorateContainerList loops through all containers and decorates any container with an existing resource control.
|
|
|
|
// Resource controls checks are based on: resource identifier, service identifier (from label), stack identifier (from label).
|
|
|
|
// Container object schema reference: https://docs.docker.com/engine/api/v1.28/#operation/ContainerList
|
|
|
|
func decorateContainerList(containerData []interface{}, resourceControls []portainer.ResourceControl) ([]interface{}, error) {
|
|
|
|
decoratedContainerData := make([]interface{}, 0)
|
|
|
|
|
|
|
|
for _, container := range containerData {
|
|
|
|
|
|
|
|
containerObject := container.(map[string]interface{})
|
|
|
|
if containerObject[containerIdentifier] == nil {
|
|
|
|
return nil, ErrDockerContainerIdentifierNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
containerID := containerObject[containerIdentifier].(string)
|
|
|
|
containerObject = decorateResourceWithAccessControl(containerObject, containerID, resourceControls)
|
|
|
|
|
|
|
|
containerLabels := extractContainerLabelsFromContainerListObject(containerObject)
|
|
|
|
containerObject = decorateResourceWithAccessControlFromLabel(containerLabels, containerObject, containerLabelForServiceIdentifier, resourceControls)
|
2018-06-11 13:13:19 +00:00
|
|
|
containerObject = decorateResourceWithAccessControlFromLabel(containerLabels, containerObject, containerLabelForSwarmStackIdentifier, resourceControls)
|
|
|
|
containerObject = decorateResourceWithAccessControlFromLabel(containerLabels, containerObject, containerLabelForComposeStackIdentifier, resourceControls)
|
2017-10-15 17:24:40 +00:00
|
|
|
|
|
|
|
decoratedContainerData = append(decoratedContainerData, containerObject)
|
|
|
|
}
|
|
|
|
|
|
|
|
return decoratedContainerData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterContainerList loops through all containers and filters public containers (no associated resource control)
|
|
|
|
// as well as authorized containers (access granted to the user based on existing resource control).
|
|
|
|
// Authorized containers are decorated during the process.
|
|
|
|
// Resource controls checks are based on: resource identifier, service identifier (from label), stack identifier (from label).
|
|
|
|
// Container object schema reference: https://docs.docker.com/engine/api/v1.28/#operation/ContainerList
|
|
|
|
func filterContainerList(containerData []interface{}, context *restrictedOperationContext) ([]interface{}, error) {
|
|
|
|
filteredContainerData := make([]interface{}, 0)
|
|
|
|
|
|
|
|
for _, container := range containerData {
|
|
|
|
containerObject := container.(map[string]interface{})
|
|
|
|
if containerObject[containerIdentifier] == nil {
|
|
|
|
return nil, ErrDockerContainerIdentifierNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
containerID := containerObject[containerIdentifier].(string)
|
|
|
|
containerObject, access := applyResourceAccessControl(containerObject, containerID, context)
|
|
|
|
if access {
|
|
|
|
containerLabels := extractContainerLabelsFromContainerListObject(containerObject)
|
2018-06-11 13:13:19 +00:00
|
|
|
containerObject, access = applyResourceAccessControlFromLabel(containerLabels, containerObject, containerLabelForComposeStackIdentifier, context)
|
2017-10-15 17:24:40 +00:00
|
|
|
if access {
|
2018-06-11 13:13:19 +00:00
|
|
|
containerObject, access = applyResourceAccessControlFromLabel(containerLabels, containerObject, containerLabelForServiceIdentifier, context)
|
2017-10-15 17:24:40 +00:00
|
|
|
if access {
|
2018-06-11 13:13:19 +00:00
|
|
|
containerObject, access = applyResourceAccessControlFromLabel(containerLabels, containerObject, containerLabelForSwarmStackIdentifier, context)
|
|
|
|
if access {
|
|
|
|
filteredContainerData = append(filteredContainerData, containerObject)
|
|
|
|
}
|
2017-10-15 17:24:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredContainerData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// filterContainersWithLabels loops through a list of containers, and filters containers that do not contains
|
|
|
|
// any labels in the labels black list.
|
|
|
|
func filterContainersWithBlackListedLabels(containerData []interface{}, labelBlackList []portainer.Pair) ([]interface{}, error) {
|
|
|
|
filteredContainerData := make([]interface{}, 0)
|
|
|
|
|
|
|
|
for _, container := range containerData {
|
|
|
|
containerObject := container.(map[string]interface{})
|
|
|
|
|
|
|
|
containerLabels := extractContainerLabelsFromContainerListObject(containerObject)
|
|
|
|
if containerLabels != nil {
|
|
|
|
if !containerHasBlackListedLabel(containerLabels, labelBlackList) {
|
|
|
|
filteredContainerData = append(filteredContainerData, containerObject)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filteredContainerData = append(filteredContainerData, containerObject)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredContainerData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func containerHasBlackListedLabel(containerLabels map[string]interface{}, labelBlackList []portainer.Pair) bool {
|
|
|
|
for key, value := range containerLabels {
|
|
|
|
labelName := key
|
|
|
|
labelValue := value.(string)
|
|
|
|
|
|
|
|
for _, blackListedLabel := range labelBlackList {
|
|
|
|
if blackListedLabel.Name == labelName && blackListedLabel.Value == labelValue {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|