| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273 |
- package registry
- import (
- "encoding/base64"
- "net/http"
- "strings"
- "time"
- "github.com/aws/aws-sdk-go/service/ecr"
- "github.com/porter-dev/porter/api/server/handlers"
- "github.com/porter-dev/porter/api/server/shared"
- "github.com/porter-dev/porter/api/server/shared/apierrors"
- "github.com/porter-dev/porter/api/server/shared/config"
- "github.com/porter-dev/porter/api/types"
- "github.com/porter-dev/porter/internal/models"
- "github.com/porter-dev/porter/internal/oauth"
- "github.com/porter-dev/porter/internal/registry"
- )
- type RegistryGetECRTokenHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewRegistryGetECRTokenHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *RegistryGetECRTokenHandler {
- return &RegistryGetECRTokenHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *RegistryGetECRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- request := &types.GetRegistryECRTokenRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- // list registries and find one that matches the region
- regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- var token string
- var expiresAt *time.Time
- for _, reg := range regs {
- if reg.AWSIntegrationID != 0 {
- awsInt, err := c.Repo().AWSIntegration().ReadAWSIntegration(reg.ProjectID, reg.AWSIntegrationID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- if awsInt.AWSRegion == request.Region {
- // get the aws integration and session
- sess, err := awsInt.GetSession()
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- ecrSvc := ecr.New(sess)
- output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- token = *output.AuthorizationData[0].AuthorizationToken
- expiresAt = output.AuthorizationData[0].ExpiresAt
- }
- }
- }
- resp := &types.GetRegistryTokenResponse{
- Token: token,
- ExpiresAt: expiresAt,
- }
- c.WriteResult(w, r, resp)
- }
- type RegistryGetGCRTokenHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewRegistryGetGCRTokenHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *RegistryGetGCRTokenHandler {
- return &RegistryGetGCRTokenHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *RegistryGetGCRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- request := &types.GetRegistryGCRTokenRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- // list registries and find one that matches the region
- regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- var token string
- var expiresAt *time.Time
- for _, reg := range regs {
- if reg.GCPIntegrationID != 0 && strings.Contains(reg.URL, request.ServerURL) {
- _reg := registry.Registry(*reg)
- tokenCache, err := _reg.GetGCRToken(c.Repo())
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- token = string(tokenCache.Token)
- expiresAt = &tokenCache.Expiry
- break
- }
- }
- resp := &types.GetRegistryTokenResponse{
- Token: token,
- ExpiresAt: expiresAt,
- }
- c.WriteResult(w, r, resp)
- }
- type RegistryGetDOCRTokenHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewRegistryGetDOCRTokenHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *RegistryGetDOCRTokenHandler {
- return &RegistryGetDOCRTokenHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *RegistryGetDOCRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- request := &types.GetRegistryDOCRTokenRequest{}
- if ok := c.DecodeAndValidate(w, r, request); !ok {
- return
- }
- // list registries and find one that matches the region
- regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- var token string
- var expiresAt *time.Time
- for _, reg := range regs {
- if reg.DOIntegrationID != 0 && strings.Contains(reg.URL, request.ServerURL) {
- oauthInt, err := c.Repo().OAuthIntegration().ReadOAuthIntegration(reg.ProjectID, reg.DOIntegrationID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- tok, expiry, err := oauth.GetAccessToken(
- oauthInt.SharedOAuthModel,
- c.Config().DOConf,
- oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, c.Repo()),
- )
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- token = tok
- expiresAt = expiry
- break
- }
- }
- resp := &types.GetRegistryTokenResponse{
- Token: token,
- ExpiresAt: expiresAt,
- }
- c.WriteResult(w, r, resp)
- }
- type RegistryGetDockerhubTokenHandler struct {
- handlers.PorterHandlerReadWriter
- }
- func NewRegistryGetDockerhubTokenHandler(
- config *config.Config,
- decoderValidator shared.RequestDecoderValidator,
- writer shared.ResultWriter,
- ) *RegistryGetDockerhubTokenHandler {
- return &RegistryGetDockerhubTokenHandler{
- PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
- }
- }
- func (c *RegistryGetDockerhubTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
- proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
- // list registries and find one that matches the region
- regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- var token string
- var expiresAt *time.Time
- for _, reg := range regs {
- if reg.BasicIntegrationID != 0 && strings.Contains(reg.URL, "index.docker.io") {
- basic, err := c.Repo().BasicIntegration().ReadBasicIntegration(reg.ProjectID, reg.BasicIntegrationID)
- if err != nil {
- c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
- return
- }
- token = base64.StdEncoding.EncodeToString([]byte(string(basic.Username) + ":" + string(basic.Password)))
- // we'll just set an arbitrary 30-day expiry time (this is not enforced)
- timeExpires := time.Now().Add(30 * 24 * 3600 * time.Second)
- expiresAt = &timeExpires
- }
- }
- resp := &types.GetRegistryTokenResponse{
- Token: token,
- ExpiresAt: expiresAt,
- }
- c.WriteResult(w, r, resp)
- }
|