client.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Copyright (c) 2018 VMware, Inc. All Rights Reserved.
  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 lookup
  14. import (
  15. "context"
  16. "crypto/x509"
  17. "encoding/base64"
  18. "log"
  19. "net/url"
  20. "github.com/vmware/govmomi/lookup/methods"
  21. "github.com/vmware/govmomi/lookup/types"
  22. "github.com/vmware/govmomi/vim25"
  23. "github.com/vmware/govmomi/vim25/soap"
  24. vim "github.com/vmware/govmomi/vim25/types"
  25. )
  26. const (
  27. Namespace = "lookup"
  28. Version = "2.0"
  29. Path = "/lookupservice" + vim25.Path
  30. )
  31. var (
  32. ServiceInstance = vim.ManagedObjectReference{
  33. Type: "LookupServiceInstance",
  34. Value: "ServiceInstance",
  35. }
  36. )
  37. // Client is a soap.Client targeting the SSO Lookup Service API endpoint.
  38. type Client struct {
  39. *soap.Client
  40. ServiceContent types.LookupServiceContent
  41. }
  42. // NewClient returns a client targeting the SSO Lookup Service API endpoint.
  43. func NewClient(ctx context.Context, c *vim25.Client) (*Client, error) {
  44. sc := c.Client.NewServiceClient(Path, Namespace)
  45. sc.Version = Version
  46. req := types.RetrieveServiceContent{
  47. This: ServiceInstance,
  48. }
  49. res, err := methods.RetrieveServiceContent(ctx, sc, &req)
  50. if err != nil {
  51. return nil, err
  52. }
  53. return &Client{sc, res.Returnval}, nil
  54. }
  55. func (c *Client) List(ctx context.Context, filter *types.LookupServiceRegistrationFilter) ([]types.LookupServiceRegistrationInfo, error) {
  56. req := types.List{
  57. This: *c.ServiceContent.ServiceRegistration,
  58. FilterCriteria: filter,
  59. }
  60. res, err := methods.List(ctx, c, &req)
  61. if err != nil {
  62. return nil, err
  63. }
  64. return res.Returnval, nil
  65. }
  66. func (c *Client) SiteID(ctx context.Context) (string, error) {
  67. req := types.GetSiteId{
  68. This: *c.ServiceContent.ServiceRegistration,
  69. }
  70. res, err := methods.GetSiteId(ctx, c, &req)
  71. if err != nil {
  72. return "", err
  73. }
  74. return res.Returnval, nil
  75. }
  76. // EndpointURL uses the Lookup Service to find the endpoint URL and thumbprint for the given filter.
  77. // If the endpoint is found, its TLS certificate is also added to the vim25.Client's trusted host thumbprints.
  78. // If the Lookup Service is not available, the given path is returned as the default.
  79. func EndpointURL(ctx context.Context, c *vim25.Client, path string, filter *types.LookupServiceRegistrationFilter) string {
  80. if lu, err := NewClient(ctx, c); err == nil {
  81. info, _ := lu.List(ctx, filter)
  82. if len(info) != 0 && len(info[0].ServiceEndpoints) != 0 {
  83. endpoint := &info[0].ServiceEndpoints[0]
  84. path = endpoint.Url
  85. if u, err := url.Parse(path); err == nil {
  86. if c.Thumbprint(u.Host) == "" {
  87. c.SetThumbprint(u.Host, endpointThumbprint(endpoint))
  88. }
  89. }
  90. }
  91. }
  92. return path
  93. }
  94. // endpointThumbprint converts the base64 encoded endpoint certificate to a SHA1 thumbprint.
  95. func endpointThumbprint(endpoint *types.LookupServiceRegistrationEndpoint) string {
  96. if len(endpoint.SslTrust) == 0 {
  97. return ""
  98. }
  99. enc := endpoint.SslTrust[0]
  100. b, err := base64.StdEncoding.DecodeString(enc)
  101. if err != nil {
  102. log.Printf("base64.Decode(%q): %s", enc, err)
  103. return ""
  104. }
  105. cert, err := x509.ParseCertificate(b)
  106. if err != nil {
  107. log.Printf("x509.ParseCertificate(%q): %s", enc, err)
  108. return ""
  109. }
  110. return soap.ThumbprintSHA1(cert)
  111. }