registry.go 18 KB

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