-
Notifications
You must be signed in to change notification settings - Fork 162
/
session.go
184 lines (142 loc) · 3.88 KB
/
session.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package cmd
import (
bosherr "github.com/cloudfoundry/bosh-utils/errors"
boshlog "github.com/cloudfoundry/bosh-utils/logger"
cmdconf "github.com/cloudfoundry/bosh-cli/cmd/config"
boshdir "github.com/cloudfoundry/bosh-cli/director"
boshuaa "github.com/cloudfoundry/bosh-cli/uaa"
boshui "github.com/cloudfoundry/bosh-cli/ui"
boshuit "github.com/cloudfoundry/bosh-cli/ui/task"
)
type SessionImpl struct {
context SessionContext
ui boshui.UI
printEnvironment bool
printDeployment bool
logger boshlog.Logger
// Memoized
director boshdir.Director
directorInfo boshdir.Info
directorInfoSet bool
}
func NewSessionImpl(
context SessionContext,
ui boshui.UI,
printEnvironment bool,
printDeployment bool,
logger boshlog.Logger,
) *SessionImpl {
return &SessionImpl{
context: context,
ui: ui,
printEnvironment: printEnvironment,
printDeployment: printDeployment,
logger: logger,
}
}
func (c SessionImpl) Environment() string { return c.context.Environment() }
func (c SessionImpl) Credentials() cmdconf.Creds { return c.context.Credentials() }
func (c SessionImpl) UAA() (boshuaa.UAA, error) {
err := c.setDirectorInfo()
if err != nil {
return nil, err
}
uaaURL := c.directorInfo.Auth.Options["url"]
uaaURLStr, ok := uaaURL.(string)
if !ok {
return nil, bosherr.Errorf("Expected URL '%s' to be a string", uaaURL)
}
uaaConfig, err := boshuaa.NewConfigFromURL(uaaURLStr)
if err != nil {
return nil, err
}
uaaConfig.CACert = c.context.CACert()
creds := c.Credentials()
uaaConfig.Client = creds.Client
uaaConfig.ClientSecret = creds.ClientSecret
if len(uaaConfig.Client) == 0 {
uaaConfig.Client = "bosh_cli"
}
return boshuaa.NewFactory(c.logger).New(uaaConfig)
}
func (c *SessionImpl) Director() (boshdir.Director, error) {
if c.director != nil {
return c.director, nil
}
dirConfig, err := boshdir.NewConfigFromURL(c.Environment())
if err != nil {
return nil, err
}
dirConfig.CACert = c.context.CACert()
creds := c.Credentials()
err = c.setDirectorInfo()
if err != nil {
return nil, err
}
if c.directorInfo.Auth.Type != "uaa" {
dirConfig.Client = creds.Client
dirConfig.ClientSecret = creds.ClientSecret
} else if creds.IsUAA() {
uaa, err := c.UAA()
if err != nil {
return nil, err
}
if creds.IsUAAClient() {
dirConfig.TokenFunc = boshuaa.NewClientTokenSession(uaa).TokenFunc
} else {
origToken := uaa.NewStaleAccessToken(creds.RefreshToken)
dirConfig.TokenFunc = boshuaa.NewAccessTokenSession(origToken).TokenFunc
}
}
if c.printEnvironment {
c.ui.PrintLinef("Using environment '%s' as %s", c.Environment(), creds.Description())
}
taskReporter := boshuit.NewReporter(c.ui, true)
fileReporter := boshui.NewFileReporter(c.ui)
director, err := boshdir.NewFactory(c.logger).New(dirConfig, taskReporter, fileReporter)
if err != nil {
return nil, err
}
// Memoize only on successfuly creation
c.director = director
return c.director, nil
}
func (c *SessionImpl) setDirectorInfo() error {
if c.directorInfoSet {
return nil
}
director, err := c.AnonymousDirector()
if err != nil {
return err
}
info, err := director.Info()
if err != nil {
return err
}
c.directorInfo = info
c.directorInfoSet = true
return nil
}
func (c *SessionImpl) AnonymousDirector() (boshdir.Director, error) {
dirConfig, err := boshdir.NewConfigFromURL(c.Environment())
if err != nil {
return nil, err
}
dirConfig.CACert = c.context.CACert()
return boshdir.NewFactory(c.logger).New(dirConfig, nil, nil)
}
func (c *SessionImpl) Deployment() (boshdir.Deployment, error) {
director, err := c.Director()
if err != nil {
return nil, err
}
deploymentName := c.context.Deployment()
deployment, err := director.FindDeployment(deploymentName)
if err != nil {
return nil, err
}
if c.printDeployment {
c.ui.PrintLinef("Using deployment '%s'", deploymentName)
}
return deployment, nil
}