secret_for_tls.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright 2015 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 kubectl
  14. import (
  15. "crypto/tls"
  16. "fmt"
  17. "io/ioutil"
  18. "k8s.io/kubernetes/pkg/api"
  19. "k8s.io/kubernetes/pkg/runtime"
  20. )
  21. // SecretForTLSGeneratorV1 supports stable generation of a TLS secret.
  22. type SecretForTLSGeneratorV1 struct {
  23. // Name is the name of this TLS secret.
  24. Name string
  25. // Key is the path to the user's private key.
  26. Key string
  27. // Cert is the path to the user's public key certificate.
  28. Cert string
  29. }
  30. // Ensure it supports the generator pattern that uses parameter injection
  31. var _ Generator = &SecretForTLSGeneratorV1{}
  32. // Ensure it supports the generator pattern that uses parameters specified during construction
  33. var _ StructuredGenerator = &SecretForTLSGeneratorV1{}
  34. // Generate returns a secret using the specified parameters
  35. func (s SecretForTLSGeneratorV1) Generate(genericParams map[string]interface{}) (runtime.Object, error) {
  36. err := ValidateParams(s.ParamNames(), genericParams)
  37. if err != nil {
  38. return nil, err
  39. }
  40. params := map[string]string{}
  41. for key, value := range genericParams {
  42. strVal, isString := value.(string)
  43. if !isString {
  44. return nil, fmt.Errorf("expected string, saw %v for '%s'", value, key)
  45. }
  46. params[key] = strVal
  47. }
  48. delegate := &SecretForTLSGeneratorV1{
  49. Name: params["name"],
  50. Key: params["key"],
  51. Cert: params["cert"],
  52. }
  53. return delegate.StructuredGenerate()
  54. }
  55. // StructuredGenerate outputs a secret object using the configured fields
  56. func (s SecretForTLSGeneratorV1) StructuredGenerate() (runtime.Object, error) {
  57. if err := s.validate(); err != nil {
  58. return nil, err
  59. }
  60. tlsCrt, err := readFile(s.Cert)
  61. if err != nil {
  62. return nil, err
  63. }
  64. tlsKey, err := readFile(s.Key)
  65. if err != nil {
  66. return nil, err
  67. }
  68. secret := &api.Secret{}
  69. secret.Name = s.Name
  70. secret.Type = api.SecretTypeTLS
  71. secret.Data = map[string][]byte{}
  72. secret.Data[api.TLSCertKey] = []byte(tlsCrt)
  73. secret.Data[api.TLSPrivateKeyKey] = []byte(tlsKey)
  74. return secret, nil
  75. }
  76. // readFile just reads a file into a byte array.
  77. func readFile(file string) ([]byte, error) {
  78. b, err := ioutil.ReadFile(file)
  79. if err != nil {
  80. return []byte{}, fmt.Errorf("Cannot read file %v, %v", file, err)
  81. }
  82. return b, nil
  83. }
  84. // ParamNames returns the set of supported input parameters when using the parameter injection generator pattern
  85. func (s SecretForTLSGeneratorV1) ParamNames() []GeneratorParam {
  86. return []GeneratorParam{
  87. {"name", true},
  88. {"key", true},
  89. {"cert", true},
  90. }
  91. }
  92. // validate validates required fields are set to support structured generation
  93. func (s SecretForTLSGeneratorV1) validate() error {
  94. // TODO: This is not strictly necessary. We can generate a self signed cert
  95. // if no key/cert is given. The only requiredment is that we either get both
  96. // or none. See test/e2e/ingress_utils for self signed cert generation.
  97. if len(s.Key) == 0 {
  98. return fmt.Errorf("key must be specified.")
  99. }
  100. if len(s.Cert) == 0 {
  101. return fmt.Errorf("certificate must be specified.")
  102. }
  103. if _, err := tls.LoadX509KeyPair(s.Cert, s.Key); err != nil {
  104. return fmt.Errorf("failed to load key pair %v", err)
  105. }
  106. // TODO: Add more validation.
  107. // 1. If the certificate contains intermediates, it is a valid chain.
  108. // 2. Format etc.
  109. return nil
  110. }