/
app.go
212 lines (175 loc) · 5.26 KB
/
app.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package common
import (
"context"
"time"
"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
"github.com/gravitational/teleport/api/client/proto"
"github.com/gravitational/teleport/integrations/access/common/teleport"
"github.com/gravitational/teleport/integrations/lib"
"github.com/gravitational/teleport/integrations/lib/logger"
)
const (
// minServerVersion is the minimal teleport version the plugin supports.
minServerVersion = "6.1.0-beta.1"
// InitTimeout is used to bound execution time of health check and teleport version check.
initTimeout = time.Second * 10
)
// BaseApp is responsible for handling the common features for a plugin.
// It will start a Teleport client, listen for events and treat them.
// It also handles signals and watches its thread.
// To instantiate a new BaseApp, use NewApp()
type BaseApp struct {
PluginName string
APIClient teleport.Client
Conf PluginConfiguration
Bot MessagingBot
Clock clockwork.Clock
apps []App
mainJob lib.ServiceJob
*lib.Process
}
// NewApp creates a new BaseApp and initialize its main job
func NewApp(conf PluginConfiguration, pluginName string) *BaseApp {
baseApp := BaseApp{
PluginName: pluginName,
Conf: conf,
}
baseApp.mainJob = lib.NewServiceJob(baseApp.run)
return &baseApp
}
// Run initializes and runs a watcher and a callback server
func (a *BaseApp) Run(ctx context.Context) error {
// Initialize the process.
a.Process = lib.NewProcess(ctx)
a.SpawnCriticalJob(a.mainJob)
<-a.Process.Done()
return a.Err()
}
// Err returns the error app finished with.
func (a *BaseApp) Err() error {
return trace.Wrap(a.mainJob.Err())
}
// WaitReady waits for http and watcher service to start up.
func (a *BaseApp) WaitReady(ctx context.Context) (bool, error) {
return a.mainJob.WaitReady(ctx)
}
func (a *BaseApp) checkTeleportVersion(ctx context.Context) (proto.PingResponse, error) {
log := logger.Get(ctx)
log.Debug("Checking Teleport server version")
pong, err := a.APIClient.Ping(ctx)
if err != nil {
if trace.IsNotImplemented(err) {
return pong, trace.Wrap(err, "server version must be at least %s", minServerVersion)
}
return pong, trace.Wrap(err, "Unable to get Teleport server version")
}
err = lib.AssertServerVersion(pong, minServerVersion)
return pong, trace.Wrap(err)
}
// initTeleport creates a Teleport client and validates Teleport connectivity.
func (a *BaseApp) initTeleport(ctx context.Context, conf PluginConfiguration) (clusterName, webProxyAddr string, err error) {
clt, err := conf.GetTeleportClient(ctx)
if err != nil {
return "", "", trace.Wrap(err)
}
a.APIClient = clt
pong, err := a.checkTeleportVersion(ctx)
if err != nil {
return "", "", trace.Wrap(err)
}
if pong.ServerFeatures.AdvancedAccessWorkflows {
webProxyAddr = pong.ProxyPublicAddr
}
return pong.ClusterName, webProxyAddr, nil
}
type App interface {
Init(baseApp *BaseApp) error
Start(process *lib.Process)
WaitReady(ctx context.Context) (bool, error)
WaitForDone()
Err() error
}
// run starts the event watcher job and blocks utils it stops
func (a *BaseApp) run(ctx context.Context) error {
log := logger.Get(ctx)
if err := a.init(ctx); err != nil {
return trace.Wrap(err)
}
for _, app := range a.apps {
app.Start(a.Process)
}
allOK := true
for _, app := range a.apps {
ok, err := app.WaitReady(ctx)
if err != nil {
return trace.Wrap(err)
}
allOK = allOK && ok
if !allOK {
break
}
}
a.mainJob.SetReady(allOK)
if allOK {
log.Info("Plugin is ready")
} else {
log.Error("Plugin is not ready")
}
for _, app := range a.apps {
app.WaitForDone()
}
var allErrs []error
for _, app := range a.apps {
allErrs = append(allErrs, app.Err())
}
return trace.NewAggregate(allErrs...)
}
func (a *BaseApp) init(ctx context.Context) error {
ctx, cancel := context.WithTimeout(ctx, initTimeout)
defer cancel()
log := logger.Get(ctx)
if a.Clock == nil {
a.Clock = clockwork.NewRealClock()
}
clusterName, webProxyAddr, err := a.initTeleport(ctx, a.Conf)
if err != nil {
return trace.Wrap(err)
}
a.Bot, err = a.Conf.NewBot(clusterName, webProxyAddr)
if err != nil {
return trace.Wrap(err)
}
a.apps = a.Bot.SupportedApps()
if len(a.apps) == 0 {
return trace.BadParameter("no apps supported for this plugin")
}
for _, app := range a.apps {
if err := app.Init(a); err != nil {
return trace.Wrap(err)
}
}
log.Debug("Starting API health check...")
if err = a.Bot.CheckHealth(ctx); err != nil {
return trace.Wrap(err, "API health check failed")
}
log.Debug("API health check finished ok")
return nil
}