This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry/bosh-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
162 lines (124 loc) · 3.52 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
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
}
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) {
director, err := c.AnonymousDirector()
if err != nil {
return nil, err
}
info, err := director.Info()
if err != nil {
return nil, err
}
uaaURL := info.Auth.Options["url"]
uaaURLStr, ok := uaaURL.(string)
if !ok {
return nil, bosherr.Errorf("Exected 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()
if creds.IsBasic() {
dirConfig.Username = creds.Username
dirConfig.Password = creds.Password
} 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) 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
}