registry.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  1. package registry
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. "net/url"
  9. "strings"
  10. "time"
  11. "github.com/aws/aws-sdk-go/aws/awserr"
  12. "github.com/aws/aws-sdk-go/service/ecr"
  13. "github.com/porter-dev/porter/internal/models"
  14. "github.com/porter-dev/porter/internal/oauth"
  15. "github.com/porter-dev/porter/internal/repository"
  16. "golang.org/x/oauth2"
  17. ints "github.com/porter-dev/porter/internal/models/integrations"
  18. "github.com/digitalocean/godo"
  19. "github.com/docker/cli/cli/config/configfile"
  20. "github.com/docker/cli/cli/config/types"
  21. )
  22. // Registry wraps the gorm Registry model
  23. type Registry models.Registry
  24. // Repository is a collection of images
  25. type Repository struct {
  26. // Name of the repository
  27. Name string `json:"name"`
  28. // When the repository was created
  29. CreatedAt time.Time `json:"created_at,omitempty"`
  30. // The URI of the repository
  31. URI string `json:"uri"`
  32. }
  33. // Image is a Docker image type
  34. type Image struct {
  35. // The sha256 digest of the image manifest.
  36. Digest string `json:"digest"`
  37. // The tag used for the image.
  38. Tag string `json:"tag"`
  39. // The image manifest associated with the image.
  40. Manifest string `json:"manifest"`
  41. // The name of the repository associated with the image.
  42. RepositoryName string `json:"repository_name"`
  43. // When the image was pushed
  44. PushedAt *time.Time `json:"pushed_at"`
  45. }
  46. // ListRepositories lists the repositories for a registry
  47. func (r *Registry) ListRepositories(
  48. repo repository.Repository,
  49. doAuth *oauth2.Config, // only required if using DOCR
  50. ) ([]*Repository, error) {
  51. // switch on the auth mechanism to get a token
  52. if r.AWSIntegrationID != 0 {
  53. return r.listECRRepositories(repo)
  54. }
  55. if r.GCPIntegrationID != 0 {
  56. return r.listGCRRepositories(repo)
  57. }
  58. if r.DOIntegrationID != 0 {
  59. return r.listDOCRRepositories(repo, doAuth)
  60. }
  61. if r.BasicIntegrationID != 0 {
  62. return r.listPrivateRegistryRepositories(repo)
  63. }
  64. return nil, fmt.Errorf("error listing repositories")
  65. }
  66. type gcrJWT struct {
  67. AccessToken string `json:"token"`
  68. ExpiresInSec int `json:"expires_in"`
  69. }
  70. type gcrRepositoryResp struct {
  71. Repositories []string `json:"repositories"`
  72. }
  73. func (r *Registry) GetGCRToken(repo repository.Repository) (*ints.TokenCache, error) {
  74. gcp, err := repo.GCPIntegration.ReadGCPIntegration(
  75. r.GCPIntegrationID,
  76. )
  77. if err != nil {
  78. return nil, err
  79. }
  80. // get oauth2 access token
  81. _, err = gcp.GetBearerToken(
  82. r.getTokenCache,
  83. r.setTokenCacheFunc(repo),
  84. "https://www.googleapis.com/auth/devstorage.read_write",
  85. )
  86. if err != nil {
  87. return nil, err
  88. }
  89. // it's now written to the token cache, so return
  90. cache, err := r.getTokenCache()
  91. if err != nil {
  92. return nil, err
  93. }
  94. return cache, nil
  95. }
  96. func (r *Registry) listGCRRepositories(
  97. repo repository.Repository,
  98. ) ([]*Repository, error) {
  99. gcp, err := repo.GCPIntegration.ReadGCPIntegration(
  100. r.GCPIntegrationID,
  101. )
  102. if err != nil {
  103. return nil, err
  104. }
  105. // Just use service account key to authenticate, since scopes may not be in place
  106. // for oauth. This also prevents us from making more requests.
  107. client := &http.Client{}
  108. req, err := http.NewRequest(
  109. "GET",
  110. "https://gcr.io/v2/_catalog",
  111. nil,
  112. )
  113. if err != nil {
  114. return nil, err
  115. }
  116. req.SetBasicAuth("_json_key", string(gcp.GCPKeyData))
  117. resp, err := client.Do(req)
  118. if err != nil {
  119. return nil, err
  120. }
  121. gcrResp := gcrRepositoryResp{}
  122. if err := json.NewDecoder(resp.Body).Decode(&gcrResp); err != nil {
  123. return nil, fmt.Errorf("Could not read GCR repositories: %v", err)
  124. }
  125. res := make([]*Repository, 0)
  126. parsedURL, err := url.Parse("https://" + r.URL)
  127. if err != nil {
  128. return nil, err
  129. }
  130. for _, repo := range gcrResp.Repositories {
  131. res = append(res, &Repository{
  132. Name: repo,
  133. URI: parsedURL.Host + "/" + repo,
  134. })
  135. }
  136. return res, nil
  137. }
  138. func (r *Registry) listECRRepositories(repo repository.Repository) ([]*Repository, error) {
  139. aws, err := repo.AWSIntegration.ReadAWSIntegration(
  140. r.AWSIntegrationID,
  141. )
  142. if err != nil {
  143. return nil, err
  144. }
  145. sess, err := aws.GetSession()
  146. if err != nil {
  147. return nil, err
  148. }
  149. svc := ecr.New(sess)
  150. resp, err := svc.DescribeRepositories(&ecr.DescribeRepositoriesInput{})
  151. if err != nil {
  152. return nil, err
  153. }
  154. res := make([]*Repository, 0)
  155. for _, repo := range resp.Repositories {
  156. res = append(res, &Repository{
  157. Name: *repo.RepositoryName,
  158. CreatedAt: *repo.CreatedAt,
  159. URI: *repo.RepositoryUri,
  160. })
  161. }
  162. return res, nil
  163. }
  164. func (r *Registry) listDOCRRepositories(
  165. repo repository.Repository,
  166. doAuth *oauth2.Config,
  167. ) ([]*Repository, error) {
  168. oauthInt, err := repo.OAuthIntegration.ReadOAuthIntegration(
  169. r.DOIntegrationID,
  170. )
  171. if err != nil {
  172. return nil, err
  173. }
  174. tok, _, err := oauth.GetAccessToken(oauthInt, doAuth, repo)
  175. if err != nil {
  176. return nil, err
  177. }
  178. client := godo.NewFromToken(tok)
  179. urlArr := strings.Split(r.URL, "/")
  180. if len(urlArr) != 2 {
  181. return nil, fmt.Errorf("invalid digital ocean registry url")
  182. }
  183. name := urlArr[1]
  184. repos, _, err := client.Registry.ListRepositories(context.TODO(), name, &godo.ListOptions{})
  185. if err != nil {
  186. return nil, err
  187. }
  188. res := make([]*Repository, 0)
  189. for _, repo := range repos {
  190. res = append(res, &Repository{
  191. Name: repo.Name,
  192. URI: r.URL + "/" + repo.Name,
  193. })
  194. }
  195. return res, nil
  196. }
  197. func (r *Registry) listPrivateRegistryRepositories(
  198. repo repository.Repository,
  199. ) ([]*Repository, error) {
  200. // handle dockerhub different, as it doesn't implement the docker registry http api
  201. if strings.Contains(r.URL, "docker.io") {
  202. // in this case, we just return the single dockerhub repository that's linked
  203. res := make([]*Repository, 0)
  204. res = append(res, &Repository{
  205. Name: strings.Split(r.URL, "docker.io/")[1],
  206. URI: r.URL,
  207. })
  208. return res, nil
  209. }
  210. basic, err := repo.BasicIntegration.ReadBasicIntegration(
  211. r.BasicIntegrationID,
  212. )
  213. if err != nil {
  214. return nil, err
  215. }
  216. // Just use service account key to authenticate, since scopes may not be in place
  217. // for oauth. This also prevents us from making more requests.
  218. client := &http.Client{}
  219. // get the host and scheme to make the request
  220. parsedURL, err := url.Parse(r.URL)
  221. req, err := http.NewRequest(
  222. "GET",
  223. fmt.Sprintf("%s://%s/v2/_catalog", parsedURL.Scheme, parsedURL.Host),
  224. nil,
  225. )
  226. if err != nil {
  227. return nil, err
  228. }
  229. req.SetBasicAuth(string(basic.Username), string(basic.Password))
  230. resp, err := client.Do(req)
  231. if err != nil {
  232. return nil, err
  233. }
  234. // if the status code is 404, fallback to the Docker Hub implementation
  235. if resp.StatusCode == 404 {
  236. req, err := http.NewRequest(
  237. "GET",
  238. fmt.Sprintf("%s/", r.URL),
  239. nil,
  240. )
  241. if err != nil {
  242. return nil, err
  243. }
  244. req.SetBasicAuth(string(basic.Username), string(basic.Password))
  245. resp, err = client.Do(req)
  246. if err != nil {
  247. return nil, err
  248. }
  249. }
  250. gcrResp := gcrRepositoryResp{}
  251. if err := json.NewDecoder(resp.Body).Decode(&gcrResp); err != nil {
  252. return nil, fmt.Errorf("Could not read private registry repositories: %v", err)
  253. }
  254. res := make([]*Repository, 0)
  255. if err != nil {
  256. return nil, err
  257. }
  258. for _, repo := range gcrResp.Repositories {
  259. res = append(res, &Repository{
  260. Name: repo,
  261. URI: parsedURL.Host + "/" + repo,
  262. })
  263. }
  264. return res, nil
  265. }
  266. func (r *Registry) getTokenCache() (tok *ints.TokenCache, err error) {
  267. return &ints.TokenCache{
  268. Token: r.TokenCache.Token,
  269. Expiry: r.TokenCache.Expiry,
  270. }, nil
  271. }
  272. func (r *Registry) setTokenCacheFunc(
  273. repo repository.Repository,
  274. ) ints.SetTokenCacheFunc {
  275. return func(token string, expiry time.Time) error {
  276. _, err := repo.Registry.UpdateRegistryTokenCache(
  277. &ints.RegTokenCache{
  278. TokenCache: ints.TokenCache{
  279. Token: []byte(token),
  280. Expiry: expiry,
  281. },
  282. RegistryID: r.ID,
  283. },
  284. )
  285. return err
  286. }
  287. }
  288. // CreateRepository creates a repository for a registry, if needed
  289. // (currently only required for ECR)
  290. func (r *Registry) CreateRepository(
  291. repo repository.Repository,
  292. name string,
  293. ) error {
  294. // if aws, create repository
  295. if r.AWSIntegrationID != 0 {
  296. return r.createECRRepository(repo, name)
  297. }
  298. // otherwise, no-op
  299. return nil
  300. }
  301. func (r *Registry) createECRRepository(
  302. repo repository.Repository,
  303. name string,
  304. ) error {
  305. aws, err := repo.AWSIntegration.ReadAWSIntegration(
  306. r.AWSIntegrationID,
  307. )
  308. if err != nil {
  309. return err
  310. }
  311. sess, err := aws.GetSession()
  312. if err != nil {
  313. return err
  314. }
  315. svc := ecr.New(sess)
  316. // determine if repository already exists
  317. _, err = svc.DescribeRepositories(&ecr.DescribeRepositoriesInput{
  318. RepositoryNames: []*string{&name},
  319. })
  320. // if the repository was not found, create it
  321. if aerr, ok := err.(awserr.Error); ok && aerr.Code() == ecr.ErrCodeRepositoryNotFoundException {
  322. _, err = svc.CreateRepository(&ecr.CreateRepositoryInput{
  323. RepositoryName: &name,
  324. })
  325. return err
  326. } else if err != nil {
  327. return err
  328. }
  329. return nil
  330. }
  331. // ListImages lists the images for an image repository
  332. func (r *Registry) ListImages(
  333. repoName string,
  334. repo repository.Repository,
  335. doAuth *oauth2.Config, // only required if using DOCR
  336. ) ([]*Image, error) {
  337. // switch on the auth mechanism to get a token
  338. if r.AWSIntegrationID != 0 {
  339. return r.listECRImages(repoName, repo)
  340. }
  341. if r.GCPIntegrationID != 0 {
  342. return r.listGCRImages(repoName, repo)
  343. }
  344. if r.DOIntegrationID != 0 {
  345. return r.listDOCRImages(repoName, repo, doAuth)
  346. }
  347. if r.BasicIntegrationID != 0 {
  348. return r.listPrivateRegistryImages(repoName, repo)
  349. }
  350. return nil, fmt.Errorf("error listing images")
  351. }
  352. func (r *Registry) listECRImages(repoName string, repo repository.Repository) ([]*Image, error) {
  353. aws, err := repo.AWSIntegration.ReadAWSIntegration(
  354. r.AWSIntegrationID,
  355. )
  356. if err != nil {
  357. return nil, err
  358. }
  359. sess, err := aws.GetSession()
  360. if err != nil {
  361. return nil, err
  362. }
  363. svc := ecr.New(sess)
  364. resp, err := svc.ListImages(&ecr.ListImagesInput{
  365. RepositoryName: &repoName,
  366. })
  367. if err != nil {
  368. return nil, err
  369. }
  370. describeResp, err := svc.DescribeImages(&ecr.DescribeImagesInput{
  371. RepositoryName: &repoName,
  372. ImageIds: resp.ImageIds,
  373. })
  374. if err != nil {
  375. return nil, err
  376. }
  377. res := make([]*Image, 0)
  378. for _, img := range describeResp.ImageDetails {
  379. for _, tag := range img.ImageTags {
  380. res = append(res, &Image{
  381. Digest: *img.ImageDigest,
  382. Tag: *tag,
  383. RepositoryName: repoName,
  384. PushedAt: img.ImagePushedAt,
  385. })
  386. }
  387. }
  388. return res, nil
  389. }
  390. type gcrImageResp struct {
  391. Tags []string `json:"tags"`
  392. }
  393. func (r *Registry) listGCRImages(repoName string, repo repository.Repository) ([]*Image, error) {
  394. gcp, err := repo.GCPIntegration.ReadGCPIntegration(
  395. r.GCPIntegrationID,
  396. )
  397. if err != nil {
  398. return nil, err
  399. }
  400. // use JWT token to request catalog
  401. client := &http.Client{}
  402. parsedURL, err := url.Parse("https://" + r.URL)
  403. if err != nil {
  404. return nil, err
  405. }
  406. trimmedPath := strings.Trim(parsedURL.Path, "/")
  407. req, err := http.NewRequest(
  408. "GET",
  409. fmt.Sprintf("https://%s/v2/%s/%s/tags/list", parsedURL.Host, trimmedPath, repoName),
  410. nil,
  411. )
  412. if err != nil {
  413. return nil, err
  414. }
  415. req.SetBasicAuth("_json_key", string(gcp.GCPKeyData))
  416. resp, err := client.Do(req)
  417. if err != nil {
  418. return nil, err
  419. }
  420. gcrResp := gcrImageResp{}
  421. if err := json.NewDecoder(resp.Body).Decode(&gcrResp); err != nil {
  422. return nil, fmt.Errorf("Could not read GCR repositories: %v", err)
  423. }
  424. res := make([]*Image, 0)
  425. for _, tag := range gcrResp.Tags {
  426. res = append(res, &Image{
  427. RepositoryName: repoName,
  428. Tag: tag,
  429. })
  430. }
  431. return res, nil
  432. }
  433. func (r *Registry) listDOCRImages(
  434. repoName string,
  435. repo repository.Repository,
  436. doAuth *oauth2.Config,
  437. ) ([]*Image, error) {
  438. oauthInt, err := repo.OAuthIntegration.ReadOAuthIntegration(
  439. r.DOIntegrationID,
  440. )
  441. if err != nil {
  442. return nil, err
  443. }
  444. tok, _, err := oauth.GetAccessToken(oauthInt, doAuth, repo)
  445. if err != nil {
  446. return nil, err
  447. }
  448. client := godo.NewFromToken(tok)
  449. urlArr := strings.Split(r.URL, "/")
  450. if len(urlArr) != 2 {
  451. return nil, fmt.Errorf("invalid digital ocean registry url")
  452. }
  453. name := urlArr[1]
  454. tags, _, err := client.Registry.ListRepositoryTags(context.TODO(), name, repoName, &godo.ListOptions{})
  455. if err != nil {
  456. return nil, err
  457. }
  458. res := make([]*Image, 0)
  459. for _, tag := range tags {
  460. res = append(res, &Image{
  461. RepositoryName: repoName,
  462. Tag: tag.Tag,
  463. })
  464. }
  465. return res, nil
  466. }
  467. func (r *Registry) listPrivateRegistryImages(repoName string, repo repository.Repository) ([]*Image, error) {
  468. // handle dockerhub different, as it doesn't implement the docker registry http api
  469. if strings.Contains(r.URL, "docker.io") {
  470. return r.listDockerHubImages(repoName, repo)
  471. }
  472. basic, err := repo.BasicIntegration.ReadBasicIntegration(
  473. r.BasicIntegrationID,
  474. )
  475. if err != nil {
  476. return nil, err
  477. }
  478. // Just use service account key to authenticate, since scopes may not be in place
  479. // for oauth. This also prevents us from making more requests.
  480. client := &http.Client{}
  481. // get the host and scheme to make the request
  482. parsedURL, err := url.Parse(r.URL)
  483. req, err := http.NewRequest(
  484. "GET",
  485. fmt.Sprintf("%s://%s/v2/%s/tags/list", parsedURL.Scheme, parsedURL.Host, repoName),
  486. nil,
  487. )
  488. if err != nil {
  489. return nil, err
  490. }
  491. req.SetBasicAuth(string(basic.Username), string(basic.Password))
  492. resp, err := client.Do(req)
  493. if err != nil {
  494. return nil, err
  495. }
  496. gcrResp := gcrImageResp{}
  497. if err := json.NewDecoder(resp.Body).Decode(&gcrResp); err != nil {
  498. return nil, fmt.Errorf("Could not read private registry repositories: %v", err)
  499. }
  500. res := make([]*Image, 0)
  501. for _, tag := range gcrResp.Tags {
  502. res = append(res, &Image{
  503. RepositoryName: repoName,
  504. Tag: tag,
  505. })
  506. }
  507. return res, nil
  508. }
  509. type dockerHubImageResult struct {
  510. Name string `json:"name"`
  511. }
  512. type dockerHubImageResp struct {
  513. Results []dockerHubImageResult `json:"results"`
  514. }
  515. func (r *Registry) listDockerHubImages(repoName string, repo repository.Repository) ([]*Image, error) {
  516. basic, err := repo.BasicIntegration.ReadBasicIntegration(
  517. r.BasicIntegrationID,
  518. )
  519. if err != nil {
  520. return nil, err
  521. }
  522. client := &http.Client{}
  523. req, err := http.NewRequest(
  524. "GET",
  525. fmt.Sprintf("https://hub.docker.com/v2/repositories/%s/tags", strings.Split(r.URL, "docker.io/")[1]),
  526. nil,
  527. )
  528. if err != nil {
  529. return nil, err
  530. }
  531. req.SetBasicAuth(string(basic.Username), string(basic.Password))
  532. resp, err := client.Do(req)
  533. if err != nil {
  534. return nil, err
  535. }
  536. imageResp := dockerHubImageResp{}
  537. if err := json.NewDecoder(resp.Body).Decode(&imageResp); err != nil {
  538. return nil, fmt.Errorf("Could not read private registry repositories: %v", err)
  539. }
  540. res := make([]*Image, 0)
  541. for _, result := range imageResp.Results {
  542. res = append(res, &Image{
  543. RepositoryName: repoName,
  544. Tag: result.Name,
  545. })
  546. }
  547. return res, nil
  548. }
  549. // GetDockerConfigJSON returns a dockerconfigjson file contents with "auths"
  550. // populated.
  551. func (r *Registry) GetDockerConfigJSON(
  552. repo repository.Repository,
  553. doAuth *oauth2.Config, // only required if using DOCR
  554. ) ([]byte, error) {
  555. var conf *configfile.ConfigFile
  556. var err error
  557. // switch on the auth mechanism to get a token
  558. if r.AWSIntegrationID != 0 {
  559. conf, err = r.getECRDockerConfigFile(repo)
  560. }
  561. if r.GCPIntegrationID != 0 {
  562. conf, err = r.getGCRDockerConfigFile(repo)
  563. }
  564. if r.DOIntegrationID != 0 {
  565. conf, err = r.getDOCRDockerConfigFile(repo, doAuth)
  566. }
  567. if r.BasicIntegrationID != 0 {
  568. conf, err = r.getPrivateRegistryDockerConfigFile(repo)
  569. }
  570. if err != nil {
  571. return nil, err
  572. }
  573. return json.Marshal(conf)
  574. }
  575. func (r *Registry) getECRDockerConfigFile(
  576. repo repository.Repository,
  577. ) (*configfile.ConfigFile, error) {
  578. aws, err := repo.AWSIntegration.ReadAWSIntegration(
  579. r.AWSIntegrationID,
  580. )
  581. if err != nil {
  582. return nil, err
  583. }
  584. sess, err := aws.GetSession()
  585. if err != nil {
  586. return nil, err
  587. }
  588. ecrSvc := ecr.New(sess)
  589. output, err := ecrSvc.GetAuthorizationToken(&ecr.GetAuthorizationTokenInput{})
  590. if err != nil {
  591. return nil, err
  592. }
  593. token := *output.AuthorizationData[0].AuthorizationToken
  594. decodedToken, err := base64.StdEncoding.DecodeString(token)
  595. if err != nil {
  596. return nil, err
  597. }
  598. parts := strings.SplitN(string(decodedToken), ":", 2)
  599. if len(parts) < 2 {
  600. return nil, err
  601. }
  602. key := r.URL
  603. if !strings.Contains(key, "http") {
  604. key = "https://" + key
  605. }
  606. return &configfile.ConfigFile{
  607. AuthConfigs: map[string]types.AuthConfig{
  608. key: types.AuthConfig{
  609. Username: parts[0],
  610. Password: parts[1],
  611. Auth: token,
  612. },
  613. },
  614. }, nil
  615. }
  616. func (r *Registry) getGCRDockerConfigFile(
  617. repo repository.Repository,
  618. ) (*configfile.ConfigFile, error) {
  619. gcp, err := repo.GCPIntegration.ReadGCPIntegration(
  620. r.GCPIntegrationID,
  621. )
  622. if err != nil {
  623. return nil, err
  624. }
  625. key := r.URL
  626. if !strings.Contains(key, "http") {
  627. key = "https://" + key
  628. }
  629. parsedURL, _ := url.Parse(key)
  630. return &configfile.ConfigFile{
  631. AuthConfigs: map[string]types.AuthConfig{
  632. parsedURL.Host: types.AuthConfig{
  633. Username: "_json_key",
  634. Password: string(gcp.GCPKeyData),
  635. Auth: generateAuthToken("_json_key", string(gcp.GCPKeyData)),
  636. },
  637. },
  638. }, nil
  639. }
  640. func (r *Registry) getDOCRDockerConfigFile(
  641. repo repository.Repository,
  642. doAuth *oauth2.Config,
  643. ) (*configfile.ConfigFile, error) {
  644. oauthInt, err := repo.OAuthIntegration.ReadOAuthIntegration(
  645. r.DOIntegrationID,
  646. )
  647. if err != nil {
  648. return nil, err
  649. }
  650. tok, _, err := oauth.GetAccessToken(oauthInt, doAuth, repo)
  651. if err != nil {
  652. return nil, err
  653. }
  654. key := r.URL
  655. if !strings.Contains(key, "http") {
  656. key = "https://" + key
  657. }
  658. parsedURL, _ := url.Parse(key)
  659. return &configfile.ConfigFile{
  660. AuthConfigs: map[string]types.AuthConfig{
  661. parsedURL.Host: types.AuthConfig{
  662. Username: tok,
  663. Password: tok,
  664. Auth: generateAuthToken(tok, tok),
  665. },
  666. },
  667. }, nil
  668. }
  669. func (r *Registry) getPrivateRegistryDockerConfigFile(
  670. repo repository.Repository,
  671. ) (*configfile.ConfigFile, error) {
  672. basic, err := repo.BasicIntegration.ReadBasicIntegration(
  673. r.BasicIntegrationID,
  674. )
  675. if err != nil {
  676. return nil, err
  677. }
  678. key := r.URL
  679. if !strings.Contains(key, "http") {
  680. key = "https://" + key
  681. }
  682. parsedURL, _ := url.Parse(key)
  683. authConfigKey := parsedURL.Host
  684. if strings.Contains(r.URL, "index.docker.io") {
  685. authConfigKey = "https://index.docker.io/v1/"
  686. }
  687. return &configfile.ConfigFile{
  688. AuthConfigs: map[string]types.AuthConfig{
  689. authConfigKey: types.AuthConfig{
  690. Username: string(basic.Username),
  691. Password: string(basic.Password),
  692. Auth: generateAuthToken(string(basic.Username), string(basic.Password)),
  693. },
  694. },
  695. }, nil
  696. }
  697. func generateAuthToken(username, password string) string {
  698. return base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
  699. }