Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cmd/aws_devops_agent/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,14 @@ func printStatus(out io.Writer, status awsdevops.Status) {
}
for _, space := range status.AgentSpaces {
_, _ = fmt.Fprintf(out, "\nAgent space %s (%s)\n", space.Name, space.AgentSpaceID)
_, _ = fmt.Fprintf(out, " Operator app: %s\n", space.OperatorAppURL)
app := space.OperatorAppURL
switch {
case space.OperatorAppError != "":
app = "unknown (" + space.OperatorAppError + ")"
case !space.OperatorAppEnabled:
app = "off"
}
_, _ = fmt.Fprintf(out, " Operator app: %s\n", app)
if len(space.Associations) > 0 {
_, _ = fmt.Fprintln(out, " Connected to:")
}
Expand Down
13 changes: 7 additions & 6 deletions cmd/aws_devops_agent/status_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ func realisticStatus() awsdevops.Status {
{ServiceID: "s-ld01", ServiceType: "mcpserver", Name: "LaunchDarkly"},
},
AgentSpaces: []awsdevops.AgentSpaceStatus{{
AgentSpaceID: "as-prod01",
Name: "Production",
OperatorAppURL: awsdevops.OperatorAppURL("as-prod01"),
AgentSpaceID: "as-prod01",
Name: "Production",
OperatorAppURL: awsdevops.OperatorAppURL("as-prod01"),
OperatorAppEnabled: true,
Associations: []awsdevops.AssociationStatus{
{AssociationID: "a-001", ServiceID: "aws", Status: "valid", AccountID: "123456789012", AccountType: "monitor"},
{AssociationID: "a-002", ServiceID: "s-ld01", Status: "valid"},
Expand All @@ -36,9 +37,9 @@ func partialStatus() awsdevops.Status {
{Name: awsdevops.OperatorAppRoleName},
},
AgentSpaces: []awsdevops.AgentSpaceStatus{{
AgentSpaceID: "as-prod01",
Name: "Production",
OperatorAppURL: awsdevops.OperatorAppURL("as-prod01"),
AgentSpaceID: "as-prod01",
Name: "Production",
OperatorAppError: "AccessDeniedException",
}},
MissingSetup: []string{"IAM role " + awsdevops.OperatorAppRoleName + " does not exist"},
}
Expand Down
24 changes: 24 additions & 0 deletions cmd/aws_devops_agent/status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ func TestStatusFullSetupOmitsSetupHint(t *testing.T) {
assert.NotContains(t, renderStatus(realisticStatus()), "ldcli aws-devops-agent setup")
}

func TestStatusShowsAnUnknownOperatorApp(t *testing.T) {
status := realisticStatus()
space := &status.AgentSpaces[0]
space.OperatorAppEnabled = false
space.OperatorAppURL = ""
space.OperatorAppError = "AccessDeniedException"
section := spaceSection(t, renderStatus(status), "Production")

assert.Equal(t, " Operator app: unknown (AccessDeniedException)", lineWith(t, section, "Operator app"))
assert.Contains(t, strings.Join(section, "\n"), "LaunchDarkly MCP server: ready")
}

func TestStatusJSONUnchanged(t *testing.T) {
cases := map[string]awsdevops.Status{
"testdata/status.json": realisticStatus(),
Expand Down Expand Up @@ -285,3 +297,15 @@ func TestStatusServicesListLabelsBlankService(t *testing.T) {
}
}
}

// An app that is off has no working URL.
func TestStatusShowsAnOperatorAppThatIsOff(t *testing.T) {
status := realisticStatus()
status.AgentSpaces[0].OperatorAppEnabled = false
status.AgentSpaces[0].OperatorAppURL = ""

out := renderStatus(status)

assert.Contains(t, out, "Operator app: off")
assert.NotContains(t, out, "aidevops.global.app.aws")
}
4 changes: 3 additions & 1 deletion cmd/aws_devops_agent/testdata/status-partial.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
{
"agentSpaceId": "as-prod01",
"name": "Production",
"operatorAppUrl": "https://as-prod01.aidevops.global.app.aws"
"operatorAppUrl": "",
"operatorAppEnabled": false,
"operatorAppError": "AccessDeniedException"
}
],
"missingSetup": [
Expand Down
1 change: 1 addition & 0 deletions cmd/aws_devops_agent/testdata/status.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"agentSpaceId": "as-prod01",
"name": "Production",
"operatorAppUrl": "https://as-prod01.aidevops.global.app.aws",
"operatorAppEnabled": true,
"associations": [
{
"associationId": "a-001",
Expand Down
2 changes: 2 additions & 0 deletions internal/awsdevops/awsdevops.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ type AgentAPI interface {
DeregisterService(context.Context, *devopsagent.DeregisterServiceInput, ...func(*devopsagent.Options)) (*devopsagent.DeregisterServiceOutput, error)
DisassociateService(context.Context, *devopsagent.DisassociateServiceInput, ...func(*devopsagent.Options)) (*devopsagent.DisassociateServiceOutput, error)
EnableOperatorApp(context.Context, *devopsagent.EnableOperatorAppInput, ...func(*devopsagent.Options)) (*devopsagent.EnableOperatorAppOutput, error)
GetOperatorApp(context.Context, *devopsagent.GetOperatorAppInput, ...func(*devopsagent.Options)) (*devopsagent.GetOperatorAppOutput, error)
ListTagsForResource(context.Context, *devopsagent.ListTagsForResourceInput, ...func(*devopsagent.Options)) (*devopsagent.ListTagsForResourceOutput, error)
GetAgentSpace(context.Context, *devopsagent.GetAgentSpaceInput, ...func(*devopsagent.Options)) (*devopsagent.GetAgentSpaceOutput, error)
ListAgentSpaces(context.Context, *devopsagent.ListAgentSpacesInput, ...func(*devopsagent.Options)) (*devopsagent.ListAgentSpacesOutput, error)
ListAssociations(context.Context, *devopsagent.ListAssociationsInput, ...func(*devopsagent.Options)) (*devopsagent.ListAssociationsOutput, error)
Expand Down
49 changes: 45 additions & 4 deletions internal/awsdevops/awsdevops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ import (
const testAccountID = "123456789012"

type fakeAgent struct {
// tags holds tags by resource ARN.
tags map[string]map[string]string
tagsErr error
// tagsErrFor fails the tag lookup of one resource ARN.
tagsErrFor map[string]error
// operatorApps holds enabled apps by space.
operatorApps map[string]*devopsagent.GetOperatorAppOutput
// operatorAppErr fails every GetOperatorApp call.
operatorAppErr error
calls []string
services []agenttypes.RegisteredService
associationInputs []*devopsagent.AssociateServiceInput
Expand Down Expand Up @@ -101,6 +110,31 @@ func (f *fakeAgent) DisassociateService(_ context.Context, in *devopsagent.Disas
return &devopsagent.DisassociateServiceOutput{}, nil
}

func (f *fakeAgent) GetOperatorApp(_ context.Context, in *devopsagent.GetOperatorAppInput, _ ...func(*devopsagent.Options)) (*devopsagent.GetOperatorAppOutput, error) {
f.calls = append(f.calls, "GetOperatorApp")
if f.operatorAppErr != nil {
return nil, f.operatorAppErr
}
app, ok := f.operatorApps[aws.ToString(in.AgentSpaceId)]
if !ok {
return nil, &agenttypes.ResourceNotFoundException{}
}

return app, nil
}

func (f *fakeAgent) ListTagsForResource(_ context.Context, in *devopsagent.ListTagsForResourceInput, _ ...func(*devopsagent.Options)) (*devopsagent.ListTagsForResourceOutput, error) {
f.calls = append(f.calls, "ListTagsForResource")
if f.tagsErr != nil {
return nil, f.tagsErr
}
if err := f.tagsErrFor[aws.ToString(in.ResourceArn)]; err != nil {
return nil, err
}

return &devopsagent.ListTagsForResourceOutput{Tags: f.tags[aws.ToString(in.ResourceArn)]}, nil
}

func (f *fakeAgent) EnableOperatorApp(_ context.Context, _ *devopsagent.EnableOperatorAppInput, _ ...func(*devopsagent.Options)) (*devopsagent.EnableOperatorAppOutput, error) {
f.calls = append(f.calls, "EnableOperatorApp")

Expand All @@ -110,11 +144,18 @@ func (f *fakeAgent) EnableOperatorApp(_ context.Context, _ *devopsagent.EnableOp
}

func (f *fakeAgent) GetAgentSpace(_ context.Context, in *devopsagent.GetAgentSpaceInput, _ ...func(*devopsagent.Options)) (*devopsagent.GetAgentSpaceOutput, error) {
id := aws.ToString(in.AgentSpaceId)
name := "launchdarkly"
var description *string
for _, space := range f.agentSpaces {
if aws.ToString(space.AgentSpaceId) == id && space.Name != nil {
name = aws.ToString(space.Name)
description = space.Description
}
}

return &devopsagent.GetAgentSpaceOutput{
AgentSpace: &agenttypes.AgentSpace{
AgentSpaceId: in.AgentSpaceId,
Name: aws.String("launchdarkly"),
},
AgentSpace: &agenttypes.AgentSpace{AgentSpaceId: in.AgentSpaceId, Name: aws.String(name), Description: description},
}, nil
}

Expand Down
106 changes: 106 additions & 0 deletions internal/awsdevops/lookup.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,112 @@ func listAssociations(ctx context.Context, clients Clients, agentSpaceID string)
}
}

// AgentSpaceSummary identifies an agent space the account already has, for a
// caller that has to pick one.
type AgentSpaceSummary struct {
AgentSpaceID string `json:"agentSpaceId"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
// ManagedByLDCLI means that setup made the space.
ManagedByLDCLI bool `json:"managedByLdcli"`
// TagErr is set if setup could not read the tags of the space. The owner
// of the space is then unknown, and ManagedByLDCLI is false.
TagErr error `json:"-"`
}

// AgentSpaces lists the agent spaces in the account and region. A space that
// setup made has the ldcli:managed tag. A user can change the description, so
// only the tag counts. Each space costs one tag lookup.
//
// If a tag lookup fails, the summary of that space has TagErr, and the other
// spaces stay in the list. Setup uses the tag to decide if it can change the
// roles and the operator app of a space, so a caller must not use a space with
// TagErr as if its owner is known. If every lookup fails, for example without
// aidevops:ListTagsForResource, AgentSpaces returns the first error. The
// status command does not call AgentSpaces.
func AgentSpaces(ctx context.Context, clients Clients) ([]AgentSpaceSummary, error) {
spaces, err := listAgentSpaces(ctx, clients)
if err != nil || len(spaces) == 0 {
return nil, err
}
accountID, err := clients.AccountID(ctx)
if err != nil {
return nil, err
}

summaries := make([]AgentSpaceSummary, 0, len(spaces))
var firstErr error
failed := 0
for _, space := range spaces {
summary := AgentSpaceSummary{
AgentSpaceID: aws.ToString(space.AgentSpaceId),
Name: aws.ToString(space.Name),
Description: aws.ToString(space.Description),
}
summary.ManagedByLDCLI, summary.TagErr = hasManagedTag(ctx, clients, accountID, summary.AgentSpaceID)
if summary.TagErr != nil {
failed++
if firstErr == nil {
firstErr = summary.TagErr
}
}
summaries = append(summaries, summary)
}
if failed == len(summaries) {
return nil, firstErr
}

return summaries, nil
}

// AgentSpace reads one agent space, with the same ownership check as
// AgentSpaces, for a caller that already has the space ID. If setup cannot
// read the tags, the summary has TagErr.
func AgentSpace(ctx context.Context, clients Clients, spaceID string) (AgentSpaceSummary, error) {
out, err := clients.Agent.GetAgentSpace(ctx, &devopsagent.GetAgentSpaceInput{AgentSpaceId: aws.String(spaceID)})
if err != nil {
return AgentSpaceSummary{}, fmt.Errorf("unable to read agent space %s: %w", spaceID, err)
}
accountID, err := clients.AccountID(ctx)
if err != nil {
return AgentSpaceSummary{}, err
}
summary := AgentSpaceSummary{
AgentSpaceID: spaceID,
Name: aws.ToString(out.AgentSpace.Name),
Description: aws.ToString(out.AgentSpace.Description),
}
// A tag error goes on the summary, as in AgentSpaces. The caller decides
// what to do with a space whose owner is unknown.
summary.ManagedByLDCLI, summary.TagErr = hasManagedTag(ctx, clients, accountID, spaceID)

return summary, nil
}

// hasManagedTag reports the ldcli:managed tag on an agent space.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wise

func hasManagedTag(ctx context.Context, clients Clients, accountID, spaceID string) (bool, error) {
out, err := clients.Agent.ListTagsForResource(ctx, &devopsagent.ListTagsForResourceInput{
ResourceArn: aws.String(agentSpaceARN(clients.Region, accountID, spaceID)),
})
// The SDK retries a throttle or a server error before it returns. Only an
// access error points to the permission.
if err != nil && errorCode(err) == "AccessDeniedException" {
return false, fmt.Errorf(
"unable to read the tags of agent space %s. Setup needs aidevops:ListTagsForResource: %w", spaceID, err)
}
if err != nil {
return false, fmt.Errorf("unable to read the tags of agent space %s: %w", spaceID, err)
}

return out.Tags[ManagedTagKey] == ManagedTagValue, nil
}

// agentSpaceARN builds the ARN that the tag API needs. An agent space does not
// report its ARN.
func agentSpaceARN(region, accountID, spaceID string) string {
return fmt.Sprintf("arn:aws:aidevops:%s:%s:agentspace/%s", region, accountID, spaceID)
}

// FindMCPServer returns the ID of the MCP server registered at endpoint, if the
// account has one. AWS keeps MCP servers at the account level, so a second
// agent space reuses the existing registration.
Expand Down
Loading
Loading