util_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2017 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "io"
  17. "os"
  18. "path/filepath"
  19. "go.etcd.io/etcd/pkg/transport"
  20. )
  21. // copyTLSFiles clones certs files to dst directory.
  22. func copyTLSFiles(ti transport.TLSInfo, dst string) (transport.TLSInfo, error) {
  23. ci := transport.TLSInfo{
  24. KeyFile: filepath.Join(dst, "server-key.pem"),
  25. CertFile: filepath.Join(dst, "server.pem"),
  26. TrustedCAFile: filepath.Join(dst, "etcd-root-ca.pem"),
  27. ClientCertAuth: ti.ClientCertAuth,
  28. }
  29. if err := copyFile(ti.KeyFile, ci.KeyFile); err != nil {
  30. return transport.TLSInfo{}, err
  31. }
  32. if err := copyFile(ti.CertFile, ci.CertFile); err != nil {
  33. return transport.TLSInfo{}, err
  34. }
  35. if err := copyFile(ti.TrustedCAFile, ci.TrustedCAFile); err != nil {
  36. return transport.TLSInfo{}, err
  37. }
  38. return ci, nil
  39. }
  40. func copyFile(src, dst string) error {
  41. f, err := os.Open(src)
  42. if err != nil {
  43. return err
  44. }
  45. defer f.Close()
  46. w, err := os.Create(dst)
  47. if err != nil {
  48. return err
  49. }
  50. defer w.Close()
  51. if _, err = io.Copy(w, f); err != nil {
  52. return err
  53. }
  54. return w.Sync()
  55. }