docker.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. package cmd
  2. import (
  3. "context"
  4. "encoding/base64"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. "net/url"
  9. "os"
  10. "os/exec"
  11. "path/filepath"
  12. "strings"
  13. "github.com/fatih/color"
  14. api "github.com/porter-dev/porter/api/client"
  15. ptypes "github.com/porter-dev/porter/api/types"
  16. "github.com/porter-dev/porter/cli/cmd/github"
  17. "github.com/spf13/cobra"
  18. "github.com/docker/cli/cli/config/configfile"
  19. "github.com/docker/cli/cli/config/types"
  20. )
  21. var dockerCmd = &cobra.Command{
  22. Use: "docker",
  23. Short: "Commands to configure Docker for a project",
  24. }
  25. var configureCmd = &cobra.Command{
  26. Use: "configure",
  27. Short: "Configures the host's Docker instance",
  28. Run: func(cmd *cobra.Command, args []string) {
  29. err := checkLoginAndRun(args, dockerConfig)
  30. if err != nil {
  31. os.Exit(1)
  32. }
  33. },
  34. }
  35. func init() {
  36. rootCmd.AddCommand(dockerCmd)
  37. dockerCmd.AddCommand(configureCmd)
  38. }
  39. func dockerConfig(user *ptypes.GetAuthenticatedUserResponse, client *api.Client, args []string) error {
  40. pID := config.Project
  41. // get all registries that should be added
  42. regToAdd := make([]string, 0)
  43. // get the list of namespaces
  44. resp, err := client.ListRegistries(
  45. context.Background(),
  46. pID,
  47. )
  48. if err != nil {
  49. return err
  50. }
  51. registries := *resp
  52. for _, registry := range registries {
  53. if registry.URL != "" {
  54. rURL := registry.URL
  55. if !strings.Contains(rURL, "http") {
  56. rURL = "http://" + rURL
  57. }
  58. // strip the protocol
  59. regURL, err := url.Parse(rURL)
  60. if err != nil {
  61. continue
  62. }
  63. regToAdd = append(regToAdd, regURL.Host)
  64. }
  65. }
  66. dockerConfigFile := filepath.Join(home, ".docker", "config.json")
  67. // determine if configfile exists
  68. if info, err := os.Stat(dockerConfigFile); info.IsDir() || os.IsNotExist(err) {
  69. // if it does not exist, create it
  70. err := ioutil.WriteFile(dockerConfigFile, []byte("{}"), 0700)
  71. if err != nil {
  72. return err
  73. }
  74. }
  75. // read the file bytes
  76. configBytes, err := ioutil.ReadFile(dockerConfigFile)
  77. if err != nil {
  78. return err
  79. }
  80. // check if the docker credential helper exists
  81. if !commandExists("docker-credential-porter") {
  82. err := downloadCredMatchingRelease()
  83. if err != nil {
  84. color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
  85. os.Exit(1)
  86. }
  87. }
  88. // otherwise, check the version flag of the binary
  89. cmdVersionCred := exec.Command("docker-credential-porter", "--version")
  90. writer := &versionWriter{}
  91. cmdVersionCred.Stdout = writer
  92. err = cmdVersionCred.Run()
  93. if err != nil || writer.Version != Version {
  94. err := downloadCredMatchingRelease()
  95. if err != nil {
  96. color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
  97. os.Exit(1)
  98. }
  99. }
  100. configFile := &configfile.ConfigFile{
  101. Filename: dockerConfigFile,
  102. }
  103. err = json.Unmarshal(configBytes, config)
  104. if err != nil {
  105. return err
  106. }
  107. if configFile.CredentialHelpers == nil {
  108. configFile.CredentialHelpers = make(map[string]string)
  109. }
  110. if configFile.AuthConfigs == nil {
  111. configFile.AuthConfigs = make(map[string]types.AuthConfig)
  112. }
  113. for _, regURL := range regToAdd {
  114. // if this is a dockerhub registry, see if an auth config has already been generated
  115. // for index.docker.io
  116. if strings.Contains(regURL, "index.docker.io") {
  117. isAuthenticated := false
  118. for key := range configFile.AuthConfigs {
  119. if key == "https://index.docker.io/v1/" {
  120. isAuthenticated = true
  121. }
  122. }
  123. if !isAuthenticated {
  124. // get a dockerhub token from the Porter API
  125. tokenResp, err := client.GetDockerhubAuthorizationToken(context.Background(), config.Project)
  126. if err != nil {
  127. return err
  128. }
  129. decodedToken, err := base64.StdEncoding.DecodeString(tokenResp.Token)
  130. if err != nil {
  131. return fmt.Errorf("Invalid token: %v", err)
  132. }
  133. parts := strings.SplitN(string(decodedToken), ":", 2)
  134. if len(parts) < 2 {
  135. return fmt.Errorf("Invalid token: expected two parts, got %d", len(parts))
  136. }
  137. configFile.AuthConfigs["https://index.docker.io/v1/"] = types.AuthConfig{
  138. Auth: tokenResp.Token,
  139. Username: parts[0],
  140. Password: parts[1],
  141. }
  142. // since we're using token-based auth, unset the credstore
  143. configFile.CredentialsStore = ""
  144. }
  145. } else {
  146. configFile.CredentialHelpers[regURL] = "porter"
  147. }
  148. }
  149. return configFile.Save()
  150. }
  151. func downloadCredMatchingRelease() error {
  152. // download the porter cred helper
  153. z := &github.ZIPReleaseGetter{
  154. AssetName: "docker-credential-porter",
  155. AssetFolderDest: "/usr/local/bin",
  156. ZipFolderDest: filepath.Join(home, ".porter"),
  157. ZipName: "docker-credential-porter_latest.zip",
  158. EntityID: "porter-dev",
  159. RepoName: "porter",
  160. IsPlatformDependent: true,
  161. Downloader: &github.ZIPDownloader{
  162. ZipFolderDest: filepath.Join(home, ".porter"),
  163. AssetFolderDest: "/usr/local/bin",
  164. ZipName: "docker-credential-porter_latest.zip",
  165. },
  166. }
  167. return z.GetRelease(Version)
  168. }
  169. func commandExists(cmd string) bool {
  170. _, err := exec.LookPath(cmd)
  171. return err == nil
  172. }