spiffe.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. *
  3. * Copyright 2020 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package credentials defines APIs for parsing SPIFFE ID.
  19. //
  20. // All APIs in this package are experimental.
  21. package credentials
  22. import (
  23. "crypto/tls"
  24. "crypto/x509"
  25. "net/url"
  26. "google.golang.org/grpc/grpclog"
  27. )
  28. var logger = grpclog.Component("credentials")
  29. // SPIFFEIDFromState parses the SPIFFE ID from State. If the SPIFFE ID format
  30. // is invalid, return nil with warning.
  31. func SPIFFEIDFromState(state tls.ConnectionState) *url.URL {
  32. if len(state.PeerCertificates) == 0 || len(state.PeerCertificates[0].URIs) == 0 {
  33. return nil
  34. }
  35. return SPIFFEIDFromCert(state.PeerCertificates[0])
  36. }
  37. // SPIFFEIDFromCert parses the SPIFFE ID from x509.Certificate. If the SPIFFE
  38. // ID format is invalid, return nil with warning.
  39. func SPIFFEIDFromCert(cert *x509.Certificate) *url.URL {
  40. if cert == nil || cert.URIs == nil {
  41. return nil
  42. }
  43. var spiffeID *url.URL
  44. for _, uri := range cert.URIs {
  45. if uri == nil || uri.Scheme != "spiffe" || uri.Opaque != "" || (uri.User != nil && uri.User.Username() != "") {
  46. continue
  47. }
  48. // From this point, we assume the uri is intended for a SPIFFE ID.
  49. if len(uri.String()) > 2048 {
  50. logger.Warning("invalid SPIFFE ID: total ID length larger than 2048 bytes")
  51. return nil
  52. }
  53. if len(uri.Host) == 0 || len(uri.Path) == 0 {
  54. logger.Warning("invalid SPIFFE ID: domain or workload ID is empty")
  55. return nil
  56. }
  57. if len(uri.Host) > 255 {
  58. logger.Warning("invalid SPIFFE ID: domain length larger than 255 characters")
  59. return nil
  60. }
  61. // A valid SPIFFE certificate can only have exactly one URI SAN field.
  62. if len(cert.URIs) > 1 {
  63. logger.Warning("invalid SPIFFE ID: multiple URI SANs")
  64. return nil
  65. }
  66. spiffeID = uri
  67. }
  68. return spiffeID
  69. }