server_inspection.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Copyright 2019 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 cert
  14. import (
  15. "crypto/tls"
  16. "crypto/x509"
  17. "fmt"
  18. "net/url"
  19. "strings"
  20. )
  21. // GetClientCANames gets the CA names for client certs that a server accepts. This is useful when inspecting the
  22. // state of particular servers. apiHost is "host:port"
  23. func GetClientCANames(apiHost string) ([]string, error) {
  24. // when we run this the second time, we know which one we are expecting
  25. acceptableCAs := []string{}
  26. tlsConfig := &tls.Config{
  27. InsecureSkipVerify: true, // this is insecure to always get to the GetClientCertificate
  28. GetClientCertificate: func(hello *tls.CertificateRequestInfo) (*tls.Certificate, error) {
  29. acceptableCAs = []string{}
  30. for _, curr := range hello.AcceptableCAs {
  31. acceptableCAs = append(acceptableCAs, string(curr))
  32. }
  33. return &tls.Certificate{}, nil
  34. },
  35. }
  36. conn, err := tls.Dial("tcp", apiHost, tlsConfig)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if err := conn.Close(); err != nil {
  41. return nil, err
  42. }
  43. return acceptableCAs, nil
  44. }
  45. // GetClientCANamesForURL is GetClientCANames against a URL string like we use in kubeconfigs
  46. func GetClientCANamesForURL(kubeConfigURL string) ([]string, error) {
  47. apiserverURL, err := url.Parse(kubeConfigURL)
  48. if err != nil {
  49. return nil, err
  50. }
  51. return GetClientCANames(apiserverURL.Host)
  52. }
  53. // GetServingCertificates returns the x509 certs used by a server as certificates and pem encoded bytes.
  54. // The serverName is optional for specifying a different name to get SNI certificates. apiHost is "host:port"
  55. func GetServingCertificates(apiHost, serverName string) ([]*x509.Certificate, [][]byte, error) {
  56. tlsConfig := &tls.Config{
  57. InsecureSkipVerify: true, // this is insecure so that we always get connected
  58. }
  59. // if a name is specified for SNI, set it.
  60. if len(serverName) > 0 {
  61. tlsConfig.ServerName = serverName
  62. }
  63. conn, err := tls.Dial("tcp", apiHost, tlsConfig)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. if err = conn.Close(); err != nil {
  68. return nil, nil, fmt.Errorf("failed to close connection : %v", err)
  69. }
  70. peerCerts := conn.ConnectionState().PeerCertificates
  71. peerCertBytes := [][]byte{}
  72. for _, a := range peerCerts {
  73. actualCert, err := EncodeCertificates(a)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. peerCertBytes = append(peerCertBytes, []byte(strings.TrimSpace(string(actualCert))))
  78. }
  79. return peerCerts, peerCertBytes, err
  80. }
  81. // GetServingCertificatesForURL is GetServingCertificates against a URL string like we use in kubeconfigs
  82. func GetServingCertificatesForURL(kubeConfigURL, serverName string) ([]*x509.Certificate, [][]byte, error) {
  83. apiserverURL, err := url.Parse(kubeConfigURL)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. return GetServingCertificates(apiserverURL.Host, serverName)
  88. }