get_token.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. package registry
  2. import (
  3. "encoding/base64"
  4. "net/http"
  5. "strings"
  6. "time"
  7. "github.com/aws/aws-sdk-go-v2/service/ecr"
  8. "github.com/porter-dev/porter/api/server/handlers"
  9. "github.com/porter-dev/porter/api/server/shared"
  10. "github.com/porter-dev/porter/api/server/shared/apierrors"
  11. "github.com/porter-dev/porter/api/server/shared/config"
  12. "github.com/porter-dev/porter/api/types"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/oauth"
  15. "github.com/porter-dev/porter/internal/registry"
  16. "github.com/aws/aws-sdk-go-v2/aws/arn"
  17. )
  18. type RegistryGetECRTokenHandler struct {
  19. handlers.PorterHandlerReadWriter
  20. }
  21. func NewRegistryGetECRTokenHandler(
  22. config *config.Config,
  23. decoderValidator shared.RequestDecoderValidator,
  24. writer shared.ResultWriter,
  25. ) *RegistryGetECRTokenHandler {
  26. return &RegistryGetECRTokenHandler{
  27. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  28. }
  29. }
  30. func (c *RegistryGetECRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  31. ctx := r.Context()
  32. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  33. request := &types.GetRegistryECRTokenRequest{}
  34. if ok := c.DecodeAndValidate(w, r, request); !ok {
  35. return
  36. }
  37. // list registries and find one that matches the region
  38. regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
  39. if err != nil {
  40. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  41. return
  42. }
  43. var token string
  44. var expiresAt *time.Time
  45. for _, reg := range regs {
  46. if reg.AWSIntegrationID != 0 {
  47. awsInt, err := c.Repo().AWSIntegration().ReadAWSIntegration(reg.ProjectID, reg.AWSIntegrationID)
  48. if err != nil {
  49. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  50. return
  51. }
  52. // if the aws integration doesn't have an ARN populated, populate it
  53. if awsInt.AWSArn == "" {
  54. err = awsInt.PopulateAWSArn(ctx)
  55. if err != nil {
  56. continue
  57. }
  58. }
  59. parsedARN, err := arn.Parse(awsInt.AWSArn)
  60. if err != nil {
  61. continue
  62. }
  63. // if the account id is passed as part of the request, verify the account id matches the account id in the ARN
  64. if awsInt.AWSRegion == request.Region && (request.AccountID == "" || request.AccountID == parsedARN.AccountID) {
  65. // get the aws integration and session
  66. ecrSvc := ecr.NewFromConfig(awsInt.Config())
  67. output, err := ecrSvc.GetAuthorizationToken(ctx, &ecr.GetAuthorizationTokenInput{})
  68. if err != nil {
  69. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  70. return
  71. }
  72. token = *output.AuthorizationData[0].AuthorizationToken
  73. expiresAt = output.AuthorizationData[0].ExpiresAt
  74. }
  75. }
  76. }
  77. resp := &types.GetRegistryTokenResponse{
  78. Token: token,
  79. ExpiresAt: expiresAt,
  80. }
  81. c.WriteResult(w, r, resp)
  82. }
  83. type RegistryGetGCRTokenHandler struct {
  84. handlers.PorterHandlerReadWriter
  85. }
  86. func NewRegistryGetGCRTokenHandler(
  87. config *config.Config,
  88. decoderValidator shared.RequestDecoderValidator,
  89. writer shared.ResultWriter,
  90. ) *RegistryGetGCRTokenHandler {
  91. return &RegistryGetGCRTokenHandler{
  92. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  93. }
  94. }
  95. func (c *RegistryGetGCRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  96. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  97. request := &types.GetRegistryGCRTokenRequest{}
  98. if ok := c.DecodeAndValidate(w, r, request); !ok {
  99. return
  100. }
  101. // list registries and find one that matches the region
  102. regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
  103. if err != nil {
  104. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  105. return
  106. }
  107. var token string
  108. var expiresAt *time.Time
  109. for _, reg := range regs {
  110. if reg.GCPIntegrationID != 0 && strings.Contains(reg.URL, request.ServerURL) {
  111. _reg := registry.Registry(*reg)
  112. oauthTok, err := _reg.GetGCRToken(c.Repo())
  113. // if the oauth token is not nil, but the error is not nil, we still return the token
  114. // but log an error
  115. if oauthTok != nil && err != nil {
  116. c.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(err))
  117. } else if err != nil {
  118. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  119. return
  120. }
  121. token = oauthTok.AccessToken
  122. expiresAt = &oauthTok.Expiry
  123. break
  124. }
  125. }
  126. resp := &types.GetRegistryTokenResponse{
  127. Token: token,
  128. ExpiresAt: expiresAt,
  129. }
  130. c.WriteResult(w, r, resp)
  131. }
  132. type RegistryGetGARTokenHandler struct {
  133. handlers.PorterHandlerReadWriter
  134. }
  135. func NewRegistryGetGARTokenHandler(
  136. config *config.Config,
  137. decoderValidator shared.RequestDecoderValidator,
  138. writer shared.ResultWriter,
  139. ) *RegistryGetGARTokenHandler {
  140. return &RegistryGetGARTokenHandler{
  141. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  142. }
  143. }
  144. func (c *RegistryGetGARTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  145. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  146. request := &types.GetRegistryGCRTokenRequest{}
  147. if ok := c.DecodeAndValidate(w, r, request); !ok {
  148. return
  149. }
  150. // list registries and find one that matches the region
  151. regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
  152. if err != nil {
  153. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  154. return
  155. }
  156. var token string
  157. var expiresAt *time.Time
  158. for _, reg := range regs {
  159. if reg.GCPIntegrationID != 0 && strings.Contains(reg.URL, request.ServerURL) {
  160. _reg := registry.Registry(*reg)
  161. oauthTok, err := _reg.GetGARToken(c.Repo())
  162. // if the oauth token is not nil, but the error is not nil, we still return the token
  163. // but log an error
  164. if oauthTok != nil && err != nil {
  165. c.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(err))
  166. } else if err != nil {
  167. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  168. return
  169. }
  170. token = oauthTok.AccessToken
  171. expiresAt = &oauthTok.Expiry
  172. break
  173. }
  174. }
  175. resp := &types.GetRegistryTokenResponse{
  176. Token: token,
  177. ExpiresAt: expiresAt,
  178. }
  179. c.WriteResult(w, r, resp)
  180. }
  181. type RegistryGetDOCRTokenHandler struct {
  182. handlers.PorterHandlerReadWriter
  183. }
  184. func NewRegistryGetDOCRTokenHandler(
  185. config *config.Config,
  186. decoderValidator shared.RequestDecoderValidator,
  187. writer shared.ResultWriter,
  188. ) *RegistryGetDOCRTokenHandler {
  189. return &RegistryGetDOCRTokenHandler{
  190. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  191. }
  192. }
  193. func (c *RegistryGetDOCRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  194. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  195. request := &types.GetRegistryDOCRTokenRequest{}
  196. if ok := c.DecodeAndValidate(w, r, request); !ok {
  197. return
  198. }
  199. // list registries and find one that matches the region
  200. regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
  201. if err != nil {
  202. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  203. return
  204. }
  205. var token string
  206. var expiresAt *time.Time
  207. for _, reg := range regs {
  208. if reg.DOIntegrationID != 0 && strings.Contains(reg.URL, request.ServerURL) {
  209. oauthInt, err := c.Repo().OAuthIntegration().ReadOAuthIntegration(reg.ProjectID, reg.DOIntegrationID)
  210. if err != nil {
  211. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  212. return
  213. }
  214. tok, expiry, err := oauth.GetAccessToken(
  215. oauthInt.SharedOAuthModel,
  216. c.Config().DOConf,
  217. oauth.MakeUpdateOAuthIntegrationTokenFunction(oauthInt, c.Repo()),
  218. )
  219. if err != nil {
  220. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  221. return
  222. }
  223. token = tok
  224. expiresAt = expiry
  225. break
  226. }
  227. }
  228. resp := &types.GetRegistryTokenResponse{
  229. Token: token,
  230. ExpiresAt: expiresAt,
  231. }
  232. c.WriteResult(w, r, resp)
  233. }
  234. type RegistryGetDockerhubTokenHandler struct {
  235. handlers.PorterHandlerReadWriter
  236. }
  237. func NewRegistryGetDockerhubTokenHandler(
  238. config *config.Config,
  239. decoderValidator shared.RequestDecoderValidator,
  240. writer shared.ResultWriter,
  241. ) *RegistryGetDockerhubTokenHandler {
  242. return &RegistryGetDockerhubTokenHandler{
  243. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  244. }
  245. }
  246. func (c *RegistryGetDockerhubTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  247. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  248. // list registries and find one that matches the region
  249. regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
  250. if err != nil {
  251. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  252. return
  253. }
  254. var token string
  255. var expiresAt *time.Time
  256. for _, reg := range regs {
  257. if reg.BasicIntegrationID != 0 && strings.Contains(reg.URL, "index.docker.io") {
  258. basic, err := c.Repo().BasicIntegration().ReadBasicIntegration(reg.ProjectID, reg.BasicIntegrationID)
  259. if err != nil {
  260. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  261. return
  262. }
  263. token = base64.StdEncoding.EncodeToString([]byte(string(basic.Username) + ":" + string(basic.Password)))
  264. // we'll just set an arbitrary 30-day expiry time (this is not enforced)
  265. timeExpires := time.Now().Add(30 * 24 * 3600 * time.Second)
  266. expiresAt = &timeExpires
  267. }
  268. }
  269. resp := &types.GetRegistryTokenResponse{
  270. Token: token,
  271. ExpiresAt: expiresAt,
  272. }
  273. c.WriteResult(w, r, resp)
  274. }
  275. type RegistryGetACRTokenHandler struct {
  276. handlers.PorterHandlerReadWriter
  277. }
  278. func NewRegistryGetACRTokenHandler(
  279. config *config.Config,
  280. decoderValidator shared.RequestDecoderValidator,
  281. writer shared.ResultWriter,
  282. ) *RegistryGetACRTokenHandler {
  283. return &RegistryGetACRTokenHandler{
  284. PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
  285. }
  286. }
  287. func (c *RegistryGetACRTokenHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
  288. proj, _ := r.Context().Value(types.ProjectScope).(*models.Project)
  289. // list registries and find one that matches the region
  290. regs, err := c.Repo().Registry().ListRegistriesByProjectID(proj.ID)
  291. if err != nil {
  292. c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
  293. return
  294. }
  295. var token string
  296. var expiresAt *time.Time
  297. for _, reg := range regs {
  298. if reg.AzureIntegrationID != 0 && strings.Contains(reg.URL, "azurecr.io") {
  299. _reg := registry.Registry(*reg)
  300. username, pw, err := _reg.GetACRCredentials(c.Repo())
  301. if err != nil {
  302. continue
  303. }
  304. token = base64.StdEncoding.EncodeToString([]byte(string(username) + ":" + string(pw)))
  305. // we'll just set an arbitrary 30-day expiry time (this is not enforced)
  306. timeExpires := time.Now().Add(30 * 24 * 3600 * time.Second)
  307. expiresAt = &timeExpires
  308. }
  309. }
  310. resp := &types.GetRegistryTokenResponse{
  311. Token: token,
  312. ExpiresAt: expiresAt,
  313. }
  314. c.WriteResult(w, r, resp)
  315. }