publish.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. Copyright 2020 Docker Compose CLI authors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package compose
  14. import (
  15. "bytes"
  16. "context"
  17. "crypto/sha256"
  18. "errors"
  19. "fmt"
  20. "io"
  21. "os"
  22. "github.com/DefangLabs/secret-detector/pkg/scanner"
  23. "github.com/DefangLabs/secret-detector/pkg/secrets"
  24. "github.com/compose-spec/compose-go/v2/loader"
  25. "github.com/compose-spec/compose-go/v2/types"
  26. "github.com/distribution/reference"
  27. "github.com/docker/buildx/util/imagetools"
  28. "github.com/docker/cli/cli/command"
  29. "github.com/docker/compose/v2/internal/ocipush"
  30. "github.com/docker/compose/v2/pkg/api"
  31. "github.com/docker/compose/v2/pkg/compose/transform"
  32. "github.com/docker/compose/v2/pkg/progress"
  33. "github.com/docker/compose/v2/pkg/prompt"
  34. )
  35. func (s *composeService) Publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  36. return progress.RunWithTitle(ctx, func(ctx context.Context) error {
  37. return s.publish(ctx, project, repository, options)
  38. }, s.stdinfo(), "Publishing")
  39. }
  40. func (s *composeService) publish(ctx context.Context, project *types.Project, repository string, options api.PublishOptions) error {
  41. accept, err := s.preChecks(project, options)
  42. if err != nil {
  43. return err
  44. }
  45. if !accept {
  46. return nil
  47. }
  48. err = s.Push(ctx, project, api.PushOptions{IgnoreFailures: true, ImageMandatory: true})
  49. if err != nil {
  50. return err
  51. }
  52. named, err := reference.ParseDockerRef(repository)
  53. if err != nil {
  54. return err
  55. }
  56. resolver := imagetools.New(imagetools.Opt{
  57. Auth: s.configFile(),
  58. })
  59. var layers []ocipush.Pushable
  60. extFiles := map[string]string{}
  61. for _, file := range project.ComposeFiles {
  62. data, err := processFile(ctx, file, project, extFiles)
  63. if err != nil {
  64. return err
  65. }
  66. layerDescriptor := ocipush.DescriptorForComposeFile(file, data)
  67. layers = append(layers, ocipush.Pushable{
  68. Descriptor: layerDescriptor,
  69. Data: data,
  70. })
  71. }
  72. extLayers, err := processExtends(ctx, project, extFiles)
  73. if err != nil {
  74. return err
  75. }
  76. layers = append(layers, extLayers...)
  77. if options.WithEnvironment {
  78. layers = append(layers, envFileLayers(project)...)
  79. }
  80. if options.ResolveImageDigests {
  81. yaml, err := s.generateImageDigestsOverride(ctx, project)
  82. if err != nil {
  83. return err
  84. }
  85. layerDescriptor := ocipush.DescriptorForComposeFile("image-digests.yaml", yaml)
  86. layers = append(layers, ocipush.Pushable{
  87. Descriptor: layerDescriptor,
  88. Data: yaml,
  89. })
  90. }
  91. w := progress.ContextWriter(ctx)
  92. w.Event(progress.Event{
  93. ID: repository,
  94. Text: "publishing",
  95. Status: progress.Working,
  96. })
  97. if !s.dryRun {
  98. err = ocipush.PushManifest(ctx, resolver, named, layers, options.OCIVersion)
  99. if err != nil {
  100. w.Event(progress.Event{
  101. ID: repository,
  102. Text: "publishing",
  103. Status: progress.Error,
  104. })
  105. return err
  106. }
  107. }
  108. w.Event(progress.Event{
  109. ID: repository,
  110. Text: "published",
  111. Status: progress.Done,
  112. })
  113. return nil
  114. }
  115. func processExtends(ctx context.Context, project *types.Project, extFiles map[string]string) ([]ocipush.Pushable, error) {
  116. var layers []ocipush.Pushable
  117. moreExtFiles := map[string]string{}
  118. for xf, hash := range extFiles {
  119. data, err := processFile(ctx, xf, project, moreExtFiles)
  120. if err != nil {
  121. return nil, err
  122. }
  123. layerDescriptor := ocipush.DescriptorForComposeFile(hash, data)
  124. layerDescriptor.Annotations["com.docker.compose.extends"] = "true"
  125. layers = append(layers, ocipush.Pushable{
  126. Descriptor: layerDescriptor,
  127. Data: data,
  128. })
  129. }
  130. for f, hash := range moreExtFiles {
  131. if _, ok := extFiles[f]; ok {
  132. delete(moreExtFiles, f)
  133. }
  134. extFiles[f] = hash
  135. }
  136. if len(moreExtFiles) > 0 {
  137. extLayers, err := processExtends(ctx, project, moreExtFiles)
  138. if err != nil {
  139. return nil, err
  140. }
  141. layers = append(layers, extLayers...)
  142. }
  143. return layers, nil
  144. }
  145. func processFile(ctx context.Context, file string, project *types.Project, extFiles map[string]string) ([]byte, error) {
  146. f, err := os.ReadFile(file)
  147. if err != nil {
  148. return nil, err
  149. }
  150. base, err := loader.LoadWithContext(ctx, types.ConfigDetails{
  151. WorkingDir: project.WorkingDir,
  152. Environment: project.Environment,
  153. ConfigFiles: []types.ConfigFile{
  154. {
  155. Filename: file,
  156. Content: f,
  157. },
  158. },
  159. }, func(options *loader.Options) {
  160. options.SkipValidation = true
  161. options.SkipExtends = true
  162. options.SkipConsistencyCheck = true
  163. options.ResolvePaths = true
  164. })
  165. if err != nil {
  166. return nil, err
  167. }
  168. for name, service := range base.Services {
  169. if service.Extends == nil {
  170. continue
  171. }
  172. xf := service.Extends.File
  173. if xf == "" {
  174. continue
  175. }
  176. if _, err = os.Stat(service.Extends.File); os.IsNotExist(err) {
  177. // No local file, while we loaded the project successfully: This is actually a remote resource
  178. continue
  179. }
  180. hash := fmt.Sprintf("%x.yaml", sha256.Sum256([]byte(xf)))
  181. extFiles[xf] = hash
  182. f, err = transform.ReplaceExtendsFile(f, name, hash)
  183. if err != nil {
  184. return nil, err
  185. }
  186. }
  187. return f, nil
  188. }
  189. func (s *composeService) generateImageDigestsOverride(ctx context.Context, project *types.Project) ([]byte, error) {
  190. project, err := project.WithProfiles([]string{"*"})
  191. if err != nil {
  192. return nil, err
  193. }
  194. project, err = project.WithImagesResolved(ImageDigestResolver(ctx, s.configFile(), s.apiClient()))
  195. if err != nil {
  196. return nil, err
  197. }
  198. override := types.Project{
  199. Services: types.Services{},
  200. }
  201. for name, service := range project.Services {
  202. override.Services[name] = types.ServiceConfig{
  203. Image: service.Image,
  204. }
  205. }
  206. return override.MarshalYAML()
  207. }
  208. //nolint:gocyclo
  209. func (s *composeService) preChecks(project *types.Project, options api.PublishOptions) (bool, error) {
  210. if ok, err := s.checkOnlyBuildSection(project); !ok || err != nil {
  211. return false, err
  212. }
  213. if ok, err := s.checkForBindMount(project); !ok || err != nil {
  214. return false, err
  215. }
  216. if options.AssumeYes {
  217. return true, nil
  218. }
  219. detectedSecrets, err := s.checkForSensitiveData(project)
  220. if err != nil {
  221. return false, err
  222. }
  223. if len(detectedSecrets) > 0 {
  224. fmt.Println("you are about to publish sensitive data within your OCI artifact.\n" +
  225. "please double check that you are not leaking sensitive data")
  226. for _, val := range detectedSecrets {
  227. _, _ = fmt.Fprintln(s.dockerCli.Out(), val.Type)
  228. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%q: %s\n", val.Key, val.Value)
  229. }
  230. if ok, err := acceptPublishSensitiveData(s.dockerCli); err != nil || !ok {
  231. return false, err
  232. }
  233. }
  234. envVariables, err := s.checkEnvironmentVariables(project, options)
  235. if err != nil {
  236. return false, err
  237. }
  238. if len(envVariables) > 0 {
  239. fmt.Println("you are about to publish environment variables within your OCI artifact.\n" +
  240. "please double check that you are not leaking sensitive data")
  241. for key, val := range envVariables {
  242. _, _ = fmt.Fprintln(s.dockerCli.Out(), "Service/Config ", key)
  243. for k, v := range val {
  244. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%s=%v\n", k, *v)
  245. }
  246. }
  247. if ok, err := acceptPublishEnvVariables(s.dockerCli); err != nil || !ok {
  248. return false, err
  249. }
  250. }
  251. return true, nil
  252. }
  253. func (s *composeService) checkEnvironmentVariables(project *types.Project, options api.PublishOptions) (map[string]types.MappingWithEquals, error) {
  254. envVarList := map[string]types.MappingWithEquals{}
  255. errorList := map[string][]string{}
  256. for _, service := range project.Services {
  257. if len(service.EnvFiles) > 0 {
  258. errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has env_file declared.", service.Name))
  259. }
  260. if len(service.Environment) > 0 {
  261. errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has environment variable(s) declared.", service.Name))
  262. envVarList[service.Name] = service.Environment
  263. }
  264. }
  265. for _, config := range project.Configs {
  266. if config.Environment != "" {
  267. errorList[config.Name] = append(errorList[config.Name], fmt.Sprintf("config %q is declare as an environment variable.", config.Name))
  268. envVarList[config.Name] = types.NewMappingWithEquals([]string{fmt.Sprintf("%s=%s", config.Name, config.Environment)})
  269. }
  270. }
  271. if !options.WithEnvironment && len(errorList) > 0 {
  272. errorMsgSuffix := "To avoid leaking sensitive data, you must either explicitly allow the sending of environment variables by using the --with-env flag,\n" +
  273. "or remove sensitive data from your Compose configuration"
  274. errorMsg := ""
  275. for _, errors := range errorList {
  276. for _, err := range errors {
  277. errorMsg += fmt.Sprintf("%s\n", err)
  278. }
  279. }
  280. return nil, fmt.Errorf("%s%s", errorMsg, errorMsgSuffix)
  281. }
  282. return envVarList, nil
  283. }
  284. func acceptPublishEnvVariables(cli command.Cli) (bool, error) {
  285. msg := "Are you ok to publish these environment variables? [y/N]: "
  286. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  287. return confirm, err
  288. }
  289. func acceptPublishSensitiveData(cli command.Cli) (bool, error) {
  290. msg := "Are you ok to publish these sensitive data? [y/N]: "
  291. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  292. return confirm, err
  293. }
  294. func envFileLayers(project *types.Project) []ocipush.Pushable {
  295. var layers []ocipush.Pushable
  296. for _, service := range project.Services {
  297. for _, envFile := range service.EnvFiles {
  298. f, err := os.ReadFile(envFile.Path)
  299. if err != nil {
  300. // if we can't read the file, skip to the next one
  301. continue
  302. }
  303. layerDescriptor := ocipush.DescriptorForEnvFile(envFile.Path, f)
  304. layers = append(layers, ocipush.Pushable{
  305. Descriptor: layerDescriptor,
  306. Data: f,
  307. })
  308. }
  309. }
  310. return layers
  311. }
  312. func (s *composeService) checkOnlyBuildSection(project *types.Project) (bool, error) {
  313. errorList := []string{}
  314. for _, service := range project.Services {
  315. if service.Image == "" && service.Build != nil {
  316. errorList = append(errorList, service.Name)
  317. }
  318. }
  319. if len(errorList) > 0 {
  320. errMsg := "your Compose stack cannot be published as it only contains a build section for service(s):\n"
  321. for _, serviceInError := range errorList {
  322. errMsg += fmt.Sprintf("- %q\n", serviceInError)
  323. }
  324. return false, errors.New(errMsg)
  325. }
  326. return true, nil
  327. }
  328. func (s *composeService) checkForBindMount(project *types.Project) (bool, error) {
  329. for name, config := range project.Services {
  330. for _, volume := range config.Volumes {
  331. if volume.Type == types.VolumeTypeBind {
  332. return false, fmt.Errorf("cannot publish compose file: service %q relies on bind-mount. You should use volumes", name)
  333. }
  334. }
  335. }
  336. return true, nil
  337. }
  338. func (s *composeService) checkForSensitiveData(project *types.Project) ([]secrets.DetectedSecret, error) {
  339. var allFindings []secrets.DetectedSecret
  340. scan := scanner.NewDefaultScanner()
  341. // Check all compose files
  342. for _, file := range project.ComposeFiles {
  343. in, err := composeFileAsByteReader(file, project)
  344. if err != nil {
  345. return nil, err
  346. }
  347. findings, err := scan.ScanReader(in)
  348. if err != nil {
  349. return nil, fmt.Errorf("failed to scan compose file %s: %w", file, err)
  350. }
  351. allFindings = append(allFindings, findings...)
  352. }
  353. for _, service := range project.Services {
  354. // Check env files
  355. for _, envFile := range service.EnvFiles {
  356. findings, err := scan.ScanFile(envFile.Path)
  357. if err != nil {
  358. return nil, fmt.Errorf("failed to scan env file %s: %w", envFile.Path, err)
  359. }
  360. allFindings = append(allFindings, findings...)
  361. }
  362. }
  363. // Check configs defined by files
  364. for _, config := range project.Configs {
  365. if config.File != "" {
  366. findings, err := scan.ScanFile(config.File)
  367. if err != nil {
  368. return nil, fmt.Errorf("failed to scan config file %s: %w", config.File, err)
  369. }
  370. allFindings = append(allFindings, findings...)
  371. }
  372. }
  373. // Check secrets defined by files
  374. for _, secret := range project.Secrets {
  375. if secret.File != "" {
  376. findings, err := scan.ScanFile(secret.File)
  377. if err != nil {
  378. return nil, fmt.Errorf("failed to scan secret file %s: %w", secret.File, err)
  379. }
  380. allFindings = append(allFindings, findings...)
  381. }
  382. }
  383. return allFindings, nil
  384. }
  385. func composeFileAsByteReader(filePath string, project *types.Project) (io.Reader, error) {
  386. composeFile, err := os.ReadFile(filePath)
  387. if err != nil {
  388. return nil, fmt.Errorf("failed to open compose file %s: %w", filePath, err)
  389. }
  390. base, err := loader.LoadWithContext(context.TODO(), types.ConfigDetails{
  391. WorkingDir: project.WorkingDir,
  392. Environment: project.Environment,
  393. ConfigFiles: []types.ConfigFile{
  394. {
  395. Filename: filePath,
  396. Content: composeFile,
  397. },
  398. },
  399. }, func(options *loader.Options) {
  400. options.SkipValidation = true
  401. options.SkipExtends = true
  402. options.SkipConsistencyCheck = true
  403. options.ResolvePaths = true
  404. options.SkipInterpolation = true
  405. options.SkipResolveEnvironment = true
  406. })
  407. if err != nil {
  408. return nil, err
  409. }
  410. in, err := base.MarshalYAML()
  411. if err != nil {
  412. return nil, err
  413. }
  414. return bytes.NewBuffer(in), nil
  415. }