1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
/*
Copyright (c) 2018 VMware, Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package lookup
import (
"context"
"crypto/x509"
"encoding/base64"
"log"
"net/url"
"github.com/vmware/govmomi/lookup/methods"
"github.com/vmware/govmomi/lookup/types"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25"
"github.com/vmware/govmomi/vim25/soap"
vim "github.com/vmware/govmomi/vim25/types"
)
const (
Namespace = "lookup"
Version = "2.0"
Path = "/lookupservice" + vim25.Path
)
var (
ServiceInstance = vim.ManagedObjectReference{
Type: "LookupServiceInstance",
Value: "ServiceInstance",
}
)
// Client is a soap.Client targeting the SSO Lookup Service API endpoint.
type Client struct {
*soap.Client
ServiceContent types.LookupServiceContent
}
// NewClient returns a client targeting the SSO Lookup Service API endpoint.
func NewClient(ctx context.Context, c *vim25.Client) (*Client, error) {
// PSC may be external, attempt to derive from sts.uri
path := &url.URL{Path: Path}
if c.ServiceContent.Setting != nil {
m := object.NewOptionManager(c, *c.ServiceContent.Setting)
opts, err := m.Query(ctx, "config.vpxd.sso.sts.uri")
if err == nil && len(opts) == 1 {
u, err := url.Parse(opts[0].GetOptionValue().Value.(string))
if err == nil {
path.Scheme = u.Scheme
path.Host = u.Host
}
}
}
sc := c.Client.NewServiceClient(path.String(), Namespace)
sc.Version = Version
req := types.RetrieveServiceContent{
This: ServiceInstance,
}
res, err := methods.RetrieveServiceContent(ctx, sc, &req)
if err != nil {
return nil, err
}
return &Client{sc, res.Returnval}, nil
}
func (c *Client) List(ctx context.Context, filter *types.LookupServiceRegistrationFilter) ([]types.LookupServiceRegistrationInfo, error) {
req := types.List{
This: *c.ServiceContent.ServiceRegistration,
FilterCriteria: filter,
}
res, err := methods.List(ctx, c, &req)
if err != nil {
return nil, err
}
return res.Returnval, nil
}
func (c *Client) SiteID(ctx context.Context) (string, error) {
req := types.GetSiteId{
This: *c.ServiceContent.ServiceRegistration,
}
res, err := methods.GetSiteId(ctx, c, &req)
if err != nil {
return "", err
}
return res.Returnval, nil
}
// EndpointURL uses the Lookup Service to find the endpoint URL and thumbprint for the given filter.
// If the endpoint is found, its TLS certificate is also added to the vim25.Client's trusted host thumbprints.
// If the Lookup Service is not available, the given path is returned as the default.
func EndpointURL(ctx context.Context, c *vim25.Client, path string, filter *types.LookupServiceRegistrationFilter) string {
if lu, err := NewClient(ctx, c); err == nil {
info, _ := lu.List(ctx, filter)
if len(info) != 0 && len(info[0].ServiceEndpoints) != 0 {
endpoint := &info[0].ServiceEndpoints[0]
path = endpoint.Url
if u, err := url.Parse(path); err == nil {
if c.Thumbprint(u.Host) == "" {
c.SetThumbprint(u.Host, endpointThumbprint(endpoint))
}
}
}
}
return path
}
// endpointThumbprint converts the base64 encoded endpoint certificate to a SHA1 thumbprint.
func endpointThumbprint(endpoint *types.LookupServiceRegistrationEndpoint) string {
if len(endpoint.SslTrust) == 0 {
return ""
}
enc := endpoint.SslTrust[0]
b, err := base64.StdEncoding.DecodeString(enc)
if err != nil {
log.Printf("base64.Decode(%q): %s", enc, err)
return ""
}
cert, err := x509.ParseCertificate(b)
if err != nil {
log.Printf("x509.ParseCertificate(%q): %s", enc, err)
return ""
}
return soap.ThumbprintSHA1(cert)
}
|