diff --git a/generator/crontabs.go b/generator/crontabs.go index 6f0a3c1..92b7707 100644 --- a/generator/crontabs.go +++ b/generator/crontabs.go @@ -6,6 +6,7 @@ import ( "katenary/logger" "log" + "github.com/alessio/shellescape" "github.com/compose-spec/compose-go/types" "gopkg.in/yaml.v3" ) @@ -14,11 +15,11 @@ const ( cronMulti = `pods=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[*].metadata.name}')` cronMultiCmd = ` for pod in $pods; do - kubectl exec -i $pod -c %s -- sh -c '%s' + kubectl exec -i $pod -c %s -- sh -c %s done` cronSingle = `pod=$(kubectl get pods --selector=%s/component=%s,%s/resource=deployment -o jsonpath='{.items[0].metadata.name}')` cronCmd = ` -kubectl exec -i $pod -c %s -- sh -c '%s'` +kubectl exec -i $pod -c %s -- sh -c %s` ) type CronDef struct { @@ -65,6 +66,7 @@ func buildCrontab(deployName string, deployment *helm.Deployment, s *types.Servi // create crontabs for _, cron := range crons { + escaped := shellescape.Quote(cron.Command) var cmd, podget string if cron.Multi { podget = cronMulti @@ -74,7 +76,7 @@ func buildCrontab(deployName string, deployment *helm.Deployment, s *types.Servi cmd = cronCmd } podget = fmt.Sprintf(podget, helm.K, deployName, helm.K) - cmd = fmt.Sprintf(cmd, s.Name, cron.Command) + cmd = fmt.Sprintf(cmd, s.Name, escaped) cmd = podget + cmd if cron.Image == "" { diff --git a/go.mod b/go.mod index 0d140ea..9301a9b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module katenary go 1.16 require ( + github.com/alessio/shellescape v1.4.1 github.com/compose-spec/compose-go v1.2.5 github.com/distribution/distribution/v3 v3.0.0-20220505155552-985711c1f414 // indirect github.com/kr/pretty v0.2.0 // indirect diff --git a/go.sum b/go.sum index ca5953c..f4c0e19 100644 --- a/go.sum +++ b/go.sum @@ -16,6 +16,8 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE github.com/Shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d/go.mod h1:HI8ITrYtUY+O+ZhtlqUnD8+KwNPOyugEhfP9fdUIaEQ= github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= +github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= github.com/aws/aws-sdk-go v1.34.9/go.mod h1:5zCpMtNQVjRREroY7sYe8lOMRSxkhG6MZveU8YkpAk0= github.com/aws/aws-sdk-go v1.43.16/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=