Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: launch ephemeral containers targetting containers running with non root access #4288

Merged
merged 11 commits into from
Dec 4, 2023
8 changes: 8 additions & 0 deletions api/k8s/application/k8sApplicationRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ func (handler *K8sApplicationRestHandlerImpl) GetResource(w http.ResponseWriter,
common.WriteJsonResp(w, err, resource, http.StatusInternalServerError)
return
}
if resource != nil {
err = resource.SetRunningEphemeralContainers()
if err != nil {
handler.logger.Errorw("error in setting running ephemeral containers and setting them in resource response", "err", err)
common.WriteJsonResp(w, err, resource, http.StatusInternalServerError)
return
}
}

canUpdate := false
// Obfuscate secret if user does not have edit access
Expand Down
5 changes: 3 additions & 2 deletions api/restHandler/AppListingRestHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ import (
"github.com/caarlos0/env/v6"
k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
"github.com/devtron-labs/common-lib/utils/k8s/health"
k8sObjectUtils "github.com/devtron-labs/common-lib/utils/k8sObjectsUtil"
"github.com/devtron-labs/devtron/api/bean"
client "github.com/devtron-labs/devtron/api/helm-app"
bean2 "github.com/devtron-labs/devtron/api/restHandler/bean"

"github.com/devtron-labs/devtron/api/restHandler/common"
"github.com/devtron-labs/devtron/client/argocdServer/application"
"github.com/devtron-labs/devtron/client/cron"
Expand Down Expand Up @@ -1542,7 +1543,7 @@ func (handler AppListingRestHandlerImpl) fetchResourceTree(w http.ResponseWriter
handler.logger.Errorw("error in getting pods by label", "err", err, "clusterId", cdPipeline.Environment.ClusterId, "namespace", cdPipeline.Environment.Namespace, "label", label)
return resourceTree, err
}
ephemeralContainersMap := bean2.ExtractEphemeralContainers(pods)
ephemeralContainersMap := k8sObjectUtils.ExtractEphemeralContainers(pods)
for _, metaData := range resp.PodMetadata {
metaData.EphemeralContainers = ephemeralContainersMap[metaData.Name]
}
Expand Down
54 changes: 0 additions & 54 deletions api/restHandler/bean/bean.go

This file was deleted.

17 changes: 10 additions & 7 deletions client/argocdServer/application/Application.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/devtron-labs/devtron/api/restHandler/bean"
k8sObjectUtils "github.com/devtron-labs/common-lib/utils/k8sObjectsUtil"
"github.com/devtron-labs/devtron/client/argocdServer/connection"
"strings"
"time"
Expand Down Expand Up @@ -99,12 +99,15 @@ type ResourceTreeResponse struct {
}

type PodMetadata struct {
Name string `json:"name"`
UID string `json:"uid"`
Containers []*string `json:"containers"`
InitContainers []*string `json:"initContainers"`
IsNew bool `json:"isNew"`
EphemeralContainers []*bean.EphemeralContainerData `json:"ephemeralContainers"`
Name string `json:"name"`
UID string `json:"uid"`
Containers []*string `json:"containers"`
InitContainers []*string `json:"initContainers"`
IsNew bool `json:"isNew"`
// EphemeralContainers are set for Pod kind manifest response only
// will always contain running ephemeral containers
// +optional
EphemeralContainers []*k8sObjectUtils.EphemeralContainerData `json:"ephemeralContainers"`
}

type Manifests struct {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/deckarep/golang-set v1.8.0
github.com/devtron-labs/authenticator v0.4.31
github.com/devtron-labs/common-lib v0.0.7
github.com/devtron-labs/common-lib v0.0.8-0.20231204083642-08f7f57000d9
github.com/devtron-labs/protos v0.0.0-20230503113602-282404f70fd2
github.com/evanphx/json-patch v5.6.0+incompatible
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,8 @@ github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4 h1:YcpmyvADG
github.com/denisenkom/go-mssqldb v0.0.0-20190707035753-2be1aa521ff4/go.mod h1:zAg7JM8CkOJ43xKXIj7eRO9kmWm/TW578qo+oDO6tuM=
github.com/devtron-labs/authenticator v0.4.31 h1:CEMLek3JnMuH9ULsC6BHNJr+NiyGzBd4lgdSxH2IGnc=
github.com/devtron-labs/authenticator v0.4.31/go.mod h1:ozNfT8WcruiSgnUbyp48WVfc41++W6xYXhKFp67lNTU=
github.com/devtron-labs/common-lib v0.0.7 h1:3WqZcOil8/Z6BcesasLjihzTa7QnntA7DnG5/En/Dxo=
github.com/devtron-labs/common-lib v0.0.7/go.mod h1:x6OdUIo2z9kxXtBfz7fJEfD4s8kiAtEmlApozOf7ECM=
github.com/devtron-labs/common-lib v0.0.8-0.20231204083642-08f7f57000d9 h1:zIYeYpnj2vB6P17xtpbgEsagmgJpSN4kzJMd8UJ/WR0=
github.com/devtron-labs/common-lib v0.0.8-0.20231204083642-08f7f57000d9/go.mod h1:x6OdUIo2z9kxXtBfz7fJEfD4s8kiAtEmlApozOf7ECM=
github.com/devtron-labs/protos v0.0.0-20230503113602-282404f70fd2 h1:/IEIsJTxDZ3hv8uOoCaqdWCXqcv7nCAgX9AP/v84dUY=
github.com/devtron-labs/protos v0.0.0-20230503113602-282404f70fd2/go.mod h1:l85jxWHlcSo910hdUfRycL40yGzC6glE93V1sVxVPto=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
Expand Down
4 changes: 2 additions & 2 deletions pkg/appStore/deployment/service/InstalledAppService.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"github.com/argoproj/argo-cd/v2/pkg/apiclient/application"
util4 "github.com/devtron-labs/common-lib/utils/k8s"
k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
k8sObjectUtils "github.com/devtron-labs/common-lib/utils/k8sObjectsUtil"
client "github.com/devtron-labs/devtron/api/helm-app"
openapi "github.com/devtron-labs/devtron/api/helm-app/openapiClient"
bean3 "github.com/devtron-labs/devtron/api/restHandler/bean"
"github.com/devtron-labs/devtron/client/argocdServer"
"github.com/devtron-labs/devtron/internal/constants"
"github.com/devtron-labs/devtron/internal/middleware"
Expand Down Expand Up @@ -1436,7 +1436,7 @@ func (impl InstalledAppServiceImpl) fetchResourceTreeForACD(rctx context.Context
impl.logger.Errorw("error in getting pods by label", "err", err, "clusterId", clusterId, "namespace", namespace, "label", label)
return resourceTree, err
}
ephemeralContainersMap := bean3.ExtractEphemeralContainers(pods)
ephemeralContainersMap := k8sObjectUtils.ExtractEphemeralContainers(pods)
for _, metaData := range resp.PodMetadata {
metaData.EphemeralContainers = ephemeralContainersMap[metaData.Name]
}
Expand Down
8 changes: 5 additions & 3 deletions pkg/k8s/application/k8sApplicationService.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/caarlos0/env/v6"
k8s2 "github.com/devtron-labs/common-lib/utils/k8s"
k8sCommonBean "github.com/devtron-labs/common-lib/utils/k8s/commonBean"
k8sObjectUtils "github.com/devtron-labs/common-lib/utils/k8sObjectsUtil"

yamlUtil "github.com/devtron-labs/common-lib/utils/yaml"
"github.com/devtron-labs/devtron/api/connector"
client "github.com/devtron-labs/devtron/api/helm-app"
Expand Down Expand Up @@ -821,8 +823,8 @@ func (impl *K8sApplicationServiceImpl) generateDebugContainer(pod *corev1.Pod, r
}
}
ephemeralContainer.Name = ephemeralContainer.Name + "-" + util2.Generate(5)
scriptCreateCommand := fmt.Sprintf("echo 'while true; do sleep 600; done;' > %s-devtron.sh", ephemeralContainer.Name)
scriptRunCommand := fmt.Sprintf("sh %s-devtron.sh", ephemeralContainer.Name)
scriptCreateCommand := fmt.Sprintf("echo 'while true; do sleep 600; done;' > "+k8sObjectUtils.EphemeralContainerStartingShellScriptFileName, ephemeralContainer.Name)
scriptRunCommand := fmt.Sprintf("sh "+k8sObjectUtils.EphemeralContainerStartingShellScriptFileName, ephemeralContainer.Name)
ephemeralContainer.Command = []string{"sh", "-c", scriptCreateCommand + " && " + scriptRunCommand}
copied.Spec.EphemeralContainers = append(copied.Spec.EphemeralContainers, *ephemeralContainer)
ephemeralContainer = &copied.Spec.EphemeralContainers[len(copied.Spec.EphemeralContainers)-1]
Expand All @@ -845,7 +847,7 @@ func (impl *K8sApplicationServiceImpl) TerminatePodEphemeralContainer(req cluste
if container == nil {
return false, errors.New("externally created ephemeral containers cannot be removed")
}
containerKillCommand := fmt.Sprintf("kill -16 $(pgrep -f '%s-devtron' -o)", terminalReq.ContainerName)
containerKillCommand := fmt.Sprintf("kill -16 $(pgrep -f '%s' -o)", fmt.Sprintf(k8sObjectUtils.EphemeralContainerStartingShellScriptFileName, terminalReq.ContainerName))
cmds := []string{"sh", "-c", containerKillCommand}
_, errBuf, err := impl.terminalSession.RunCmdInRemotePod(terminalReq, cmds)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package k8s

import (
"github.com/devtron-labs/common-lib/utils/k8s"
"github.com/devtron-labs/devtron/api/helm-app"
client "github.com/devtron-labs/devtron/api/helm-app"
"github.com/devtron-labs/devtron/pkg/k8s/application/bean"
)

Expand Down
Empty file removed util/k8s/K8sUtil_test.go
Empty file.
10 changes: 5 additions & 5 deletions vendor/github.com/devtron-labs/common-lib/utils/k8s/K8sUtil.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions vendor/github.com/devtron-labs/common-lib/utils/k8s/bean.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ github.com/devtron-labs/authenticator/jwt
github.com/devtron-labs/authenticator/middleware
github.com/devtron-labs/authenticator/oidc
github.com/devtron-labs/authenticator/password
# github.com/devtron-labs/common-lib v0.0.7
# github.com/devtron-labs/common-lib v0.0.8-0.20231204083642-08f7f57000d9
## explicit; go 1.20
github.com/devtron-labs/common-lib/blob-storage
github.com/devtron-labs/common-lib/pubsub-lib
Expand Down
Loading