/
credentialrefresher.go
549 lines (482 loc) · 20 KB
/
credentialrefresher.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
// Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not
// use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 credentialrefresher
import (
"bufio"
"context"
"errors"
"fmt"
"io"
"math/rand"
"os"
"path/filepath"
"runtime/debug"
"strings"
"sync"
"time"
"github.com/aws/amazon-ssm-agent/agent/appconfig"
"github.com/aws/amazon-ssm-agent/agent/backoffconfig"
"github.com/aws/amazon-ssm-agent/agent/fileutil"
"github.com/aws/amazon-ssm-agent/agent/log"
"github.com/aws/amazon-ssm-agent/agent/log/logger"
"github.com/aws/amazon-ssm-agent/agent/managedInstances/sharedCredentials"
"github.com/aws/amazon-ssm-agent/agent/versionutil"
"github.com/aws/amazon-ssm-agent/common/identity"
"github.com/aws/amazon-ssm-agent/common/identity/availableidentities/ec2"
"github.com/aws/amazon-ssm-agent/common/identity/credentialproviders"
"github.com/aws/amazon-ssm-agent/common/identity/credentialproviders/ec2roleprovider"
"github.com/aws/amazon-ssm-agent/common/identity/endpoint"
identity2 "github.com/aws/amazon-ssm-agent/common/identity/identity"
"github.com/aws/amazon-ssm-agent/common/runtimeconfig"
agentctx "github.com/aws/amazon-ssm-agent/core/app/context"
"github.com/aws/amazon-ssm-agent/core/executor"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/cenkalti/backoff/v4"
"github.com/cihub/seelog"
)
const (
// last version in 3.2 that share cred file between workers for EC2
defaultSSMEC2SharedFileUsageLastVersion = "3.2.1241.0"
defaultSSMStartVersion = "3.2.0.0"
credentialSourceEC2 = ec2roleprovider.CredentialSourceEC2
)
var (
storeSharedCredentials = sharedCredentials.Store
purgeSharedCredentials = sharedCredentials.Purge
backoffRetry = backoff.Retry
newSharedCredentials = credentials.NewSharedCredentials
fileExists = fileutil.Exists
getFileNames = fileutil.GetFileNames
newProcessExecutor = executor.NewProcessExecutor
osOpen = os.Open
isCredSaveDefaultSSMAgentVersionPresentUsingReader = isCredSaveDefaultSSMAgentVersionPresentUsingIoReader
)
type ICredentialRefresher interface {
Start() error
Stop()
GetCredentialsReadyChan() chan struct{}
}
type credentialsRefresher struct {
log log.T
appConfig *appconfig.SsmagentConfig
agentIdentity identity.IAgentIdentity
provider credentialproviders.IRemoteProvider
runtimeConfigClient runtimeconfig.IIdentityRuntimeConfigClient
identityRuntimeConfig runtimeconfig.IdentityRuntimeConfig
endpointHelper endpoint.IEndpointHelper
backoffConfig *backoff.ExponentialBackOff
credsReadyOnce sync.Once
credentialsReadyChan chan struct{}
stopCredentialRefresherChan chan struct{}
isCredentialRefresherRunning bool
getCurrentTimeFunc func() time.Time
timeAfterFunc func(time.Duration) <-chan time.Time
}
func NewCredentialRefresher(context agentctx.ICoreAgentContext) ICredentialRefresher {
return &credentialsRefresher{
log: context.Log().WithContext("[CredentialRefresher]"),
agentIdentity: context.Identity(),
provider: nil,
runtimeConfigClient: runtimeconfig.NewIdentityRuntimeConfigClient(),
identityRuntimeConfig: runtimeconfig.IdentityRuntimeConfig{},
credsReadyOnce: sync.Once{},
credentialsReadyChan: make(chan struct{}, 1),
stopCredentialRefresherChan: make(chan struct{}),
isCredentialRefresherRunning: false,
getCurrentTimeFunc: time.Now,
timeAfterFunc: time.After,
endpointHelper: endpoint.NewEndpointHelper(context.Log().WithContext("[EndpointHelper]"), *context.AppConfig()),
appConfig: context.AppConfig(),
}
}
func (c *credentialsRefresher) durationUntilRefresh() time.Duration {
timeNow := c.getCurrentTimeFunc()
// Credentials are already expired, should be rotated now
expiresAt := c.identityRuntimeConfig.CredentialsExpiresAt
if expiresAt.Before(timeNow) || expiresAt.Equal(timeNow) {
return time.Duration(0)
}
retrievedAt := c.identityRuntimeConfig.CredentialsRetrievedAt
credentialsDuration := expiresAt.Sub(retrievedAt)
// Set the expiration window to be half of the token's lifetime. This allows credential refreshes to survive transient
// network issues more easily. Expiring at half the lifetime also follows the behavior of other protocols such as DHCP
// https://tools.ietf.org/html/rfc2131#section-4.4.5. Note that not all of the behavior specified in that RFC is
// implemented, just the suggestion to start renewals at 50% of token validity.
rotateBeforeExpiryDuration := credentialsDuration / 2
rotateAtTime := expiresAt.Add(-rotateBeforeExpiryDuration)
rotateDuration := rotateAtTime.Sub(timeNow)
c.log.Infof("Next credential rotation will be in %v minutes", rotateDuration.Minutes())
return rotateDuration
}
func (c *credentialsRefresher) Start() error {
var err error
credentialProvider, ok := identity2.GetRemoteProvider(c.agentIdentity)
if !ok {
c.log.Info("Identity does not require credential refresher")
c.sendCredentialsReadyMessage()
return nil
}
if !credentialProvider.SharesCredentials() {
c.log.Info("Identity does not want core agent to rotate credentials")
c.sendCredentialsReadyMessage()
return nil
}
c.provider = credentialProvider
// Initialize the identity runtime config from disk
if c.identityRuntimeConfig, err = c.runtimeConfigClient.GetConfig(); err != nil {
return err
}
c.backoffConfig, err = backoffconfig.GetDefaultExponentialBackoff()
if err != nil {
return fmt.Errorf("error creating backoff config: %v", err)
}
// Seed random number generator used to generate jitter during retry
rand.Seed(time.Now().UnixNano())
go c.credentialRefresherRoutine()
c.isCredentialRefresherRunning = true
c.log.Infof("credentialRefresher has started")
return nil
}
func (c *credentialsRefresher) Stop() {
if !c.isCredentialRefresherRunning {
return
}
c.log.Info("Sending credential refresher stop signal")
c.stopCredentialRefresherChan <- struct{}{}
c.isCredentialRefresherRunning = false
}
func (c *credentialsRefresher) GetCredentialsReadyChan() chan struct{} {
return c.credentialsReadyChan
}
func (c *credentialsRefresher) sendCredentialsReadyMessage() {
c.log.Info("Credentials ready")
c.credsReadyOnce.Do(func() {
c.credentialsReadyChan <- struct{}{}
c.log.Flush()
})
}
// retrieveCredsWithRetry will never exit unless it receives a message on stopChan or is able to successfully call Retrieve
func (c *credentialsRefresher) retrieveCredsWithRetry(ctx context.Context) (credentials.Value, bool) {
retryCount := 0
identityGetDurationMap, ok := identityGetDurationMaps[c.agentIdentity.IdentityType()]
if !ok {
c.log.Errorf("Unexpected identity retrieved: %v. Stopping credential refresher.", c.agentIdentity.IdentityType())
return credentials.Value{}, true
}
for {
creds, err := c.provider.RemoteRetrieve(ctx)
if err == nil {
return creds, false
}
// this will log as debug when the credential refresher retries exceeds 3
logMessage := fmt.Sprintf("Retrieve credentials produced error: %v", err)
c.minLog(seelog.ErrorLvl, logMessage, retryCount)
var sleepDuration time.Duration
// initialize default sleep duration
if sleepDurationFunc, ok := identityGetDurationMap[ErrAllOtherNonAWSErrors]; ok {
sleepDuration = sleepDurationFunc(retryCount)
} else {
sleepDuration = getDefaultBackoffRetryJitterSleepDuration(retryCount)
}
// override sleep duration for aws errors
var awsErr awserr.Error
if isAwsErr := errors.As(err, &awsErr); isAwsErr {
// Check if error is a non-retryable error if fingerprint changes or response is access denied exception
if awsErr.Code() == ssm.ErrCodeMachineFingerprintDoesNotMatch || awsErr.Code() == ErrCodeAccessDeniedException {
if sleepDurationFunc, ok := identityGetDurationMap[awsErr.Code()]; ok {
sleepDuration = sleepDurationFunc(retryCount)
}
} else {
if sleepDurationFunc, ok := identityGetDurationMap[ErrAllOtherAWSErrors]; ok {
sleepDuration = sleepDurationFunc(retryCount)
}
var awsRequestFailure awserr.RequestFailure
if isRequestFailure := errors.As(err, &awsRequestFailure); isRequestFailure {
logMessage = fmt.Sprintf("Status code %v returned from AWS API. RequestId: %s Message: %s",
awsRequestFailure.StatusCode(),
awsRequestFailure.RequestID(),
awsRequestFailure.Message())
// this will log as debug when the credential refresher retries exceeds 3
c.minLog(seelog.ErrorLvl, logMessage, retryCount)
}
}
}
logMessage = fmt.Sprintf("Sleeping for %v before retrying retrieve credentials", sleepDuration)
// this will log as debug when the credential refresher retries exceeds 3
c.minLog(seelog.InfoLvl, logMessage, retryCount)
// Max retry count is 16, which will sleep for about 18-22 hours for long sleep strategy
if retryCount < 16 {
retryCount++
}
select {
case <-c.stopCredentialRefresherChan:
return creds, true
case <-c.timeAfterFunc(sleepDuration):
}
}
}
func (c *credentialsRefresher) minLog(defaultLevel int, logMessage string, retryCount int) {
isEc2 := c.agentIdentity.IdentityType() == ec2.IdentityType
maxRetryCount := 3
if retryCount >= maxRetryCount && isEc2 {
c.log.Debug(logMessage)
return
}
switch defaultLevel {
case seelog.ErrorLvl:
c.log.Error(logMessage)
case seelog.InfoLvl:
c.log.Info(logMessage)
case seelog.DebugLvl:
c.log.Debug(logMessage)
default:
c.log.Info(logMessage)
}
}
func (c *credentialsRefresher) credentialRefresherRoutine() {
var err error
defer func() {
if err := recover(); err != nil {
c.log.Errorf("credentials refresher panic: %v", err)
c.log.Errorf("Stacktrace:\n%s", debug.Stack())
c.log.Flush()
// We never want to exit this loop unless explicitly asked to do so, restart loop
time.Sleep(5 * time.Minute)
go c.credentialRefresherRoutine()
}
}()
// if credentials are not expired, verify that credentials are available.
if c.identityRuntimeConfig.CredentialsExpiresAt.After(c.getCurrentTimeFunc()) {
if c.identityRuntimeConfig.ShareFile == "" && c.identityRuntimeConfig.CredentialSource == credentialSourceEC2 {
c.sendCredentialsReadyMessage()
} else {
localCredsProvider := newSharedCredentials(c.identityRuntimeConfig.ShareFile, c.identityRuntimeConfig.ShareProfile)
if _, err := localCredsProvider.Get(); err != nil {
c.log.Warnf("Credentials are not available when they should be: %v", err)
// set expiration and retrieved to beginning of time if shared credentials are not available to force credential refresh
c.identityRuntimeConfig.CredentialsExpiresAt = time.Time{}
} else {
c.sendCredentialsReadyMessage()
}
}
}
c.log.Info("Starting credentials refresher loop")
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
select {
case <-c.stopCredentialRefresherChan:
c.log.Info("Stopping credentials refresher")
c.log.Flush()
return
case <-c.timeAfterFunc(c.durationUntilRefresh()):
c.log.Debug("Calling Retrieve on credentials provider")
creds, stopped := c.retrieveCredsWithRetry(ctx)
credentialsRetrievedAt := c.getCurrentTimeFunc()
if stopped {
c.log.Info("Stopping credentials refresher")
c.log.Flush()
return
}
credentialSource := c.provider.CredentialSource()
isEC2CredentialSource := credentialSource == ec2roleprovider.CredentialSourceEC2
isEc2CredFilePresent := fileExists(appconfig.DefaultEC2SharedCredentialsFilePath)
c.log.Tracef("Credential source %v", isEC2CredentialSource)
c.log.Tracef("Cred file present %v", isEc2CredFilePresent)
isCredFilePurged := false
if isEC2CredentialSource && isEc2CredFilePresent {
documentSessionWorkerRunning := c.isDocumentSessionWorkerProcessRunning()
credSaveDefaultSSMAgentPresent := c.credentialFileConsumerPresent()
c.log.Tracef("Document/session worker source %v", documentSessionWorkerRunning)
c.log.Tracef("Cred save default ssm agent %v", credSaveDefaultSSMAgentPresent)
if !(documentSessionWorkerRunning && credSaveDefaultSSMAgentPresent) {
c.log.Info("Starting credential purging")
err = backoffRetry(func() error {
return purgeSharedCredentials(appconfig.DefaultEC2SharedCredentialsFilePath)
}, c.backoffConfig)
if err != nil {
c.log.Warnf("error while purging cred file: %v", err)
} else {
isCredFilePurged = true
}
}
}
// ShareFile may be updated after retrieveCredsWithRetry()
newShareFile := c.provider.ShareFile()
if isCredFilePurged {
c.log.Info("Credential file purged")
} else {
// when ShouldPurgeInstanceProfileRoleCreds config is used,
// the credential file created in 3.2 for EC2 will be deleted irrespective of whether doc/session worker is running or not
c.tryPurgeCreds(newShareFile)
}
// skip saving when the credential source is EC2
if !isEC2CredentialSource && newShareFile != "" {
err = backoffRetry(func() error {
return storeSharedCredentials(c.log, creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken,
newShareFile, c.identityRuntimeConfig.ShareProfile, false)
}, c.backoffConfig)
// If failed, try once more with force
if err != nil {
c.log.Warn("Failed to write credentials to disk, attempting force write")
err = storeSharedCredentials(c.log, creds.AccessKeyID, creds.SecretAccessKey, creds.SessionToken,
newShareFile, c.identityRuntimeConfig.ShareProfile, true)
}
if err != nil {
// Saving credentials has been retried 6 times at this point.
c.log.Errorf("Failed to write credentials to disk even with force, retrying: %v", err)
continue
}
c.log.Debug("Successfully stored credentials")
}
c.log.Debug("Writing runtime configuration with updated expiration time")
configCopy := c.identityRuntimeConfig
configCopy.CredentialsRetrievedAt = credentialsRetrievedAt
configCopy.CredentialsExpiresAt = c.provider.RemoteExpiresAt()
configCopy.ShareFile = newShareFile
configCopy.CredentialSource = credentialSource
err = backoffRetry(func() error {
return c.runtimeConfigClient.SaveConfig(configCopy)
}, c.backoffConfig)
if err != nil {
c.log.Warnf("Failed to save new expiration: %v", err)
continue
}
c.identityRuntimeConfig = configCopy
c.sendCredentialsReadyMessage()
}
}
}
func (c *credentialsRefresher) tryPurgeCreds(newShareFile string) {
// Credentials are not purged until agent versions where EC2 agent workers
// only consume shared credentials are fully deprecated
shouldPurgeCreds := newShareFile != c.identityRuntimeConfig.ShareFile && c.appConfig.Agent.ShouldPurgeInstanceProfileRoleCreds
purgeFileLocation := c.identityRuntimeConfig.ShareFile
if shouldPurgeCreds && purgeFileLocation != "" {
if defaultSharedCredsFilePath, err := sharedCredentials.GetSharedCredsFilePath(""); err != nil {
c.log.Warn("Failed to check whether old credential file location is default aws share location." +
"Skipping purge of old credentials")
} else if purgeFileLocation == defaultSharedCredsFilePath {
c.log.Warn("Skipping purge of default aws shared credentials path")
} else if err = purgeSharedCredentials(purgeFileLocation); err != nil {
c.log.Warnf("Failed to purge old credentials. Err: %v", err)
} else {
c.log.Info("Credential file purged")
}
}
}
// isDocumentSessionWorkerProcessRunning checks whether document and session worker is running or not
func (c *credentialsRefresher) isDocumentSessionWorkerProcessRunning() bool {
var isDocumentSessionWorkerFound = false
var allProcesses []executor.OsProcess
var err error
processExecutor := newProcessExecutor(c.log)
if allProcesses, err = processExecutor.Processes(); err != nil {
c.log.Warnf("error while getting process list: %v", err)
return isDocumentSessionWorkerFound
}
for _, process := range allProcesses {
executableName := strings.ToLower(process.Executable)
if strings.Contains(executableName, appconfig.SSMDocumentWorkerName) {
c.log.Infof("document worker with pid <%v> running", process.Pid)
isDocumentSessionWorkerFound = true
break
}
if strings.Contains(executableName, appconfig.SSMSessionWorkerName) {
c.log.Infof("session worker with pid <%v> running", process.Pid)
isDocumentSessionWorkerFound = true
break
}
}
return isDocumentSessionWorkerFound
}
// credentialFileConsumerPresent checks whether default SSM agent which saves credential file was installed
// during the last 72 hours by looking into audit file
func (c *credentialsRefresher) credentialFileConsumerPresent() bool {
credSaveDefaultSSMAgentVersionPresent := false
auditFileDateTimeFormat := "2006-01-02"
auditFolderPath := filepath.Join(logger.DefaultLogDir, logger.AuditFolderName)
auditFileNames, err := getFileNames(auditFolderPath)
if err != nil {
c.log.Warnf("error while getting file names: %v", err)
return credSaveDefaultSSMAgentVersionPresent
}
currentTimeStamp := time.Now()
threeDaysBeforeDate := time.Date(currentTimeStamp.Year(), currentTimeStamp.Month(), currentTimeStamp.Day(), 0, 0, 0, 0, time.UTC).AddDate(0, 0, -3)
for _, fileName := range auditFileNames {
// In random cases, non audit files are present on customer instances.
// Ignore non audit files created in this folder
if !strings.HasPrefix(fileName, logger.EventLogFile) {
continue
}
// get date time from audit file name considering datetime format will be in "2006-01-02"
dateFromAuditFileName := fileName[len(fileName)-len(auditFileDateTimeFormat):]
// checks whether datetime in file name matches with format "2006-01-02"
eventFileDateStamp, err := time.Parse(auditFileDateTimeFormat, dateFromAuditFileName)
if err != nil {
c.log.Warnf("error while parsing audit file name for date stamp: %v", err)
credSaveDefaultSSMAgentVersionPresent = false
}
eventFileDateStampUTC := eventFileDateStamp.UTC()
if err == nil && eventFileDateStampUTC.After(threeDaysBeforeDate) {
func() {
file, err := osOpen(filepath.Join(auditFolderPath, fileName))
if err != nil {
c.log.Warnf("error while reading audit file: %v", err)
return
}
defer file.Close()
if isCredSaveDefaultSSMAgentVersionPresentUsingReader(file) {
credSaveDefaultSSMAgentVersionPresent = true
}
}()
}
if credSaveDefaultSSMAgentVersionPresent == true {
c.log.Infof("audit file with cred save default SSM Agent present")
break
}
}
return credSaveDefaultSSMAgentVersionPresent
}
// isCredSaveDefaultSSMAgentVersionPresentUsingIoReader checks whether default SSM agent which saves
// credential file is present in reader or not
func isCredSaveDefaultSSMAgentVersionPresentUsingIoReader(reader io.Reader) bool {
credSaveDefaultSSMAgentVersionPresent := false
// Create a new scanner for the file.
scanner := bufio.NewScanner(reader)
// Loop over the lines in the file.
for scanner.Scan() {
splitVal := strings.Split(scanner.Text(), " ")
// agent_telemetry event type should have only four fields
if len(splitVal) != 4 {
continue
}
// not an agent telemetry event type
if splitVal[0] != logger.AgentTelemetryMessage {
continue
}
versionCompare, err := versionutil.VersionCompare(splitVal[2], defaultSSMEC2SharedFileUsageLastVersion)
if err != nil || versionCompare > 0 {
continue
}
versionCompare, err = versionutil.VersionCompare(splitVal[2], defaultSSMStartVersion)
if err != nil || versionCompare < 0 {
continue
}
credSaveDefaultSSMAgentVersionPresent = true
break
}
return credSaveDefaultSSMAgentVersionPresent
}