cp.go 14 KB

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