Skip to content

Commit

Permalink
Update task IAM role model
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiyuanzzz committed Nov 27, 2023
1 parent 0bab10f commit 4a67b5a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 22 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions ecs-agent/acs/session/session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ const (
"expiration": "2016-03-25T06:17:19.318+0000",
"roleArn": "r1",
"secretAccessKey": "secretAccessKey",
"sessionToken": "token"
"sessionToken": "token",
"credentialScope": "scope"
},
"version": "3",
"volumes": [],
Expand Down Expand Up @@ -125,7 +126,8 @@ const (
"expiration": "later",
"roleArn": "r1",
"secretAccessKey": "newskid",
"sessionToken": "newstkn"
"sessionToken": "newstkn",
"credentialScope": "cdsp"
}
}
}
Expand Down Expand Up @@ -976,6 +978,7 @@ func TestStartSessionHandlesRefreshCredentialsMessages(t *testing.T) {
SessionToken: "newstkn",
Expiration: "later",
CredentialsID: credentialsIdInRefreshMessage,
CredentialScope: "cdsp",
RoleType: rolecredentials.ApplicationRoleType,
},
}
Expand Down
4 changes: 3 additions & 1 deletion ecs-agent/credentials/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ type IAMRoleCredentials struct {
// Expiration is a string instead of a timestamp. This is to avoid any loss of context
// while marshalling/unmarshalling this field in the agent. The agent just echo's
// whatever is sent by the backend.
Expiration string `json:"Expiration"`
Expiration string `json:"Expiration"`
CredentialScope string `json:"CredentialScope"`
// RoleType distinguishes between TaskRole and ExecutionRole for the
// credentials that are sent from the backend
RoleType string `json:"-"`
Expand Down Expand Up @@ -100,6 +101,7 @@ func IAMRoleCredentialsFromACS(roleCredentials *ecsacs.IAMRoleCredentials, roleT
AccessKeyID: aws.StringValue(roleCredentials.AccessKeyId),
SecretAccessKey: aws.StringValue(roleCredentials.SecretAccessKey),
Expiration: aws.StringValue(roleCredentials.Expiration),
CredentialScope: aws.StringValue(roleCredentials.CredentialScope),
RoleType: roleType,
}
}
Expand Down
6 changes: 6 additions & 0 deletions ecs-agent/credentials/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func TestIAMRoleCredentialsFromACS(t *testing.T) {
RoleArn: aws.String("roleArn"),
SecretAccessKey: aws.String("OhhSecret"),
SessionToken: aws.String("sessionToken"),
CredentialScope: aws.String("credentialScope"),
}

roleType := "roleType"
Expand All @@ -47,6 +48,7 @@ func TestIAMRoleCredentialsFromACS(t *testing.T) {
RoleArn: "roleArn",
SecretAccessKey: "OhhSecret",
SessionToken: "sessionToken",
CredentialScope: "credentialScope",
RoleType: "roleType",
}
assert.Equal(t, credentials, expectedCredentials, "Mismatch between expected and constructed credentials")
Expand Down Expand Up @@ -99,6 +101,7 @@ func TestSetAndGetTaskCredentialsHappyPath(t *testing.T) {
SessionToken: "stkn",
Expiration: "ts",
CredentialsID: "cid1",
CredentialScope: "cdsp",
},
}

Expand All @@ -118,6 +121,7 @@ func TestSetAndGetTaskCredentialsHappyPath(t *testing.T) {
SessionToken: "stkn2",
Expiration: "ts2",
CredentialsID: "cid1",
CredentialScope: "cdsp",
},
}
err = manager.SetTaskCredentials(&updatedCredentials)
Expand All @@ -138,6 +142,7 @@ func TestGenerateCredentialsEndpointRelativeURI(t *testing.T) {
SessionToken: "stkn",
Expiration: "ts",
CredentialsID: "cid1",
CredentialScope: "cdsp",
}
generatedURI := credentials.GenerateCredentialsEndpointRelativeURI()
expectedURI := fmt.Sprintf(credentialsEndpointRelativeURIFormat, CredentialsPath, "cid1")
Expand All @@ -157,6 +162,7 @@ func TestRemoveExistingCredentials(t *testing.T) {
SessionToken: "stkn",
Expiration: "ts",
CredentialsID: "cid1",
CredentialScope: "cdsp",
},
}
err := manager.SetTaskCredentials(&credentials)
Expand Down
2 changes: 2 additions & 0 deletions ecs-agent/tmds/handlers/credentials_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ func testCredentialsHandlerSuccess(t *testing.T, makePath MakePath, makeHandler
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
Expiration: "expiration",
CredentialScope: "credential_scope",
RoleType: credentials.ApplicationRoleType,
}

Expand All @@ -278,6 +279,7 @@ func testCredentialsHandlerSuccess(t *testing.T, makePath MakePath, makeHandler
SecretAccessKey: "secret_access_key",
SessionToken: "session_token",
Expiration: "expiration",
CredentialScope: "credential_scope",
}

auditLogger.EXPECT().Log(
Expand Down
18 changes: 9 additions & 9 deletions ecs-agent/tmds/handlers/v1/credentials_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import (
"net/http"

"github.com/aws/amazon-ecs-agent/ecs-agent/credentials"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger"
auditinterface "github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit"
"github.com/aws/amazon-ecs-agent/ecs-agent/logger/audit/request"
handlersutils "github.com/aws/amazon-ecs-agent/ecs-agent/tmds/handlers/utils"
"github.com/aws/amazon-ecs-agent/ecs-agent/utils"
"github.com/cihub/seelog"
)

const (
Expand Down Expand Up @@ -104,7 +104,7 @@ func processCredentialsRequest(
) ([]byte, string, string, *handlersutils.ErrorMessage, error) {
if credentialsID == "" {
errText := errPrefix + "No Credential ID in the request"
seelog.Errorf("Error processing credential request: %s", errText)
logger.Error(fmt.Sprintf("Error processing credential request: %s", errText))
msg := &handlersutils.ErrorMessage{
Code: ErrNoIDInRequest,
Message: errText,
Expand All @@ -116,7 +116,7 @@ func processCredentialsRequest(
credentials, ok := credentialsManager.GetTaskCredentials(credentialsID)
if !ok {
errText := errPrefix + "Credentials not found"
seelog.Errorf("Error processing credential request: %s", errText)
logger.Error(fmt.Sprintf("Error processing credential request: %s", errText))
msg := &handlersutils.ErrorMessage{
Code: ErrInvalidIDInRequest,
Message: errText,
Expand All @@ -125,14 +125,14 @@ func processCredentialsRequest(
return nil, "", "", msg, errors.New(errText)
}

seelog.Infof("Processing credential request, credentialType=%s taskARN=%s",
credentials.IAMRoleCredentials.RoleType, credentials.ARN)
logger.Debug("Processing credential request, credentialType=%s taskARN=%s credentialScope=%s",
credentials.IAMRoleCredentials.RoleType, credentials.ARN, credentials.IAMRoleCredentials.CredentialScope)

if utils.ZeroOrNil(credentials.ARN) && utils.ZeroOrNil(credentials.IAMRoleCredentials) {
// This can happen when the agent is restarted and is reconciling its state.
errText := errPrefix + "Credentials uninitialized for ID"
seelog.Errorf("Error processing credential request credentialType=%s taskARN=%s: %s",
credentials.IAMRoleCredentials.RoleType, credentials.ARN, errText)
logger.Error(fmt.Sprintf("Error processing credential request credentialType=%s taskARN=%s credentialScope=%s: %s",
credentials.IAMRoleCredentials.RoleType, credentials.ARN, credentials.IAMRoleCredentials.CredentialScope, errText))
msg := &handlersutils.ErrorMessage{
Code: ErrCredentialsUninitialized,
Message: errText,
Expand All @@ -144,8 +144,8 @@ func processCredentialsRequest(
credentialsJSON, err := json.Marshal(credentials.IAMRoleCredentials)
if err != nil {
errText := errPrefix + "Error marshaling credentials"
seelog.Errorf("Error processing credential request credentialType=%s taskARN=%s: %s",
credentials.IAMRoleCredentials.RoleType, credentials.ARN, errText)
logger.Error(fmt.Sprintf("Error processing credential request credentialType=%s taskARN=%s credentialScope=%s: %s",
credentials.IAMRoleCredentials.RoleType, credentials.ARN, credentials.IAMRoleCredentials.CredentialScope, errText))
msg := &handlersutils.ErrorMessage{
Code: ErrInternalServer,
Message: "Internal server error",
Expand Down

0 comments on commit 4a67b5a

Please sign in to comment.