-
Notifications
You must be signed in to change notification settings - Fork 13
/
agent.go
585 lines (496 loc) · 16.7 KB
/
agent.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
package agent
import (
"context"
"fmt"
"os"
"os/signal"
"runtime"
"runtime/pprof"
"strings"
"sync"
"syscall"
"time"
agentcache "github.com/Axway/agent-sdk/pkg/agent/cache"
"github.com/Axway/agent-sdk/pkg/agent/handler"
"github.com/Axway/agent-sdk/pkg/agent/resource"
"github.com/Axway/agent-sdk/pkg/agent/stream"
"github.com/Axway/agent-sdk/pkg/api"
"github.com/Axway/agent-sdk/pkg/apic"
apiV1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
"github.com/Axway/agent-sdk/pkg/apic/auth"
"github.com/Axway/agent-sdk/pkg/apic/provisioning"
"github.com/Axway/agent-sdk/pkg/authz/oauth"
"github.com/Axway/agent-sdk/pkg/cache"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/migrate"
"github.com/Axway/agent-sdk/pkg/util"
"github.com/Axway/agent-sdk/pkg/util/errors"
hc "github.com/Axway/agent-sdk/pkg/util/healthcheck"
"github.com/Axway/agent-sdk/pkg/util/log"
)
// AgentStatus - status for Agent resource
const (
AgentRunning = "running"
AgentStopped = "stopped"
AgentFailed = "failed"
AgentUnhealthy = "unhealthy"
)
// AgentResourceType - Holds the type for agent resource in Central
var AgentResourceType string
// APIValidator - Callback for validating the API
type APIValidator func(apiID, stageName string) bool
// ConfigChangeHandler - Callback for Config change event
type ConfigChangeHandler func()
type agentData struct {
agentResourceManager resource.Manager
apicClient apic.Client
cfg config.CentralConfig
agentFeaturesCfg config.AgentFeaturesConfig
tokenRequester auth.PlatformTokenGetter
teamMap cache.Cache
cacheManager agentcache.Manager
apiValidator APIValidator
apiValidatorLock sync.Mutex
apiValidatorJobID string
configChangeHandler ConfigChangeHandler
agentResourceChangeHandler ConfigChangeHandler
proxyResourceHandler *handler.StreamWatchProxyHandler
isInitialized bool
provisioner provisioning.Provisioning
marketplaceMigration migrate.Migrator
streamer *stream.StreamerClient
authProviderRegistry oauth.ProviderRegistry
publishingLock *sync.Mutex
ardLock sync.Mutex
// profiling
profileDone chan struct{}
}
var agent agentData
var agentMutex sync.RWMutex
var logger log.FieldLogger
func init() {
logger = log.NewFieldLogger().
WithPackage("sdk.agent").
WithComponent("agent")
agent.proxyResourceHandler = handler.NewStreamWatchProxyHandler()
agentMutex = sync.RWMutex{}
agent.publishingLock = &sync.Mutex{}
}
// Initialize - Initializes the agent
func Initialize(centralCfg config.CentralConfig) error {
return InitializeWithAgentFeatures(centralCfg, config.NewAgentFeaturesConfiguration())
}
// InitializeWithAgentFeatures - Initializes the agent with agent features
func InitializeWithAgentFeatures(centralCfg config.CentralConfig, agentFeaturesCfg config.AgentFeaturesConfig) error {
if agent.teamMap == nil {
agent.teamMap = cache.New()
}
err := checkRunningAgent()
if err != nil {
return err
}
err = config.ValidateConfig(agentFeaturesCfg)
if err != nil {
return err
}
agent.agentFeaturesCfg = agentFeaturesCfg
centralCfg.SetIsMarketplaceSubsEnabled(agentFeaturesCfg.MarketplaceProvisioningEnabled())
// validate the central config
if agentFeaturesCfg.ConnectionToCentralEnabled() {
err = config.ValidateConfig(centralCfg)
if err != nil {
return err
}
}
// Only create the api map cache if it does not already exist
if agent.cacheManager == nil {
agent.cacheManager = agentcache.NewAgentCacheManager(centralCfg, agentFeaturesCfg.PersistCacheEnabled())
}
setCentralConfig(centralCfg)
if centralCfg.GetUsageReportingConfig().IsOfflineMode() {
// Offline mode does not need more initialization
return nil
}
singleEntryFilter := []string{
// Traceability host URL will be added by the traceability factory
centralCfg.GetURL(),
centralCfg.GetPlatformURL(),
centralCfg.GetAuthConfig().GetTokenURL(),
centralCfg.GetUsageReportingConfig().GetURL(),
}
api.SetConfigAgent(
centralCfg.GetEnvironmentName(),
centralCfg.IsUsingGRPC(),
isRunningInDockerContainer(),
centralCfg.GetAgentName(),
centralCfg.GetSingleURL(),
singleEntryFilter,
)
if agentFeaturesCfg.ConnectionToCentralEnabled() {
err = handleCentralConfig(centralCfg)
if err != nil {
return err
}
}
if !agent.isInitialized {
err = handleInitialization()
if err != nil {
return err
}
}
agent.isInitialized = true
return nil
}
func handleCentralConfig(centralCfg config.CentralConfig) error {
err := initializeTokenRequester(centralCfg)
if err != nil {
return fmt.Errorf("could not authenticate to Amplify, please check your keys and key password")
}
// Init apic client when the agent starts, and on config change.
agent.apicClient = apic.New(centralCfg, agent.tokenRequester, agent.cacheManager)
if util.IsNotTest() {
err = initEnvResources(centralCfg, agent.apicClient)
if err != nil {
return err
}
}
if centralCfg.GetAgentName() != "" {
if agent.agentResourceManager == nil {
agent.agentResourceManager, err = resource.NewAgentResourceManager(
agent.cfg, agent.apicClient, agent.agentResourceChangeHandler,
)
if err != nil {
return err
}
} else {
agent.agentResourceManager.OnConfigChange(agent.cfg, agent.apicClient)
}
}
return nil
}
func handleInitialization() error {
setupSignalProcessor()
// only do the periodic health check stuff if NOT in unit tests and running binary agents
if util.IsNotTest() {
hc.StartPeriodicHealthCheck()
}
if util.IsNotTest() && agent.agentFeaturesCfg.ConnectionToCentralEnabled() {
// if credentials can expire and need to be deprovisioned then start the credential checker
registerCredentialChecker()
registerExternalIDPs()
startTeamACLCache()
err := registerSubscriptionWebhook(agent.cfg.GetAgentType(), agent.apicClient)
if err != nil {
return errors.Wrap(errors.ErrRegisterSubscriptionWebhook, err.Error())
}
}
return nil
}
// InitializeProfiling - setup the CPU and Memory profiling if options given
func InitializeProfiling(cpuProfile, memProfile string) {
if memProfile != "" || cpuProfile != "" {
setupProfileSignalProcessor(cpuProfile, memProfile)
}
}
func registerExternalIDPs() {
if agent.cfg.GetAgentType() != config.TraceabilityAgent {
idPCfg := agent.agentFeaturesCfg.GetExternalIDPConfig()
proxy := agent.cfg.GetProxyURL()
timeout := agent.cfg.GetClientTimeout()
for _, idp := range idPCfg.GetIDPList() {
tlsCfg := idp.GetTLSConfig()
if idp.GetTLSConfig() == nil {
tlsCfg = agent.cfg.GetTLSConfig()
}
registerCredentialProvider(idp, tlsCfg, proxy, timeout)
}
}
}
func registerCredentialProvider(idp config.IDPConfig, tlsCfg config.TLSConfig, proxyURL string, clientTimeout time.Duration) {
err := GetAuthProviderRegistry().RegisterProvider(idp, tlsCfg, proxyURL, clientTimeout)
if err != nil {
logger.
WithField("name", idp.GetIDPName()).
WithField("type", idp.GetIDPType()).
WithField("metadata-url", idp.GetMetadataURL()).
Errorf("unable to register external IdP provider, any credential request to the IdP will not be processed. %s", err.Error())
}
}
func initEnvResources(cfg config.CentralConfig, client apic.Client) error {
env, err := client.GetEnvironment()
if err != nil {
return err
}
cfg.SetAxwayManaged(env.Spec.AxwayManaged)
if cfg.GetEnvironmentID() == "" {
// need to save this ID for the traceability agent for later
cfg.SetEnvironmentID(env.Metadata.ID)
}
// Set up credential config from environment resource policies
cfg.GetCredentialConfig().SetShouldDeprovisionExpired(env.Policies.Credentials.Expiry.Action == "deprovision")
cfg.GetCredentialConfig().SetExpirationDays(int(env.Policies.Credentials.Expiry.Period))
if cfg.GetTeamID() == "" {
team, err := client.GetCentralTeamByName(cfg.GetTeamName())
if err != nil {
return err
}
cfg.SetTeamID(team.ID)
}
return nil
}
func checkRunningAgent() error {
// Check only on startup of binary agents
if !agent.isInitialized && util.IsNotTest() && !isRunningInDockerContainer() {
return hc.CheckIsRunning()
}
return nil
}
// InitializeForTest - Initialize for test
func InitializeForTest(apicClient apic.Client) {
if agent.cfg != nil {
agent.cacheManager = agentcache.NewAgentCacheManager(agent.cfg, false)
}
agent.apicClient = apicClient
}
// GetConfigChangeHandler - returns registered config change handler
func GetConfigChangeHandler() ConfigChangeHandler {
return agent.configChangeHandler
}
// OnConfigChange - Registers handler for config change event
func OnConfigChange(configChangeHandler ConfigChangeHandler) {
agent.configChangeHandler = configChangeHandler
}
// OnAgentResourceChange - Registers handler for resource change event
func OnAgentResourceChange(agentResourceChangeHandler ConfigChangeHandler) {
agent.agentResourceChangeHandler = agentResourceChangeHandler
}
// RegisterResourceEventHandler - Registers handler for resource events
func RegisterResourceEventHandler(name string, resourceEventHandler handler.Handler) {
agent.proxyResourceHandler.RegisterTargetHandler(name, resourceEventHandler)
}
// UnregisterResourceEventHandler - removes the specified resource event handler
func UnregisterResourceEventHandler(name string) {
agent.proxyResourceHandler.UnregisterTargetHandler(name)
}
// GetAuthProviderRegistry - Returns the auth provider registry
func GetAuthProviderRegistry() oauth.ProviderRegistry {
if agent.authProviderRegistry == nil {
agent.authProviderRegistry = oauth.NewProviderRegistry()
}
return agent.authProviderRegistry
}
func registerSubscriptionWebhook(at config.AgentType, client apic.Client) error {
if at == config.DiscoveryAgent {
return client.RegisterSubscriptionWebhook()
}
return nil
}
func startTeamACLCache() {
// Only discovery agents need to start the ACL handler
if agent.cfg.GetAgentType() == config.DiscoveryAgent {
registerAccessControlListHandler()
}
registerTeamMapCacheJob()
}
func isRunningInDockerContainer() bool {
// Within the cgroup file, if you are not in a docker container all entries are like these devices:/
// If in a docker container, entries are like this: devices:/docker/xxxxxxxxx.
// So, all we need to do is see if ":/docker" exists somewhere in the file.
bytes, err := os.ReadFile("/proc/1/cgroup")
if err != nil {
return false
}
// Convert []byte to string and print to screen
text := string(bytes)
return strings.Contains(text, ":/docker")
}
// initializeTokenRequester - Create a new auth token requester
func initializeTokenRequester(centralCfg config.CentralConfig) error {
var err error
agent.tokenRequester = auth.NewPlatformTokenGetterWithCentralConfig(centralCfg)
if util.IsNotTest() {
_, err = agent.tokenRequester.GetToken()
}
return err
}
// GetCentralAuthToken - Returns the Auth token from AxwayID to make API call to Central
func GetCentralAuthToken() (string, error) {
if agent.tokenRequester == nil {
return "", apic.ErrAuthenticationCall
}
return agent.tokenRequester.GetToken()
}
// GetCentralClient - Returns the APIC Client
func GetCentralClient() apic.Client {
return agent.apicClient
}
// GetCentralConfig - Returns the APIC Client
func GetCentralConfig() config.CentralConfig {
agentMutex.Lock()
defer agentMutex.Unlock()
return agent.cfg
}
// setCentralConfig - Sets the central config
func setCentralConfig(cfg config.CentralConfig) {
agentMutex.Lock()
defer agentMutex.Unlock()
agent.cfg = cfg
}
// GetAPICache - Returns the cache
func GetAPICache() cache.Cache {
if agent.cacheManager == nil {
agent.cacheManager = agentcache.NewAgentCacheManager(agent.cfg, agent.agentFeaturesCfg.PersistCacheEnabled())
}
return agent.cacheManager.GetAPIServiceCache()
}
// GetCacheManager - Returns the cache
func GetCacheManager() agentcache.Manager {
if agent.cacheManager == nil {
agent.cacheManager = agentcache.NewAgentCacheManager(agent.cfg, agent.agentFeaturesCfg.PersistCacheEnabled())
}
return agent.cacheManager
}
func GetResourceManager() resource.Manager {
if agent.agentResourceManager == nil {
return nil
}
return agent.agentResourceManager
}
// GetAgentResource - Returns Agent resource
func GetAgentResource() *apiV1.ResourceInstance {
if agent.agentResourceManager == nil {
return nil
}
return agent.agentResourceManager.GetAgentResource()
}
// GetAgentResourceManager - Returns Agent resource
func GetAgentResourceManager() resource.Manager {
return agent.agentResourceManager
}
// AddUpdateAgentDetails - Adds a new or Updates an existing key on the agent details sub resource
func AddUpdateAgentDetails(key, value string) {
if agent.agentResourceManager != nil {
agent.agentResourceManager.AddUpdateAgentDetails(key, value)
}
}
// GetDetailFromAgentResource - gets the value of an agent detail from the resource
func GetDetailFromAgentResource(key string) string {
if agent.agentResourceManager == nil {
return ""
}
val, _ := util.GetAgentDetailsValue(agent.agentResourceManager.GetAgentResource(), key)
return val
}
// UpdateStatus - Updates the agent state
func UpdateStatus(status, description string) {
UpdateStatusWithPrevious(status, status, description)
}
// UpdateStatusWithPrevious - Updates the agent state providing a previous state
func UpdateStatusWithPrevious(status, prevStatus, description string) {
ctx := context.WithValue(context.Background(), ctxLogger, logger)
UpdateStatusWithContext(ctx, status, prevStatus, description)
}
// UpdateStatusWithContext - Updates the agent state providing a context
func UpdateStatusWithContext(ctx context.Context, status, prevStatus, description string) {
logger := ctx.Value(ctxLogger).(log.FieldLogger)
if agent.agentResourceManager != nil {
err := agent.agentResourceManager.UpdateAgentStatus(status, prevStatus, description)
if err != nil {
logger.WithError(err).Warnf("could not update the agent status reference")
}
}
}
func setupSignalProcessor() {
if !agent.agentFeaturesCfg.ProcessSystemSignalsEnabled() {
return
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
<-sigs
logger.Info("Stopping agent")
if agent.profileDone != nil {
<-agent.profileDone
}
cleanUp()
agent.cacheManager.SaveCache()
os.Exit(0)
}()
}
func setupProfileSignalProcessor(cpuProfile, memProfile string) {
if agent.agentFeaturesCfg.ProcessSystemSignalsEnabled() {
// create channel for base signal processor
agent.profileDone = make(chan struct{})
}
// start the CPU profiling
var cpuFile *os.File
if cpuProfile != "" {
var err error
cpuFile, err = os.Create(cpuProfile)
if err != nil {
fmt.Printf("Error creating cpu profiling file: %v", err)
}
if err := pprof.StartCPUProfile(cpuFile); err != nil {
fmt.Printf("Error running the cpu profiling: %v", err)
}
}
// Listen for a system signal to stop the agent
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
go func() {
<-sigs
// stop cpu profiling if it was started
if cpuProfile != "" {
pprof.StopCPUProfile()
cpuFile.Close()
}
// run the memory profiling
if memProfile != "" {
memFile, err := os.Create(memProfile)
if err != nil {
fmt.Printf("Error creating memory profiling file: %v", err)
}
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(memFile); err != nil {
fmt.Printf("Error running the memory profiling: %v", err)
}
memFile.Close() // error handling omitted for example
}
if agent.agentFeaturesCfg.ProcessSystemSignalsEnabled() {
// signal the base signal processor now that the profiling output is complete
agent.profileDone <- struct{}{}
}
}()
}
// cleanUp - AgentCleanup
func cleanUp() {
UpdateStatusWithPrevious(AgentStopped, AgentRunning, "")
}
func newHandlers() []handler.Handler {
handlers := []handler.Handler{
handler.NewAPISvcHandler(agent.cacheManager),
handler.NewInstanceHandler(agent.cacheManager),
handler.NewAgentResourceHandler(agent.agentResourceManager),
handler.NewWatchResourceHandler(agent.cacheManager, agent.cfg),
agent.proxyResourceHandler,
}
if agent.cfg.GetAgentType() == config.DiscoveryAgent {
handlers = append(
handlers,
handler.NewCategoryHandler(agent.cacheManager),
handler.NewCRDHandler(agent.cacheManager),
handler.NewARDHandler(agent.cacheManager),
handler.NewEnvironmentHandler(agent.cacheManager, agent.cfg.GetCredentialConfig()),
)
}
// Register managed application and access handler for traceability agent
// For discovery agent, the handlers gets registered while setting up provisioner
if agent.cfg.GetAgentType() == config.TraceabilityAgent {
handlers = append(
handlers,
handler.NewTraceAccessRequestHandler(agent.cacheManager, agent.apicClient),
handler.NewTraceManagedApplicationHandler(agent.cacheManager),
)
}
return handlers
}