cp.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package cp
  14. import (
  15. "archive/tar"
  16. "bytes"
  17. "errors"
  18. "fmt"
  19. "io"
  20. "io/ioutil"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "strings"
  25. "github.com/lithammer/dedent"
  26. "github.com/spf13/cobra"
  27. "k8s.io/cli-runtime/pkg/genericclioptions"
  28. "k8s.io/client-go/kubernetes"
  29. restclient "k8s.io/client-go/rest"
  30. "k8s.io/kubectl/pkg/cmd/exec"
  31. cmdutil "k8s.io/kubectl/pkg/cmd/util"
  32. "k8s.io/kubectl/pkg/util/i18n"
  33. "k8s.io/kubectl/pkg/util/templates"
  34. )
  35. var (
  36. cpExample = templates.Examples(i18n.T(`
  37. # !!!Important Note!!!
  38. # Requires that the 'tar' binary is present in your container
  39. # image. If 'tar' is not present, 'kubectl cp' will fail.
  40. #
  41. # For advanced use cases, such as symlinks, wildcard expansion or
  42. # file mode preservation consider using 'kubectl exec'.
  43. # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  44. tar cf - /tmp/foo | kubectl exec -i -n <some-namespace> <some-pod> -- tar xf - -C /tmp/bar
  45. # Copy /tmp/foo from a remote pod to /tmp/bar locally
  46. kubectl exec -n <some-namespace> <some-pod> -- tar cf - /tmp/foo | tar xf - -C /tmp/bar
  47. # Copy /tmp/foo_dir local directory to /tmp/bar_dir in a remote pod in the default namespace
  48. kubectl cp /tmp/foo_dir <some-pod>:/tmp/bar_dir
  49. # Copy /tmp/foo local file to /tmp/bar in a remote pod in a specific container
  50. kubectl cp /tmp/foo <some-pod>:/tmp/bar -c <specific-container>
  51. # Copy /tmp/foo local file to /tmp/bar in a remote pod in namespace <some-namespace>
  52. kubectl cp /tmp/foo <some-namespace>/<some-pod>:/tmp/bar
  53. # Copy /tmp/foo from a remote pod to /tmp/bar locally
  54. kubectl cp <some-namespace>/<some-pod>:/tmp/foo /tmp/bar`))
  55. cpUsageStr = dedent.Dedent(`
  56. expected 'cp <file-spec-src> <file-spec-dest> [-c container]'.
  57. <file-spec> is:
  58. [namespace/]pod-name:/file/path for a remote file
  59. /file/path for a local file`)
  60. )
  61. // CopyOptions have the data required to perform the copy operation
  62. type CopyOptions struct {
  63. Container string
  64. Namespace string
  65. NoPreserve bool
  66. ClientConfig *restclient.Config
  67. Clientset kubernetes.Interface
  68. ExecParentCmdName string
  69. genericclioptions.IOStreams
  70. }
  71. // NewCopyOptions creates the options for copy
  72. func NewCopyOptions(ioStreams genericclioptions.IOStreams) *CopyOptions {
  73. return &CopyOptions{
  74. IOStreams: ioStreams,
  75. }
  76. }
  77. // NewCmdCp creates a new Copy command.
  78. func NewCmdCp(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra.Command {
  79. o := NewCopyOptions(ioStreams)
  80. cmd := &cobra.Command{
  81. Use: "cp <file-spec-src> <file-spec-dest>",
  82. DisableFlagsInUseLine: true,
  83. Short: i18n.T("Copy files and directories to and from containers."),
  84. Long: "Copy files and directories to and from containers.",
  85. Example: cpExample,
  86. Run: func(cmd *cobra.Command, args []string) {
  87. cmdutil.CheckErr(o.Complete(f, cmd))
  88. cmdutil.CheckErr(o.Run(args))
  89. },
  90. }
  91. cmd.Flags().StringVarP(&o.Container, "container", "c", o.Container, "Container name. If omitted, the first container in the pod will be chosen")
  92. cmd.Flags().BoolVarP(&o.NoPreserve, "no-preserve", "", false, "The copied file/directory's ownership and permissions will not be preserved in the container")
  93. return cmd
  94. }
  95. type fileSpec struct {
  96. PodNamespace string
  97. PodName string
  98. File string
  99. }
  100. var (
  101. errFileSpecDoesntMatchFormat = errors.New("filespec must match the canonical format: [[namespace/]pod:]file/path")
  102. errFileCannotBeEmpty = errors.New("filepath can not be empty")
  103. )
  104. func extractFileSpec(arg string) (fileSpec, error) {
  105. if i := strings.Index(arg, ":"); i == -1 {
  106. return fileSpec{File: arg}, nil
  107. } else if i > 0 {
  108. file := arg[i+1:]
  109. pod := arg[:i]
  110. pieces := strings.Split(pod, "/")
  111. if len(pieces) == 1 {
  112. return fileSpec{
  113. PodName: pieces[0],
  114. File: file,
  115. }, nil
  116. }
  117. if len(pieces) == 2 {
  118. return fileSpec{
  119. PodNamespace: pieces[0],
  120. PodName: pieces[1],
  121. File: file,
  122. }, nil
  123. }
  124. }
  125. return fileSpec{}, errFileSpecDoesntMatchFormat
  126. }
  127. // Complete completes all the required options
  128. func (o *CopyOptions) Complete(f cmdutil.Factory, cmd *cobra.Command) error {
  129. if cmd.Parent() != nil {
  130. o.ExecParentCmdName = cmd.Parent().CommandPath()
  131. }
  132. var err error
  133. o.Namespace, _, err = f.ToRawKubeConfigLoader().Namespace()
  134. if err != nil {
  135. return err
  136. }
  137. o.Clientset, err = f.KubernetesClientSet()
  138. if err != nil {
  139. return err
  140. }
  141. o.ClientConfig, err = f.ToRESTConfig()
  142. if err != nil {
  143. return err
  144. }
  145. return nil
  146. }
  147. // Validate makes sure provided values for CopyOptions are valid
  148. func (o *CopyOptions) Validate(cmd *cobra.Command, args []string) error {
  149. if len(args) != 2 {
  150. return cmdutil.UsageErrorf(cmd, cpUsageStr)
  151. }
  152. return nil
  153. }
  154. // Run performs the execution
  155. func (o *CopyOptions) Run(args []string) error {
  156. if len(args) < 2 {
  157. return fmt.Errorf("source and destination are required")
  158. }
  159. srcSpec, err := extractFileSpec(args[0])
  160. if err != nil {
  161. return err
  162. }
  163. destSpec, err := extractFileSpec(args[1])
  164. if err != nil {
  165. return err
  166. }
  167. if len(srcSpec.PodName) != 0 && len(destSpec.PodName) != 0 {
  168. if _, err := os.Stat(args[0]); err == nil {
  169. return o.copyToPod(fileSpec{File: args[0]}, destSpec, &exec.ExecOptions{})
  170. }
  171. return fmt.Errorf("src doesn't exist in local filesystem")
  172. }
  173. if len(srcSpec.PodName) != 0 {
  174. return o.copyFromPod(srcSpec, destSpec)
  175. }
  176. if len(destSpec.PodName) != 0 {
  177. return o.copyToPod(srcSpec, destSpec, &exec.ExecOptions{})
  178. }
  179. return fmt.Errorf("one of src or dest must be a remote file specification")
  180. }
  181. // checkDestinationIsDir receives a destination fileSpec and
  182. // determines if the provided destination path exists on the
  183. // pod. If the destination path does not exist or is _not_ a
  184. // directory, an error is returned with the exit code received.
  185. func (o *CopyOptions) checkDestinationIsDir(dest fileSpec) error {
  186. options := &exec.ExecOptions{
  187. StreamOptions: exec.StreamOptions{
  188. IOStreams: genericclioptions.IOStreams{
  189. Out: bytes.NewBuffer([]byte{}),
  190. ErrOut: bytes.NewBuffer([]byte{}),
  191. },
  192. Namespace: dest.PodNamespace,
  193. PodName: dest.PodName,
  194. },
  195. Command: []string{"test", "-d", dest.File},
  196. Executor: &exec.DefaultRemoteExecutor{},
  197. }
  198. return o.execute(options)
  199. }
  200. func (o *CopyOptions) copyToPod(src, dest fileSpec, options *exec.ExecOptions) error {
  201. if len(src.File) == 0 || len(dest.File) == 0 {
  202. return errFileCannotBeEmpty
  203. }
  204. reader, writer := io.Pipe()
  205. // strip trailing slash (if any)
  206. if dest.File != "/" && strings.HasSuffix(string(dest.File[len(dest.File)-1]), "/") {
  207. dest.File = dest.File[:len(dest.File)-1]
  208. }
  209. if err := o.checkDestinationIsDir(dest); err == nil {
  210. // If no error, dest.File was found to be a directory.
  211. // Copy specified src into it
  212. dest.File = dest.File + "/" + path.Base(src.File)
  213. }
  214. go func() {
  215. defer writer.Close()
  216. err := makeTar(src.File, dest.File, writer)
  217. cmdutil.CheckErr(err)
  218. }()
  219. var cmdArr []string
  220. // TODO: Improve error messages by first testing if 'tar' is present in the container?
  221. if o.NoPreserve {
  222. cmdArr = []string{"tar", "--no-same-permissions", "--no-same-owner", "-xmf", "-"}
  223. } else {
  224. cmdArr = []string{"tar", "-xmf", "-"}
  225. }
  226. destDir := path.Dir(dest.File)
  227. if len(destDir) > 0 {
  228. cmdArr = append(cmdArr, "-C", destDir)
  229. }
  230. options.StreamOptions = exec.StreamOptions{
  231. IOStreams: genericclioptions.IOStreams{
  232. In: reader,
  233. Out: o.Out,
  234. ErrOut: o.ErrOut,
  235. },
  236. Stdin: true,
  237. Namespace: dest.PodNamespace,
  238. PodName: dest.PodName,
  239. }
  240. options.Command = cmdArr
  241. options.Executor = &exec.DefaultRemoteExecutor{}
  242. return o.execute(options)
  243. }
  244. func (o *CopyOptions) copyFromPod(src, dest fileSpec) error {
  245. if len(src.File) == 0 || len(dest.File) == 0 {
  246. return errFileCannotBeEmpty
  247. }
  248. reader, outStream := io.Pipe()
  249. options := &exec.ExecOptions{
  250. StreamOptions: exec.StreamOptions{
  251. IOStreams: genericclioptions.IOStreams{
  252. In: nil,
  253. Out: outStream,
  254. ErrOut: o.Out,
  255. },
  256. Namespace: src.PodNamespace,
  257. PodName: src.PodName,
  258. },
  259. // TODO: Improve error messages by first testing if 'tar' is present in the container?
  260. Command: []string{"tar", "cf", "-", src.File},
  261. Executor: &exec.DefaultRemoteExecutor{},
  262. }
  263. go func() {
  264. defer outStream.Close()
  265. err := o.execute(options)
  266. cmdutil.CheckErr(err)
  267. }()
  268. prefix := getPrefix(src.File)
  269. prefix = path.Clean(prefix)
  270. // remove extraneous path shortcuts - these could occur if a path contained extra "../"
  271. // and attempted to navigate beyond "/" in a remote filesystem
  272. prefix = stripPathShortcuts(prefix)
  273. return o.untarAll(src, reader, dest.File, prefix)
  274. }
  275. // stripPathShortcuts removes any leading or trailing "../" from a given path
  276. func stripPathShortcuts(p string) string {
  277. newPath := path.Clean(p)
  278. trimmed := strings.TrimPrefix(newPath, "../")
  279. for trimmed != newPath {
  280. newPath = trimmed
  281. trimmed = strings.TrimPrefix(newPath, "../")
  282. }
  283. // trim leftover {".", ".."}
  284. if newPath == "." || newPath == ".." {
  285. newPath = ""
  286. }
  287. if len(newPath) > 0 && string(newPath[0]) == "/" {
  288. return newPath[1:]
  289. }
  290. return newPath
  291. }
  292. func makeTar(srcPath, destPath string, writer io.Writer) error {
  293. // TODO: use compression here?
  294. tarWriter := tar.NewWriter(writer)
  295. defer tarWriter.Close()
  296. srcPath = path.Clean(srcPath)
  297. destPath = path.Clean(destPath)
  298. return recursiveTar(path.Dir(srcPath), path.Base(srcPath), path.Dir(destPath), path.Base(destPath), tarWriter)
  299. }
  300. func recursiveTar(srcBase, srcFile, destBase, destFile string, tw *tar.Writer) error {
  301. srcPath := path.Join(srcBase, srcFile)
  302. matchedPaths, err := filepath.Glob(srcPath)
  303. if err != nil {
  304. return err
  305. }
  306. for _, fpath := range matchedPaths {
  307. stat, err := os.Lstat(fpath)
  308. if err != nil {
  309. return err
  310. }
  311. if stat.IsDir() {
  312. files, err := ioutil.ReadDir(fpath)
  313. if err != nil {
  314. return err
  315. }
  316. if len(files) == 0 {
  317. //case empty directory
  318. hdr, _ := tar.FileInfoHeader(stat, fpath)
  319. hdr.Name = destFile
  320. if err := tw.WriteHeader(hdr); err != nil {
  321. return err
  322. }
  323. }
  324. for _, f := range files {
  325. if err := recursiveTar(srcBase, path.Join(srcFile, f.Name()), destBase, path.Join(destFile, f.Name()), tw); err != nil {
  326. return err
  327. }
  328. }
  329. return nil
  330. } else if stat.Mode()&os.ModeSymlink != 0 {
  331. //case soft link
  332. hdr, _ := tar.FileInfoHeader(stat, fpath)
  333. target, err := os.Readlink(fpath)
  334. if err != nil {
  335. return err
  336. }
  337. hdr.Linkname = target
  338. hdr.Name = destFile
  339. if err := tw.WriteHeader(hdr); err != nil {
  340. return err
  341. }
  342. } else {
  343. //case regular file or other file type like pipe
  344. hdr, err := tar.FileInfoHeader(stat, fpath)
  345. if err != nil {
  346. return err
  347. }
  348. hdr.Name = destFile
  349. if err := tw.WriteHeader(hdr); err != nil {
  350. return err
  351. }
  352. f, err := os.Open(fpath)
  353. if err != nil {
  354. return err
  355. }
  356. defer f.Close()
  357. if _, err := io.Copy(tw, f); err != nil {
  358. return err
  359. }
  360. return f.Close()
  361. }
  362. }
  363. return nil
  364. }
  365. func (o *CopyOptions) untarAll(src fileSpec, reader io.Reader, destDir, prefix string) error {
  366. symlinkWarningPrinted := false
  367. // TODO: use compression here?
  368. tarReader := tar.NewReader(reader)
  369. for {
  370. header, err := tarReader.Next()
  371. if err != nil {
  372. if err != io.EOF {
  373. return err
  374. }
  375. break
  376. }
  377. // All the files will start with the prefix, which is the directory where
  378. // they were located on the pod, we need to strip down that prefix, but
  379. // if the prefix is missing it means the tar was tempered with.
  380. // For the case where prefix is empty we need to ensure that the path
  381. // is not absolute, which also indicates the tar file was tempered with.
  382. if !strings.HasPrefix(header.Name, prefix) {
  383. return fmt.Errorf("tar contents corrupted")
  384. }
  385. // basic file information
  386. mode := header.FileInfo().Mode()
  387. destFileName := filepath.Join(destDir, header.Name[len(prefix):])
  388. if !isDestRelative(destDir, destFileName) {
  389. fmt.Fprintf(o.IOStreams.ErrOut, "warning: file %q is outside target destination, skipping\n", destFileName)
  390. continue
  391. }
  392. baseName := filepath.Dir(destFileName)
  393. if err := os.MkdirAll(baseName, 0755); err != nil {
  394. return err
  395. }
  396. if header.FileInfo().IsDir() {
  397. if err := os.MkdirAll(destFileName, 0755); err != nil {
  398. return err
  399. }
  400. continue
  401. }
  402. if mode&os.ModeSymlink != 0 {
  403. if !symlinkWarningPrinted && len(o.ExecParentCmdName) > 0 {
  404. fmt.Fprintf(o.IOStreams.ErrOut, "warning: skipping symlink: %q -> %q (consider using \"%s exec -n %q %q -- tar cf - %q | tar xf -\")\n", destFileName, header.Linkname, o.ExecParentCmdName, src.PodNamespace, src.PodName, src.File)
  405. symlinkWarningPrinted = true
  406. continue
  407. }
  408. fmt.Fprintf(o.IOStreams.ErrOut, "warning: skipping symlink: %q -> %q\n", destFileName, header.Linkname)
  409. continue
  410. }
  411. outFile, err := os.Create(destFileName)
  412. if err != nil {
  413. return err
  414. }
  415. defer outFile.Close()
  416. if _, err := io.Copy(outFile, tarReader); err != nil {
  417. return err
  418. }
  419. if err := outFile.Close(); err != nil {
  420. return err
  421. }
  422. }
  423. return nil
  424. }
  425. // isDestRelative returns true if dest is pointing outside the base directory,
  426. // false otherwise.
  427. func isDestRelative(base, dest string) bool {
  428. relative, err := filepath.Rel(base, dest)
  429. if err != nil {
  430. return false
  431. }
  432. return relative == "." || relative == stripPathShortcuts(relative)
  433. }
  434. func getPrefix(file string) string {
  435. // tar strips the leading '/' if it's there, so we will too
  436. return strings.TrimLeft(file, "/")
  437. }
  438. func (o *CopyOptions) execute(options *exec.ExecOptions) error {
  439. if len(options.Namespace) == 0 {
  440. options.Namespace = o.Namespace
  441. }
  442. if len(o.Container) > 0 {
  443. options.ContainerName = o.Container
  444. }
  445. options.Config = o.ClientConfig
  446. options.PodClient = o.Clientset.CoreV1()
  447. if err := options.Validate(); err != nil {
  448. return err
  449. }
  450. if err := options.Run(); err != nil {
  451. return err
  452. }
  453. return nil
  454. }