/
token_getter.go
126 lines (106 loc) · 3.49 KB
/
token_getter.go
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
/*
Copyright 2018 The Kubernetes Authors.
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 keystone
import (
"crypto/tls"
"fmt"
"net/http"
"os"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"github.com/gophercloud/utils/client"
certutil "k8s.io/client-go/util/cert"
osClient "k8s.io/cloud-provider-openstack/pkg/client"
"k8s.io/cloud-provider-openstack/pkg/version"
"k8s.io/klog/v2"
)
type Options struct {
AuthOptions gophercloud.AuthOptions
ClientCertPath string
ClientKeyPath string
ClientCAPath string
}
// GetToken creates a token by authenticate with keystone.
func GetToken(options Options) (*tokens3.Token, error) {
var token *tokens3.Token
var setTransport bool
// Create new identity client
provider, err := openstack.NewClient(options.AuthOptions.IdentityEndpoint)
if err != nil {
msg := fmt.Errorf("failed: Initializing openstack authentication client: %v", err)
return token, msg
}
tlsConfig := &tls.Config{}
setTransport = false
userAgent := gophercloud.UserAgent{}
userAgent.Prepend(fmt.Sprintf("client-keystone-auth/%s", version.Version))
provider.UserAgent = userAgent
if options.ClientCertPath != "" && options.ClientKeyPath != "" {
clientCert, err := os.ReadFile(options.ClientCertPath)
if err != nil {
msg := fmt.Errorf("failed: Cannot read cert file: %v", err)
return token, msg
}
clientKey, err := os.ReadFile(options.ClientKeyPath)
if err != nil {
msg := fmt.Errorf("failed: Cannot read key file: %v", err)
return token, msg
}
cert, err := tls.X509KeyPair([]byte(clientCert), []byte(clientKey))
if err != nil {
msg := fmt.Errorf("failed: Cannot create keypair:: %v", err)
return token, msg
}
tlsConfig.Certificates = []tls.Certificate{cert}
setTransport = true
}
if options.ClientCAPath != "" {
roots, err := certutil.NewPool(options.ClientCAPath)
if err != nil {
msg := fmt.Errorf("failed: Cannot read CA file: %v", err)
return token, msg
}
tlsConfig.RootCAs = roots
setTransport = true
}
if setTransport {
transport := &http.Transport{Proxy: http.ProxyFromEnvironment, TLSClientConfig: tlsConfig}
provider.HTTPClient.Transport = transport
}
if klog.V(6).Enabled() {
if provider.HTTPClient.Transport == nil {
provider.HTTPClient.Transport = http.DefaultTransport
}
provider.HTTPClient.Transport = &client.RoundTripper{
Rt: provider.HTTPClient.Transport,
Logger: &osClient.Logger{},
}
}
v3Client, err := openstack.NewIdentityV3(provider, gophercloud.EndpointOpts{})
if err != nil {
msg := fmt.Errorf("failed: Initializing openstack authentication client: %v", err)
return token, msg
}
// Issue new unscoped token
result := tokens3.Create(v3Client, &options.AuthOptions)
if result.Err != nil {
return token, result.Err
}
token, err = result.ExtractToken()
if err != nil {
msg := fmt.Errorf("failed: Cannot extract the token from the response")
return token, msg
}
return token, nil
}