publish.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. bindMounts := s.checkForBindMount(project)
  214. if len(bindMounts) > 0 {
  215. fmt.Println("you are about to publish bind mounts declaration within your OCI artifact.\n" +
  216. "only the bind mount declarations will be added to the OCI artifact (not content)\n" +
  217. "please double check that you are not mounting potential user's sensitive directories or data")
  218. for key, val := range bindMounts {
  219. _, _ = fmt.Fprintln(s.dockerCli.Out(), key)
  220. for _, v := range val {
  221. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%s\n", v.String())
  222. }
  223. }
  224. if ok, err := acceptPublishBindMountDeclarations(s.dockerCli); err != nil || !ok {
  225. return false, err
  226. }
  227. }
  228. if options.AssumeYes {
  229. return true, nil
  230. }
  231. detectedSecrets, err := s.checkForSensitiveData(project)
  232. if err != nil {
  233. return false, err
  234. }
  235. if len(detectedSecrets) > 0 {
  236. fmt.Println("you are about to publish sensitive data within your OCI artifact.\n" +
  237. "please double check that you are not leaking sensitive data")
  238. for _, val := range detectedSecrets {
  239. _, _ = fmt.Fprintln(s.dockerCli.Out(), val.Type)
  240. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%q: %s\n", val.Key, val.Value)
  241. }
  242. if ok, err := acceptPublishSensitiveData(s.dockerCli); err != nil || !ok {
  243. return false, err
  244. }
  245. }
  246. envVariables, err := s.checkEnvironmentVariables(project, options)
  247. if err != nil {
  248. return false, err
  249. }
  250. if len(envVariables) > 0 {
  251. fmt.Println("you are about to publish environment variables within your OCI artifact.\n" +
  252. "please double check that you are not leaking sensitive data")
  253. for key, val := range envVariables {
  254. _, _ = fmt.Fprintln(s.dockerCli.Out(), "Service/Config ", key)
  255. for k, v := range val {
  256. _, _ = fmt.Fprintf(s.dockerCli.Out(), "%s=%v\n", k, *v)
  257. }
  258. }
  259. if ok, err := acceptPublishEnvVariables(s.dockerCli); err != nil || !ok {
  260. return false, err
  261. }
  262. }
  263. return true, nil
  264. }
  265. func (s *composeService) checkEnvironmentVariables(project *types.Project, options api.PublishOptions) (map[string]types.MappingWithEquals, error) {
  266. envVarList := map[string]types.MappingWithEquals{}
  267. errorList := map[string][]string{}
  268. for _, service := range project.Services {
  269. if len(service.EnvFiles) > 0 {
  270. errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has env_file declared.", service.Name))
  271. }
  272. if len(service.Environment) > 0 {
  273. errorList[service.Name] = append(errorList[service.Name], fmt.Sprintf("service %q has environment variable(s) declared.", service.Name))
  274. envVarList[service.Name] = service.Environment
  275. }
  276. }
  277. for _, config := range project.Configs {
  278. if config.Environment != "" {
  279. errorList[config.Name] = append(errorList[config.Name], fmt.Sprintf("config %q is declare as an environment variable.", config.Name))
  280. envVarList[config.Name] = types.NewMappingWithEquals([]string{fmt.Sprintf("%s=%s", config.Name, config.Environment)})
  281. }
  282. }
  283. if !options.WithEnvironment && len(errorList) > 0 {
  284. errorMsgSuffix := "To avoid leaking sensitive data, you must either explicitly allow the sending of environment variables by using the --with-env flag,\n" +
  285. "or remove sensitive data from your Compose configuration"
  286. errorMsg := ""
  287. for _, errors := range errorList {
  288. for _, err := range errors {
  289. errorMsg += fmt.Sprintf("%s\n", err)
  290. }
  291. }
  292. return nil, fmt.Errorf("%s%s", errorMsg, errorMsgSuffix)
  293. }
  294. return envVarList, nil
  295. }
  296. func acceptPublishEnvVariables(cli command.Cli) (bool, error) {
  297. msg := "Are you ok to publish these environment variables? [y/N]: "
  298. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  299. return confirm, err
  300. }
  301. func acceptPublishSensitiveData(cli command.Cli) (bool, error) {
  302. msg := "Are you ok to publish these sensitive data? [y/N]: "
  303. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  304. return confirm, err
  305. }
  306. func acceptPublishBindMountDeclarations(cli command.Cli) (bool, error) {
  307. msg := "Are you ok to publish these bind mount declarations? [y/N]: "
  308. confirm, err := prompt.NewPrompt(cli.In(), cli.Out()).Confirm(msg, false)
  309. return confirm, err
  310. }
  311. func envFileLayers(project *types.Project) []ocipush.Pushable {
  312. var layers []ocipush.Pushable
  313. for _, service := range project.Services {
  314. for _, envFile := range service.EnvFiles {
  315. f, err := os.ReadFile(envFile.Path)
  316. if err != nil {
  317. // if we can't read the file, skip to the next one
  318. continue
  319. }
  320. layerDescriptor := ocipush.DescriptorForEnvFile(envFile.Path, f)
  321. layers = append(layers, ocipush.Pushable{
  322. Descriptor: layerDescriptor,
  323. Data: f,
  324. })
  325. }
  326. }
  327. return layers
  328. }
  329. func (s *composeService) checkOnlyBuildSection(project *types.Project) (bool, error) {
  330. errorList := []string{}
  331. for _, service := range project.Services {
  332. if service.Image == "" && service.Build != nil {
  333. errorList = append(errorList, service.Name)
  334. }
  335. }
  336. if len(errorList) > 0 {
  337. errMsg := "your Compose stack cannot be published as it only contains a build section for service(s):\n"
  338. for _, serviceInError := range errorList {
  339. errMsg += fmt.Sprintf("- %q\n", serviceInError)
  340. }
  341. return false, errors.New(errMsg)
  342. }
  343. return true, nil
  344. }
  345. func (s *composeService) checkForBindMount(project *types.Project) map[string][]types.ServiceVolumeConfig {
  346. allFindings := map[string][]types.ServiceVolumeConfig{}
  347. for serviceName, config := range project.Services {
  348. bindMounts := []types.ServiceVolumeConfig{}
  349. for _, volume := range config.Volumes {
  350. if volume.Type == types.VolumeTypeBind {
  351. bindMounts = append(bindMounts, volume)
  352. }
  353. }
  354. if len(bindMounts) > 0 {
  355. allFindings[serviceName] = bindMounts
  356. }
  357. }
  358. return allFindings
  359. }
  360. func (s *composeService) checkForSensitiveData(project *types.Project) ([]secrets.DetectedSecret, error) {
  361. var allFindings []secrets.DetectedSecret
  362. scan := scanner.NewDefaultScanner()
  363. // Check all compose files
  364. for _, file := range project.ComposeFiles {
  365. in, err := composeFileAsByteReader(file, project)
  366. if err != nil {
  367. return nil, err
  368. }
  369. findings, err := scan.ScanReader(in)
  370. if err != nil {
  371. return nil, fmt.Errorf("failed to scan compose file %s: %w", file, err)
  372. }
  373. allFindings = append(allFindings, findings...)
  374. }
  375. for _, service := range project.Services {
  376. // Check env files
  377. for _, envFile := range service.EnvFiles {
  378. findings, err := scan.ScanFile(envFile.Path)
  379. if err != nil {
  380. return nil, fmt.Errorf("failed to scan env file %s: %w", envFile.Path, err)
  381. }
  382. allFindings = append(allFindings, findings...)
  383. }
  384. }
  385. // Check configs defined by files
  386. for _, config := range project.Configs {
  387. if config.File != "" {
  388. findings, err := scan.ScanFile(config.File)
  389. if err != nil {
  390. return nil, fmt.Errorf("failed to scan config file %s: %w", config.File, err)
  391. }
  392. allFindings = append(allFindings, findings...)
  393. }
  394. }
  395. // Check secrets defined by files
  396. for _, secret := range project.Secrets {
  397. if secret.File != "" {
  398. findings, err := scan.ScanFile(secret.File)
  399. if err != nil {
  400. return nil, fmt.Errorf("failed to scan secret file %s: %w", secret.File, err)
  401. }
  402. allFindings = append(allFindings, findings...)
  403. }
  404. }
  405. return allFindings, nil
  406. }
  407. func composeFileAsByteReader(filePath string, project *types.Project) (io.Reader, error) {
  408. composeFile, err := os.ReadFile(filePath)
  409. if err != nil {
  410. return nil, fmt.Errorf("failed to open compose file %s: %w", filePath, err)
  411. }
  412. base, err := loader.LoadWithContext(context.TODO(), types.ConfigDetails{
  413. WorkingDir: project.WorkingDir,
  414. Environment: project.Environment,
  415. ConfigFiles: []types.ConfigFile{
  416. {
  417. Filename: filePath,
  418. Content: composeFile,
  419. },
  420. },
  421. }, func(options *loader.Options) {
  422. options.SkipValidation = true
  423. options.SkipExtends = true
  424. options.SkipConsistencyCheck = true
  425. options.ResolvePaths = true
  426. options.SkipInterpolation = true
  427. options.SkipResolveEnvironment = true
  428. })
  429. if err != nil {
  430. return nil, err
  431. }
  432. in, err := base.MarshalYAML()
  433. if err != nil {
  434. return nil, err
  435. }
  436. return bytes.NewBuffer(in), nil
  437. }