-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_getter.go
108 lines (91 loc) · 2.97 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
/*
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"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
tokens3 "github.com/gophercloud/gophercloud/openstack/identity/v3/tokens"
"io/ioutil"
certutil "k8s.io/client-go/util/cert"
"net/http"
)
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
client, 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
if options.ClientCertPath != "" && options.ClientKeyPath != "" {
clientCert, err := ioutil.ReadFile(options.ClientCertPath)
if err != nil {
msg := fmt.Errorf("failed: Cannot read cert file: %v", err)
return token, msg
}
clientKey, err := ioutil.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}
tlsConfig.BuildNameToCertificate()
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}
client.HTTPClient.Transport = transport
}
v3Client, err := openstack.NewIdentityV3(client, 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
}