docker.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. return setDockerConfig(client)
  41. }
  42. func setDockerConfig(client *api.Client) error {
  43. pID := config.Project
  44. // get all registries that should be added
  45. regToAdd := make([]string, 0)
  46. // get the list of namespaces
  47. resp, err := client.ListRegistries(
  48. context.Background(),
  49. pID,
  50. )
  51. if err != nil {
  52. return err
  53. }
  54. registries := *resp
  55. for _, registry := range registries {
  56. if registry.URL != "" {
  57. rURL := registry.URL
  58. if !strings.Contains(rURL, "http") {
  59. rURL = "http://" + rURL
  60. }
  61. // strip the protocol
  62. regURL, err := url.Parse(rURL)
  63. if err != nil {
  64. continue
  65. }
  66. regToAdd = append(regToAdd, regURL.Host)
  67. }
  68. }
  69. // create a docker dir if it does not exist
  70. dockerDir := filepath.Join(home, ".docker")
  71. if _, err := os.Stat(dockerDir); os.IsNotExist(err) {
  72. err = os.Mkdir(dockerDir, 0700)
  73. if err != nil {
  74. return err
  75. }
  76. }
  77. dockerConfigFile := filepath.Join(home, ".docker", "config.json")
  78. // determine if configfile exists
  79. if _, err := os.Stat(dockerConfigFile); os.IsNotExist(err) {
  80. // if it does not exist, create it
  81. err := ioutil.WriteFile(dockerConfigFile, []byte("{}"), 0700)
  82. if err != nil {
  83. return err
  84. }
  85. }
  86. // read the file bytes
  87. configBytes, err := ioutil.ReadFile(dockerConfigFile)
  88. if err != nil {
  89. return err
  90. }
  91. // check if the docker credential helper exists
  92. if !commandExists("docker-credential-porter") {
  93. err := downloadCredMatchingRelease()
  94. if err != nil {
  95. color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
  96. os.Exit(1)
  97. }
  98. }
  99. // otherwise, check the version flag of the binary
  100. cmdVersionCred := exec.Command("docker-credential-porter", "--version")
  101. writer := &versionWriter{}
  102. cmdVersionCred.Stdout = writer
  103. err = cmdVersionCred.Run()
  104. if err != nil || writer.Version != Version {
  105. err := downloadCredMatchingRelease()
  106. if err != nil {
  107. color.New(color.FgRed).Println("Failed to download credential helper binary:", err.Error())
  108. os.Exit(1)
  109. }
  110. }
  111. configFile := &configfile.ConfigFile{
  112. Filename: dockerConfigFile,
  113. }
  114. err = json.Unmarshal(configBytes, config)
  115. if err != nil {
  116. return err
  117. }
  118. if configFile.CredentialHelpers == nil {
  119. configFile.CredentialHelpers = make(map[string]string)
  120. }
  121. if configFile.AuthConfigs == nil {
  122. configFile.AuthConfigs = make(map[string]types.AuthConfig)
  123. }
  124. for _, regURL := range regToAdd {
  125. // if this is a dockerhub registry, see if an auth config has already been generated
  126. // for index.docker.io
  127. if strings.Contains(regURL, "index.docker.io") {
  128. isAuthenticated := false
  129. for key := range configFile.AuthConfigs {
  130. if key == "https://index.docker.io/v1/" {
  131. isAuthenticated = true
  132. }
  133. }
  134. if !isAuthenticated {
  135. // get a dockerhub token from the Porter API
  136. tokenResp, err := client.GetDockerhubAuthorizationToken(context.Background(), config.Project)
  137. if err != nil {
  138. return err
  139. }
  140. decodedToken, err := base64.StdEncoding.DecodeString(tokenResp.Token)
  141. if err != nil {
  142. return fmt.Errorf("Invalid token: %v", err)
  143. }
  144. parts := strings.SplitN(string(decodedToken), ":", 2)
  145. if len(parts) < 2 {
  146. return fmt.Errorf("Invalid token: expected two parts, got %d", len(parts))
  147. }
  148. configFile.AuthConfigs["https://index.docker.io/v1/"] = types.AuthConfig{
  149. Auth: tokenResp.Token,
  150. Username: parts[0],
  151. Password: parts[1],
  152. }
  153. // since we're using token-based auth, unset the credstore
  154. configFile.CredentialsStore = ""
  155. }
  156. } else {
  157. configFile.CredentialHelpers[regURL] = "porter"
  158. }
  159. }
  160. return configFile.Save()
  161. }
  162. func downloadCredMatchingRelease() error {
  163. // download the porter cred helper
  164. z := &github.ZIPReleaseGetter{
  165. AssetName: "docker-credential-porter",
  166. AssetFolderDest: "/usr/local/bin",
  167. ZipFolderDest: filepath.Join(home, ".porter"),
  168. ZipName: "docker-credential-porter_latest.zip",
  169. EntityID: "porter-dev",
  170. RepoName: "porter",
  171. IsPlatformDependent: true,
  172. Downloader: &github.ZIPDownloader{
  173. ZipFolderDest: filepath.Join(home, ".porter"),
  174. AssetFolderDest: "/usr/local/bin",
  175. ZipName: "docker-credential-porter_latest.zip",
  176. },
  177. }
  178. return z.GetRelease(Version)
  179. }
  180. func commandExists(cmd string) bool {
  181. _, err := exec.LookPath(cmd)
  182. return err == nil
  183. }