|
@@ -3,6 +3,12 @@ package registry
|
|
|
import (
|
|
import (
|
|
|
"fmt"
|
|
"fmt"
|
|
|
"net/http"
|
|
"net/http"
|
|
|
|
|
+ strings "strings"
|
|
|
|
|
+
|
|
|
|
|
+ "connectrpc.com/connect"
|
|
|
|
|
+
|
|
|
|
|
+ porterv1 "github.com/porter-dev/api-contracts/generated/go/porter/v1"
|
|
|
|
|
+ ints "github.com/porter-dev/porter/internal/models/integrations"
|
|
|
|
|
|
|
|
"github.com/porter-dev/porter/api/server/handlers"
|
|
"github.com/porter-dev/porter/api/server/handlers"
|
|
|
"github.com/porter-dev/porter/api/server/shared"
|
|
"github.com/porter-dev/porter/api/server/shared"
|
|
@@ -30,6 +36,7 @@ func NewRegistryListImagesHandler(
|
|
|
|
|
|
|
|
func (c *RegistryListImagesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
func (c *RegistryListImagesHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
ctx := r.Context()
|
|
ctx := r.Context()
|
|
|
|
|
+ project, _ := ctx.Value(types.ProjectScope).(*models.Project)
|
|
|
reg, _ := ctx.Value(types.RegistryScope).(*models.Registry)
|
|
reg, _ := ctx.Value(types.RegistryScope).(*models.Registry)
|
|
|
|
|
|
|
|
repoName, reqErr := requestutils.GetURLParamString(r, types.URLParamWildcard)
|
|
repoName, reqErr := requestutils.GetURLParamString(r, types.URLParamWildcard)
|
|
@@ -53,22 +60,70 @@ func (c *RegistryListImagesHandler) ServeHTTP(w http.ResponseWriter, r *http.Req
|
|
|
_reg := registry.Registry(*reg)
|
|
_reg := registry.Registry(*reg)
|
|
|
regAPI := &_reg
|
|
regAPI := &_reg
|
|
|
|
|
|
|
|
- if regAPI.AWSIntegrationID != 0 {
|
|
|
|
|
- if request.Num == 0 {
|
|
|
|
|
- request.Num = 1000
|
|
|
|
|
- } else if request.Num < 1 || request.Num > 1000 {
|
|
|
|
|
- c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
|
|
|
|
|
- fmt.Errorf("num should be between 1 and 1000 for ECR images"), http.StatusBadRequest,
|
|
|
|
|
- ))
|
|
|
|
|
|
|
+ if request.Num == 0 {
|
|
|
|
|
+ request.Num = 1000
|
|
|
|
|
+ } else if request.Num < 1 || request.Num > 1000 {
|
|
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrPassThroughToClient(
|
|
|
|
|
+ fmt.Errorf("num should be between 1 and 1000 for ECR images"), http.StatusBadRequest,
|
|
|
|
|
+ ))
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ var nextToken *string
|
|
|
|
|
+ if request.Next != "" {
|
|
|
|
|
+ nextToken = &request.Next
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // TODO (POR-2170): remove this once fully migrated, only supported for recently-migrated legacy users with AWS registries
|
|
|
|
|
+ if project.GetFeatureFlag(models.CapiProvisionerEnabled, c.Config().LaunchDarklyClient) {
|
|
|
|
|
+ uri := strings.TrimPrefix(regAPI.URL, "https://")
|
|
|
|
|
+ splits := strings.Split(uri, ".")
|
|
|
|
|
+ if len(splits) < 4 {
|
|
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("invalid registry url: must be aws")))
|
|
|
return
|
|
return
|
|
|
}
|
|
}
|
|
|
|
|
+ accountID := splits[0]
|
|
|
|
|
+ region := splits[3]
|
|
|
|
|
+ // nolint:staticcheck // need this deprecated method
|
|
|
|
|
+ req := connect.NewRequest(&porterv1.AssumeRoleCredentialsRequest{
|
|
|
|
|
+ ProjectId: int64(regAPI.ProjectID),
|
|
|
|
|
+ AwsAccountId: accountID,
|
|
|
|
|
+ })
|
|
|
|
|
+ // nolint:staticcheck // need this deprecated method
|
|
|
|
|
+ creds, err := c.Config().ClusterControlPlaneClient.AssumeRoleCredentials(ctx, req)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+ aws := &ints.AWSIntegration{
|
|
|
|
|
+ AWSAccessKeyID: []byte(creds.Msg.AwsAccessId),
|
|
|
|
|
+ AWSSecretAccessKey: []byte(creds.Msg.AwsSecretKey),
|
|
|
|
|
+ AWSSessionToken: []byte(creds.Msg.AwsSessionToken),
|
|
|
|
|
+ AWSRegion: region,
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
- var nextToken *string
|
|
|
|
|
- if request.Next != "" {
|
|
|
|
|
- nextToken = &request.Next
|
|
|
|
|
|
|
+ imgs, nextToken, err := regAPI.GetECRPaginatedImages(repoName, request.Num, nextToken, aws)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
|
|
+ return
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if nextToken != nil {
|
|
|
|
|
+ res.Next = *nextToken
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ res.Images = append(res.Images, imgs...)
|
|
|
|
|
+ } else if regAPI.AWSIntegrationID != 0 {
|
|
|
|
|
+ aws, err := c.Repo().AWSIntegration().ReadAWSIntegration(
|
|
|
|
|
+ regAPI.ProjectID,
|
|
|
|
|
+ regAPI.AWSIntegrationID,
|
|
|
|
|
+ )
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
|
|
+ return
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- imgs, nextToken, err := regAPI.GetECRPaginatedImages(repoName, c.Repo(), request.Num, nextToken)
|
|
|
|
|
|
|
+ imgs, nextToken, err := regAPI.GetECRPaginatedImages(repoName, request.Num, nextToken, aws)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
|
|
|
return
|
|
return
|