Skip to content
This repository was archived by the owner on Mar 27, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions handlers_containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ type PsPayload struct {
ContainerID string `json:"id,omitempty"`
}

type ImagesPayload struct {
ImageName string `json:"name,omitempty"`
}

type ChangeNamePayload struct {
ContainerID string `json:"id"`
ContainerName string `json:"name"`
Expand All @@ -72,7 +76,7 @@ func (s *Status) ContainersPsEvent(client mqtt.Client, msg mqtt.Message) {
psPayload := PsPayload{}
err := json.Unmarshal(msg.Payload(), &psPayload)
if err != nil {
s.Error("/containers/action", errors.Wrapf(err, "unmarshal %s", msg.Payload()))
s.Error("/containers/ps", errors.Wrapf(err, "unmarshal %s", msg.Payload()))
return
}

Expand All @@ -98,7 +102,19 @@ func (s *Status) ContainersPsEvent(client mqtt.Client, msg mqtt.Message) {

// ContainersListImagesEvent implements docker images
func (s *Status) ContainersListImagesEvent(client mqtt.Client, msg mqtt.Message) {
images, err := s.dockerClient.ImageList(context.Background(), types.ImageListOptions{})
imagesPayload := ImagesPayload{}
err := json.Unmarshal(msg.Payload(), &imagesPayload)
if err != nil {
s.Error("/containers/images", errors.Wrapf(err, "unmarshal %s", msg.Payload()))
return
}

imageListOptions := types.ImageListOptions{All: true}
if imagesPayload.ImageName != "" {
imageListOptions.Filters = filters.NewArgs(filters.Arg("reference", imagesPayload.ImageName))
}

images, err := s.dockerClient.ImageList(context.Background(), imageListOptions)
if err != nil {
s.Error("/containers/images", fmt.Errorf("images result: %s", err))
return
Expand Down Expand Up @@ -166,6 +182,7 @@ func (s *Status) ContainersActionEvent(client mqtt.Client, msg mqtt.Message) {
if authConfig != nil {
_, err = s.dockerClient.RegistryLogin(ctx, *authConfig)
if err != nil {
ClearRegistryAuth(runParams)
s.Error("/containers/action", fmt.Errorf("auth test failed: %s", err))
return
}
Expand Down Expand Up @@ -304,6 +321,18 @@ func ConfigureRegistryAuth(runParams RunPayload) (types.ImagePullOptions, *types
return pullOpts, authConfig, err
}

// ClearRegistryAuth removes credential for a certain registry from docker config
func ClearRegistryAuth(runParams RunPayload) {
loadedConfigFile, err := dockerConfig.Load(dockerConfig.Dir())
if err != nil {
panic(err)
}
imageRegistryEndpoint := strings.Split(runParams.ImageName, "/")[0]
delete(loadedConfigFile.AuthConfigs, imageRegistryEndpoint)
loadedConfigFile.Save()

}

// checkAndInstallDocker implements steps from https://docs.docker.com/install/linux/docker-ce/ubuntu/
func checkAndInstallDocker() {
cli, err := docker.NewClientWithOpts(docker.WithVersion("1.38"))
Expand Down
32 changes: 27 additions & 5 deletions handlers_containers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import (
"testing"
"time"

"github.com/docker/docker/api/types"

"github.com/stretchr/testify/assert"

"github.com/eclipse/paho.mqtt.golang"
Expand Down Expand Up @@ -493,20 +495,25 @@ func TestContainersRunWithAuthTestFail(t *testing.T) {
"password":"%s",
"name": "my-private-img"
}`, os.Getenv("CONNECTOR_PRIV_IMAGE"), os.Getenv("CONNECTOR_PRIV_USER"), "MYWRONGPASSWORD")

registryEndpoint := strings.Split(os.Getenv("CONNECTOR_PRIV_IMAGE"), "/")[0]
response := mqtt.MqttSendAndReceiveSync(t, topic, RunMqttRequest)
t.Log(response)
outputMessage, err := ExecAsVagrantSshCmd("sudo cat /root/.docker/config.json")
if err != nil {
t.Error(err)
}
t.Log(outputMessage)
assert.Equal(t, false, strings.Contains(outputMessage, registryEndpoint))
assert.Equal(t, true, strings.Contains(response, "ERROR: "))
assert.Equal(t, true, strings.Contains(response, "auth test failed"))
}

func TestMultipleContainersRunWithPsFilterCheck(t *testing.T) {
func TestMultipleContainersRunWithPsAndImageFilterCheck(t *testing.T) {
mqtt := NewMqttTestClient()
defer mqtt.Close()

topic := "containers/action"
// container run both test mqtt response andon VM

responseAlfa := mqtt.MqttSendAndReceiveSync(t, topic, `{"action": "run","image": "redis","name": "redis-alfa"}`)
responseAlfa = strings.Replace(responseAlfa, "INFO: ", "", 1)
t.Log(responseAlfa)
Expand All @@ -516,7 +523,7 @@ func TestMultipleContainersRunWithPsFilterCheck(t *testing.T) {
t.Fatalf("Unmarshal error: %s", responseAlfa)

}
responseBeta := mqtt.MqttSendAndReceiveSync(t, topic, `{"action": "run","image": "redis","name": "redis-beta"}`)
responseBeta := mqtt.MqttSendAndReceiveSync(t, topic, `{"action": "run","image": "mongo","name": "mongo-beta"}`)
responseBeta = strings.Replace(responseBeta, "INFO: ", "", 1)
t.Log(responseBeta)
betaParams := RunPayload{}
Expand All @@ -540,19 +547,34 @@ func TestMultipleContainersRunWithPsFilterCheck(t *testing.T) {
t.Error(err)
}
areContainersNotReady = !(strings.Contains(outputMessage, "redis-alfa") &&
strings.Contains(outputMessage, "redis-beta"))
strings.Contains(outputMessage, "mongo-beta"))
}
t.Log(outputMessage)
//container ps with filter test
psAlfaResponse := mqtt.MqttSendAndReceiveSync(t, "containers/ps", fmt.Sprintf(`{"id": "%s" }`, alfaParams.ContainerID))
t.Log(psAlfaResponse)
psBetaResponse := mqtt.MqttSendAndReceiveSync(t, "containers/ps", fmt.Sprintf(`{"id": "%s" }`, betaParams.ContainerID))
t.Log(psAlfaResponse)
psWrongIdResponse := mqtt.MqttSendAndReceiveSync(t, "containers/ps", `{"id": "NON EXISTENT CONTAINER ID" }`)

assert.Equal(t, true, strings.Contains(psAlfaResponse, alfaParams.ContainerID))
assert.Equal(t, false, strings.Contains(psAlfaResponse, betaParams.ContainerID))
assert.Equal(t, true, strings.Contains(psBetaResponse, betaParams.ContainerID))
assert.Equal(t, false, strings.Contains(psBetaResponse, alfaParams.ContainerID))
assert.Equal(t, false, strings.Contains(psWrongIdResponse, alfaParams.ContainerID))
assert.Equal(t, false, strings.Contains(psWrongIdResponse, betaParams.ContainerID))

// test also for image filtering
containers := make([]types.Container, 10)
psAlfaResponse = strings.Replace(psAlfaResponse, "INFO: ", "", 1)
merr = json.Unmarshal([]byte(psAlfaResponse), &containers)
if merr != nil {
t.Fatalf("Unmarshal error: %s", responseBeta)
}
imageAlfaResponse := mqtt.MqttSendAndReceiveSync(t, "containers/images", fmt.Sprintf(`{"name": "%s" }`, containers[0].Image))
t.Log(imageAlfaResponse)
assert.Equal(t, true, strings.Contains(imageAlfaResponse, "redis"))
assert.Equal(t, false, strings.Contains(imageAlfaResponse, "mongo"))

// cleanup
RemoveMqttRequest := fmt.Sprintf(`{"action": "remove","id":"%s"}`, alfaParams.ContainerID)
Expand Down