helpers.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 api
  14. import (
  15. "encoding/base64"
  16. "errors"
  17. "fmt"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "path/filepath"
  22. )
  23. func init() {
  24. sDec, _ := base64.StdEncoding.DecodeString("REDACTED+")
  25. redactedBytes = []byte(string(sDec))
  26. }
  27. // IsConfigEmpty returns true if the config is empty.
  28. func IsConfigEmpty(config *Config) bool {
  29. return len(config.AuthInfos) == 0 && len(config.Clusters) == 0 && len(config.Contexts) == 0 &&
  30. len(config.CurrentContext) == 0 &&
  31. len(config.Preferences.Extensions) == 0 && !config.Preferences.Colors &&
  32. len(config.Extensions) == 0
  33. }
  34. // MinifyConfig read the current context and uses that to keep only the relevant pieces of config
  35. // This is useful for making secrets based on kubeconfig files
  36. func MinifyConfig(config *Config) error {
  37. if len(config.CurrentContext) == 0 {
  38. return errors.New("current-context must exist in order to minify")
  39. }
  40. currContext, exists := config.Contexts[config.CurrentContext]
  41. if !exists {
  42. return fmt.Errorf("cannot locate context %v", config.CurrentContext)
  43. }
  44. newContexts := map[string]*Context{}
  45. newContexts[config.CurrentContext] = currContext
  46. newClusters := map[string]*Cluster{}
  47. if len(currContext.Cluster) > 0 {
  48. if _, exists := config.Clusters[currContext.Cluster]; !exists {
  49. return fmt.Errorf("cannot locate cluster %v", currContext.Cluster)
  50. }
  51. newClusters[currContext.Cluster] = config.Clusters[currContext.Cluster]
  52. }
  53. newAuthInfos := map[string]*AuthInfo{}
  54. if len(currContext.AuthInfo) > 0 {
  55. if _, exists := config.AuthInfos[currContext.AuthInfo]; !exists {
  56. return fmt.Errorf("cannot locate user %v", currContext.AuthInfo)
  57. }
  58. newAuthInfos[currContext.AuthInfo] = config.AuthInfos[currContext.AuthInfo]
  59. }
  60. config.AuthInfos = newAuthInfos
  61. config.Clusters = newClusters
  62. config.Contexts = newContexts
  63. return nil
  64. }
  65. var redactedBytes []byte
  66. // Flatten redacts raw data entries from the config object for a human-readable view.
  67. func ShortenConfig(config *Config) {
  68. // trick json encoder into printing a human readable string in the raw data
  69. // by base64 decoding what we want to print. Relies on implementation of
  70. // http://golang.org/pkg/encoding/json/#Marshal using base64 to encode []byte
  71. for key, authInfo := range config.AuthInfos {
  72. if len(authInfo.ClientKeyData) > 0 {
  73. authInfo.ClientKeyData = redactedBytes
  74. }
  75. if len(authInfo.ClientCertificateData) > 0 {
  76. authInfo.ClientCertificateData = redactedBytes
  77. }
  78. config.AuthInfos[key] = authInfo
  79. }
  80. for key, cluster := range config.Clusters {
  81. if len(cluster.CertificateAuthorityData) > 0 {
  82. cluster.CertificateAuthorityData = redactedBytes
  83. }
  84. config.Clusters[key] = cluster
  85. }
  86. }
  87. // Flatten changes the config object into a self contained config (useful for making secrets)
  88. func FlattenConfig(config *Config) error {
  89. for key, authInfo := range config.AuthInfos {
  90. baseDir, err := MakeAbs(path.Dir(authInfo.LocationOfOrigin), "")
  91. if err != nil {
  92. return err
  93. }
  94. if err := FlattenContent(&authInfo.ClientCertificate, &authInfo.ClientCertificateData, baseDir); err != nil {
  95. return err
  96. }
  97. if err := FlattenContent(&authInfo.ClientKey, &authInfo.ClientKeyData, baseDir); err != nil {
  98. return err
  99. }
  100. config.AuthInfos[key] = authInfo
  101. }
  102. for key, cluster := range config.Clusters {
  103. baseDir, err := MakeAbs(path.Dir(cluster.LocationOfOrigin), "")
  104. if err != nil {
  105. return err
  106. }
  107. if err := FlattenContent(&cluster.CertificateAuthority, &cluster.CertificateAuthorityData, baseDir); err != nil {
  108. return err
  109. }
  110. config.Clusters[key] = cluster
  111. }
  112. return nil
  113. }
  114. func FlattenContent(path *string, contents *[]byte, baseDir string) error {
  115. if len(*path) != 0 {
  116. if len(*contents) > 0 {
  117. return errors.New("cannot have values for both path and contents")
  118. }
  119. var err error
  120. absPath := ResolvePath(*path, baseDir)
  121. *contents, err = ioutil.ReadFile(absPath)
  122. if err != nil {
  123. return err
  124. }
  125. *path = ""
  126. }
  127. return nil
  128. }
  129. // ResolvePath returns the path as an absolute paths, relative to the given base directory
  130. func ResolvePath(path string, base string) string {
  131. // Don't resolve empty paths
  132. if len(path) > 0 {
  133. // Don't resolve absolute paths
  134. if !filepath.IsAbs(path) {
  135. return filepath.Join(base, path)
  136. }
  137. }
  138. return path
  139. }
  140. func MakeAbs(path, base string) (string, error) {
  141. if filepath.IsAbs(path) {
  142. return path, nil
  143. }
  144. if len(base) == 0 {
  145. cwd, err := os.Getwd()
  146. if err != nil {
  147. return "", err
  148. }
  149. base = cwd
  150. }
  151. return filepath.Join(base, path), nil
  152. }