Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ROX-12151: cloud accounts endpoint #517

Merged
merged 5 commits into from
Oct 28, 2022
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
55 changes: 55 additions & 0 deletions internal/dinosaur/pkg/api/public/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,37 @@ paths:
security:
- Bearer: []
summary: Returns the list of supported regions of the supported cloud provider
/api/rhacs/v1/cloud_accounts:
get:
operationId: getCloudAccounts

Choose a reason for hiding this comment

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

The endpoint looks good to me. The only thing I'm wondering about is this operationId. @stehessel mentioned in his comment how to access the organization.id using ocm command. Is operationId supposed to be that? Also, accessing that id using the ocm command is helpful, but I can't do that through the browser so I'll need to find another way to do that. If you or @stehessel might know how that's possible, let me know. I am searching through https://api.openshift.com/ to see if there might be some API endpoint that might give me that information

Copy link

@sachaudh sachaudh Oct 25, 2022

Choose a reason for hiding this comment

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

Is /api/accounts_mgmt/v1/organizations the API i'm looking for? Looks like that gives me a list of organizations (there seems to be just one though). I see fields like id and external_id which seem relevant

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@sachaudh organization.id will be extracted by fleet-manager from AMS using orgId provided in the Bearer token. The endpoint doesn't take any parameters.

operationId is swagger parameter that is used to uniquely identify the operation within the API spec:
https://swagger.io/docs/specification/paths-and-operations/

Choose a reason for hiding this comment

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

Ok, then I think that's fine with me. Thanks for adding the endpoint @ivan-degtiarenko 👍🏼

responses:
"200":
content:
application/json:
schema:
$ref: '#/components/schemas/CloudAccountsList'
description: Returned list of supported cloud provider regions
"401":
content:
application/json:
examples:
"401Example":
$ref: '#/components/examples/401Example'
schema:
$ref: '#/components/schemas/Error'
description: Auth token is invalid
"500":
content:
application/json:
examples:
"500Example":
$ref: '#/components/examples/500Example'
schema:
$ref: '#/components/schemas/Error'
description: Unexpected error occurred
security:
- Bearer: []
summary: Returns the list of cloud accounts which belong to user's organization
/api/rhacs/v1/centrals/{id}/metrics/query_range:
get:
operationId: getMetricsByRangeQuery
Expand Down Expand Up @@ -905,6 +936,11 @@ components:
topic: __consumer_offsets
timestamp: 1611670230000
value: 84154
CloudAccountsList:
value:
cloudAccounts:
- cloudAccountId: cloudAccountId
cloudProviderId: cloudProviderId
"400DeletionExample":
value:
id: "103"
Expand Down Expand Up @@ -1457,6 +1493,25 @@ components:
required:
- value
type: object
CloudAccountsList:
example:
cloudAccounts:
- ""
- ""
properties:
cloudAccounts:
items:
allOf:
- $ref: '#/components/schemas/CloudAccount'
type: array
type: object
CloudAccount:
properties:
cloudAccountId:
type: string
cloudProviderId:
type: string
type: object
Error_allOf:
properties:
code:
Expand Down
93 changes: 93 additions & 0 deletions internal/dinosaur/pkg/api/public/api_default.go

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

17 changes: 17 additions & 0 deletions internal/dinosaur/pkg/api/public/model_cloud_account.go

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

16 changes: 16 additions & 0 deletions internal/dinosaur/pkg/api/public/model_cloud_accounts_list.go

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

4 changes: 2 additions & 2 deletions internal/dinosaur/pkg/generated/bindata.go

Large diffs are not rendered by default.

52 changes: 52 additions & 0 deletions internal/dinosaur/pkg/handlers/cloud_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package handlers

import (
"net/http"

"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/presenters"
"github.com/stackrox/acs-fleet-manager/pkg/auth"
"github.com/stackrox/acs-fleet-manager/pkg/client/ocm"
"github.com/stackrox/acs-fleet-manager/pkg/errors"
"github.com/stackrox/acs-fleet-manager/pkg/handlers"
)

const rhacsMarketplaceQuotaID = "cluster|rhinfra|rhacs|marketplace"

type cloudAccountsHandler struct {
client ocm.AMSClient
}

// NewCloudAccountsHandler ...
func NewCloudAccountsHandler(client ocm.AMSClient) *cloudAccountsHandler {
return &cloudAccountsHandler{
client: client,
}
}

// Get ...
func (h *cloudAccountsHandler) Get(w http.ResponseWriter, r *http.Request) {
cfg := &handlers.HandlerConfig{
Action: h.actionFunc(r),
}
handlers.HandleGet(w, r, cfg)
}

func (h *cloudAccountsHandler) actionFunc(r *http.Request) func() (i interface{}, serviceError *errors.ServiceError) {
return func() (i interface{}, serviceError *errors.ServiceError) {
ctx := r.Context()
claims, err := auth.GetClaimsFromContext(ctx)
if err != nil {
return nil, errors.NewWithCause(errors.ErrorUnauthenticated, err, "user not authenticated")
}
orgID, err := claims.GetOrgID()
if err != nil {
return nil, errors.NewWithCause(errors.ErrorForbidden, err, "cannot make request without orgID claim")
}

cloudAccounts, err := h.client.GetCustomerCloudAccounts(orgID, []string{rhacsMarketplaceQuotaID})
if err != nil {
return nil, errors.NewWithCause(errors.ErrorGeneral, err, "failed to fetch cloud accounts from AMS")
}
return presenters.PresentCloudAccounts(cloudAccounts), nil
}
}
87 changes: 87 additions & 0 deletions internal/dinosaur/pkg/handlers/cloud_accounts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package handlers

import (
"context"
"net/http"
"testing"

"github.com/stackrox/acs-fleet-manager/pkg/errors"

"github.com/google/uuid"
v1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/api/public"
"github.com/stackrox/acs-fleet-manager/pkg/auth"
"github.com/stackrox/acs-fleet-manager/pkg/client/ocm"
"github.com/stretchr/testify/assert"
)

const (
JwtKeyFile = "test/support/jwt_private_key.pem"
JwtCAFile = "test/support/jwt_ca.pem"
)

func TestGetSuccess(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be good to test GetCustomerCloudAccounts as well, in particular testing that the filtering by resource id works. I think this is possible by mocking the other OCM client methods like QuotaCost (or GetQuotaCostsForProduct).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

GetQuotaCostsForProduct can't be reused for the reasons mentioned above. I'm not sure how one can mock QuotaCost method on the organizationClient - it is created by Connection:
https://github.com/stackrox/acs-fleet-manager/blob/ivan%2FROX-12151-aws-accounts-endpoint/pkg/client/ocm/client.go#L608-L608

Copy link
Contributor

Choose a reason for hiding this comment

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

Hm yes that is impractical, I thought I saw it in the method list of the client, but you are right it's not. That would leave us with mocks on http level, which maybe are supported via the "mock modes" (https://github.com/stackrox/acs-fleet-manager/blob/ivan/ROX-12151-aws-accounts-endpoint/pkg/client/ocm/config.go#L12). But probably a lot of effort for a simple feature...

testCloudAccount, err := v1.NewCloudAccount().
CloudAccountID("cloudAccountID").
CloudProviderID("cloudProviderID").
Build()
assert.NoError(t, err)
c := ocm.ClientMock{
GetCustomerCloudAccountsFunc: func(externalID string, quotaID []string) ([]*v1.CloudAccount, error) {
return []*v1.CloudAccount{
testCloudAccount,
}, nil
},
}
handler := NewCloudAccountsHandler(&c)

authHelper, err := auth.NewAuthHelper(JwtKeyFile, JwtCAFile, "")
assert.NoError(t, err)
account, err := authHelper.NewAccount("username", "test-user", "", "org-id-0")
assert.NoError(t, err)
jwt, err := authHelper.CreateJWTWithClaims(account, nil)
assert.NoError(t, err)
authenticatedCtx := auth.SetTokenInContext(context.TODO(), jwt)
r := &http.Request{}
r = r.WithContext(authenticatedCtx)

res, err := handler.actionFunc(r)()
assert.Nil(t, err)
cloudAccountsList, ok := res.(public.CloudAccountsList)
assert.True(t, ok)

assert.Len(t, cloudAccountsList.CloudAccounts, 1)
assert.Equal(t, cloudAccountsList.CloudAccounts[0].CloudAccountId, testCloudAccount.CloudAccountID())
assert.Equal(t, cloudAccountsList.CloudAccounts[0].CloudProviderId, testCloudAccount.CloudProviderID())
}

func TestGetNoOrgId(t *testing.T) {
timesClientCalled := 0
c := ocm.ClientMock{
GetCustomerCloudAccountsFunc: func(externalID string, quotaID []string) ([]*v1.CloudAccount, error) {
timesClientCalled++
return []*v1.CloudAccount{}, nil
},
}
handler := NewCloudAccountsHandler(&c)

authHelper, err := auth.NewAuthHelper(JwtKeyFile, JwtCAFile, "")
assert.NoError(t, err)
builder := v1.NewAccount().
ID(uuid.New().String()).
Username("username").
FirstName("Max").
LastName("M").
Email("[email protected]")
account, err := builder.Build()
assert.NoError(t, err)
jwt, err := authHelper.CreateJWTWithClaims(account, nil)
assert.NoError(t, err)
authenticatedCtx := auth.SetTokenInContext(context.TODO(), jwt)
r := &http.Request{}
r = r.WithContext(authenticatedCtx)

_, serviceErr := handler.actionFunc(r)()
assert.Equal(t, serviceErr.Code, errors.ErrorForbidden)
assert.Equal(t, 0, timesClientCalled)
}
20 changes: 20 additions & 0 deletions internal/dinosaur/pkg/presenters/cloud_accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package presenters

import (
amsv1 "github.com/openshift-online/ocm-sdk-go/accountsmgmt/v1"
"github.com/stackrox/acs-fleet-manager/internal/dinosaur/pkg/api/public"
)

// PresentCloudAccounts ...
func PresentCloudAccounts(cloudAccounts []*amsv1.CloudAccount) public.CloudAccountsList {
transformedCloudAccounts := make([]public.CloudAccount, 0, len(cloudAccounts))
for _, cloudAccount := range cloudAccounts {
transformedCloudAccounts = append(transformedCloudAccounts, public.CloudAccount{
CloudAccountId: cloudAccount.CloudAccountID(),
CloudProviderId: cloudAccount.CloudProviderID(),
})
}
return public.CloudAccountsList{
CloudAccounts: transformedCloudAccounts,
}
}
6 changes: 6 additions & 0 deletions internal/dinosaur/pkg/routes/route_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func (s *options) buildAPIBaseRouter(mainRouter *mux.Router, basePath string, op
errorsHandler := coreHandlers.NewErrorsHandler()
metricsHandler := handlers.NewMetricsHandler(s.Observatorium)
serviceStatusHandler := handlers.NewServiceStatusHandler(s.Dinosaur, s.AccessControlListConfig)
cloudAccountsHandler := handlers.NewCloudAccountsHandler(s.AMSClient)

authorizeMiddleware := s.AccessControlListMiddleware.Authorize
requireOrgID := auth.NewRequireOrgIDMiddleware().RequireOrgID(errors.ErrorUnauthenticated)
Expand Down Expand Up @@ -175,6 +176,11 @@ func (s *options) buildAPIBaseRouter(mainRouter *mux.Router, basePath string, op
Name(logger.NewLogEvent("list-regions", "list cloud provider regions").ToString()).
Methods(http.MethodGet)

apiV1CloudAccountsRouter := apiV1Router.PathPrefix("/cloud_accounts").Subrouter()
apiV1CloudAccountsRouter.HandleFunc("", cloudAccountsHandler.Get).
Name(logger.NewLogEvent("get-cloud-accounts", "list all cloud accounts belonging to user org").ToString()).
Methods(http.MethodGet)

v1Metadata := api.VersionMetadata{
ID: "v1",
Collections: v1Collections,
Expand Down
Loading